I think you want to use the collate_fn function in the DataLoader class.

  
      github.com
  
  
    dhpollack/programming_notebooks/blob/master/pytorch_attention_audio.py#L245 786

    return Feats


class Labeler(object):
"""Labels from text to int + 1


"""


def __call__(self, labels):
    return torch.LongTensor([int(l)+1 for l in labels])


def pad_packed_collate(batch):
"""Puts data, and lengths into a packed_padded_sequence then returns
   the packed_padded_sequence and the labels. Set use_lengths to True
   to use this collate function.


   Args:
     batch: (list of tuples) [(audio, target)].
         audio is a FloatTensor
         target is a LongTensor with a length of 8
   Output:
     packed_batch: (PackedSequence), see torch.nn.utils.rnn.pack_padded_sequence



  
  
    
    
  
  


I did one there with packed sequences.  I don’t know if this is the fastest way, but it would accomplish what you want to do.  Also you could use any of the pre-built samplers that you wanted.