You could create a Dataset and use a DataLoader to yield your batches from it.
See the data loading tutorial 45 for more information.
Using this approach you won’t have to implement the batching itself. The DataLoader will handle it for you.