打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Pytorch错误汇总

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]
  • 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.
  • 3.TypeError: expected Variable as element 0 in argument 0, but got tuple
  • 4. RuntimeError: zero-dimensional tensor (at position 1) cannot be concatenated
  • 5.ValueError: only one element tensors can be converted to Python scalars
  • 6.RuntimeError: multi-target not supported at /pytorch/torch/lib/THCUNN/generic/ClassNLLCriteri
  • 5.NotImplementedError
  • 6. RuntimeError: invalid argument 2: input is not contiguous at /pytorch/torch/lib/THC/generic/THCTensor.c:

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)
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
成功解决RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu
PyTorch:view() 与 reshape() 区别详解
【Deep Learning with PyTorch 中文手册】(九)Size,Offset,Strides
Pytorch autograd,backward详解
框架|最新最先进的自然语言处理库介绍,涵盖BERT,GPT-2等
万字长文,60分钟闪电战
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服