如何将张量流模型保存到pickle文件
我想保存 Tensorflow 模型,然后将其用于部署目的。我不想使用 model.save() 来保存它,因为我的目的是以某种方式“pickle”它并在未安装tensorflow的不同系统中使用它,例如:
model = pickle.load(open(path, 'rb'))
model.predict(prediction_array)
早些时候使用sklearn,当我正在腌制一个 KNN 模型,它很成功,并且我能够在不安装 sklearn 的情况下运行推理。
但是当我尝试pickle我的Tensorflow模型时,我收到了这个错误:
Traceback (most recent call last):
File "e:/VA_nlu_addition_branch_lite/nlu_stable2/train.py", line 21, in <module>
pickle.dump(model, open('saved/model.p', 'wb'))
TypeError: can't pickle _thread.RLock objects
我的模型看起来像这样:
model = keras.Sequential([
keras.Input(shape=(len(x[0]))),
keras.layers.Dense(units=16, activation='elu'),
keras.layers.Dense(units=8, activation='elu'),
keras.layers.Dense(units=len(y[0]), activation='softmax'),
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x, y, epochs=200, batch_size=8)
pickle.dump(model, open('saved/model.p', 'wb'))
模型摘要
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 16) 1680
_________________________________________________________________
dense_1 (Dense) (None, 8) 136
_________________________________________________________________
dense_2 (Dense) (None, 20) 180
=================================================================
Total params: 1,996
Trainable params: 1,996
Non-trainable params: 0
这是一个有关此问题的 StackOverflow 问题,但答案中的链接已过期。
这里还有另一个类似的问题,但我不太明白。
我有一个非常简单的模型,没有检查点,没有什么复杂的,那么有没有办法将 Tensorflow 模型对象保存到二进制文件?或者即使它是多个二进制文件,我也不介意,但它只是不需要使用tensoflow,如果 numpy 解决方案 会有所帮助,我会使用它,但我不知道如何在这里实现它。任何帮助将不胜感激,谢谢!
I want to save a Tensorflow model and then later use it for deployment purposes. I dont want to use model.save()
to save it because my purpose is to somehow 'pickle' it and use it in a different system where tensorflow is not installed, like:
model = pickle.load(open(path, 'rb'))
model.predict(prediction_array)
Earlier with sklearn, when i was pickling a KNN model, it was successful and i was able to run inference without installing sklearn.
But when I tried to pickle my Tensorflow model, I got this error:
Traceback (most recent call last):
File "e:/VA_nlu_addition_branch_lite/nlu_stable2/train.py", line 21, in <module>
pickle.dump(model, open('saved/model.p', 'wb'))
TypeError: can't pickle _thread.RLock objects
My model looks like this:
model = keras.Sequential([
keras.Input(shape=(len(x[0]))),
keras.layers.Dense(units=16, activation='elu'),
keras.layers.Dense(units=8, activation='elu'),
keras.layers.Dense(units=len(y[0]), activation='softmax'),
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x, y, epochs=200, batch_size=8)
pickle.dump(model, open('saved/model.p', 'wb'))
Model summary
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 16) 1680
_________________________________________________________________
dense_1 (Dense) (None, 8) 136
_________________________________________________________________
dense_2 (Dense) (None, 20) 180
=================================================================
Total params: 1,996
Trainable params: 1,996
Non-trainable params: 0
Here is a StackOverflow question regarding this problem, but the link in the answer was expired.
Also here is another similar question, but i didn't quite get it.
I have a very simple model, no checkpoints, nothing much complicated, so is there some way to save the Tensorflow model object to a binary file? Or even if its multiple binary files, i dont mind, but it just doesn't need to use tensoflow, if the numpy solution would help, i would use that, but i dont know how to implement it here. Any help would be appreciated, Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
joblib
似乎适用于TF
2.8,并且由于您有一个非常简单的模型,因此您可以在 Google Colab 上训练它,然后只需在其他系统上使用 pickled 文件:不使用
tf
加载模型:但是,在不了解系统规格的情况下,很难判断它是否真的那么“直接”。
Using
joblib
seems to work onTF
2.8 and since you have a very simple model, you can train it on Google Colab and then just use the pickled file on your other system:Load model without
tf
:But it is hard to tell if it is really that "straight-forward" without knowing your system specs.
对于使用
keras
创建的TensorFlow
模型,请将模型保存在.h5
扩展名中,如此处类似答案中所述:错误,不成功的 TensorSliceReader 构造函数:无法找到 ram 的任何匹配文件来 unpickle 文件:
For
TensorFlow
models created usingkeras
, please save models in.h5
extension as explained in a similar answer here:Error , Unsuccessful TensorSliceReader constructor: Failed to find any matching files for ram to unpickle a file: