TypeError: transpose() received an invalid combination of arguments - got (int, int, int, int), but
程序员文章站
2022-05-27 11:55:59
...
TypeError: transpose() received an invalid combination of arguments - got (int, int, int, int), but expected one of:
*(int dim0, int dim1)
*(name dim0, name dim1)
出现这个错误是因为给transpose传入了错误数量的参数,transpose一次只能调换两个维度,正确使用方式如下:
import torch
x=torch.rand(2,3,4,5)
torch.transpose(x,0,3)
#或者
x.transpose(0,3)
若要交换多个维度建议调用permute
,参考如下
import torch
x=torch.rand(2,3,4,5) #这是一个4维的矩阵
print(x.size()) # torch.Size([2, 3, 4, 5])
# 若要将 C T H W 转换为 T H W C
#先转置0维和1维,之后在第2,3维间转置,之后在第1,3间转置
y = x.transpose(0,1).transpose(3,2).transpose(1,3)
print(y.size()) #torch.Size([3, 4, 5, 2])
# 可以看到用transpose需要转换三次
# 此时直接用permute更见简便
print(x.permute(1, 2, 3, 0).shape) #torch.Size([3, 4, 5, 2])
#若在 维度变换后还需要进行reshape操作的话,需要在后面加.contiguous(),保持连续
x.transpose(0,3).contiguous()
x.permute(1, 2, 3, 0).contiguous()
参考:
- https://blog.csdn.net/weicao1990/article/details/93618136
推荐阅读
-
TypeError: new() received an invalid combination of arguments-got(float, int)
-
TypeError: new() received an invalid combination of arguments - got (float, int, int, int), but expe
-
TypeError: transpose() received an invalid combination of arguments - got (Tensor)的问题
-
TypeError: sum() received an invalid combination of arguments - got (axis=int, ), but expected one
-
TypeError: transpose() received an invalid combination of arguments - got (int, int, int, int), but
-
TypeError: new() received an invalid combination of arguments - got (float, int), but expected one
-
numpy矩阵求和 TypeError: sum() received an invalid combination of arguments - got (axis=int, )