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)