How to combine a PyTorch network and a non-differentiable simulator

I would like to combine a neural network Net with a non-differentiable fixed simulator Sim as follows:

aNetSimc_pred

  1. Net takes as input vectors a and predicts vectors b,
  2. Sim (implemented in Python/numpy) takes as input b and, after integrating PDEs, produces vectors c_pred,
  3. My ground truth are data (a, c)
  4. I would like to tune the Net to minimize the loss L(c, c_pred)

So far I wrap the Sim in a th.autograd.Function but the learning is not great because I can’t define a good gradient of the Sim in the custom backward().

How can I solve this?

Kind regards,
Carlo

You approach is valid and you would need to define a “proper” backward method in your custom module.