训练时的日志精度度量tf.estim公司

2024-04-26 04:58:08 发布

您现在位置:Python中文网/ 问答频道 /正文

当训练一个预先设定好的估计员时,打印精度指标和损失的最简单方法是什么?在

大多数教程和文档似乎都在解决何时创建自定义估计器的问题——如果打算使用其中一个可用的估计器,那么这似乎有些过头了。在

在tf.contrib.学习有几个(现在已弃用)监视器挂钩。TF现在建议使用hook API,但它实际上并没有提供任何可以利用标签和预测来生成精确数字的东西。在


Tags: 方法文档api利用tf精度教程hook
2条回答

除了@Aldream的答案,您还可以使用TensorBoard来查看custom_metric的一些图形。为此,请将其添加到TensorFlow摘要中,如下所示:

tf.summary.scalar('custom_metric', custom_metric)

当您使用tf.estimator.Estimator时,最酷的事情是您不需要将摘要添加到FileWriter,因为它是自动完成的(默认情况下每100步合并并保存一次)。在

要查看TensorBoard,您需要打开一个新终端并键入:

^{pr2}$

之后,您将能够在浏览器中的localhost:6006上看到图形。在

你试过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)

日志:

^{pr2}$

如您所见,custom_metric将与默认度量和损失一起返回。在

相关问题 更多 >

    热门问题