**我正在尝试保存该模型以在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())
#train模型
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模型结果
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:无法腌制_thread.RLock对象 是否有解决此错误的想法
答案 0 :(得分:0)
尝试这样做:
model_json = model.to_json()
with open("my_model.json", "w") as json_file:
json_file.write(model_json)
答案 1 :(得分:0)
您可以通过以下方式直接保存keras模型:
model.save('path/xyz.h5')
建议将模型保存为H5格式并使用model.save()
您也可以使用model.load()