The DataLoader loop looks alright.
You shouldn’t call model.forward, but the model directly via outputs = model(loc_X_train) so that registered hooks will be properly called.
Also, usually you would update the parameters after the backward call in each iteration not once per epoch, so you might want to call optimizer.step() inside the DataLoader loop.