Hi,
I’m working on a RNN at the moment, however the retain_graph option is consuming all of my gpu memory eventually. And training seems to get slower every epoch.
def learn(X, y, hidden):
  model.zero_grad()
  
  output, hidden = model(X, hidden)
  
  loss = criterion(output, y)
  
  optimizer.zero_grad()
  loss.backward(retain_graph=True)
  optimizer.step()
  
  return loss, output, hidden

However, when I don’t specify retain_graph=True I get the following error: “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”.  And I notice this only happens when I keep the hidden value, if my hidden is always None, then it works just fine.
Is there a way around this?