-
Notifications
You must be signed in to change notification settings - Fork 948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to include control variables? #128
Comments
So the way you're handling Our ODE solvers rely on Taylor series, so x(t) needs to have enough continuous derivatives. But with a discontinuous u(t), the first derivative of x is discontinuous (in general this should be true; I didn't fully delve into how u affects x in your example). To test this, I changed
to
then, just for sanity, used a finer grid
There's a simple fix to this! Provided that you know exactly where the discontinuities are. We can view the combined system
to
Optimization then proceeds okay:
The small gotcha is that there's an |
Many thanks for your great work. The library is really solving a relevant problem. It's working well for me for ODE's purely based on state variables. However, I am facing some issues when trying to include control variables
u
in addition to state variablesx
. Although this is working well for the forward simulation (i.e. prediction ofx
by applying the odeint function once), it is not working when I try to fit parameters (i.e. multiple iterations, in each of which there is a forward simulation followed by a pytorch optimization step). By that, I mean that I am getting a very bad fit and during optimization the losses can go up.Hence, my main question is: What is your recommended way to include control variables
u
into the ODE if the goal is to fit the parameters of the ODE?To elaborate, I am sharing the following reproducible example (Google Colab Notebook) with you, which shows how I have tried to include control variables
u
so far: https://drive.google.com/file/d/1_F80gHZG5tJzKKWU4Ku-po_iroo2B1kz/view?usp=sharingtorchdiffeq.odeint expects as first argument the system function, which is a function of time t and states x (https://github.com/rtqichen/torchdiffeq/blob/master/torchdiffeq/_impl/odeint.py). In order to handle controls, I have defined this function s.t. it internally gets the controls u corresponding to the input t. For this, I have used an interpolation function (based on a historic sequence of u values and corresponding timepoints t). I wrote a custom interpolation function because existing functions generally use numpy (which results in errors if the inputs are tensors that require gradients).
As you can see, the fitted parameters are not better than the initial parameters when I apply this approach. Once I remove the control
u
(and replace it with statex
), then fitting works well.The text was updated successfully, but these errors were encountered: