Getting buffers have already been freed even though retain_graph is True

tbhaxor: <p>I am making my own linear regression model from scratch</p>
<p>Here is the code</p>
<pre><code class="lang-python">w = torch.tensor(-10.0, requires_grad=True)

X = torch.arange(-3, 3.1, 0.1, requires_grad=True).view(-1, 1)
F = -3*X

Y = f + 0.1 * torch.randn(X.size())

def forward(x):
    global w
    y = w*x  
    return y

def mse(yhat, y):
    return torch.mean((y-yhat)**2)

learning_rate = 0.1

for epoch in range(1, 11):
    yhat = forward(x)
    loss = mse(yhat, y)
    loss.backward(retain_graph=True) # error occurs here
    pass
</code></pre>
<p>The following exception is thrown after a successful iteration of the dataset</p>
<pre><code class="lang-auto">RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.
</code></pre>

albanD: <p>Hi,</p>
<p>Which version of pytorch are you using? I can run your code without any issue with the latest version (changing X-&gt;x, F-&gt;f and Y-&gt;y)</p>

tbhaxor: <p>I am using 1.4.0 of pytorch on google colab</p>

albanD: <p>It runs fine for me in colab as well:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/8/e866ef39c0524c1adfde3d5c4779eb5cb4e699b8.png" data-download-href="https://discuss.pytorch.org/uploads/default/e866ef39c0524c1adfde3d5c4779eb5cb4e699b8" title="Screen Shot 2020-02-18 at 1.24.24 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/8/e866ef39c0524c1adfde3d5c4779eb5cb4e699b8_2_552x500.png" alt="Screen Shot 2020-02-18 at 1.24.24 PM" data-base62-sha1="x9VjaItyzpwn3bJv7m0SziKmGdW" width="552" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/8/e866ef39c0524c1adfde3d5c4779eb5cb4e699b8_2_552x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/8/e866ef39c0524c1adfde3d5c4779eb5cb4e699b8_2_828x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/8/e866ef39c0524c1adfde3d5c4779eb5cb4e699b8_2_1104x1000.png 2x" data-dominant-color="F1F1F1"><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">Screen Shot 2020-02-18 at 1.24.24 PM</span><span class="informations">1222×1106 85.8 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>

tbhaxor: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/3/b34bba2de0539f99b8f8bdec713c21faab810908.png" data-download-href="https://discuss.pytorch.org/uploads/default/b34bba2de0539f99b8f8bdec713c21faab810908" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/3/b34bba2de0539f99b8f8bdec713c21faab810908_2_690x357.png" alt="image" data-base62-sha1="pA7I9oCMfhatzRo93Zm64NgScvC" width="690" height="357" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/3/b34bba2de0539f99b8f8bdec713c21faab810908_2_690x357.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/3/b34bba2de0539f99b8f8bdec713c21faab810908_2_1035x535.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/3/b34bba2de0539f99b8f8bdec713c21faab810908.png 2x" data-dominant-color="282828"><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">image</span><span class="informations">1109×575 36.6 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>
<p>Am i missing something?</p>

albanD: <p>Can you try to install a nighly version of pytorch?<br>
Also which python version are you using?</p>

tbhaxor: <p>I am using python 3 and colab</p>

tbhaxor: <p>It’s working when i am removing <code>requires_grad=True</code> from</p>
<pre><code class="lang-auto">X  = torch.arange(-3, 3.1, 0.1, requires_grad=True).view(-1, 1)
</code></pre>