TensorFlow 使用 tf.summary.FileWriter 添加对标量的统计与观察时报错

报错信息:

InvalidArgumentError (see above for traceback): tags and values not the same shape: [] != [1,1] (tag 'y')
         [[node y (defined at <stdin>:1)  = ScalarSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](y/tags, add/_5)]]

 

报错源码

(1) 二维数据

>>> import tensorflow as tf
>>> import math
>>> import numpy as np
>>> x = np.linspace(-math.pi,math.pi,300)[:,np.newaxis]
>>> x.shape
# (300, 1)
>>> noise = np.random.normal(loc=0.0, scale=0.9, size=x.shape)
>>> noise.shape
# (300, 1)
>>> x_holder = tf.placeholder(dtype=tf.float32, shape=[None,1], name="x")
>>> x_holder
# <tf.Tensor 'x:0' shape=(?, 1) dtype=float32>
>>> noise_holder = tf.placeholder(dtype=tf.float32, shape=[None,1], name="x")
>>> noise_holder
# <tf.Tensor 'x_1:0' shape=(?, 1) dtype=float32>
>>> y = tf.math.sin(x=x_holder) + noise_holder
>>> 
# <tf.Tensor 'add:0' shape=(?, 1) dtype=float32>

>>> summary_writer = tf.summary.FileWriter("./summaries/logdir/")
>>> tf.summary.scalar(name="scalar_y", tensor=y)
>>> merged_summary = tf.summary.merge_all()
>>> init_op = tf.global_variables_initializer()
>>> sess = tf.InteractiveSession()
>>> sess.run(init_op)
>>> for i in range(300):
    each_x = np.asarray(a=[x[i]], dtype=np.float32)
    each_noise = np.asarray(a=[noise[i]], dtype=np.float32)
    summary_result = sess.run(fetches=merged_summary, feed_dict={x_holder:each_x, noise_holder:each_noise})
    summary_writer.add_summary(summary=summary_result, global_step=i)

(2)一维数据

>>> import tensorflow as tf
>>> import math
>>> import numpy as np

>>> x = np.linspace(-math.pi,math.pi,1000)
>>> x.shape
# (1000,)
>>> noise = np.random.normal(loc=0.0, scale=0.9, size=x.shape)
>>> noise.shape
# (1000,)

>>> x_holder = tf.placeholder(dtype=tf.float32, shape=[None], name="x")
>>> x_holder
# <tf.Tensor 'x:0' shape=(?,) dtype=float32>
>>> noise_holder = tf.placeholder(dtype=tf.float32, shape=[None], name="x")
>>> noise_holder
# <tf.Tensor 'x_1:0' shape=(?,) dtype=float32>

>>> y = tf.math.sin(x=x_holder) + noise_holder
>>> y
# <tf.Tensor 'add:0' shape=(?,) dtype=float32>


>>> summary_writer = tf.summary.FileWriter("./summaries/logdir_1d_sin/")
>>> tf.summary.scalar(name="scalar_y", tensor=y)
# <tf.Tensor 'scalar_y:0' shape=() dtype=string>

>>> merged_summary = tf.summary.merge_all()

>>> init_op = tf.global_variables_initializer()

>>> sess = tf.InteractiveSession()
>>> sess.run(init_op)

>>> for i in range(1000):
    each_x = np.asarray(a=[x[i]], dtype=np.float32)
    each_noise = np.asarray(a=[noise[i]], dtype=np.float32)
    summary_result = sess.run(fetches=merged_summary, feed_dict={x_holder:each_x, noise_holder:each_noise})
    summary_writer.add_summary(summary=summary_result, global_step=i)
>>> sess.close()

 

解决方法:将输出结果 y 转换为标量,即在  y = tf.math.sin(x=x_holder) + noise_holder 之后添加

y = tf.reduce_mean(input_tensor=y)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn-WJW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值