Error during back propagation

edshkim98: <p>Hello everyone, I am doing a research project which requires to use sum of two loss functions.<br>
Below is a snippet of my code:</p>
<pre><code class="lang-auto">    for epoch in range(epochs):
        e = epoch
        model.train()
        running_loss = 0
        for i, data in enumerate(train_loader):
            #scheduler.step()
            inputs, labels = data['points'].to(device), data['labels'].to(device)
            
            #Discriminator
            #Real image
            inputs2 = torch.cat((inputs, labels.type(torch.float64).reshape(1,2048,1)),axis=2)
            output = disc(inputs2.transpose(1,2).float())
            ls_real = disc_loss(output,num=1)
            optimizerD.zero_grad()
            ls_real.backward()
            
            #Prediction
            output = model(inputs.transpose(1,2),onehot)
            output_pred = output
            _,pred = torch.max(output.data,1)
            #criterion = torch.nn.CrossEntropyLoss()
            #loss = criterion(output, labels)
            
            #Fake image
            inputs2 = torch.cat((inputs, pred.type(torch.float64).reshape(1,2048,1)),axis=2)
            output = disc(inputs2.transpose(1,2).float())
            ls_fake = disc_loss(output, num=0)
            ls_fake.backward(retain_graph=True)
            
            ls_D = ls_real + ls_fake
            optimizerD.step()
            
            optimizer.zero_grad()
            loss = main_loss(output_pred,labels)
            loss = loss + ls_fake
            loss.backward()
            optimizer.step()
</code></pre>
<p>However, I get an error at <strong>loss.backward()</strong> after loss = loss + ls_fake with this error message:</p>
<p><strong>RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [256, 2]], which is output 0 of TBackward, is at version 11; expected version 9 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).</strong></p>
<hr>
<p>The output of <strong>loss</strong> is different to <strong>ls_fake</strong>, so the model for <strong>ls_fake</strong> has two outputs and the model for <strong>loss</strong> has four outputs.</p>
<hr>
<p>Please can someone help me with this problem? Thank you!</p>

CedricLy: <p>Try:</p>
<aside class="quote no-group quote-modified" data-username="edshkim98" data-post="1" data-topic="109030">
<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/edshkim98/48/29279_2.png" class="avatar"> edshkim98:</div>
<blockquote>
<pre><code class="lang-auto">            loss = main_loss(output_pred,labels)
            total_loss = loss + ls_fake
            total_loss.backward()
</code></pre>
</blockquote>
</aside>
<p>Like the error say, there might be a problem with you replacing the loss value.</p>

edshkim98: <p><a class="mention" href="/u/cedricly">@CedricLy</a>, Hi thank you for the quick reply! I’ve just tried your way, but it still gives me the same error message <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"><br>
Could there be another way to solve this issue?<br>
Oh, by the way the output of <strong>loss</strong> is different to <strong>ls_fake</strong>, so the model for <strong>ls_fake</strong> has two outputs and the model for <strong>loss</strong> has four outputs.</p>

CedricLy: <p>OK, so the found problem is the tensor ls_fake.<br>
After doing optimizerD.step(), the gradients have to be recalculated, before using it for optimizer.step().</p>

edshkim98: <p><a class="mention" href="/u/cedricly">@CedricLy</a> Thank you it works now <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>