首页 > 解决方案 > NameError:名称“模型”未在用于隔离语音识别的 python 代码中定义

问题描述

我从https://github.com/prakashpandey9/IsolatedSpeechRecognition下载了代码

该代码与 Python 2 兼容,我为 Python 3 编写了它。

但我收到此错误:

~/Desktop/IsolatedSpeechRecognition-master $ python Isolated_Speech_Recognition.py 
    List of spoken words: ['dog', 'human', 'god', 'eye', 'book', 'fast', 'apple', 'cat']
    {'book', 'human', 'fast', 'apple', 'god', 'cat', 'dog', 'eye'}
    Number of total files: 120
    Labels and label indices [6. 6. 6. 6. 6. 6. 6. 6. 6. 6. 6. 6. 6. 6. 6. 1. 1. 1. 1. 1. 1. 1. 1. 1.
     1. 1. 1. 1. 1. 1. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 7. 7. 7.
     7. 7. 7. 7. 7. 7. 7. 7. 7. 7. 7. 7. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 3. 3. 3. 3. 3. 3.
     3. 3. 3. 3. 3. 3. 3. 3. 3. 5. 5. 5. 5. 5. 5. 5. 5. 5. 5. 5. 5. 5. 5. 5.]
    (120, 3841)
    Processed observation 0
    Processed observation 10
    Processed observation 20
    Processed observation 30
    Processed observation 40
    Processed observation 50
    Processed observation 60
    Processed observation 70
    Processed observation 80
    Processed observation 90
    Processed observation 100
    Processed observation 110
    (120, 7, 33)
    Traceback (most recent call last):
      File "Isolated_Speech_Recognition.py", line 269, in <module>
        _ = [model.train(X_train[y_train == y, :, :]) for m, y in zip(ms, ys)]
      File "Isolated_Speech_Recognition.py", line 269, in <listcomp>
        _ = [model.train(X_train[y_train == y, :, :]) for m, y in zip(ms, ys)]
    NameError: name 'model' is not defined

这是我收到错误的代码片段:

from sklearn.model_selection import StratifiedShuffleSplit
sss = StratifiedShuffleSplit(test_size=0.1, random_state=0)

for n,i in enumerate(all_obs):
    all_obs[n] /= all_obs[n].sum(axis=0)


for train_index, test_index in sss.split(all_obs, all_labels):
    X_train, X_test = all_obs[train_index, ...], all_obs[test_index, ...]
    y_train, y_test = all_labels[train_index], all_labels[test_index]
ys = set(all_labels)
ms = [gmmhmm(7) for y in ys]

_ = [model.train(X_train[y_train == y, :, :]) for m, y in zip(ms, ys)]
ps1 = [model.test(X_test) for m in ms]
res1 = np.vstack(ps1)
predicted_label1 = np.argmax(res1, axis=0)
dictionary = ['apple', 'banana', 'elephant', 'dog', 'frog', 'cat', 'jack', 'god', 'Intelligent', 'hello']
spoken_word = []
for i in predicted_label1:
    spoken_word.append(dictionary[i])
print(spoken_word)
missed = (predicted_label1 != y_test)
print('Test accuracy: %.2f percent' % (100 * (1 - np.mean(missed))))

我尝试了但无法找到我收到此错误的原因以及如何解决此问题。

标签: python-3.xnameerrorhidden-markov-models

解决方案


您在model.train()没有定义model错误消息的情况下调用。

如果您要在列表理解中训练的模型是,m那么它应该是:

[m.train(X_train[y_train == y, :, :]) for m, y in zip(ms, ys)]

以下行需要相同的更改:

[m.test(X_test) for m in ms]


推荐阅读