RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one.

报错信息

报错信息:

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 (1) passing the keyword argument find_unused_parameters=True to torch.nn.parallel.DistributedDataParallel; (2) making sure all forward function outputs participate in calculating loss. If you already have done the above two steps, then the distributed data parallel module wasn’t able to locate the output tensors in the return value of your module’s forward function. Please include the loss function and the structure of the return value of forward of your module when reporting this issue (e.g. list, dict, iterable).

遇到这个报错的原因可能有很多,设置torch.nn.parallel.DistributedDataParallel的参数find_unused_parameters=True之类的方法就不提了,报错信息中给的很清楚,看不懂的话google翻译一下即可。

运行时错误:预计在开始新迭代之前已完成前一次迭代的减少。此错误表明您的模块具有未用于产生损耗的参数。您可以通过 (1) 将关键字参数 find_unused_parameters=True 传递给 torch.nn.parallel.DistributedDataParallel 来启用未使用的参数检测; (2) 确保所有 forward 函数输出都参与计算损失。如果您已经完成了上述两个步骤,那么分布式数据并行模块无法在模块的 forward 函数的返回值中定位输出张量。报告此问题时,请包括损失函数和模块 forward 返回值的结构(例如 list、dict、iterable)。

如果改个参数能够就能够解决你的问题的话,你也不会找到这篇博客了^^。

解决方法(之一)

这里其实报错的最后一句值得注意:

如果您已经完成了上述两个步骤,那么分布式数据并行模块无法在模块的 forward 函数的返回值中定位输出张量。报告此问题时,请包括损失函数和模块 forward 返回值的结构(例如 list、dict、iterable)。

但是第一次遇到这个问题只看官方的提示信息可能还是云里雾里,这里笔者将自己的理解和解决过程分享出来。

说的简单点,其实就一句话:确保你的所有的forward的函数的所有输出都被用于计算损失函数了

注意,不仅仅是你的模型的forward函数的输出,可能你的损失函数也是通过forward函数来计算的。也就是说,所有继承自nn.Module的模块(不只是模型本身)的forward函数的所有输出都要参与损失函数的计算

笔者本身遇到的问题就是,在多任务学习中,损失函数是通过一个整个继承自nn.Module的模块来计算的,但是在forward返回的loss中少加了一个任务的loss,导致这个报错。


class multi_task_loss(nn.Module):def __init__(self, device, batch_size):super().__init__()self.ce_loss_func = nn.CrossEntropyLoss()self.l1_loss_func = nn.L1Loss()self.contra_loss_func = ContrastiveLoss(batch_size, device)def forward(self, rot_p, rot_t, pert_p, pert_t, emb_o, emb_h, emb_p,original_imgs, rect_imgs):rot_loss = self.ce_loss_func(rot_p, rot_t)pert_loss = self.ce_loss_func(pert_p, pert_t)contra_loss = self.contra_loss_func(emb_o, emb_h) \+ self.contra_loss_func(emb_o, emb_p) \+ self.contra_loss_func(emb_p, emb_h)rect_loss = self.l1_loss_func(original_imgs, rect_imgs)# tol_loss = rot_loss + pert_loss + rect_loss               # 少加了一个loss,但是所有loss都返回了tol_loss = rot_loss + pert_loss + contra_loss + rect_loss        # 修改为此行后正常return tol_loss, (rot_loss, pert_loss, contra_loss, rect_loss)

读者可以检查一下自己整个的计算过程中(不只是模型本身),是否所有的forward的函数的所有输出都被用于计算损失函数了。

Ref:

https://discuss.pytorch.org/t/need-help-runtimeerror-expected-to-have-finished-reduction-in-the-prior-iteration-before-starting-a-new-one/119247

RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one.相关推荐

  1. [debug] Expected to have finished reduction in the prior iteration before starting a new one.

    问题描述 训练网络时出现错误: RuntimeError: Expected to have finished reduction in the prior iteration before star ...

  2. RuntimeError: Expected object of backend CUDA but got backend CPU for argument

    RuntimeError: Expected object of backend CUDA but got backend CPU for argument #4 'mat1' 原因:变量没有加cud ...

  3. RuntimeError: Expected object of device type cuda but got device type cpu for argument pytorch数据位置

    RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'target' i ...

  4. RuntimeError: Expected object of backend CUDA but got backend CPU for argument #4 'mat1'

    RuntimeError: Expected object of backend CUDA but got backend CPU for argument #4 'mat1' 原因:变量没有加cud ...

  5. 异常解决(一)-- RuntimeError: expected device cpu but got device cuda:0

    最近在编写深度学习的相关代码,基于PyTorch,运行程序的时候,报错,报错内容如下所示: RuntimeError: expected device cpu but got device cuda: ...

  6. RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'target'

    RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'target' i ...

  7. 解决pytorch RuntimeError: expected scalar type XXXX but found XXXX

    比如报错为:RuntimeError: expected scalar type Long but found Float,就是希望输入为torch.Long,结果得到一个torch.Float 解决 ...

  8. 使用PyTorch前向运算时出现“RuntimeError: Expected object of scalar type Long but got scalar type Float for ……”

    1 问题描述 今天在使用PyTorch搭建模型时,出现了一个问题: RuntimeError: Expected object of scalar type Long but got scalar t ...

  9. 常见报错:RuntimeError: expected scalar type Long but found Float

    RuntimeError: expected scalar type Long but found Float 这是一个非常常见的报错,我已经遇到过这个报错很多次了,但是之前没有仔细研究过,今天好好好 ...

最新文章

  1. JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解 | 必须收藏!
  2. LeetCode.961-2N数组中N次重复的元素(N-Repeated Element in Size 2N Array)
  3. 使用 frida+dexdump对apk脱壳
  4. 蚁族之痛:过年如过关
  5. Java8————Optional
  6. WebSocket实战
  7. sqlyog通过跳板机ssh连接mysql数据库
  8. 如何打造标签式IE浏览器 (共享源码)
  9. 使用GameKit实现IOS设备之间的蓝牙通信
  10. JAVA调用shell脚本利用ansible修改多节点上的redis参数
  11. ElasticSearch Java api 详解_V1.0
  12. 5g/4g工业无线路由器
  13. 什么是PY平台?Python平台用途及安装的详细方法
  14. java中cleanup的使用_【Lombok注解】@Cleanup 自动资源管理:安全无困扰地调用close方法...
  15. C++字母大小写转换
  16. 一枚钻戒如何成功借势世界杯,与粉丝秀恩爱
  17. 服务器开发——定时器
  18. 一文看懂人工智能芯片的产业生态及竞争格局
  19. 重温前端基础(一) HTML、CSS、H5C3
  20. 感觉自己不会的东西太多了,不知道如何下手?

热门文章

  1. ETL异构数据源Datax_部署前置环境_01
  2. vue3数据绑定显示列表数据局
  3. 使用PLSQL 远程连接oracle数据库
  4. 第7篇:Flowable快速工作流脚手架Jsite_请假实战_HR审批
  5. ESLint is disabled since its execution has not been approved or denied yet
  6. mybatis报错:Could not find result map java.lang.Integer
  7. 数组,三种初始化和内存分析
  8. asp.net + ajax + sqlserver 自动补全功能,asp.net+ajax+sqlserver自动补全功能实现解析
  9. android unix时间,android: 日期转Unix时间戳,Unix时间戳转日期,带时区
  10. 服务器的可维护性,可靠性和可维护性