训练 tf.estimator 时记录准确度指标
Posted
技术标签:
【中文标题】训练 tf.estimator 时记录准确度指标【英文标题】:Log accuracy metric while training a tf.estimator 【发布时间】:2018-09-28 03:37:21 【问题描述】:在训练预先确定的估计器时,打印准确度指标和损失的最简单方法是什么?
大多数教程和文档似乎都解决了您何时创建自定义估算器的问题——如果打算使用其中一个可用估算器,这似乎有点矫枉过正。
tf.contrib.learn 有几个(现已弃用)监视器挂钩。 TF 现在建议使用 hook API,但似乎它实际上并没有任何可以利用标签和预测来生成准确度数字的东西。
【问题讨论】:
【参考方案1】:你试过tf.contrib.estimator.add_metrics(estimator, metric_fn)
(doc)吗?它需要一个初始化的估算器(可以预先设定)并向其添加metric_fn
定义的指标。
用法示例:
def custom_metric(labels, predictions):
# This function will be called by the Estimator, passing its predictions.
# Let's suppose you want to add the "mean" metric...
# Accessing the class predictions (careful, the key name may change from one canned Estimator to another)
predicted_classes = predictions["class_ids"]
# Defining the metric (value and update tensors):
custom_metric = tf.metrics.mean(labels, predicted_classes, name="custom_metric")
# Returning as a dict:
return "custom_metric": custom_metric
# Initializing your canned Estimator:
classifier = tf.estimator.DNNClassifier(feature_columns=columns_feat, hidden_units=[10, 10], n_classes=NUM_CLASSES)
# Adding your custom metrics:
classifier = tf.contrib.estimator.add_metrics(classifier, custom_metric)
# Training/Evaluating:
tf.logging.set_verbosity(tf.logging.INFO) # Just to have some logs to display for demonstration
train_spec = tf.estimator.TrainSpec(input_fn=lambda:your_train_dataset_function(),
max_steps=TRAIN_STEPS)
eval_spec=tf.estimator.EvalSpec(input_fn=lambda:your_test_dataset_function(),
steps=EVAL_STEPS,
start_delay_secs=EVAL_DELAY,
throttle_secs=EVAL_INTERVAL)
tf.estimator.train_and_evaluate(classifier, train_spec, eval_spec)
日志:
...
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Evaluation [20/200]
INFO:tensorflow:Evaluation [40/200]
...
INFO:tensorflow:Evaluation [200/200]
INFO:tensorflow:Finished evaluation at 2018-04-19-09:23:03
INFO:tensorflow:Saving dict for global step 1: accuracy = 0.5668, average_loss = 0.951766, custom_metric = 1.2442, global_step = 1, loss = 95.1766
...
如您所见,custom_metric
与默认指标和损失一起返回。
【讨论】:
我认为 OP 想要一个适用于预制估算器的解决方案,您不应该直接更改model_fn
。因此,这可能不是我们想要的答案。
如果我错了请纠正我,但我相信即使 Github 上的示例展示了一个自定义分类器,提取行中显示的钩子的使用也适用于任何预制的估计器,因为它们都使用hooks
参数实现train(...)
、evaluate(...)
、predict(...)
。
一个快速的想法是 logging_hook
需要在 model_fn
中计算准确度,并且需要准确度的名称。您提取的第 185 行来自 model_fn
内部,这对于预制估算器来说将是无法控制的。现在我不确定是否有可以使用的预制估算器的对应物。
嗯,你可能是对的。我从 OP 的帖子中假设他已经以某种方式访问了预测张量。我已经更新了我的帖子,希望使用tf.contrib.estimator.add_metrics
得到更清晰的答案。
很好的答案!这似乎是正确的解决方案。真的学到了新东西,tf.contrib.estimator.add_metrics
。谢谢!【参考方案2】:
除了@Aldream 的回答,您还可以使用TensorBoard 来查看custom_metric
的一些图形。为此,请将其添加到 TensorFlow 摘要中,如下所示:
tf.summary.scalar('custom_metric', custom_metric)
当您使用tf.estimator.Estimator
时,很酷的事情是您不需要将摘要添加到FileWriter
,因为它是自动完成的(默认情况下每 100 步合并和保存它们)。
要查看 TensorBoard,您需要打开一个新终端并输入:
tensorboard --logdir=$MODEL_DIR
之后,您将能够在浏览器中localhost:6006
看到图形。
【讨论】:
以上是关于训练 tf.estimator 时记录准确度指标的主要内容,如果未能解决你的问题,请参考以下文章
如何从检查点使用 tf.estimator.Estimator 进行预测?
如何使用自定义 tf.Estimator 在张量板事件文件中仅创建一份图形副本?