2017-01-01 45 views
0

我有兩個張量:通過克隆一個張量連接3D張量?

a = tf.placeholder(tf.float32, [None, 20, 100]) 
b = tf.placeholder(tf.float32, [None, 1, 100]) 

我要追加到ba[i, 20, 100],創造cc這樣有[None, 20, 200]的形狀。

這似乎很簡單,但我還沒有想出如何與tf.concat做到這一點:

tf.concat(0, [a, b]) -> Shapes (20, 100) and (1, 100) are not compatible 
tf.concat(1, [a, b]) => shape=(?, 28, 100) which is not what I wanted 
tf.concat(2, [a, b]) -> Shapes (?, 20) and (?, 1) are not compatible 

我需要重塑ab第一然後Concat的?

+0

您需要之前'tf.concat' – martianwars

回答

1

這可以使用tf.tile來完成。您將需要克隆沿維1的張量,20次使其與a兼容。然後,沿着維度2的簡單連接會給你結果。

下面是完整的代碼,

import tensorflow as tf 

a = tf.placeholder(tf.float32, [None, 20, 100]) 
b = tf.placeholder(tf.float32, [None, 1, 100]) 
c = tf.tile(b, [1,20,1]) 
print c.get_shape() 
# Output - (?, 20, 100) 
d = tf.concat(2, [a,c]) 
print d.get_shape() 
# Output - (?, 20, 200) 
+0

非常感謝使用'tf.tile'!實際上,如果我不知道「a」的第二維度,我該怎麼做?即'a = tf.placeholder(tf.float32,[None,None,100]); b = tf.placeholder(tf.float32,[None,1,100])'。 'tf.tile'需要'int32/64'類型的參數。 – Blue482

+1

使用'tf.unpack(tf.shape(a))[1]解決! :) 再次感謝 – Blue482