theano中的dimshuffle


theano中的dimshuffle函數用於對張量的維度進行操作,可以增加維度,也可以交換維度,刪除維度。
注意的是只有shared才能調用dimshuffle()
'x'表示增加一維,從0d scalar到1d vector
(0, 1)表示一個與原先相同的2D向量
(1, 0)表示將2D向量的兩維交換
(‘x’, 0) 表示將一個1d vector變為一個1xN矩陣
(0, ‘x’)將一個1d vector變為一個Nx1矩陣
(2, 0, 1) -> AxBxC to CxAxB (2表示第三維也就是C,0表示第一維A,1表示第二維B)
(0, ‘x’, 1) -> AxB to Ax1xB 表示A,B順序不變在中間增加一維
(1, ‘x’, 0) -> AxB to Bx1xA 同理自己理解一下
(1,) -> 刪除維度0,(1xA to A)

寫了個小程序來驗證猜想

from __future__ import print_function
import theano
import numpy as np
def print_hline(file):
    print('------------------------------------------',file=file,end='\r\n')
write_file=open('G:\data\dimshuffle_output.txt','wb')
v = theano.shared(np.arange(3))
# v.shape is a symbol expression, need theano.function or eval to compile it
print_hline(write_file)
v_disp = v.dimshuffle(0)
print('v.dimshuffle(0):',v_disp.eval(),file=write_file,end='\r\n')
print('v.dimshuffle(0).shape:',v_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
v_disp = v.dimshuffle('x', 0)
print("v.dimshuffle('x',0):",v_disp.eval(),file=write_file,end='\r\n')
print("v.dimshuffle('x',0).shape:",v_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
v_disp = v.dimshuffle(0,'x')
print("v.dimshuffle(0,'x'):",v_disp.eval(),file=write_file,end='\r\n')
print("v.dimshuffle(0,'x').shape:",v_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
v_disp = v.dimshuffle(0,'x','x')
print("v.dimshuffle(0,'x','x'):",v_disp.eval(),file=write_file,end='\r\n')
print("v.dimshuffle(0,'x','x').shape:",v_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
v_disp = v.dimshuffle('x',0,'x')
print("v.dimshuffle('x',0,'x'):",v_disp.eval(),file=write_file,end='\r\n')
print("v.dimshuffle('x',0,'x').shape:",v_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
v_disp = v.dimshuffle('x','x',0)
print("v.dimshuffle('x','x',0):",v_disp.eval(),file=write_file,end='\r\n')
print("v.dimshuffle('x','x',0).shape:",v_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
m = theano.shared(np.arange(6).reshape(2,3))
print("m:",m.eval(),file=write_file,end='\r\n')
print("m.shape:",m.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
m_disp = m.dimshuffle(0,'x',1)
print("m.dimshuffle(0,'x',1):",m_disp.eval(),file=write_file,end='\r\n')
print("m.dimshuffle(0,'x',1).shape:",m_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
m_disp = m.dimshuffle('x',0,1)
print("m.dimshuffle('x',0,1):",m_disp.eval(),file=write_file,end='\r\n')
print("m.dimshuffle('x',0,1).shape:",m_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
m_disp = m.dimshuffle(0,1,'x')
print("m.dimshuffle(0,1,'x'):",m_disp.eval(),file=write_file,end='\r\n')
print("m.dimshuffle(0,1,'x').shape:",m_disp.shape.eval(),file=write_file,end='\r\n')
print_hline(write_file)
# amount to transpose
m_disp = m.dimshuffle(1,'x',0)
print("m.dimshuffle(1,'x',0):",m_disp.eval(),file=write_file,end='\r\n')
print("m.dimshuffle(1,'x',0).shape:",m_disp.shape.eval(),file=write_file,end='\r\n')
write_file.close()

首先定義了一個[0 1 2]的1D vector:v,v.dimshuffle(0)中的0表示第一維:3,也只有一維,所以不變。因為是1D的,所以shape只有(3,)

v.dimshuffle(0): [0 1 2]
v.dimshuffle(0).shape: [3]

v.dimshuffle('x',0)表示在第一維前加入一維,只要記住加了'x'就加了一維,所以大小變成了1x3

v.dimshuffle('x',0): [[0 1 2]]
v.dimshuffle('x',0).shape: [1 3]

剩下的同理可理解

v.dimshuffle(0,'x'): [[0]
 [1]
 [2]]
v.dimshuffle(0,'x').shape: [3 1]
v.dimshuffle(0,'x','x'): [[[0]]

 [[1]]

 [[2]]]
v.dimshuffle(0,'x','x').shape: [3 1 1]
v.dimshuffle('x',0,'x'): [[[0]
  [1]
  [2]]]
v.dimshuffle('x',0,'x').shape: [1 3 1]
v.dimshuffle('x','x',0): [[[0 1 2]]]
v.dimshuffle('x','x',0).shape: [1 1 3]

第二個例子,m是一個2x3矩陣

m: [[0 1 2]
 [3 4 5]]
m.shape: [2 3]

先確定0,'x',1的維數,0對應第一維(2),1表示第二維(3),'x'表示新加入的維度(1)
所以結果維度是2x1x3
加括號的順序按照從左到右(外->內)的順序
1.先加最內層3,3表示括號內有3個數,因此是[0 1 2]和[3 4 5]
2.再加中間層1,1表示括號內只有一個匹配的"[]",因此是[[0 1 2]],[[3 4 5]]
3.最后加最外層2,2表示括號內有兩個匹配的"[]"(只算最外層的匹配),於是最后結果是
[[[0 1 2]]
[[3 4 5]]]

m.dimshuffle(0,'x',1): [[[0 1 2]]

 [[3 4 5]]]
m.dimshuffle(0,'x',1).shape: [2 1 3]

剩下的同理可以理解

m.dimshuffle('x',0,1): [[[0 1 2]
  [3 4 5]]]
m.dimshuffle('x',0,1).shape: [1 2 3]
m.dimshuffle(0,1,'x'): [[[0]
  [1]
  [2]]

 [[3]
  [4]
  [5]]]
m.dimshuffle(0,1,'x').shape: [2 3 1]
m.dimshuffle(1,'x',0): [[[0 3]]

 [[1 4]]

 [[2 5]]]
m.dimshuffle(1,'x',0).shape: [3 1 2]


免責聲明!

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



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