【Tensorflow】tf.concat函數

tf.concat是連接兩個矩陣的操作

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

----   tensorflow 1.3.0 以前

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

tf.concat(concat_dim, values, name='concat')

除去name參數用以指定該操作的name,與方法有關的一共兩個參數:

第一個參數concat_dim:必須是一個數,表明在哪一維上連接

     如果concat_dim是0,那麼在某一個shape的第一個維度上連,對應到實際,就是疊放到列上

[python] view plain copy
  1. t1 = [[123], [456]]  
  2. t2 = [[789], [101112]]  
  3. tf.concat(0, [t1, t2]) == > [[123], [456], [789], [101112]]  
             如果concat_dim是1,那麼在某一個shape的第二個維度上連

[python] view plain copy
  1. t1 = [[123], [456]]  
  2. t2 = [[789], [101112]]  
  3. tf.concat(1, [t1, t2]) ==> [[123789], [456101112  

             如果有更高維,最後連接的依然是指定那個維:

             values[i].shape = [D0, D1, ... Dconcat_dim(i), ...Dn]連接後就是:[D0, D1, ... Rconcat_dim, ...Dn]

[python] view plain copy
  1. # tensor t3 with shape [2, 3]  
  2. # tensor t4 with shape [2, 3]  
  3. tf.shape(tf.concat(0, [t3, t4])) ==> [43]  
  4. tf.shape(tf.concat(1, [t3, t4])) ==> [26]  

第二個參數values:就是兩個或者一組待連接的tensor了


/×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××/

這裏要注意的是:如果是兩個向量,它們是無法調用

[python] view plain copy
  1. tf.concat(1, [t1, t2])  
來連接的,因爲它們對應的shape只有一個維度,當然不能在第二維上連了,雖然實際中兩個向量可以在行上連,但是放在程序裏是會報錯的

如果要連,必須要調用tf.expand_dims來擴維:

[python] view plain copy
  1. t1=tf.constant([1,2,3])  
  2. t2=tf.constant([4,5,6])  
  3. #concated = tf.concat(1, [t1,t2])這樣會報錯  
  4. t1=tf.expand_dims(tf.constant([1,2,3]),1)  
  5. t2=tf.expand_dims(tf.constant([4,5,6]),1)  
  6. concated = tf.concat(1, [t1,t2])#這樣就是正確的  


tf.concat是連接兩個矩陣的操作

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

----   tensorflow 1.3.0 以後

----   否則會報如下錯誤

----   Shapes (2, 2, 3) and () are incompatible

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

tf.concat(values, concat_dim, name='concat')

除去name參數用以指定該操作的name,與方法有關的一共兩個參數:

第二個參數concat_dim:必須是一個數,表明在哪一維上連接

     如果concat_dim是0,那麼在某一個shape的第一個維度上連,對應到實際,就是疊放到列上

[python] view plain copy
  1. t1 = [[123], [456]]  
  2. t2 = [[789], [101112]]  
  3. tf.concat([t1, t2], 0) == > [[123], [456], [789], [101112]]  
             如果concat_dim是1,那麼在某一個shape的第二個維度上連

[python] view plain copy
  1. t1 = [[123], [456]]  
  2. t2 = [[789], [101112]]  
  3. tf.concat([t1, t2], 1) ==> [[123789], [456101112  

             如果有更高維,最後連接的依然是指定那個維:

             values[i].shape = [D0, D1, ... Dconcat_dim(i), ...Dn]連接後就是:[D0, D1, ... Rconcat_dim, ...Dn]

[python] view plain copy
  1. # tensor t3 with shape [2, 3]  
  2. # tensor t4 with shape [2, 3]  
  3. tf.shape(tf.concat([t3, t4], 0)) ==> [43]  
  4. tf.shape(tf.concat([t3, t4], 1)) ==> [26]  

第二個參數values:就是兩個或者一組待連接的tensor了


/×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××/

這裏要注意的是:如果是兩個向量,它們是無法調用

[python] view plain copy
  1. tf.concat([t1, t2], 1)  
來連接的,因爲它們對應的shape只有一個維度,當然不能在第二維上連了,雖然實際中兩個向量可以在行上連,但是放在程序裏是會報錯的

如果要連,必須要調用tf.expand_dims來擴維:

[python] view plain copy
  1. t1=tf.constant([1,2,3])  
  2. t2=tf.constant([4,5,6])  
  3. #concated = tf.concat(1, [t1,t2])這樣會報錯  
  4. t1=tf.expand_dims(tf.constant([1,2,3]),1)  
  5. t2=tf.expand_dims(tf.constant([4,5,6]),1)  
  6. concated = tf.concat([t1,t2], 1)#這樣就是正確的  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章