1. 程式人生 > >torch.reshape /torch.Tensor.reshape

torch.reshape /torch.Tensor.reshape

y = x.reshape([batchsize, -1, sentsize, wordsize])

把 x 改變形狀為(batch,-1, sentsize, wordsize)-1 維度會自動根據其他維度計算



x = np.transpose(x,axes=(1,0,2,3)) 

把x 轉置 

axes: 要進行轉置 的軸兌換序號

arr1 = np.arange(12).reshape(2,2,3)
>>>
array([[[ 0,  1,  2],
        [ 3,  4,  5]],

       [[ 6,  7,  8],
        [ 9, 10, 11]]])
arr1.shape
>>>(2, 2, 3) #說明這是一個2*2*3的陣列(矩陣),返回的是一個元組,可以對元組進行索引,也就是0,1,2

transpose axes 引數的指的就是這個shape的索引

arr1.transpose((1,0,2))
>>>
array([[[ 0,  1,  2],
        [ 6,  7,  8]],

       [[ 3,  4,  5],
        [ 9, 10, 11]]])

比如,數值6開始的索引是[1,0,0],變換後變成了[0,1,0]。# 將原來索引號0,1,2 置換成1,0,2 

可以理解為把所有值的索引前兩位置換下(就是片數和行數置換?)

這也說明了,transpose依賴於shape。

(慢慢理解。。。。。。)

>>> a
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]]])
a.shape = (2,3,3)-->[0,1,2]
>>> a.transpose(1,0,2)
array([[[ 0,  1,  2],
        [ 9, 10, 11]],

       [[ 3,  4,  5],
        [12, 13, 14]],

       [[ 6,  7,  8],
        [15, 16, 17]]])
>>>