When a new tensor is passed to model.encode() function  (or to model.forward()) then self.hidden_state become reinitialized.

reinitializing the hidden states between samples

Earlier, when I passed one training sample to the model at a time, the hidden_state was reinitialized between samples each time.
But now I pass whole batch to the model and there’s no need to reinitialize hidden_state between samples.
The problem I have is I need to calculate loss for the whole batch. When I’m trying to pass my decoder outputs and my target indexes (torch.Size([3, 2, 35620]) and torch.Size([3, 2])) to NLLLoss, I get an error about inconsistent tensor sizes. It seems NLLLoss asks me to use one-hot encoding for computing loss. But one-hot encoding will greatly increase memory usage, that makes impossible to use big batches in Google Colab.