Pytorch 错误汇总

1. Expected stride to be a single integer value or a list of 1 values to match the convolution dimensions, but got stride=[1, 1]

model输入tensor错误,形状应为(batch, c, w, h)

2.RuntimeError: Trying to backward through the graph second time, but the buffers have already been freed. Please specify retain_variables=True when calling backward for the first time.

loss.backward(retain_graph=True)

3.TypeError: expected Variable as element 0 in argument 0, but got tuple

错误代码

torch.cat((tensor1, tensor2), 0) 

错误原因:tensor1 和 tensor2 存在一个不为tensor的对象

4. RuntimeError: zero-dimensional tensor (at position 1) cannot be concatenated

错误代码:

torch.cat((tensor1, tensor2), 0)

错误原因:tensor1 和 tensor2 中存在单个值的tensor

5.ValueError: only one element tensors can be converted to Python scalars

错误代码:

torch.tensor(list_of_tensors)

错误原因:tensor列表不能转换成一个tensor
修改代码:

torch.tensor([tensor.detach().numpy() for tensor in list_of_tensors])

6.RuntimeError: multi-target not supported at /pytorch/torch/lib/THCUNN/generic/ClassNLLCriteri

使用loss = nn.CrossEntropyLoss()作为损失函数时,target的形状应该是和label的形状一致或者是只有batchsize这一个维度的。

如果target是这样的[batchszie,1]就会出现上述的错误。用np.squeeze()函数降低纬度

5.NotImplementedError

定义的模型没有实现forward(self, input)方法

6. RuntimeError: invalid argument 2: input is not contiguous at /pytorch/torch/lib/THC/generic/THCTensor.c:

错误代码:

x = x.transpose(2, 0, 1)
x.view(1, -1)

错误原因:
转置后内存中空间不连续,而view操作需要对象的内存连续。使用contiguous函数使内存连续
修改代码

x = x.transpose(2, 0, 1).contiguous()
x.view(1, -1)
  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值