[英]save and load model in tensorflow 2.0
I saved a model from premade estimator in tensorflow 2.x with this code我使用此代码从 tensorflow 2.x 中的预制估算器中保存了 model
import os
serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
tf.feature_column.make_parse_example_spec(my_feature_columns))
estimator_base_path = os.path.join( 'from_estimator')
estimator_path = classifier.export_saved_model(estimator_base_path, serving_input_fn)
this code create a folder which contains a.pb file i need to reuse this model in the future, i try to load woth this function此代码创建一个包含 a.pb 文件的文件夹,我需要在将来重用此 model,我尝试加载此 function
saved_model_obj = tf.compat.v2.saved_model.load(export_dir="/model_dir/")
but when i try to make a prediction on using the loaded model it raises this error但是当我尝试对使用加载的 model 进行预测时,它会引发此错误
predictions = saved_model_obj.predict(
input_fn=lambda: input_fn(predict_x))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-23-a9902ff8210c> in <module>
----> 1 predictions = saved_model_obj.predict(
2 input_fn=lambda: input_fn(predict_x))
AttributeError: 'AutoTrackable' object has no attribute 'predict'
how can i load a.pb file and make prediction, like if i've never saved and loaded it?我如何加载 a.pb 文件并进行预测,就像我从未保存和加载它一样?
When I save models for later use I usually do this:当我保存模型供以后使用时,我通常这样做:
Assuming that your model is model
:假设您的 model 是
model
:
model.save('my_model.h5')
This will save the modoel in a hdf5 format.这将以 hdf5 格式保存模型。
Then when I have to use it to predict again I can just:然后,当我必须再次使用它进行预测时,我可以:
new_model = tf.keras.models.load_model('my_model.h5')
and than you can new_model.predict()
而不是你可以
new_model.predict()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.