2017-04-17 6 views
1

Zuerst finde ich hier eine andere Frage No broadcasting for tf.matmul in TensorFlow
Aber diese Frage löst nicht mein Problem.keine Übertragung für tf.matmul in Tensorflow für 4D 3D Tensoren

Mein Problem ist eine Charge von Matrizen multiplizieren eine weitere Charge von Vektoren.

x=tf.placeholder(tf.float32,shape=[10,1000,3,4]) 
y=tf.placeholder(tf.float32,shape=[1000,4]) 

x eine Charge von matrices.There 10 sind * 1000 matrices.Each Matrix von Form [3,4]
y eine Charge von vectors.There ist, werden 1000 vectors.Each Vektor der Form [4]
Dim 1 von x und dim 0 von y sind gleich. (hier 1000)
Wenn tf.matmul Rundfunk unterstützt hatte, konnte ich schreiben

y=tf.reshape(y,[1,1000,4,1]) 
result=tf.matmul(x,y) 
result=tf.reshape(result,[10,1000,3]) 

Aber tf.matmul unterstützt nicht
Rundfunk Wenn ich den Ansatz der Frage verwende ich oben

verwiesen
x=tf.reshape(x,[10*1000*3,4]) 
y=tf.transpose(y,perm=[1,0]) #[4,1000] 
result=tf.matmul(x,y) 
result=tf.reshape(result,[10,1000,3,1000]) 

Das Ergebnis hat die Form [10,1000,3,1000], nicht [10,1000,3].
Ich weiß nicht, wie Sie die redundante 1000
Wie bekomme ich das gleiche Ergebnis wie die tf.matmul, die das Senden unterstützt?

Antwort

1

Ich löse es selbst.

x=tf.transpose(x,perm=[1,0,2,3]) #[1000,10,3,4] 
x=tf.reshape(x,[1000,30,4]) 
y=tf.reshape(y,[1000,4,1]) 
result=tf.matmul(x,y) #[1000,30,1] 
result=tf.reshape(result,[1000,10,3]) 
result=tf.transpose(result,perm=[1,0,2]) #[10,1000,3] 
Verwandte Themen