Tensorboard在TF2.0中显示没有keras.fit或estimator的标量

时间:2019-06-29 08:55:14

标签: tensorboard tensorflow2.0

我已阅读TF 2.0教程:Using TensorBoard with other methods 然后我写了这个简单的代码,但似乎不起作用:

import tensorflow as tf

train_loss = tf.keras.metrics.Mean('train_loss', dtype=tf.float32)
test_loss = tf.keras.metrics.Mean('test_loss', dtype=tf.float32)

train_summary_writer = tf.summary.create_file_writer('logs/tr')
test_summary_writer = tf.summary.create_file_writer('logs/ts')

for ep in range(1000):
    train_loss(1*ep) # here I just want to display 1*ep...
    test_loss(2*ep) 
    tf.summary.scalar('trloss', train_loss.result(), step=ep)
    tf.summary.scalar('tsloss', test_loss.result(), step=ep)

%tensorboard --logdir logs

我得到了(在浏览器中):

No scalar data was found.
Probable causes:

You haven’t written any scalar data to your event files.
TensorBoard can’t find your event files.

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我自己回答:

with train_summary_writer.as_default():
    tf.summary.scalar('trloss', train_loss.result(), step=ep)
with test_summary_writer.as_default():
    tf.summary.scalar('tsloss', test_loss.result(), step=ep)

有效!但是,我找不到与此相关的任何文档。