Tensorflow export estimators for prediction

家住魔仙堡 提交于 2019-12-01 10:41:12

问题


I wonder how can I export the estimator and then import it for prediction from MNIST tutorial, Tensorflow's page. Thank you!


回答1:


The Estimator has model_dir args where the model will be saved. So during prediction we use the Estimator and call the predict method which recreates the graph and the checkpoints are loaded.

For the MNIST example, the prediction code would be:

tf.reset_default_graph()

# An input-function to predict the class of new data.
predict_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"x": eval_data},
    num_epochs=1,
    shuffle=False)

mnist_classifier = tf.estimator.Estimator(
      model_fn=cnn_model_fn, model_dir="/tmp/mnist_convnet_model")

#Prediction call
predictions = mnist_classifier.predict(input_fn=predict_input_fn)

pred_class = np.array([p['classes'] for p in predictions]).squeeze()
print(pred_class)

# Output
# [7 2 1 ... 4 5 6]


来源:https://stackoverflow.com/questions/50612923/tensorflow-export-estimators-for-prediction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!