Package: pydens
PyDEnsPyDEnsis a framework for solving Ordinary and Partial Differential Equations (ODEs & PDEs) using neural networks. WithPyDEnsone can solvePDEs & ODEs from a large family includingheat-equation,poisson equationandwave-equationparametric families of PDEsPDEs with trainable coefficients.This page outlines main capabilities ofPyDEns. To get an in-depth understanding we suggest you to also readthe tutorial.Getting started withPyDEns: solving common PDEsLet's solve poisson equationusing simple feed-forward neural network. Let's start by importingSolver-class along with other needed libraries:frompydensimportSolver,NumpySamplerimportnumpyasnpimporttorchYou can now set up aPyDEns-model for solving the task at hand. For this you need to supply the equation into aSolver-instance. Note the use of differentiation tokenD:# Define the equation as a callable.defpde(f,x,y):returnD(D(f,x),x)+D(D(f,y),y)-5*torch.sin(np.pi*(x+y))# Supply the equation, initial condition, the number of variables (`ndims`)# and the configration of neural network in Solver-instance.solver=Solver(equation=pde,ndims=2,boundary_condition=1,layout='fa fa fa f',activation='Tanh',units=[10,12,15,1])Note that we defined the architecture of the neural network by supplyinglayout,activationandunitsparameters. Herelayoutconfigures the sequence of layers:fa fa fa fstands forfully connected architecture with four layers and threeactivations. In its turn,unitsandactivationcotrol the number of units in dense layers and activation-function. When defining neural network this way useConvBlockfromBatchFlow.It's time to run the optimization proceduresolver.fit(batch_size=100,niters=1500)in a fraction of second we've got a mesh-free approximation of the solution on[0, 1]X[0, 1]-square:Going deeper intoPyDEns-capabilitiesPyDEnsallows to do much more than just solve common PDEs: it also deals with (i) parametric families of PDEs and (ii) PDEs with trainable coefficients.Solving parametric families of PDEsConsider afamilyof ordinary differential equationsClearly, the solution is asinwave with a phase parametrized by ϵ:Solving this problem is just as easy as solving common PDEs. You only need to introduce parameterein the equation and supply the number of parameters (nparams) into aSolver-instance:defodeparam(f,x,e):returnD(f,x)-e*np.pi*torch.cos(e*np.pi*x)# One for argument, one for parameters=NumpySampler('uniform')&NumpySampler('uniform',low=1,high=5)solver=Solver(equation=odeparam,ndims=1,nparams=1,initial_condition=1)solver.fit(batch_size=1000,sampler=s,niters=5000,lr=0.01)# solving the whole family takes no more than a couple of seconds!Check out the result:Solving PDEs with trainable coefficientsWithPyDEnsthings can get even more interesting! Assume that theinitial state of the system is unknown and yet to be determined:Of course, without additional information,the problem is undefined. To make things better, let's fix the state of the system at some other point:Setting this problem requires aslightly more complex configuring. Note the use ofV-token, that stands for trainable variable, in the initial condition of the problem. Also pay attention to the additional constraint supplied into theSolverinstance. This constraint binds the final solution to zero att=0.5:defodevar(u,t):returnD(u,t)-2*np.pi*torch.cos(2*np.pi*t)definitial(*args):returnV('init',data=torch.Tensor([3.0]))solver=Solver(odevar,ndims=1,initial_condition=initial,constraints=lambdau,t:u(torch.tensor([0.5])))When tackling this problem,pydenswill not only solve the equation, but also adjust the variable (initial condition) to satisfy the additional constraint.
Hence, model-fitting comes in two parts now: (i) solving the equation and (ii) adjusting initial condition to satisfy the additional constraint. Inbetween
the steps we need to freeze layers of the network to adjust only the adjustable variable:solver.fit(batch_size=150,niters=100,lr=0.05)solver.model.freeze_layers(['fc1','fc2','fc3'],['log_scale'])solver.fit(batch_size=150,niters=100,lr=0.05)Check out the results:InstallationFirst of all, you have to manually installpytorch,
as you might need a certain version or a specific build for CPU / GPU.Stable python packageWith modernpipenvpipenv install pydensWith old-fashionedpippip3 install pydensDevelopment versionpipenv install git+https://github.com/analysiscenter/pydens.gitpip3 install git+https://github.com/analysiscenter/pydens.gitInstallation as a project repository:Do not forget to use the flag--recursiveto make sure thatBatchFlowsubmodule is also cloned.git clone --recursive https://github.com/analysiscenter/pydens.gitIn this case you need to manually install the dependencies.Citing PyDEnsPlease citePyDEnsif it helps your research.Roman Khudorozhkov, Sergey Tsimfer, Alexander Koryagin. PyDEns framework for solving differential equations with deep learning. 2019.@misc{pydens_2019,
  author       = {Khudorozhkov R. and Tsimfer S. and Koryagin. A.},
  title        = {PyDEns framework for solving differential equations with deep learning},
  year         = 2019
}