Usually you are using a criterion to calculate the loss. In the default setting the loss is averaged by the batch size, so that you don’t have to scale it.
In your example the model returns directly the loss, which is also valid, but I’m not sure, if you need to scale it.
You should definitely check if it’s right.
Back to your original question.
In your first example you are using batch gradient descent, i.e. all training samples are used for one weight update. Each iteration is thus one epoch.
This procedure might be OK, but it has been shown, that mini-batch gradient descent generalizes better in a lot of use cases.
I’m not sure, if your second example uses only one single example or a few.
If it’s the former, you are applying stochastic gradient descent. You will have much more weight updates in your epoch, since each sample will give you gradients.
The latter case is called mini-batch gradient descent and is usually the way to go.
You use a specific batch size and update your model using the loss of this batch.
Some layers like BatchNorm need a certain amount of samples to accurately estimate the running statistics, i.e. mean and var. It’s usually approx. 64 samples, but might differ based on your problem.
The final results of the training will most likely differ! The batch size is one hyperparameter which can change your model’s performance.