It’s not necessary to use for-loop,
mse_criterion = torch.nn. MSELoss (reduction=‘none’ )
loss = mse_criterion(input, label) # input->BxCxHxW, loss->Bx1 or B,
loss.backward(gradient=torch.ones_like(loss))
Then, loss.grad will be individual grad for each sample.