I dont think there is any implementation of maskedcriterion. The best way is to mask the score vector/logits yourself as per this thread, using masked_select. gather doesn’t works for variable length sequences.
Having said that, i’m also interested to know if there’s a better approach to this

  
    
    
    How can i compute seq2seq loss using mask? 
  
  
    I am working on image captioning task with PyTorch. 
In seq2seq, padding is used to handle the variable-length sequence problems. 
Additionally, mask is multiplied by the calculated loss (vector not scalar) so that the padding does not affect the loss. 
In TensorFlow, i can do this as below. 
# targets is an int64 tensor of shape (batch_size, padded_length) which contains word indices.    
# masks is a tensor of shape (batch_size, padded_length) which contains 0 or 1 (0 if pad otherwise 1).

out…