I thought we cannot do inplace loss update like loss2 += criterion(output2, y_) or am I wrong and I can do it in loop without any issue (I am not considering the speed efficiency)?
also just out of curiosity why the second case that you wrote is different from this
# Use mini-batches
loss2 = 0
for x_, y_ in zip(x, y):
    x_.unsqueeze(0)
    y_.unsqueeze(0)
    
    output2 = model(x_)
    loss2 = loss2 + criterion(output2, y_)
    loss2 /= len(x)
    loss2.backward(retain_graph=True)
# Scale the loss, since we accumulated it

print(model.weight.grad)
``