Why do we need to set the gradients manually to zero in pytorch?

pinocchio: <p>Why do we need to set the gradients manually to zero in pytorch? e.g:</p>
<p><code>w1.grad.data.zero_()</code></p>
<p>why do we need that? What happens if we don’t use that?</p>
<p>It feels that needing to ask this question means there is something conceptual/fundamental about the design of pytorch that I dont understand.</p>

ruotianluo: <p>Every time a variable is back propogated through, the gradient will be accumulated instead of being  replaced. (This makes it easier for rnn, because each module will be back propogated through several times.)</p>

pinocchio: <p><a class="mention" href="/u/ruotianluo">@ruotianluo</a> I still don’t understand. Though, is there a link I can just read to understand this?</p>
<p>Is there no link to understand how pytorch works and so I can form a mental model of it?</p>
<p>Like something like this seems very strange to someone coming from tensorflow.</p>

samarth-robo: <p>Since the <code>backward()</code> function accumulates gradients, and you don’t want to mix up gradients between minibatches, you have to zero them out at the start of a new minibatch. This is exactly like how a general (additive) accumulator variable is initialized to 0 in code.</p>
<p>By the way, the best practice is to use the <a href="http://pytorch.org/docs/master/optim.html#torch.optim.Optimizer.zero_grad" rel="nofollow noopener">zero_grad()</a> function on the optimizer.</p>

tom: <p>A more explicit example in a similar direction as <a class="mention" href="/u/ruotianluo">@ruotianluo</a> is the ability to add gradients from several forward passes, for example in GANs:</p><aside class="quote quote-modified" data-post="5" data-topic="1826">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tom/48/3162_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-to-use-the-backward-functions-for-multiple-losses/1826/5">How to use the backward functions for multiple losses?</a> 
  </div>
  <blockquote>
    Hello <a class="mention" href="/u/nabarun_goswami">@Nabarun_Goswami</a>, 
to try to clear this up, in the DCGAN example, you have (think about mathematical functions here, I left out everything not relevant). 
loss = criterion(netD(real, params))+criterion(netD(fake, params)) 
Spelling out the chain rule for the gradient of the loss w.r.t. the params: 
∇params loss = ∇params netD(real, params)* ∇netD loss(netD (real,params)) +  ∇params netD(fake, params)* ∇netD loss(netD (fake,params)), 
note how ∇params netD is evaluated at two different poin…
  </blockquote>
</aside>

<p>If you wanted, you could also achieve minibatches that are larger than fit in your memory by combining several sub-minibatches into one gradient step, but I have not really seen that done.</p>
<p>Best regards</p>
<p>Thomas</p>

pinocchio: <p>I think I don’t even know what “accumulating gradients” even means though. So I’m not sure what we are even talking about.</p>

hughperkins: <p>You mean like, why doesnt <code>backward</code> just zero out the gradients before doing the back propagation?  I somewhat agree on this point actually… I would have to really struggle to think of a time when I’ve called <code>backward</code> without first zeroing out the gradients.</p>

hughperkins: <p>(like, it seems like we could have an option in backward to not zero out the gradients, like <code>backward(preserve_grads=True)</code>, but by default, seems like zeroing out the gradients could be the default action)</p>

albanD: <p>Hi,</p>
<p>I think the big difference with tensorflow is the following.<br>
Since you use a static graph, you define exactly what should be done to make one gradient computation/update. And then you just tell it to do it using a given input/target.</p>
<p>In pytorch, it is significantly more flexible as the autograd engine will just “remember” how to compute the gradient for a given variable while you are performing computations with this Variable. This means that you can get the gradients wrt a variable, then perform computation with it again, then recompute gradients corresponding to these new operations.<br>
In this scheme, there is a not a single point where you stop performing “forward” operations and you know that the only thing that is left to be done is compute the gradients. So it is trickier to automatically set the gradients to 0 because you don’t know when a computation end, and when a new starts.</p>
<p>An example where the gradient accumulation is useful is for example if you share some part of a network for two different tasks:</p>
<pre><code class="lang-auto">input = Variable(data)
# Get the features
features = feature_extractor(input)

