That’s certainly the right sequence of the right steps; this stuff is no fun to debug but I’d try printing out the indices and making sure things end up in the right places. If at all possible (i.e. if you don’t have two coupled RNNs like for inference or translation) you can instead sort both the data and the labels in your data preprocessing (e.g. with torchtext) but I’m guessing you’re doing it this way because you can’t do it that way.