简体   繁体   English

尝试存储神经网络时获取“无法腌制 _thread.RLock 对象”

[英]Getting 'can't pickle _thread.RLock objects' when trying to store a neural network

I am currently training a neural network and I try to store the trained model for future use.我目前正在训练一个神经网络,并尝试存储训练后的模型以备将来使用。 The model is based on Sequential from keras (see below).该模型基于keras Sequential (见下文)。 I am using joblib.dump(model, output_file_gen) to store the information.我正在使用joblib.dump(model, output_file_gen)来存储信息。 However, I get the error message:但是,我收到错误消息:

TypeError: can't pickle _thread.RLock objects.

I have looked at some StackOverflow posts regarding this error message and it seems to relate to multithreading.我查看了一些关于此错误消息的 StackOverflow 帖子,它似乎与多线程有关。 I am not sure what happens in the model but maybe somebody can give me advice how to store the model either by taking steps to get rid of this error or by suggesting a better route to store a neural network.我不确定模型中会发生什么,但也许有人可以通过采取措施消除此错误或通过建议更好的方法来存储神经网络来给我建议如何存储模型。

NN setup is included below: NN 设置如下:

model = Sequential()

model.add(Dense(256, input_dim=self.latent_dim))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(1024))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(np.prod(self.img_shape), activation='tanh'))
model.add(Reshape(self.img_shape))

It is not recommended to use pickle or cPickle to save a Keras model.which is the cause of the error here (loosely reasoned)不建议使用 pickle 或 cPickle 来保存 Keras 模型。这是这里错误的原因(松散推理)

You can use model.save(filepath) to save the model into a single HDF5 file which will contain:您可以使用model.save(filepath)将模型保存到单个 HDF5 文件中,该文件将包含:

  1. the architecture of the model, allowing to re-create the model模型的架构,允许重新创建模型
  2. the weights of the model模型的权重
  3. the training configuration (loss, optimizer)训练配置(损失,优化器)
  4. 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/reload your model.然后您可以使用keras.models.load_model(filepath)重新实例化/重新加载您的模型。

The above will use a lot of disk space.以上将使用大量磁盘空间。 so you can alternatively save the model weights.因此您可以选择保存模型权重。 see here for more details请参阅此处了解更多详情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 TypeError:无法pickle _thread.RLock 对象 - Getting TypeError: can't pickle _thread.RLock objects 使用Web服务时无法腌制_thread.RLock对象 - can't pickle _thread.RLock objects when using a webservice 尝试腌制 ML 模型无法在 google colab 中腌制 _thread.RLock 对象 - trying to pickle ML model can't pickle _thread.RLock objects in google colab 类型错误:无法pickle _thread.RLock 对象 - TypeError: can't pickle _thread.RLock objects TypeError:无法在 python 3 中腌制 _thread.RLock 对象 - TypeError: can't pickle _thread.RLock objects in python 3 TypeError: can't pickle _thread.RLock objects in pandas with multiprocessing - TypeError: can't pickle _thread.RLock objects in pandas with multiprocessing TypeError: can't pickle _thread.RLock objects (Deep Learning) - TypeError: can't pickle _thread.RLock objects ( Deep Learning) Tensflow Keras: TypeError: can't pickle _thread.RLock objects when using multiprocessing - Tensflow Keras: TypeError: can't pickle _thread.RLock objects when using multiprocessing “TypeError: can't pickle _thread.RLock objects”,同时使用 pickle 保存 Facebook Prophet 模型 - "TypeError: can't pickle _thread.RLock objects" while saving Facebook Prophet model using pickle 使用pickle保存keras模型时面临“无法pickle _thread.rlock对象”错误 - Facing 'can't pickle _thread.rlock objects' error while saving keras model using pickle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM