简体   繁体   English

Pytorch autograd 的 * 和 + 之间有什么本质区别吗?

[英]Is there any essential difference between * and + for Pytorch autograd?

I was trying to understand the autograd mechanism in more depth.我试图更深入地了解 autograd 机制。 To test my understanding, I tried to write the following code which I expected to produce an error (ie, Trying to backward through the graph a second time ).为了测试我的理解,我尝试编写以下我预计会产生错误的代码(即,再次尝试向后查看图形)。

b = torch.Tensor([0.5])
for i in range(5):
    b.data.zero_().add_(0.5)
    b = b + a
    c = b*a
    c.backward()

Apparently, it should report an error when c.backward() is called for the second time in the for loop, because the history of b has been freed, however, nothing happens.显然,在for循环中第二次调用c.backward()时应该会报错,因为b的history已经被释放了,但是没有任何反应。

But when I tried to change b + a to b * a as follows,但是当我尝试将 b + a 更改为 b * a 时,

b = torch.Tensor([0.5])
for i in range(5):
    b.data.zero_().add_(0.5)
    b = b * a
    c = b*a
    c.backward()

It did report the error I was expecting.它确实报告了我期望的错误。 This looks pretty weird to me.这对我来说看起来很奇怪。 I don't understand why there is no error evoked for the former case, and why it makes a difference to change from + to *.我不明白为什么前一种情况没有引起错误,以及为什么从 + 更改为 * 会有所不同。

The difference is adding a constant doesn't change the gradient, but mull by const does.不同之处在于添加一个常量不会改变梯度,但 mull by const 会。 It seems, autograd is aware of it, and optimizes out 'b = b + a'.看来,autograd 意识到了这一点,并优化了 'b = b + a'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM