2016-09-02 121 views
0

我目前有一個網絡,我從16 x 16 x 2輸入張量開始,我執行一些卷積和合並操作,並將其降低到一個如此聲明的張量:Tensorflow Concat錯誤:形狀不匹配

x1 = tf.Variable(tf.constant(1.0, shape=[32])) 

那張張然後通過幾層矩陣乘法和relus在輸出一個類別之前。

我想要做的是通過向上面的向量添加另外10個參數來擴展卷積階段的輸出。

我有當數據在裏面裝的佔位符的定義如下:

x2 = tf.placeholder(tf.float32, [None,10]) 

我想這些變量串連在一起,就像這樣:

xnew = tf.concat(0,[x1,x2]) 

我越來越以下錯誤信息:

ValueError: Shapes (32,) and (10,) are not compatible 

我確信有一些簡單的東西我做錯了,但我看不到它。

回答

0

我真的不明白爲什麼你的None佔位符的形狀。如果你刪除它,它應該工作

+0

如果我這樣做,我得到'ValueError異常:形狀(32)和()不兼容 ' – James

2

x1x2分別有不同的行列,1和2,所以沒有什麼奇怪的,concat失敗。下面是對我工作的例子:

x1 = tf.Variable(tf.constant(1.0, shape=[32])) 
# create a placeholder that will hold another 10 parameters 
x2 = tf.placeholder(tf.float32, shape=[10]) 
# concatenate x1 and x2 
xnew = tf.concat(0, [x1, x2]) 
init = tf.initialize_all_variables() 
with tf.Session() as sess: 
    sess.run(init) 
    _xnew = sess.run([xnew], feed_dict={x2: range(10)}) 

enter image description here

1

的原因很可能在tensorflow版本

從tensorflow最新官方API,tf.conat被定義爲

tf.concat

的concat( 值, 軸, 名= '的concat' )

所以,更好的方法是通過鍵值調用該函數。 我試過下面的代碼,沒有錯誤。

xnew = tf.concat(axis=0, values=[x1, x2]) 

------------------------------------------- -------

複製官方api解釋如下。

tf.concat 的concat( 值, 軸, 名= '的concat' )

在tensorflow /蟒/操作/ array_ops.py定義。

參見引導:張量變換>切片和加入

串接張量沿着一個維度。

沿尺寸軸連接張量值列表。如果values [i] .shape = [D0,D1,... Daxis(i),... Dn],連接結果的形狀爲

[D0,D1,... Raxis,... Dn ] 其中

Raxis = sum(Da​​xis(i)) 也就是說,來自輸入張量的數據沿軸維度連接。

輸入張量的維數必須匹配,除軸以外的所有維必須相等。

例如:

t1 = [[1, 2, 3], [4, 5, 6]] 
t2 = [[7, 8, 9], [10, 11, 12]] 
tf.concat([t1, t2], 0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] 
tf.concat([t1, t2], 1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]] 

# tensor t3 with shape [2, 3] 
# tensor t4 with shape [2, 3] 
tf.shape(tf.concat([t3, t4], 0)) ==> [4, 3] 
tf.shape(tf.concat([t3, t4], 1)) ==> [2, 6] 
相關問題