I would suggest to leave the code as is but:


Don’t use .data 2, I think it helps clearly understanding what’s going on if you don’t.
I think you want with torch.no_grad(): model.QC_optvar2.copy_(init) instead of the assignment, otherwise you are integrating init into the (new) graph.

Best regards
Thomas