简体   繁体   English

Tensorflow错误:形状必须为0级,但对于'cond_1 / Switch'为1级

[英]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 and y of shape 300 from any normal distribution. 从任何正态分布创建两个形状300的张量xy Use tf.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.

相关问题 Tensorflow Shape必须为1级但为2级 - Tensorflow Shape must be rank 1 but is rank 2 Tensorflow错误“形状Tensorshape()必须具有等级1” - Tensorflow error “shape Tensorshape() must have rank 1” Tensorflow: ValueError: Shape must be rank 4 but is rank 5 - Tensorflow: ValueError: Shape must be rank 4 but is rank 5 TensorFlow 推荐者 - ValueError:形状必须为 2 级,但为 3 级 - TensorFlow Recommenders - ValueError: Shape must be rank 2 but is rank 3 Tensorflow:ValueError:Shape必须是等级2,但是等级3 - Tensorflow : ValueError: Shape must be rank 2 but is rank 3 TensorFlow: Value Error Shape and Rank Do Not Match: ValueError: Shape (?, 128, 128, 2) must have rank 2 - TensorFlow: Value Error Shape and Rank Do Not Match: ValueError: Shape (?, 128, 128, 2) must have rank 2 Tensorflow DrawBoundingBoxes 的形状等级错误 - Tensorflow Shape rank error for DrawBoundingBoxes Tensorflow - ValueError:Shape必须为1,但对于&#39;ParseExample / ParseExample&#39;为0 - Tensorflow - ValueError: Shape must be rank 1 but is rank 0 for 'ParseExample/ParseExample' Tensorflow:形状必须为5级,但&#39;conv3D&#39;为1级 - Tensorflow: shape must be rank 5 but is rank 1 for 'conv3D' Tensorflow:optimizer.minimize()-“ ValueError:形状必须为0级,但为1级 - Tensorflow: optimizer.minimize() - "ValueError: Shape must be rank 0 but is rank 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM