TensorFlow patch块划分(transpose and reshape)
生活随笔
收集整理的這篇文章主要介紹了
TensorFlow patch块划分(transpose and reshape)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用transformer處理圖像數(shù)據(jù),需要照特定格式對矩陣分塊,并拉伸flatten,在完成最后的卷積后,需要重新將token的channel重新reshape成圖像格式。類似下圖,將輸入首先分塊,然后拉伸為NxC的vector,然后重新reshape為圖像格式,這里使用一通道簡要說明。
?
小代碼
def reshape():h = 6a = tf.random_uniform([h,h],maxval=40,dtype=tf.int32)b = tf.reshape(a,[2,3,2,3])c = tf.transpose(b,[0,2,1,3])d = tf.reshape(tf.reshape(c,[-1,3,3]),[4,-1])return a,d def rereshape(x):a = tf.reshape(x,[2,2,3,3])b = tf.transpose(a,[0,2,1,3])c = tf.reshape(tf.reshape(b,[6,2,3]),[6,-1])return c with tf.Session() as se:a,b = se.run(reshape())print('a:',a)print('b:',b)c = se.run(rereshape(b))print('c:',c)輸出
總結(jié)
以上是生活随笔為你收集整理的TensorFlow patch块划分(transpose and reshape)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哭了,复现TensorFlow版本MAE
- 下一篇: 高等数学第七版-习题解答:总复习3