【错误处理】RuntimeError: Output 1 of UnbindBackward is a view and its base or another view of its

RuntimeError: Output 1 of UnbindBackward is a view and its base or another view of its

项目场景

项目场景:运行项目文件 - train.py文件时报错。
项目地址:https://github.com/RayneSun/VTPLSTM


问题描述

运行项目文件 - train.py文件时报错:

RuntimeError: Output 1 of UnbindBackward is a view and its base or another view of its base has been modified inplace. This view is the output of a function that returns multiple views. Such functions do not allow the output views to be modified inplace. You should replace the inplace operation by an out-of-place one.

train.py文件报错代码区间:

for index, num in enumerate(loss):
    loss[index] = num * max(1 / (index + 1), 0.2)  # [1,1/2,1/3,1/4,1/5,1/5,...]

原因分析

错误提示:UnbindBackward的输出1是一个视图,它的基视图的另一个视图已经被修改了。该视图返回多个视图的函数的输出。不允许就地修改输出视图。应该用一个其它变量来替代当前变量,来完成修改操作。

for循环运行的过程中,num中的元素会被修改,然而在下一轮循环中还会读取num的值并修改,此时Python会迷惑:是操作原始的num值还是在第一轮修改后的num值。


解决方案

代码不使用遍历方式,改为索引的方式,相当于引入了一个新的变量,避免了原地修改的错误。

for index, num in enumerate(loss):
    # 修改: 添加下面的这一条语句。
    num = loss[index]
    loss[index] = num * max(1 / (index + 1), 0.2)  # [1,1/2,1/3,1/4,1/5,1/5,...]
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值