用于预测的 Tensorflow 导出估算器

Tensorflow export estimators for prediction

我想知道如何导出估计器,然后从 MNIST 教程中导入它进行预测,Tensorflow's page。 谢谢!

Estimatormodel_dir 个参数用于保存模型。因此,在预测期间,我们使用 Estimator 并调用 predict 方法重新创建图形并加载检查点。

对于 MNIST 示例,预测代码为:

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]