Using TensorFlow gradient


I’ve been trying many things without success, unfortunately. Basically, this is what I want:
X --> PyTorch model --> X* --> TF model
Then, I can obtain the gradient w.r.t X* through the TF model, but I want to propagate it through the PyTorch model to obtain an X.grad.

This is how far I’ve come:

I’ve been using this and this to get so far.

I’m new to writing custom gradient functions, so my code probably doesn’t make much sense. :slight_smile: Hope anyone can help out.


Hi, I managed to figure it out myself! :slight_smile: It’s actually much simpler than I thought. For any future readers: