How to create batches of a list of varying dimension tensors?

Thank you for the script! Before I learned about the DataLoader object and the iterables, I wrote my own Batch Generator. The one you provided is great and I wanted to share my version for the last bit where you create the Batch feature tensor.

features = torch.zeros((len(data), max_len, n_ftrs))

for idx,value in enumerate(data):
     features[idx,:value.shape[1],:] = torch.tensor(val)

I guess this one is a little faster.

What about this?

from functools import partial
pad_frames = partial(torch.nn.utils.rnn.pad_sequence, batch_first=True, padding_value=-1.)
dl = torch.utils.data.DataLoader(vl, batch_size=10, shuffle=True, collate_fn=pad_frames)

Looks working good to me

stuck on the same doubt can anyone help?

1 Like