本文介绍了在add_summary 中为summary.value 中的值:AttributeError: 'Tensor' 对象没有属性'value'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常基本的张量板标量日志:

This is a very basic tensorboard scalar log:

import numpy as np
import tensorflow as tf
a = np.arange(10)
x = tf.convert_to_tensor(a, dtype=tf.float32)
x_summ = tf.summary.scalar("X", x)
writer = tf.summary.FileWriter('/tmp/logdir')
writer.add_summary(x_summ)

但是,我在 add_summary 中为 summary.value 中的值出现错误:

However, I get an error in add_summary for value in summary.value:

AttributeError: 'Tensor' object has no attribute 'value'. 

有什么解决办法吗?

TensorFlow 文档说,当汇总张量的形状或类型错误时,会引发 ValueError.当我打印 x_summ 它显示:

TensorFlow documentation says ValueError is raised when the summary tensor has a wrong shape or type. When I print x_summ it shows:

Tensor("X:0", shape=(), dtype=string)

我不明白为什么形状 NULL 在这里.

I don't understand why is the shape NULL here.

推荐答案

出现值错误是因为您必须在会话中评估摘要节点.

The value error is raised because you have to evaluate the summary node within a session.

with tf.Session() as sess:

    s = sess.run(x_summ)
    writer.add_summary(s)

但请注意,当您尝试在标量摘要中跟踪 10 个值时,这将引发另一个错误.但我想你会在训练期间跟踪一些有意义的变量(比如损失),这并不重要.

But notice that this will raise another error, as you try to track 10 values in a scalar summary. But as I suppose that you will track some meaningful variable during training (like loss), this doesn't matter much.

这篇关于在add_summary 中为summary.value 中的值:AttributeError: 'Tensor' 对象没有属性'value'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 21:27