S_J:

print(‘pred’, preds.shape) #1240xvocab targets = batch[:, 1:].contiguous().view(-1) print(‘tgt’, targets.shape) #1248 loss = criterion(preds, targets) #error here


you have mentioned in your comments already.
You are giving 1240 predictions and 1248 targets. hence the error. Ideally the shapes should have been predictions=1248 x vocab, target=1248.
Is there something thats confusing or did I misunderstand the issue here?