[英]Tensorflow error: Shape must be rank 0 but is rank 1 for 'cond_1/Switch'
I am new to tensorflow and I am following some online exercises to get familiar with tensorflow. 我是tensorflow的新手,我正在进行一些在线练习以熟悉tensorflow。 I want to do the following task:
我想做以下任务:
Create two tensors
x
andy
of shape 300 from any normal distribution.从任何正态分布创建两个形状300的张量
x
和y
。 Usetf.cond()
to return:使用
tf.cond()
返回:
The mean squared error of
(x - y)
, if the average of all elements in(x - y)
is negative.均方误差
(x - y)
如果在所有元素的平均(x - y)
为负。The sum of absolute value of all elements in the tensor
(x - y)
otherwise.否则,张量中所有元素的绝对值之和
(x - y)
。
My implementation: 我的实施:
x = tf.random_normal([300])
y = tf.random_normal([300])
mse = lambda: tf.losses.mean_squared_error(y, x)
absval = lambda: tf.abs(tf.subtract(x, y))
out = tf.cond(tf.less(x, y), mse, absval)
Error: 错误:
Shape must be rank 0 but is rank 1 for 'cond_1/Switch' (op: 'Switch') with input shapes: [300], [300]
Try this: 试试这个:
x = tf.random_normal([300])
y = tf.random_normal([300])
mse = lambda: tf.losses.mean_squared_error(y, x)
absval = lambda: tf.reduce_sum(tf.abs(tf.subtract(x, y)))
out = tf.cond(tf.reduce_mean(x - y) < 0, mse, absval)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.