<jupyter_start><jupyter_text>神经网络解常微分方程例子<jupyter_code>from ode_example.forward_problem.model import *
from ode_example.forward_problem.data import *
from ode_example.forward_problem.criterion import *
from ode_example.forward_problem.train import *
# %pylab
# %matplotlib qt5<jupyter_output><empty_output><jupyter_text>1. 正问题(forward problem)所谓正问题即已知偏微分方程的形式，初始条件或边界条件，对其进行求解，DGM最初提出来只是作为PDE求解器(解决正问题)。在ode_example/forward_problem/文件夹下给了一个非常简单的例子，即求解如下常微分方程$$\left\{\begin{aligned}&\frac{dy}{dx} = 1 \\& y(0) = 0 \\ & x \in [0, 4]\end{aligned}\right.$$<jupyter_code>model = SolveOde(5, 30) # 3 layer 30 node
data_sampler = DataSampler(100, 1)  # 100 data, 1 boundary data
criterion = Criterion(model, data_sampler)

train = Train(criterion)
train.train(1000, 1e-4)<jupyter_output>Step 999 - lr 0.0001 -  loss: 0.0032575593423098325<jupyter_text>而它对应的期望损失函数为$$ loss = E_{x\sim U_[0, 4]}[||\frac{dy}{dx} - 1||^2] + E_{x \sim U_{\{0\}}}[||y(x)||^2]$$离散成经验损失函数(empirical loss function)，对应pytorch代码如下```x_batch, x_0 = self.data_sampler.sample()x_batch.requires_grad = Truey_batch = self.model(x_batch)y_0 = self.model(x_0)jacob_matrix = torch.autograd.grad(y_batch, x_batch, grad_outputs=torch.ones_like(y_batch),                                   create_graph=True)dy_dx = jacob_matrix[0][:, 0].reshape(-1, 1) ode loss + initial losserror = torch.mean((dy_dx - 1) ** 2) + torch.mean(y_0 ** 2)```  2. 逆问题(inverse problem)所谓逆问题即已知偏微分方程的形式，并且对解有**一组观测**，需要**估计方程中的参数**(常数或也是自变量的函数)。在ode_example/inverse_problem/文件夹下给了一个非常简单的例子，即对上一节正问题中方程的解有一组观测，需要估计参数函数$k(x) = 1$。$$\left\{\begin{aligned}&k(x)\frac{dy}{dx} = 1 \\& y(0) = 0 \\ & x \in [0, 4]\end{aligned}\right.$$<jupyter_code>from ode_example.inverse_problem.model import *
from ode_example.inverse_problem.data import *
from ode_example.inverse_problem.criterion import *
from ode_example.inverse_problem.train import *
import torch


model = EstimateCond(3, 30, 3, 330)
# model = torch.load("model.pth")

x = torch.linspace(0, 4, 50, dtype=torch.float32).reshape(-1, 1)
observe_x_y = torch.cat([x, x], dim=1)
# print("observe_x_y.shape = ", observe_x_y.shape)

data_sampler = DataSampler(100, 1, 30, observe_x_y)  # 100 data, 1 boundary data
criterion = Criterion(model, data_sampler)

train = Train(criterion)
train.train(1000, 3e-4)<jupyter_output>Step 999 - lr 0.0003 -  loss: 2.935027823448181
{'fraction_non_alphanumeric': 0.15692963752665245, 'fraction_numerical': 0.057569296375266525, 'mean_word_length': 6.002985074626865, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '640789', 'license': 'MIT', 'n_tokens_mistral': 1179, 'n_tokens_neox': 1104, 'n_words': 146, 'path': 'docs/ODE_example.ipynb', 'repo_name': 'GCaptainNemo/Deep-Galerkin-Method', 'size': '2345'}