【TensorFlow】tf.concat的用法


網址:http://blog.csdn.net/mao_xiao_feng/article/details/53366163

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

 

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

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

 

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

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

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

             如果concat_dim是1,那么在某一個shape的第二個維度上連

 

 

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

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

             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])) ==> [4, 3]  
  4. tf.shape(tf.concat(1, [t3, t4])) ==> [2, 6]  

 

 

第二個參數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])#這樣就是正確的  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM