It depends on the loss formula, but you would want to vectorize the loss instead of using for loops if possible l. Most PyTorch functions lend themselves to it quite naturally through broadcasting.
Best regards
Thomas