Python 3.x 名称错误:名称';型号';未在python代码中为孤立语音识别定义
我从网上下载了代码 该代码与Python2兼容,我是为Python3编写的 但我得到了这个错误:Python 3.x 名称错误:名称';型号';未在python代码中为孤立语音识别定义,python-3.x,nameerror,hidden-markov-models,Python 3.x,Nameerror,Hidden Markov Models,我从网上下载了代码 该代码与Python2兼容,我是为Python3编写的 但我得到了这个错误: ~/Desktop/IsolatedSpeechRecognition-master $ python Isolated_Speech_Recognition.py List of spoken words: ['dog', 'human', 'god', 'eye', 'book', 'fast', 'apple', 'cat'] {'book', 'human', 'fast'
~/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
我尝试了,但无法找到出现此错误的原因以及如何解决此问题。您调用model.train()
,但没有如错误消息所示定义model
如果您想在列表中训练的模型是m
,那么它应该是:
[m.train(X_train[y_train==y,:,:])用于邮政编码中的m,y(ms,ys)]
以下行需要相同的更改:
[m.test(X_test)用于m in ms]
您调用model.train()
,但未定义model
,如错误消息所示
如果您想在列表中训练的模型是m
,那么它应该是:
[m.train(X_train[y_train==y,:,:])用于邮政编码中的m,y(ms,ys)]
以下行需要相同的更改:
[m.test(X_测试)用于m(毫秒)]
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))))