pytorch中的transpose方法的作用是交換矩陣的兩個維度,transpose(dim0, dim1) → tensor,其和torch.transpose()函式作用一樣。
torch.transpose():
torch.transpose(
input
, dim0, dim1) → tensor
parameters
input
(tensor) – the input tensor.
dim0 (
int) – the first dimension to be transposed
dim1 (
int) – the second dimension to be transposed
例:
>>
> x = torch.randn(2,
3)>>
> x
tensor([[
1.0028,-
0.9893
,0.5809],
[-0.1669
,0.7299
,0.4942]]
)>>
> torch.transpose(x,0,
1)tensor([[
1.0028,-
0.1669],
[-0.9893
,0.7299],
[0.5809
,0.4942]]
)
需要注意的幾點:
1、transpose中的兩個維度引數的順序是可以交換位置的,即transpose(x, 0, 1,) 和transpose(x, 1, 0)效果是相同的。如下:
>>
>
import torch
>>
> x = torch.randn(2,
3)>>
> x
tensor([[
-0.4343
,0.4643,-
1.1345],
[-0.3667,-
1.9913
,1.3485]]
)>>
> torch.transpose(x,1,
0)tensor([[
-0.4343,-
0.3667],
[0.4643,-
1.9913],
[-1.1345
,1.3485]]
)>>
> torch.transpose(x,0,
1)tensor([[
-0.4343,-
0.3667],
[0.4643,-
1.9913],
[-1.1345
,1.3485]]
)
2、transpose.()中只有兩個引數,而torch.transpose()函式中有三個引數。 Pytorch 中 torchvision的錯誤
在學習pytorch的時候,使用 torchvision的時候發生了乙個小小的問題 安裝都成功了,並且import torch也沒問題,但是在import torchvision的時候,出現了如下所示的錯誤資訊 dll load failed 找不到指定模組。首先,我們得知道torchvision在...
pytorch中的乘法
總結 按元素相乘用torch.mul,二維矩陣乘法用torch.mm,batch二維矩陣用torch.bmm,batch 廣播用torch.matmul if name main a torch.tensor 1 2,3 b torch.arange 0,12 reshape 4 3 c torch...
pytorch中index select 的用法
a torch.linspace 1,12,steps 12 view 3,4 print a b torch.index select a,0,torch.tensor 0,2 print b print a.index select 0,torch.tensor 0,2 c torch.inde...