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


#1

Hello Guys,
error
Traceback (most recent call last):

File “”, line 1, in
runfile(‘C:/Users/SRIKANTH R/.spyder-py3/pyro_bayesian_ternary.py’, wdir=‘C:/Users/SRIKANTH R/.spyder-py3’)

File “C:\Users\SRIKANTH R\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 705, in runfile
execfile(filename, namespace)

File “C:\Users\SRIKANTH R\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 102, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)

File “C:/Users/SRIKANTH R/.spyder-py3/pyro_bayesian_ternary.py”, line 198, in
instance.do_inference()

File “C:/Users/SRIKANTH R/.spyder-py3/pyro_bayesian_ternary.py”, line 171, in do_inference
elbo=svi.step(images,labels)

File “C:\Users\SRIKANTH R\Anaconda3\lib\site-packages\pyro\infer\svi.py”, line 99, in step
loss = self.loss_and_grads(self.model, self.guide, *args, **kwargs)

File “C:\Users\SRIKANTH R\Anaconda3\lib\site-packages\pyro\infer\trace_elbo.py”, line 136, in loss_and_grads
surrogate_loss_particle.backward(retain_graph=self.retain_graph)

File “C:\Users\SRIKANTH R\Anaconda3\lib\site-packages\torch\tensor.py”, line 102, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)

File “C:\Users\SRIKANTH R\Anaconda3\lib\site-packages\torch\autograd_init_.py”, line 90, in backward
allow_unreachable=True) # allow_unreachable flag

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.
I have got the following error and have surfed through many posts in pytorch forums. It says specify retain_graph =True when calling backward for the first time. However, I have used SVI in my code instead of loss. backward()

I am not sure where I need to include it in the code.

I have enclosed the code here


#2

I think the bug might be somewhere in your model or guide, so please post the complete code. It will be much more readable and easier for others to replicate if you were to post your code inside code blocks as below:


```python
# code 
x = y

#3

Hi Neeraj,

I replaced trace_ELBO() with traceEnum_ELBO() and it worked. As in I removed the error. :slight_smile: