深度学习中的踩过的一些坑
(1)pytorch 中的Data.DataLoader:
1.Pytorch中已经实现的Sampler
有如下几种:
SequentialSampler
RandomSampler
WeightedSampler
SubsetRandomSampler
需要注意的是DataLoader的部分初始化参数之间存在互斥关系,这个你可以通过阅读源码更深地理解,这里只做总结:
-
如果你自定义了
batch_sampler
,那么这些参数都必须使用默认值:batch_size
,shuffle
,sampler
,drop_last
. -
如果你自定义了
sampler
,那么shuffle
需要设置为False
-
如果
sampler
和
batch_sampler
都为
None
,那么
batch_sampler
使用Pytorch已经实现好的
BatchSampler
,而
sampler
分两种情况:
-
若
shuffle=True
,则sampler=RandomSampler(dataset)
-
若
shuffle=False
,则sampler=SequentialSampler(dataset)
-
2.Pytorch中已经实现的MyDataset:
(1)其中__len__一定要设置 成你选取样本的长度
(2)getitem 当有索引产生时 调用该函数内容
(2) DDP遇到的问题:
(1)RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. This error indicates that your module has parameters that were not used in producing loss. You can enable unused parameter detection by passing the keyword argument find_unused_parameters=True
to torch.nn.parallel.DistributedDataParallel
, and by making sure all forward
function outputs participate in calculating loss.
出现这个问题是你构建的net中有一些层数定义了(self) 但是没用到 这个错误的字面意思是说,有模型没有累积的参数,也就是说有一些参数没有被加入到模型的参数中。所以可以得到初步结论,使用分布式的框架时候某些参数某有更新,但是这些参数在其他进程中可能会更新,从这个错误来看DistributedDataParallel希望你使用find_unused_parameters=True来让其他子进程都不更新这个不用更新的参数。
————————————————
版权声明:本文为CSDN博主「iuhiyuh」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/huuuuuuuu/article/details/106381157
解决方法:
(1)在net中添加find_unused_parameters=True
net = DistributedDataParallel(net,device_ids=[gpu_id],find_unused_parameters=True, output_device=gpu_id)#
(2) 在net中找到没有使用的层数 然后注释掉。
(3) 解决linux部分环境中用pip安装是报错:NameError: name ‘reload’ is not defined
到类似于/home/tongx/.conda/envs/pytorch_tx/lib/python3.8/mimetypes.py文件中
添加import importlib 并且将reload(sys) 改为 importlib.reload(sys)
部分环境中如果报错 module ‘sys’ has no attribute ‘setdefaultencoding’
则把sys.setdefaultencoding(‘gbk’)这句话注释。