Solved by BramVanroy in post #4 
              
              
                Just an FYI maybe https://pytorch.org/docs/stable/generated/torch.nn.utils.rnn.pad_sequence.html pad_sequence can simplify your code a bit.