Python Tensorflow MeanSquaredError doens';I don’我不在一个数字上工作
我试图用tensorflow均方误差计算网络的损耗,但由于某种原因,如果输入张量只有一个数,它就不起作用。我们应该怎么做呢 下面是一些代码:Python Tensorflow MeanSquaredError doens';I don’我不在一个数字上工作,python,tensorflow,loss-function,mean-square-error,Python,Tensorflow,Loss Function,Mean Square Error,我试图用tensorflow均方误差计算网络的损耗,但由于某种原因,如果输入张量只有一个数,它就不起作用。我们应该怎么做呢 下面是一些代码: 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
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\u impl.InvalidArgumentError:无效的缩减维度(-1,用于维度为0的输入)[Op:Mean]
您需要传递一个数组:
a=loss(y_true=tf.constant([1.0],dtype=tf.float32),y_pred=tf.constant([2.0],dtype=tf.float32))
。