Using the .data attribute is not recommended, as Autograd won’t track this change.
Based on your description, it seems however as if that’s exactly your use case. I.e. you want to inject a custom value into the graph, which was not created in a differentiable way.
Given that, I would highly recommend to manually check the gradient calculation and make sure that the gradients are indeed calculated as expected.



 yuri:

why should y_pred1 be freed when calling loss2.backward() when the gradient is not a function of neither y_pred1, base_l1 nor base_l2?


I don’t know where this approach is coming from so could you explain it a bit more?