I want to mask output of the seq2seq(decoder) in my case I need only last time step of the decoder. As sequences have different lengths in the batch I use padding. I can’t use pack_padded_sequence because of lengths can’t be in a decreasing order.
So I mask output in such way:
output_mask = create_mask(batch_size, lengths_dec, len(dec_outputs[0][0]))
masked_outputs = torch.masked_select(dec_outputs, output_mask)
masked_outputs = masked_outputs.view((-1, self.hidden_size))

Finally, I get only last time steps in the batch which I put to the linear layer.
Can I use this approach? Can you give me a hint if there is an easier way to do so?