Because the “for loop” code also calculate the average loss of batch_size examples. the gradient is depend on loss. the loss between code1 and code2 is the same? so the gradient is the same? I guess.
Thanks