深度学习中的踩过的一些坑

深度学习中的踩过的一些坑

(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’)这句话注释。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值