Ok sounds good!
Then the second example should do what you want 
Note that you can play with the tradeof between memory and speed by accumulating a bunch of losses in the double for loop before calling backward. Doing so will use more memory but will speed things up a little bit.
Like for example accumulate the loss for the j loop and call backward once at the end.