for batch in range(num_data // batch_size + (num_data % batch_size > 0)):
Ever run into errors when training models that have to do with running one too many or two few batches?
This snippet should fix it.
- First get the number of iterations that the batch fits cleanly in the number of data points. E.g.
num_data // batch_size
. - Add
1
if there are remaining data points. E.g.(num_data % batch_size > 0)1
.
This does assume python3.