分享

Pytorch错误汇总

 行走在理想边缘 2023-02-08 发布于四川

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)

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多