How to use pytorch to fit parameters of differential equations
Project description
paramfittorchdemo
This file will become your README and also the index of your documentation.
Install
pip install paramfittorchdemo
How to use
Using the integration routines in the library torchdiffeq we can integrate differential equations using torch modules as the base class for describing the systems. This allows for gradients to be computed with respect to the model parameters. This can be combined with the local optimization schemes used to train neural networks to give a powerful framework for fitting differential equation models found in the scientific literature.
In this package I provide a few example systems and parameter fitting routines for fitting the parameters.
The below code demonstrates how the van der Pol oscillator system defined as `torch.nn.module1 can be integrated against inside of pytorch.
import torch
from torchdiffeq import odeint_adjoint as odeint
import matplotlib.pyplot as plt
vdp_model = VDP(mu=0.5)
ts = torch.linspace(0,30.0,1000)
# Create a batch of initial conditions
batch_size = 30
initial_conditions = torch.tensor([0.01, 0.01]) + 0.2*torch.randn((batch_size,2))
sol = odeint(vdp_model, initial_conditions, ts, method='dopri5').detach().numpy()
# Check the solution
plt.plot(sol[:,:,0], sol[:,:,1], color='black', lw=0.5);
plt.title("Phase plot of the VDP oscillator");
plt.xlabel("x");
plt.ylabel("y");
The automatic batching dimensions means we can easily integrate a collection of initial conditions at the same time using these routines as well. The above shows the phase plane plot of the VDP oscillator for 30 random initial conditions.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file paramfittorchdemo-0.0.1.tar.gz
.
File metadata
- Download URL: paramfittorchdemo-0.0.1.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f240d455f0eff274bca27dccd4371a56d8f538b984a2f4e1019d9f5d36884af |
|
MD5 | 77ea96d73e4d8ef747811b2fb3f33d01 |
|
BLAKE2b-256 | 59467b271dcc71340f687baa3df79ed9bc6e21330d19656c9d741cc70fb987b8 |
File details
Details for the file paramfittorchdemo-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: paramfittorchdemo-0.0.1-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70e905ca0de1b5490535bd7951af0d6f865ab388941313198ff6a15c11773df5 |
|
MD5 | cf88bf98b5c8799a3673cf1faf15f73c |
|
BLAKE2b-256 | 17fbe89bdae2858ade7545173f35ba667bd63c8bd1aabacfddb275a9d883b26e |