PicklingError:不能使< class'module'>的pickle:内置的属性查找模块失败 [英] PicklingError: Can't pickle <class 'module'>: attribute lookup module on builtins failed
本文介绍了PicklingError:不能使< class'module'>的pickle:内置的属性查找模块失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们可以自己保存任何创建的LSTM模型吗?我相信腌制"是将python对象序列化为文件的标准方法.理想情况下,我想创建一个包含一个或多个函数的python模块,这些函数允许我指定要加载的LSTM模型,或者使用硬编码的预拟合模型根据传入的数据初始化模型以生成预测.
Can we save any of the created LSTM models themselves? I believe that "pickling" is the standard method to serialize python objects to a file. Ideally, I wanted to create a python module that contained one or more functions that either allowed me to specify an LSTM model to load or used a hard-coded pre-fit model to generate forecasts based on data passed in to initialize the model.
我尝试使用它,但是给了我一个错误.
I tried to use it but gave me an error.
我使用的代码:
# create and fit the LSTM network
batch_size = 1
model = Sequential()
model.add(LSTM(50, batch_input_shape=(batch_size, look_back, 1), stateful=True, return_sequences=True))
model.add(Dropout(0.3))
model.add(Activation('relu'))
model.add(LSTM(50, batch_input_shape=(batch_size, look_back, 1), stateful=True))
model.add(Dropout(0.3))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('relu'))
model.compile(loss='mean_squared_error', optimizer='adam', metrics = ['accuracy'])
for i in range(10):
model.fit(trainX, trainY, epochs=1, batch_size=batch_size, verbose=2, shuffle=False)
model.reset_states()
with open ('sequential.pickle','wb') as f:
pickle.dump(model,f)
pickle_in = open ('sequential.pickle','rb')
model = pickle.load(pickle_in)
# make predictions
trainPredict = model.predict(trainX, batch_size=batch_size)
model.reset_states()
testPredict = model.predict(testX, batch_size=batch_size)
推荐答案
查看全文