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]]])
>>> 

猜你喜欢

转载自blog.csdn.net/Z_lbj/article/details/79789579