I don’t know how the forward method is implemented, but assume that output contains the log probabilities, while hidden is created in another way?
Could you check output for invalid values such as NaNs and Infs?
If you can find some, I would check the inputs for invalid values first, and in case the inputs are looking fine, I would check the value range of the output and hidden tensors inside the loop to see, if the model is diverging during the line_tensor loop.