Python 如何解决此错误?名称错误:名称‘;型号’;没有定义
当我尝试输入文本以进行预测时,execute会给出“NameError:名称‘model’未定义” 评估神经网络模型 准备文档的文字编码包Python 如何解决此错误?名称错误:名称‘;型号’;没有定义,python,machine-learning,scikit-learn,deep-learning,Python,Machine Learning,Scikit Learn,Deep Learning,当我尝试输入文本以进行预测时,execute会给出“NameError:名称‘model’未定义” 评估神经网络模型 准备文档的文字编码包 如果您在evaluate\u mode()中执行培训过程,则该模型是一个局部变量,不能与predict\u情绪()共享。你应该让评估模式()返回模型并让预测情绪()将其作为第四个参数。在evaluate\u模式下函数中,如果不返回模型,则不会返回模型,因此会出现此类错误。请在predict\u语句中为下一个预测返回模型。能否显示完整的回溯? def eval
如果您在
evaluate\u mode()
中执行培训过程,则该模型是一个局部变量,不能与predict\u情绪()
共享。你应该让评估模式()
返回模型
并让预测情绪()
将其作为第四个参数。在evaluate\u模式下
函数中,如果不返回模型,则不会返回模型,因此会出现此类错误。请在predict\u语句中为下一个预测返回模型。能否显示完整的回溯?
def evaluate_mode(Xtrain, ytrain, Xtest, ytest):
scores = list()
n_repeats = 2
n_words = Xtest.shape[1]
for i in range(n_repeats):
# define network
model = Sequential()
model.add(Dense(50, input_shape=(n_words,), activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile network
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
model.fit(Xtrain, ytrain, epochs=10, verbose=2)
# evaluate
loss, acc = model.evaluate(Xtest, ytest, verbose=0)
scores.append(acc)
print('%d accuracy: %s' % ((i+1), acc))
return scores
def prepare_data(train_docs, test_docs, mode):
# create the tokenizer
tokenizer = Tokenizer()
# fit the tokenizer on the documents
tokenizer.fit_on_texts(train_docs)
# encode training data set
Xtrain = tokenizer.texts_to_matrix(train_docs, mode=mode)
# encode testing data set
Xtest = tokenizer.texts_to_matrix(test_docs, mode=mode)
return Xtrain, Xtest
def predict_sentiment(review, vocab, tokenizer, model):
# clean
tokens = clean_doc(review)
# filter by vocab
tokens = [w for w in tokens if w in vocab]
# convert to line
line = ' '.join(tokens)
# encode
encoded = tokenizer.texts_to_matrix([line], mode='freq')
# prediction
yhat = model.predict(encoded, verbose=0)
return round(yhat[0,0])