Ich bin neu bei Tensorflow und folge einigen Online-Übungen, um mich mit Tensorflow vertraut zu machen. Ich möchte folgende Aufgabe tun:Tensorflow-Fehler: Shape muss Rang 0 sein, ist aber Rang 1 für 'cond_1/Switch'
Create two tensors
x
andy
of shape 300 from any normal distribution. Usetf.cond()
to return:
The mean squared error of
(x - y)
, if the average of all elements in(x - y)
is negative.The sum of absolute value of all elements in the tensor
(x - y)
otherwise.
Meine Implementierung:
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)
Fehler:
Shape must be rank 0 but is rank 1 for 'cond_1/Switch' (op: 'Switch') with input shapes: [300], [300]
'mse' 0 zählt,' Absval "rangiert" 1 "," tf.less (x, y) "zählt" 1 ". Deshalb erhalten Sie den Fehler. – Psidom
@Psidom Ich sehe, danke! Soll ich pred, true_fn und false_fn gleichrangig machen? Die API sagt nicht explizit, welche von denen denselben Rang haben sollte. –
Ziemlich sicher, dass sie die gleiche Form haben müssen. – Psidom