在使用tensorboard来可视化参数变化时,报出这样的错误:
InvalidArgumentError (see above for traceback): tags and values not the same shape: [] != [100] (tag 'loss/loss')
[[node loss/loss (defined at E:/PycharmProjects/tensorboard/Tensorboard_View_Data.py:69) = ScalarSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](loss/loss/tags, loss/softmax_cross_entropy_with_logits/_47)]]
报错源码如下:
# 损失函数
with tf.name_scope('loss'):
loss = tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=prediction)
tf.summary.scalar(name = "loss", tensor=loss) # 分析一下loss的变化
这里出错的原因是定义的 loss 采用 sparse_softmax_cross_entropy_with_logits
loss = tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=prediction)
查阅 TensorFlow API:https://tensorflow.google.cn/api_docs/python/tf/losses/sparse_softmax_cross_entropy
sparse_softmax_cross_entropy_with_logits 返回:Weighted loss Tensor of the same type as logits. If reduction is NONE, this has the same shape as labels; otherwise, it is scalar.
即返回和输入标签具有相同形状的张量。
解决方法:
将 tf.summary.scalar(name="loss", tensor=loss) 改为 tf.summary.histogram(name="loss", values=loss)