RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

本文详细解释了PyTorch中的动态图机制,特别是在反向传播过程中的注意事项。当尝试进行第二次反向传播时,由于计算图会被自动释放,需要设置`retain_graph=True`来保留图。另外,直接对`data`属性操作会切断与原有变量的梯度关系,导致无法计算梯度。理解这些概念对于有效和正确地训练神经网络模型至关重要。
摘要由CSDN通过智能技术生成
b = Variable(torch.FloatTensor([2]),requires_grad = True)
a = b * b
print("a: {}".format(a))
print("b: {}".format(b))

a.backward()
print("b.grad.data: {}".format(b.grad.data))

上面的代码运行没有报错。

b = Variable(torch.FloatTensor([2]),requires_grad = True)
a = b * b
print("a: {}".format(a))
print("b: {}".format(b))

for i in range(2):
    a.backward()
    print("b.grad.data: {}".format(b.grad.data))

报错:RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.

pytorch是动态图机制,在训练模型时候,每迭代一次都会构建一个新的计算图(计算图是代表程序中变量之间的关系),为了节约内存,所以每次一轮迭代完也即是进行了一次backward函数计算之后计算图就被在内存释放,所以当计算第二次backward()时,计算图的结构已经被破坏,因此如果需要多次backward时只需要在第一次反向传播时候添加一个retain_graph=True标识,让计算图不被立即释放。

开始认为错误是不能反复对一个数值进行backward,修改代码,不对原变量进行操作

a = a.data  #读取a的值
a.backward()
print("b.grad.data: {}".format(b.grad.data))

报错:RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

添加代码:a.requires_grad = True

报错:AttributeError: 'NoneType' object has no attribute 'data'

print(id(a))
a = a.data
print(id(a))

新的a和原来的a已经完全不一样,对新的a进行backward,自动计算所有的梯度, 这个张量的所有梯度将会自动积累到 .grad 属性,而新的a与b之间没有关系,所以b的grad值为空。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值