[英]Python3, word2vec, How can I get the list of similarity rank about “price” in my model
[英]How I can save the model python3
**我正在尝试保存模型以在 Web 应用程序中使用它,但出现此错误 **
X = []
sentences = list(review_df['text'])
for sen in sentences:
X.append(clean_text(sen))
y = review_df['Label']
y = np.array(list(map(lambda x: 1 if x=="fake" else 0, y)))
#使用循环神经网络 (LSTM) 进行文本分类
from keras.layers.recurrent import LSTM
model = Sequential()
embedding_layer = Embedding(vocab_size, 100, weights=[embedding_matrix], input_length=maxlen ,
trainable=False)
model.add(embedding_layer)
model.add(LSTM(128))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])
print(model.summary())
#训练模型
history = model.fit(X_train, y_train, batch_size=128, epochs=6, verbose=1, validation_split=0.2)
score = model.evaluate(X_test, y_test, verbose=1)
#打印模型结果
print("Test Score:", score[0])
print("Test Accuracy:", score[1])
#对单个实例进行预测
instance = X[57]
print(instance)
instance = tokenizer.texts_to_sequences(instance)
flat_list = []
for sublist in instance:
for item in sublist:
flat_list.append(item)
flat_list = [flat_list]
instance = pad_sequences(flat_list, padding='post', maxlen=maxlen)
model.predict(instance)
#保存模型
import pickle
with open('model.pkl', 'wb') as f:
pickle.dump(model, f)
当我尝试保存模型时,我收到此错误:TypeError: can't pickle _thread.RLock objects Is there any idea to solve this error
尝试这样做:
model_json = model.to_json()
with open("my_model.json", "w") as json_file:
json_file.write(model_json)
您可以通过直接节省keras模型: model.save('path/xyz.h5')
建议保存H5格式和使用模式model.save()
您也可以加载与格式model.load()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.