UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.

@[TOC](UserWarning: volatile was removed and now has no effect. Use with torch.no_grad(): instead.
input_var = torch.autograd.Variable(input, volatile=True)

UserWarning: volatile was removed and now has no effect. Use with torch.no_grad(): instead.

pytorch进行自动梯度下降时出现错误

        input_var = torch.autograd.Variable(input, volatile=True)
        target_var = torch.autograd.Variable(target, volatile=True)

当执行上面的代码时出现如下错误:

E:/hscnn_main.py:165: UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.
  input_var = torch.autograd.Variable(input, volatile=True)
E:/hscnn_main.py:166: UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.
  target_var = torch.autograd.Variable(target, volatile=True)

由于版本迭代,我们只需要按照提示步骤将其改为如下表示方式即可:

        with torch.no_grad():
            input_var = torch.autograd.Variable(input)
        with torch.no_grad():
            target_var = torch.autograd.Variable(target)

成功解除报错!
"with torch.no_grad():"表示后面的执行代码得到的结果不会参与反向传播,因此我们只需要知道forword的结果就可以了,不需要记录网络参数。在交叉检验或者测试过程中我们只需要网络输出的结果,不需要记录传播过程的参数,因此会节约内存。

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值