This error could be resolved by setting the ‘drop_last’ parameter in DataLoader. You might be getting this error because your training data is not divisible by batch size. In that case, you will get this error at the end of epoch. Suppose your training data has 1005 records and batch size is 100, for the last batch, it will have only 5 records and you may get dimension error. If you keep ‘drop_last’ as True, those 5 records will be ignored.
train_loader = DataLoader(train_data, shuffle=True, batch_size=batch_size, drop_last=True)