Pytorch: inplace operation runtimeError

在PyTorch框架下训练网络时遇到RuntimeError,该错误通常由于在损失反向传播前对参与计算的变量进行了inplace操作。检查包括:确保forward函数中计算输出后不再修改相关变量,以及训练函数中正确设置反向传播和参数更新的顺序。解决方法包括使用.clone()避免inplace操作和调整训练流程顺序。
摘要由CSDN通过智能技术生成

 问题描述:
  

在PyTorch框架下搭建简单的网络结构并训练时,报以下错误:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.DoubleTensor [100, 300]], which is output 0 of TBackward, is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

这个错误产生的原因是,和loss反向传播梯度计算有关的变量在反向传播前通过inplace操作被修改了。inplace操作包括如:x += 1、x[0] = 1、torch.add_()等。

出现这个问题可以检查下:

1、forward()函数中,是否在输出计算后又修改了和输出计算有关的变量。

因为输出和loss的计算有关,在通过某些变量计算得到输出后,就不能再通过inplace操作修改这些变量了。(可以通过.clone()或者另赋一个变量来修改)

比如:

   代码例来自:https://github.com/pytorch/pytorch/issues/15803
    b = a ** 2 * c ** 2
    b +&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值