Issue in running loss.backward()

Ayush_Sarangi_4-Year: <p>i am clustering the target image into k clusters and i want pixels of my predicted image to learn those cluster values.<br>
for that i am running loop for each cluster in range(k), and then i am computing mse loss for all those pixels that belongs to same cluster. since i am using a batch size of 8 images so it is computed across whole batch for each cluster.<br>
but then i am running into the following issue:</p>
<p>RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.</p>
<p>whereas i just want all the losses obtained from each cluster to get accumulated  and then run backprop.<br>
kindly help me in this issue.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/b/9b760e7d53d43a0e5fbdbaf015910e4f8f171c20.png" data-download-href="https://discuss.pytorch.org/uploads/default/9b760e7d53d43a0e5fbdbaf015910e4f8f171c20" title="Screenshot (88)"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/b/9b760e7d53d43a0e5fbdbaf015910e4f8f171c20_2_690x388.png" alt="Screenshot (88)" data-base62-sha1="mbgWsXEtETtWGWP5T6d6J0xlVYc" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/b/9b760e7d53d43a0e5fbdbaf015910e4f8f171c20_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/b/9b760e7d53d43a0e5fbdbaf015910e4f8f171c20_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/b/9b760e7d53d43a0e5fbdbaf015910e4f8f171c20_2_1380x776.png 2x" data-dominant-color="DEDADD"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot (88)</span><span class="informations">1920×1080 222 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ElLoboLoco: <p>When calling loss.backward() you are implicitely erasing the computation graph that was built when you made a prediction with your model :</p>
<pre><code class="lang-auto">Y_pred = model(X_batch, embeds)
</code></pre>
<p>Thus inside the inner loop where you call backward() n_clusters time, you ask autograd to backward through a graph that was freed after the first inner loop execution. As suggested, you can try setting ‘retain_graph=True’ inside backward to keep the graph but I’m not sure this will work as you are also repetitively computing the loss. The simplest solution might be to make the prediction inside the inner loop.</p>

AlphaBetaGamma96: <p>Hi <a class="mention" href="/u/ayush_sarangi_4-year">@Ayush_Sarangi_4-Year</a>,</p>
<p>Also, when sharing code please copy and paste the code and wrapped it in three ``` symbols, so that the code is properly formatted like <a class="mention" href="/u/elloboloco">@ElLoboLoco</a> did above with,</p>
<aside class="quote no-group" data-username="ElLoboLoco" data-post="2" data-topic="208233">
<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/elloboloco/48/30094_2.png" class="avatar"> ElLoboLoco:</div>
<blockquote>
<pre><code class="lang-auto">Y_pred = model(X_batch, embeds)
</code></pre>
</blockquote>
</aside>