"One of the variables need for gradient computation"

Erik_Bertolino: <p>has been modified by an inplace operation: [torch.FloatTensor [1]] is at version 2, expected version 1.</p>
<p>I know that this error implies that the computational graph has been used previously, and one should not use it twice. However - what does “[torch.FloatTensor [1]]” specifiy? And what does “version 2” and “version 1” mean?</p>
<p>My code is quite long but in short:</p>
<pre><code class="lang-auto">
ref_grad = []
        for i in range(2):
            layer_grad = utils.AverageMeter()
            k = 0
            for param in neuralnet.decoder.de_dense.parameters():
                if(k == 0 or k == 2):
                    wrtt = param
                    print(param.shape)
                k = k + 1
            layer_grad.avg = torch.zeros(wrtt.shape).to(device)
            
            ref_grad.append(layer_grad)
            

    for j in range(2):
                k = 0
                for param in neuralnet.encoder.en_dense.parameters():
                    if(k == 0 or k == 2):
                        wrt = param
                        print(param.shape)
                    k = k + 1                                                        
                target_grad = torch.autograd.grad(recon_loss, wrt, create_graph = True, retain_graph = True)[0]
                print(j)
                grad_loss += -1*func.cosine_similarity(target_grad.view(-1,1), ref_grad[j].avg.view(-1,1), dim = 0)
                grad_loss = grad_loss/l_2



            neuralnet.optimizer.zero_grad()
            l_tot.backward(retain_graph = True)
                
</code></pre>
<p>If I remove target_grad and grad_loss, the code is functional (bus useless). So obviously target_grad is the problem here. How do I fix this?</p>

albanD: <p>Hi,</p>
<blockquote>
<p>I know that this error implies that the computational graph has been used previously, and one should not use it twice.</p>
</blockquote>
<p>No it is not what this error means. This error means that a Tensor that is needed to compute the backward has been modified inplace. And so the backward cannot be computed anymore.</p>
<blockquote>
<p>However - what does “[torch.FloatTensor [1]]” specifiy? And what does “version 2” and “version 1” mean?</p>
</blockquote>
<p>This means that the faulty Tensor (that was modified inplace) is a float Tensor of shape <code>[1]</code> and is at version 2 (second time it has been modified inplace) while the autograd expect version 1 (the result after it was modified inplace once).</p>

Erik_Bertolino: <p>Hello. Thank you for answering.</p>
<p>The code is functional if I remove</p>
<pre><code class="lang-auto"> target_grad = torch.autograd.grad(recon_loss, wrt, create_graph = True, retain_graph = True)[0]
</code></pre>
<p>What I seem to understand is that this computed the gradient of recon_loss with respect to the tensor wrt. It retains also the graph. Why does this interfere later in the code when I create my loss function l_tot and apply backward() to it, l.backward()?</p>

albanD: <p>I think the <code>grad_loss</code> might be the problematic Tensor as it is of size 1 and is modified inplace when you do <code>+=</code>.<br>
Could you change that to be out of place with <code>grad_loss = grad_loss + ...</code> to see if it fixes the problem?</p>

Erik_Bertolino: <p>Sadly, I received another error.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://i.gyazo.com/3b85147b41a59b5fdda57d7d28ea1d18.png" title="" rel="noopener nofollow ugc"><img src="https://i.gyazo.com/3b85147b41a59b5fdda57d7d28ea1d18.png" alt="" width="690" height="201"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use xlink:href="#far-image"></use></svg><span class="filename"></span><span class="informations">1322×387</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use xlink:href="#discourse-expand"></use></svg></div></a></div></p>

albanD: <p>Looks like an issue with a values saved by batchnorm? You might want to enable anomaly during the forward as proposed in the warning to know which one is faulty.</p>

Erik_Bertolino: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://i.gyazo.com/795f3a046f1d33a1d16262d116682bfc.png" title="" rel="noopener nofollow ugc"><img src="https://i.gyazo.com/795f3a046f1d33a1d16262d116682bfc.png" alt="" width="690" height="214"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use xlink:href="#far-image"></use></svg><span class="filename"></span><span class="informations">2721×846</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use xlink:href="#discourse-expand"></use></svg></div></a></div></p>

albanD: <p>Yep that one batchnorm <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

Erik_Bertolino: <p>Is there any canonical solution to this? Or is this a very rare error?</p>

albanD: <p>It is an error that happens quite often but there is no canonical solution as it depends on what your code does.<br>
Basically your code modifies inplace a Tensor that this batchnorm needs to compute its backward. You need to remove that inplace operation. But where that inplace is depends a lot on your code and what you do <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"></p>