This is how I solved it:
def collate_fn_padd(batch):
    '''
    Padds batch of variable length

    note: it converts things ToTensor manually here since the ToTensor transform
    assume it takes in images rather than arbitrary tensors.
    '''
    ## get sequence lengths
    lengths = torch.tensor([ t.shape[0] for t in batch ]).to(device)
    ## padd
    batch = [ torch.Tensor(t).to(device) for t in batch ]
    batch = torch.nn.utils.rnn.pad_sequence(batch)
    ## compute mask
    mask = (batch != 0).to(device)
    return batch, lengths, mask


Related posts:

How to create batches of a list of varying dimension tensors? 107
How to create a dataloader with variable-size input 61
Using variable sized input - Is padding required? 31
DataLoader for various length of data
How to do padding based on lengths? 17

bucketing:

Tensorflow-esque bucket by sequence length 12


Stack overflows version:

  
      stackoverflow.com
  
  
      
    
  

  How does Pytorch Dataloader handle variable size data? 128



  python, pytorch, tensor, variable-length



  asked by
  
  
    Trung Le
  
  on 10:08AM - 07 Mar 19 UTC


  
  
    
    
  
  



crossposted: https://www.quora.com/unanswered/How-does-Pytorch-Dataloader-handle-variable-size-data 14