# Compute first loss and get the gradients for it
loss1 = task1(features)
loss1.backward(retain_graph=True)
# This add the gradients wrt loss1 in both the "task1" net and the "feature_extractor" net
# So each parameter "w" in "feature_extractor" has it gradient d(loss1)/dw

# Perform the second task and get the gradients for it as well
loss2 = task2(features)
loss2.backward()
# This will add gradients in "task2" and accumulate in "feature_extractor"
# Now each parameter in "feature_extractor" contains d(loss1)/dw + d(loss2)/dw
</code></pre>
<p>So the fact that the gradients are accumulated allows you to get the correct gradient for all the computations that you do with a given Variable even if you use it at multiple places in convoluted ways.<br>
The drawback here is that you have to manually reset the values to 0 so that the gradients computed previously do not interfere with the ones you are currently computing.</p>

pinocchio: <p>thats interesting. I would have thought that the “sharing” would have been multiplicative since RNNs a composition of functions and not a mere addition (as in ur transfer learning example). I think I am more confused. <img src="//discuss.pytorch.org/images/emoji/apple/frowning.png?v=5" title=":frowning:" class="emoji" alt=":frowning:"></p>

albanD: <p>The addition comes from the rules of differentiation. Iif <code>f = f1 + f2</code>, then the gradients for a parameter in both branches is the sum of the contributions of each branch.</p>

Jiawei_Zhuang: <p><code>y.backward()</code> doesn’t just assign the value of y’(x) to  <code>x.grad</code> (say <code>y</code> depends on <code>x</code>). It actually adds y’(x) to the current value of <code>x.grad</code> (think it as <code>x.grad += true_gradient</code>).</p>
<p>In the following example, <code>y.backward()</code> is called 5 times, so the final value of <code>x.grad</code> will be 5*cos(0)=5.</p>
<pre><code>import torch
from torch.autograd import Variable

x = Variable(torch.Tensor([[0]]), requires_grad=True)

for t in range(5):
    y = x.sin() 
    y.backward()
    
print(x.grad) # shows 5
</code></pre>
<p>Calling <code>x.grad.data.zero_()</code> before <code>y.backward()</code> can make sure <code>x.grad</code> is exactly the same as current y’(x), not a sum of y’(x) in all previous iterations.</p>
<pre><code>x = Variable(torch.Tensor([[0]]), requires_grad=True) 

for t in range(5):
    if x.grad is not None:
        x.grad.data.zero_()
    y = x.sin() 
    y.backward()

print(x.grad) # shows 1
</code></pre>
<p>I also got confused by this “zeroing gradient” when first learning pytorch. The doc of <code>torch.autograd.backward</code> does mention that</p>
<blockquote>
<p>This function accumulates gradients in the leaves - you might need to zero them before calling it.</p>
</blockquote>
<p>But this is quite hard to find and pretty confusing for (say) tensorflow users.</p>
<p>Official tutorials like <a href="http://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html#" rel="noopener nofollow ugc">60 Minute Blitz</a> or <a href="http://pytorch.org/tutorials/beginner/pytorch_with_examples.html" rel="noopener nofollow ugc">PyTorch with Examples</a> both say nothing about why one needs to call <code>grad.data.zero_()</code> during training. I think it would be useful to explain this a little more in beginner-level tutorials. RNN is a good example for why accumulating gradient (instead of refreshing) is useful, but I guess new users wouldn’t even know that <code>backward()</code> is accumulating gradient <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=12" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"></p>

jdhao: <p><a class="mention" href="/u/tom">@tom</a>,  since it is possible to accumulate the loss of several minibatch and do one parameter update. For example I want to update the parameter every 64 minibatch, I have the following code</p>
<pre><code class="lang-auto">total_loss = Variable(torch.zeros(1), requires_grad=True)

