模块 'Tensorflow' 没有属性 'summary'

Module 'Tensorflow' has no attribute 'summary'

我正在执行包含以下几行的程序:

def write_log(callback, name, loss, batch_no):
    """
    Write training summary to TensorBoard
    """
    summary = tf.Summary()
    summary_value = summary.value.add()
    summary_value.simple_value = loss
    summary_value.tag = name
    callback.writer.add_summary(summary, batch_no)
    callback.writer.flush()

summary = tf.Summary() 导致以下错误

Error: AttributeError: module 'tensorflow' has no attribute 'Summary'

我使用的Tensorflow版本是2.3.0。与 'TensorFlow' 相关的其余功能工作正常。

我该如何解决这个问题?

看tensorflow的documentation,现在tensorboard的用法总结不一样了:

writer = tf.summary.create_file_writer("/tmp/mylogs")
with writer.as_default():
  for step in range(100):
    # other model code would go here
    tf.summary.scalar("my_metric", 0.5, step=step)
    writer.flush()