首页 文章

Tensorflow:有没有办法将训练损失存储在tf.Estimator中

提问于
浏览
0

我正在使用tensorflow估算器对象来训练官方tensorflow图层文档(https://www.tensorflow.org/tutorials/layers)中的模型 . 我可以看到训练期间训练损失会显示在控制台上 . 有没有办法存储这些训练损失值?

谢谢!

2 回答

  • 0

    显示通过 logging.info 完成 . tf.estimator 为训练损失创建 LoggingTensorHook 来执行此操作,请参阅here .

    我想你可以将日志记录输出重新路由到某个文件,但这仍然不会给你原始值 .

    我能想到的两种方式:

    • 编写自己的钩子来存储值;这可能看起来与 LoggingTensorHook 非常相似,你只需要将数字写入文件而不是打印它们 .

    • 默认情况下, tf.estimator 还会在Tensorboard中为培训损失创建摘要数据;你可以在Tensorboard中打开"Scalar"标签,你应该看到损失曲线 . 勾选左上角的"Show data download links" . 这将为您提供下载每个图表的选项's data in either CSV or JSON format. By default, both the logging and summary hooks are set up such that they both log values every 100 steps. So the graph should have the same information you saw in the console. If you'不熟悉Tensorboard,Tensorflow网站上也有教程;基本用法应该很简单!

  • 0

    在estimator.train()训练估算器后,您可以在model_dir中使用TensorBoard事件文件

    model = tf.estimator.Estimator(..., model_dir= 'tmp') 
    # model data will be save in tmp directory after training
    

    image

    事件文件的名称为events.out.tfevents.15121254 ....,此文件保存训练过程的日志(eval文件夹中还有一个保存评估日志的其他事件文件) . 您可以通过以下方式获得培训

    for e in tf.train.summary_iterator(path_to_events_file):
    for v in e.summary.value:
        if v.tag == 'loss':
            print(v.simple_value)
    

    此外,您可以在训练期间通过在model_fn中添加tf.summary来保存其他值:

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

    参考:https://www.tensorflow.org/api_docs/python/tf/train/summary_iterator

相关问题