The code looks alright, but note that train_loss += loss.item() and the print statement won’t be executed, as you are using a return statement before these lines of code.
Besides that I don’t know, if you really need to use retain_graph=True, since you are re-calculating the loss in each iteration.