1.BUG:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation.
什么是inplace operation:in-place operation在pytorch中是指改变一个tensor的值的时候,不经过复制操作,而是直接在原来的内存上改变它的值。可以把它成为原地操作符。
pytorch中,通常加后缀“_”来表示原地运算符,例如.add_(),或者python中的 “+=”。
2.Bug:
RuntimeError: Expected object of backend CPU but got backend CUDA for argument #2 'weight'
这种情况通常是数据还在cpu上,又要用到gpu上计算导致的,可以尝试数据后面加上.cuda()。
cpu上的tensor和gpu上的tensor是太一样的:PyTorch中的数据类型为Tensor,Tensor与Numpy中的ndarray类似,同样可以用于标量,向量,矩阵乃至更高维度上面的计算。PyTorch中的tensor又包括CPU上的数据类型和GPU上的数据类型,一般GPU上的Tensor是CPU上的Tensor加cuda()函数得到。通过使用Type函数可以查看变量类型。系统默认的torch.Tensor是torch.FloatTensor类型。例如data = torch.Tensor(2,3)是一个2*3的张量,类型为FloatTensor; data.cuda()就将其转换为GPU的张量类型,torch.cuda.FloatTensor类型。
3.Bug:
xxxxxx is not implemented for type torch.LongTensor
尝试将torch.LongTensor转换为:torch.FolatTensor类型。
4.Bug:
bool value of Tensor with more than one value is ambiguous
函数或者可调用对象使用时候没有加括号。
5.注意:关于减少时间消耗
(1)只要是用到for循环都是在cpu上进行的,会消耗巨量的时间
(2)只要是用到生成矩阵这种操作都是在cpu上进行的,会很消耗时间。
(3)数据往cuda()上搬运会比较消耗时间,也就是说 .cuda()会比较消耗时间,能去掉就去掉。
(4)在服务器上,如果可以在一块gpu上运行就不要采用net = nn.DataParallel(net),这种gpu并行方式比单个gpu要耗时。
6. pytorch debug :断点调试 和 打印可能出错步的结果 真的可以很快的找到错误所在的地方
关于断点调试:pycharm单步调试 - qq_33485434的博客 - CSDN博客
7.UserWarning: To copy construct from a tensor
x= torch.tensor(x) ------> x= x.clone()
8.RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'mat2'(期望对象为标量类型长,但得到标量类型浮点数)
这个好奇怪,对于x= torch.matmul(x,one_hot_copy), 只要提前把x和one_hot_copy后面加上.float()就可以解决。
9. 张量乘法(未定):
二维张量相乘: 二维矩阵相乘 A*B: A.mm(B), 多维矩阵相乘 A*B: A.matmul(B), 注意需要long()数据类型。tensor.mul(tensor)
10.CUDA error: device-side assert triggered
检查Label是不是从0开始的
如果使用了Embedding层,检查参数
在算loss时计算机发现类别数目不对
CUDA ERROR: device-side assert triggered at 问题及解决思路_HuaYuuuu的博客-CSDN博客_device-side assert triggered
11.CUDNN_STATUS_MAPPING_ERROR
model = model.cuda()
12.invalid argument 0: Sizes of tensors must match except in dimension 0. Got 182 and 360 in dimension 2 at /pytorch/aten/src/TH/generic/THTensorMath.c:3586
见Pytorch学习笔记(二)使用Pytorch的常见错误汇总_cg101202的博客-CSDN博客
13.AttributeError: 'dict' object has no attribute 'seek'. You can only torch.load from a file that is seekable. Please pre-load the data into a buffer like io.BytesIO and try to load from it instead.
pytorch加载自定义网络权重_wuming无名的博客-CSDN博客
14.'DataParallel' object has no attribute 'copy'
15. load_state_dict时报错RuntimeError:"module.xxx" is xxx
Error(s) in loading state_dict
实际上这是由于训练出的权重是分布式训练得到的,会在每个层的名字前加上module.的前缀,这样的权重是不能直接load的,需要先让模型
model = nn.DataParallel(model)
pytorch错误:Missing key(s) in state_dict、Unexpected key(s) in state_dict解决 - 碧水青山 - 博客园
http://www.voidcn.com/article/p-cugnbrpt-bwh.html
pytorch中存储各层权重参数时的命名规则,为什么有些层的名字中带module._南国那片枫叶的博客-CSDN博客_dist.get_rank()
16.pytorch中使用apex遇到的一些问题
pytorch 使用 apex问题_挡不住三千问的BlueCat的博客-CSDN博客
17.TypeError: 'builtin_function_or_method' object is not iterable
在定义transform时,一定要先调整图片大小,再ToTensor (),即ToTensor需要放在transform最后
18. RuntimeError : PyTorch was compiled without NumPy support
将torch-0.4.1更新为 torch-0.4.1.post2
可能还要降numpy版本
https://download.pytorch.org/whl/cu90/torch_stable.html pytorch低版本安装包下载
19.RuntimeError: Invalid DISPLAY variable
在Windows下使用matplotlib绘图可以,但是在ssh远程绘图的时候会报如上错误,
原因:matplotlib的默认backend是TkAgg,而FltAgg、GTK、GTKCairo、TkAgg、Wx和WxAgg这几个backend都要求有GUI图形界面,所以在ssh操作的时候会报错。
解决办法:在导入matplotlib的时候指定不需要GUI的backend(Agg、Cairo、PS、PDF和SVG)
import matplotlib.pyplot as plt
plt.switch_backend('agg')
20.'numpy.float64' object cannot be interpreted as an integer
报这个错不一定是类型错误,有可能是因为读入数据为空
21. DataLoader worker (pid(s) 1044, 1053, 1087) exited unexpectedly
将dataloader中的num_workers进行修改,比如改成4或者0
22.RuntimeError: Tensors must be non-overlapping and dense
这个报错主要是使用ddp时输入的tensor不是Continguous的
参考https://github.com/Lightning-AI/lightning/issues/4781
his happens because all_gather
requires all tensors to be contiguous
(data is stored in an uninterrupted block of memory). When you take a slice of a tensor, you don´t alter the underlying memory location of data, just how you read it (basically changing the stride). If you try this in your code, you can see for yourself
23.Torchrun command not found for distributed training, does it need to be installed separetely?
这个报错主要是pytorch版本太低,不支持torchrun,还是得用python -m torch.distributed.run