Hi @shavitamit, I’m facing the same issue.
Target tensor of BxS where S is variable length padded with <PAD> token.
Output of my model is BxV where V is a probability distribution over the vocabulary.
I’ve currently ended up doing exactly what you did originally:
crit = NLLoss(ignore_index='<PAD>')
for token in range(seqlen):
   pred,hidden = model(words[:,token],hidden)
   loss += crit(pred,target[:,token])

I’d love to figure out a smarter way to do it, and I do have a mask, flattening all my tokens without the padding token, but then the dimensions don’t add up, as the loss function expects a two dimensional input.