Could you check the shape of init_pred and target?
Based on your code, it should be a single element tensor, but apparently that’s not the case.
Yes, your code currently should only work with batch_size=1, since you are using .item() ops and calculate the dataset length based on the length of your DataLoader, which will be wrong for another batch size.