NameError:在用于隔离语音识别的python代码中未定义名称“model”

问题描述 投票:1回答:1

我从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.x nameerror hidden-markov-models
1个回答
0
投票

如错误消息所示,您可以在不定义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]

© www.soinside.com 2019 - 2024. All rights reserved.