2017-12-15 1 views
2

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 and y of shape 300 from any normal distribution. Use tf.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] 
+0

'mse' 0 zählt,' Absval "rangiert" 1 "," tf.less (x, y) "zählt" 1 ". Deshalb erhalten Sie den Fehler. – Psidom

+0

@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. –

+0

Ziemlich sicher, dass sie die gleiche Form haben müssen. – Psidom

Antwort

1

Versuchen Sie folgendes:

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) 
Verwandte Themen