Hello,
I am running in the backpropagation error that states that “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”.
However, I don’t see where is my code trying to run through the graph a second time. Let me show you two examples, the first which works fine, and the second which does not, and I do not understand why it fails.
x = torch.randn( (5, 4), requires_grad=True )  
theta = torch.randn( (4, 3), requires_grad=False )

optimizer = optim.Adam( [x] )

xx = x  # Apparently this is the conflicting line

for k in range(10):
    m = torch.matmul( xx, theta )
    
    optimizer.zero_grad()
    m.sum().backward()
    optimizer.step()
    

This works fine. However, if I change the xx declaration to simply
xx = x/10

then I get the backpropagation error. Also setting “retain_graph=True” as suggested simply makes “x” not optimise at all, it behaves as if “requires_gradient” were set to “False”, which is not the behaviour I need.
I don’t understand what is going on.