RuntimeError: stack expects each tensor to be equal size, but got [8, 57] at entry 0 and [4, 57] at entry 1

I’m following this official PyTorch tutorial:
https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html 2
However, I’m altering the code as I go to be a bit more “modern” and extensible as well. So I’ve also implemented a custom dataset that in combination with a DataLoader is giving me the error above.
Here’s what I think is happening:


The dataset is a bunch of names and their ethnic origins. In __getitem__ the names are one-hot encoded at the character level. A tensor with shape (len(name), len(character_vocab) is returned.


The dataloader doesn’t like that the names are variable length and freaks out if batch_size > 1


Question: What technique could I use to overcome this?