我按照教程"A Guide to TF Layers: Building a Convolutional Neural Network"(这里是代码:https://github.com/tensorflow/tensorflow/blob/r1.1/tensorflow/examples/tutorials/layers/cnn_mnist.py) .
我根据自己的需求调整了教程,即手动检测 .
据我所知,本教程创建了估算器(它是一个CNN),然后进行拟合,最后,它评估估计器的性能 . 现在,我的问题是我想在另一个文件中使用estimator,这将是我的主程序 . 如何从其他文件访问估算器?每次我想使用它时,我是否必须适合估算器? (我希望不是)
我想知道是否有人可以帮助我了解如何保存估算器以便以后使用它 . (据我所知,我不能用 tf.train.Saver
创建一个保护程序,因为我没有运行会话) .
这是我的 train.py
文件中的代码:
def main(unused_argv):
#Load training and eval data (part missing)
# Create the estimator
hand_detector = learn.Estimator(model_fn=cnn_model_fn, model_dir="\cnn_model_fn")
# Set up logging for predictions
# Log the values in the "Softmax" tensor with label "probabilities"
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=50)
# Train the model
hand_detector.fit(
x=train_data,
y=train_labels,
batch_size=100,
steps=20000,
monitors=[logging_hook])
# Configure the accuracy metric for evaluation
metrics = {
"accuracy":
learn.MetricSpec(
metric_fn=tf.metrics.accuracy, prediction_key="classes"),
}
# Evaluate the model and print results
eval_results = hand_detector.evaluate(
x=eval_data, y=eval_labels, metrics=metrics)
print(eval_results)
# Save the model for later use (part missing!)
2 回答
几乎所有机器学习的实际应用都试图训练一次模型,然后将其保存以备将来使用新数据 . 大多数分类器在训练阶段花费数小时,在测试阶段只需几秒钟,因此基本学习如何成功保存训练有素的模型 .
我将解释如何导出"high level" Tensorflow模型(使用 export_savedmodel ) . 函数 export_savedmodel 需要参数serving_input_receiver_fn,即不带参数的函数,它定义模型和预测变量的输入 . 因此,您必须创建自己的 serving_input_receiver_fn ,其中模型输入类型与训练脚本中的模型输入匹配,并且预测变量输入类型与测试脚本中的预测变量输入匹配 . 另一方面,如果创建自定义模型,则必须定义由函数 tf.estimator.export.PredictOutput 定义的export_outputs,该输入是一个字典,用于定义必须与测试脚本中的预测变量输出名称匹配的名称 .
例如:
TRAINING SCRIPT
测试脚本
(在Python 3.6.3中测试的代码,Tensorflow 1.4.0)
Estimator
具有export_savedmodel
成员函数用于此目的 . 你会找到文档here .