python – 如何解决此错误? NameError:未定义名称“model”

weixin_38056302 2019-09-12 10:56:35
当我尝试输入文本以进行预测时,执行给我“NameError:name’model’未定义” 评估神经网络模型 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])
...全文
1467 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38056905 2019-09-12
  • 打赏
  • 举报
回复
如果在evaluate_mode()中执行训练过程,则模型是局部变量,不能与predict_sentiment()共享.你应该使evaluate_mode()返回模型,让predict_sentiment()将它作为第四个参数.

476

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