Hello, thanks for the nice answer, it explains a lot! But I still have a question:
Although in the example given by @wasiahmad the hidden states are detached in train() (using repackeage_hidden() ),  in some examples of nn.lstm(), it’s just simply used as:
def forward(self, input):
outputs, (h_n, c_n) = self.lstm(input)
return outputs
, where input is tensor of sequences (or a packed sequence), and the initial hidden states h_0, c_0 are initialized automatically.
In the example above, the hidden states are not detached manually. I am wondering if the attachment is done automatically here, or BPTT is computed based on all previous hidden states without doing any detachments? Thanks.