You use torch.flatten(x) in your code, it reshape x without considering number of batches that you enter. To consider it in your calculation you can
Replace x = Torch.flatten(x) with x = x.reshape(x.shape[0], -1)

this will guarantee that your network takes into account the batch size before feeding input into Linear layer.