Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow MeanSquaredError doens';I don’我不在一个数字上工作_Python_Tensorflow_Loss Function_Mean Square Error - Fatal编程技术网

Python Tensorflow MeanSquaredError doens';I don’我不在一个数字上工作

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

我试图用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.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))