Problem backwarding a second time

Simone256: <p>I’m facing an issue, which is probably stupid, that I can’t solve.</p>
<p>I would like to train a generator of images. My architecture is made by generator that maps some noise in a 28 x 28 tensor, and after this I compute an estimator of the maximum mean discrepancy (using a Gaussian kernel) between the generated images and my imported images (MNIST). This discrepancy will be my loss, that I want to back-propagate with respect to the parameters of my generator. Not sure if this is gonna work, but <a href="https://auai.org/uai2015/proceedings/papers/230.pdf" rel="noopener nofollow ugc">it should</a> <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"> .</p>
<p>This is the main part of my optimization:</p>
<pre><code class="lang-auto">for epoch in range(epochs):

    noise = torch.randn((noise_batch_size, latent_size))
    generated_images = net(noise)

    for input_images, _ in iter(train_loader):
        input_images = input_images.to(device)
        optimizer.zero_grad()
        loss = mmd(input_images.squeeze(), generated_images.squeeze())
        loss.backward()
        optimizer.step()
</code></pre>
<p>Sadly, It returns a RuntimeError when it backpropagates the second time:</p>
<blockquote>
<p>RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling backward the first time.</p>
</blockquote>
<p>At this point I try to “correct” my code with <code>loss.backward(retain_graph=True)</code>, but I get another RuntimeError always at the second backpropagation:</p>
<blockquote>
<p>RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [1, 10, 3, 3]] is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!</p>
</blockquote>
<p>Unfortunately, I wasn’t able to fix this (I tried setting some <code>inplace=False</code> or random suggestions I found in old topics, but I guess I’m missing what is the actual problem here…)</p>
<p>I ask again sorry, this will definitely be a newbie error.</p>

sio277: <p>I don’t know much about the paper you mentioned, but the error is caused because you tried to backpropagate the created graph (<code>generated_images = net(noise)</code>)  more than once through the inner for loop. General usage is, put your forward (<code>generated_images = net(noise)</code>) inside the inner for loop, such as,</p>
<pre><code class="lang-auto">for epoch in range(epochs):
    for input_images, _ in iter(train_loader):
        noise = torch.randn((noise_batch_size, latent_size))
        generated_images = net(noise)
        input_images = input_images.to(device)
        optimizer.zero_grad()
        loss = mmd(input_images.squeeze(), generated_images.squeeze())
        loss.backward()
        optimizer.step()
</code></pre>

Simone256: <p>You’re definitely right. As I said it was really really stupid! Thanks a lot!!! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>