Solved by phan_phan in post #4 
              
              
                You could sum the padded tensor, and then divide it by the lengths of the sequences. 
I suppose padded is shaped [batch_size, seq_len, embed_size] 
so padded.sum(dim=1) is shaped [batch_size, embed_size] 
and lengths is shaped [batch_size] 
out = padded.sum(dim=1).div(lengths.float().unsqueeze(dim=1…