for idx, (data, target) in train_loader:

    data, target = Variable(data), Variable(target)
    output = model(data)
    loss = criterion(output, target)
    total_loss = total_loss + loss

    if (idx+1)%64 == 0:
        total_loss = total_loss/(64*batchsize)
        total_loss.backward()
        optimizer.step()
        optimizer.zero_grad()
        total_loss = Variable(torch.zeros(1), requires_grad=True)

</code></pre>
<p>Is the above code correct to achieve the desired effect?</p>

tom: <p>I’d do the backward on the (reweighted) loss in each run and not do total loss.</p>
<p>Best regards</p>
<p>Thomas</p>

jdhao: <p>I find that doing the backward for each reweighted loss is much slower than accumulate the loss and then backward.</p>

tom: <p>Thanks for sharing this insight. I would have thought that you get similar speed at much less memory with separate backward calls. But clearly, the experiment proves my modest intuition to be wrong.</p>
<p>Best regards</p>
<p>Thomas</p>

jdhao: <p><a class="mention" href="/u/alband">@albanD</a>, I  have some doubt about what the computation graph looks like in the case that we accumulate the loss manually.</p>
<p>Assume there are total 256 batches for the dataset.  For the normal case (case1), for each batch in an epoch, a new computation graph is created and after the backward pass, the graph is freed. So 256 computation graphs are created and freed during one epoch.</p>
<p>In this case(case2), since we only do backward on every 64 batches. Does that mean only 4 graphs are created?  The graph are created by composing 64 smaller graphs in case1 and the root node in the bigger graph is <code>total_loss</code>. The 64 smaller graph all have the same set of learnable parameters. If that is the case, the bigger graph will consume a lot of memory since it have 64 copies of the small graph.</p>
<p>Is that right? Do you have any ideas?</p>

albanD: <p>Hi,</p>
<p>Indeed, in one case, you will create 256 graphs that work with one input.<br>
In the second case, you will create only 4 graphs. but each of these 4 graphs is actually composed of 64 times the graph above and some <code>Add</code> operations at the end that sum the loss.</p>
<p>Indeed, in the second case you will use much more memory. Indeed, for the 64 iterations, you will create a single graph that just keep growing, and so you will use more and more memory.</p>

jdhao: <p>So We have to make sure that batchsize is not too large, or we will run out of memory.</p>

albanD: <p>Here are three equivalent code, with different runtime/memory comsumption.<br>
Assume that you want to run sgd with a batch size of 100.<br>
(I didn’t run the code below there might be some typos, sorry in advance)</p>
<p>1: single batch of 100 (least runtime, more memory)</p>
<pre><code class="lang-auto"># some code
# Initialize dataset with batch size 100
for input, target in dataset:
    pred = net(input)
    loss = crit(pred, target)
    # one graph is created here
    opt.zero_grad()
    loss.backward()
    # graph is cleared here
    opt.step()
</code></pre>
<p>2: multiple small batches of 10 (more runtime, least memory)</p>
<pre><code class="lang-auto"># some code
# Initialize dataset with batch size 10
opt.zero_grad()
for i, (input, target) in enumerate(dataset):
    pred = net(input)
    loss = crit(pred, target)
    # one graph is created here
    loss.backward()
    # graph is cleared here
    if (i+1)%10 == 0:
        # every 10 iterations of batches of size 10
        opt.step()
        opt.zero_grad()
</code></pre>
<p>3: accumulate loss for multiple batches (more runtime, more memory)</p>
<pre><code class="lang-auto"># some code
# Initialize dataset with batch size 10
loss = 0
for i, (input, target) in enumerate(dataset):
    pred = net(input)
    current_loss = crit(pred, target)
    # current graph is appended to existing graph
    loss = loss + current_loss
    if (i+1)%10 == 0:
        # every 10 iterations of batches of size 10
        opt.zero_grad()
        loss.backward()
        # huge graph is cleared here
        opt.step()
</code></pre>
<p>It should be clear that case 3 is not what you want.<br>
The choice between case 1 and 2 is a tradeoff between memory and speed so that depends on what you want to do.<br>
Note that if you can fit a batch size of 50 in your memory, you can do a variation of case 2 with batch size of 50 and update every 2 iterations.</p>