You are right, I want to update the weights in each batch. I’m not sure which way should I call detach ().
I have tried to use the following code:
for i in range(epochs):
    
    for x_train, y_train in zip(X_train, Y_train):
        model.train()
        model.zero_grad()
        y_pred = model(x_train)
        single_loss = loss_function(y_pred, y_train)
        loss_train_value=single_loss.item()
        train_loss.append(loss_train_value)
        
        single_loss.backward()  
        optimizer.step()


    
    model.lstm.reset_hidden_cell()
    model.lstm.reset_hidden_state()



I receive the following error code:
RuntimeError                              Traceback (most recent call last)
<ipython-input-22-a544b1c4beff> in <module>
     20         train_loss.append(loss_train_value)
     21 
---> 22         single_loss.backward()
     23         optimizer.step()
     24 

~/anaconda3/lib/python3.7/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
    196                 products. Defaults to ``False``.
    197         """
--> 198         torch.autograd.backward(self, gradient, retain_graph, create_graph)
    199 
    200     def register_hook(self, hook):

~/anaconda3/lib/python3.7/site-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
     98     Variable._execution_engine.run_backward(
     99         tensors, grad_tensors, retain_graph, create_graph,
--> 100         allow_unreachable=True)  # allow_unreachable flag
    101 
    102 

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.

However I do not receive this error code if I use
model.lstm.reset_hidden_cell()
model.lstm.reset_hidden_state()


After each batch and not only one time  for each epoch.
for i in range(epochs):
    
    for x_train, y_train in zip(X_train, Y_train):
        model.train()
        model.zero_grad()
        y_pred = model(x_train)
        single_loss = loss_function(y_pred, y_train)
        loss_train_value=single_loss.item()
        train_loss.append(loss_train_value)
        
        single_loss.backward()  
        optimizer.step()


    
        model.lstm.reset_hidden_cell()
        model.lstm.reset_hidden_state()


However, this implies that the state between batches is not maintained, so this is not the solution.
I have tried to specify retain_graph=True , single_loss.backward(retain_graph=True)
for all batch except the last one.
But this doesn’t seem to work either.