python - Tensorflow MeanSquaredError 不适用于一个数字
问题描述
我正在尝试使用张量流均方误差计算网络的损失,但由于某种原因,如果输入张量只有一个数字,它就不起作用。应该怎么做呢。
这是一些代码:
import tensorflow as tf
loss = tf.keras.losses.MeanSquaredError()
a = loss(y_true=tf.constant([1.0, 2.0, 3.0]), y_pred=tf.constant([2.0, 2.0, 4.0]))
print(a)
a = loss(y_true=tf.constant(1.0, dtype=tf.float32), y_pred=tf.constant(2.0, dtype=tf.float32)) #this is where the error occurs.
print(a)
错误
tensorflow.python.framework.errors_impl.InvalidArgumentError:无效的缩减维度(-1 用于 0 维度的输入 [Op:Mean]
解决方案
推荐阅读
- python - Tensorflow 分布式学习不行,core 被转储了?
- android - 两个玩家之间的同步
- angular - 我有两个不同的组件,只有一个共同的导航栏,但是当我单击链接时,两个组件中都会保留一个列表,但我在 ngOnInit 中更新它
- c# - Serenity v5.0 - 模拟不起作用
- laravel - 加载 excel\laravel-excel 失败
- java - 我认为 datasnapshot 值返回 null 因为当我运行它时 TextView 为空,即使数据库 firebase 中有一个名称
- javascript - 如何更改 Recharts 中每个条的颜色?
- python - Python Pandas applymap na_action 参数未被识别
- python - Python/Pandas:根据多个列/行值为列赋值
- swift - 在范围内找不到“SFExtensionMessageKey”