如何在此代码段中使用 pickle?
How do I use pickle in this code snippet?
我有一个简单的代码片段来训练模型,但是当我使用 pickle 保存模型以备将来使用时,它给了我一个
错误信息:
cannot pickle thread.LOCK objects
我使用了不止一种格式的泡菜,但它给了我同样的错误。
import pickle
model = keras.Sequential([
keras.layers.Dense(SHAPE, input_shape=(SHAPE,)),
keras.layers.Dense(300, activation='sigmoid'),
keras.layers.Dense(10, activation='softmax')
])
#****************** COMPILING THE MODE *****************
LEARNING_RATE = 0.0005
model.compile(optimizer=keras.optimizers.Adam(lr=LEARNING_RATE),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# *********** TRAINING THE MODEL **********
EPOCHS = 20
BATCH_SIZE=50
history_original_data = model.fit(X_original_train_images, y_original_train_labels, epochs=EPOCHS, batch_size=BATCH_SIZE)
hist_original=history_original_data.history
### PICKLE TO SAVE THE MODEL TO BE USED WITHOU PRO-TRAINING IT
pickname ="SequentialNeuroNetwork.pkl"
PickleSeq = open(pickname, 'wb')
pickle.dump(model, PickleSeq)
PickleSeq.close()
我原以为上面的代码片段 运行 会很顺利,但它让我付出了代价。
您使用的是哪个版本的keras?我几乎可以肯定旧版本不支持 pickle。
或者,建议使用 model.save()
将您的模型保存在 keras 中。正如keras页面常见问题解答中所述:
You can use model.save(filepath) to save a Keras model into a single
HDF5 file which will contain:
- the architecture of the model, allowing to re-create the model
- the weights of the model
- the training configuration (loss, optimizer)
- the state of the optimizer, allowing to resume training exactly where you left off.
You can then use keras.models.load_model(filepath)
to reinstantiate
your model. load_model will also take care of compiling the model
using the saved training configuration (unless the model was never
compiled in the first place).
来源:https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model
我有一个简单的代码片段来训练模型,但是当我使用 pickle 保存模型以备将来使用时,它给了我一个 错误信息:
cannot pickle thread.LOCK objects
我使用了不止一种格式的泡菜,但它给了我同样的错误。
import pickle
model = keras.Sequential([
keras.layers.Dense(SHAPE, input_shape=(SHAPE,)),
keras.layers.Dense(300, activation='sigmoid'),
keras.layers.Dense(10, activation='softmax')
])
#****************** COMPILING THE MODE *****************
LEARNING_RATE = 0.0005
model.compile(optimizer=keras.optimizers.Adam(lr=LEARNING_RATE),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# *********** TRAINING THE MODEL **********
EPOCHS = 20
BATCH_SIZE=50
history_original_data = model.fit(X_original_train_images, y_original_train_labels, epochs=EPOCHS, batch_size=BATCH_SIZE)
hist_original=history_original_data.history
### PICKLE TO SAVE THE MODEL TO BE USED WITHOU PRO-TRAINING IT
pickname ="SequentialNeuroNetwork.pkl"
PickleSeq = open(pickname, 'wb')
pickle.dump(model, PickleSeq)
PickleSeq.close()
我原以为上面的代码片段 运行 会很顺利,但它让我付出了代价。
您使用的是哪个版本的keras?我几乎可以肯定旧版本不支持 pickle。
或者,建议使用 model.save()
将您的模型保存在 keras 中。正如keras页面常见问题解答中所述:
You can use model.save(filepath) to save a Keras model into a single HDF5 file which will contain:
- the architecture of the model, allowing to re-create the model
- the weights of the model
- the training configuration (loss, optimizer)
- the state of the optimizer, allowing to resume training exactly where you left off.
You can then use
keras.models.load_model(filepath)
to reinstantiate your model. load_model will also take care of compiling the model using the saved training configuration (unless the model was never compiled in the first place).
来源:https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model