Quick Start¶
This guide demonstrates how to use PaddleScience to train a model, solve equation learning and prediction problems, and visualize results through a simple demo and its extension.
1. Problem Introduction¶
Consider the task of using a neural network to fit the function \(u=\sin(x)\) over the interval \(x \in [-\pi, \pi]\). We explore two scenarios to fit \(u=\sin(x)\) as accurately as possible: one where the target function is known, and another where it is unknown.
In the first scenario, assuming the analytical solution \(u=\sin(x)\) is known, we employ supervised learning. We generate labeled data pairs \((x, u)\) using the formula and train the model to map the independent variable \(x\) to the dependent variable \(u\).
In the second scenario, assuming the analytical solution \(u\) is unknown but satisfies a specific differential relationship, we demonstrate how to train the model using a differential equation, specifically \(\dfrac{\partial u} {\partial x}=\cos(x)\), along with boundary conditions.
2. Scenario 1¶
Target fitted function:
We generate \(N\) pairs of data \((x_i, u_i), i=1,...,N\) as supervised data for training.
Before writing the code, we first import the necessary packages.
Next, create directories for logging and model checkpoints. This step is standard practice before initializing most training tasks.
Now, we proceed with the core implementation.
First, define the problem domain. We use ppsci.geometry.Interval to define a 1D line segment, facilitating the sampling of points \(x\) within this domain.
Then define a simple 3-layer MLP model.
This configuration specifies that the model takes the independent variable \(x\) as input and outputs the prediction \(\hat{u}\).
Next, we define the calculation function for the known solution \(u=\sin(x)\) and use it within ppsci.constraint.InteriorConstraint to generate label data. InteriorConstraint guides model optimization by enforcing consistency between model predictions and label data sampled within the specified geometry.
Here, interior_constraint encapsulates the training objective: optimizing the model such that its prediction \(\hat{u}\) approximates the label value \(u\) as closely as possible within the interval \([-\pi, \pi]\).
We then define the training configuration, including epochs, the optimizer, and the visualizer.
Finally, pass the defined objects to the Solver class to initiate model training.
Post-training, we calculate the L2 relative error against the analytical solution using the 1,000 sampled points.
Then visualize the prediction results of these 1000 points.
The training log is displayed below.
...
...
ppsci INFO: [Train][Epoch 9/10][Iter 80/100] lr: 0.00200, loss: 0.00663, EQ: 0.00663, batch_cost: 0.00180s, reader_cost: 0.00011s, ips: 17756.64, eta: 0:00:00
ppsci INFO: [Train][Epoch 9/10][Iter 90/100] lr: 0.00200, loss: 0.00598, EQ: 0.00598, batch_cost: 0.00180s, reader_cost: 0.00011s, ips: 17793.97, eta: 0:00:00
ppsci INFO: [Train][Epoch 9/10][Iter 100/100] lr: 0.00200, loss: 0.00547, EQ: 0.00547, batch_cost: 0.00179s, reader_cost: 0.00011s, ips: 17864.08, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 10/100] lr: 0.00200, loss: 0.00079, EQ: 0.00079, batch_cost: 0.00182s, reader_cost: 0.00012s, ips: 17547.05, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 20/100] lr: 0.00200, loss: 0.00075, EQ: 0.00075, batch_cost: 0.00183s, reader_cost: 0.00011s, ips: 17482.92, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 30/100] lr: 0.00200, loss: 0.00077, EQ: 0.00077, batch_cost: 0.00182s, reader_cost: 0.00011s, ips: 17539.51, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 40/100] lr: 0.00200, loss: 0.00074, EQ: 0.00074, batch_cost: 0.00182s, reader_cost: 0.00011s, ips: 17587.51, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 50/100] lr: 0.00200, loss: 0.00071, EQ: 0.00071, batch_cost: 0.00182s, reader_cost: 0.00011s, ips: 17563.59, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 60/100] lr: 0.00200, loss: 0.00070, EQ: 0.00070, batch_cost: 0.00182s, reader_cost: 0.00011s, ips: 17604.60, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 70/100] lr: 0.00200, loss: 0.00074, EQ: 0.00074, batch_cost: 0.00181s, reader_cost: 0.00011s, ips: 17699.28, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 80/100] lr: 0.00200, loss: 0.00077, EQ: 0.00077, batch_cost: 0.00180s, reader_cost: 0.00011s, ips: 17764.92, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 90/100] lr: 0.00200, loss: 0.00075, EQ: 0.00075, batch_cost: 0.00180s, reader_cost: 0.00011s, ips: 17795.87, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 100/100] lr: 0.00200, loss: 0.00071, EQ: 0.00071, batch_cost: 0.00179s, reader_cost: 0.00011s, ips: 17872.00, eta: 0:00:00
After training, calculate the L2-relative error with the standard solution using the 1000 points just taken.
The results demonstrate that supervised training using the analytical solution yields strong predictive performance, achieving an L2 relative error of 0.02677.
The prediction result visualization is shown below.
The complete code for Scenario 1 is shown below.
3. Scenario 2¶
While the supervised approach in Scenario 1 effectively solves the fitting problem, the analytical expression of the target function is often unknown in practice, preventing direct construction of supervised labels.
However, even without an analytical formula, the target function often satisfies specific mathematical relationships, such as differential equations. We can thus optimize the model via "indirect supervision" by enforcing these relationships.
In this scenario, we assume the formula \(u=\sin(x)\) is unavailable. Instead, we rely on the following system, comprising a differential equation and a boundary condition:
We construct data pairs \((x_i, \cos(x_i))\) for \(i=1,...,N\). The model input and output remain unchanged, but the optimization objective shifts: we aim to minimize the difference between \(\dfrac{\partial \hat{u}} {\partial x}\) and \(\cos(x)\), while ensuring \(\hat{u}(-\pi)\) approximates \(2\).
Based on this principle, we adapt the code from Scenario 1 for Scenario 2.
First, since we need to use the first-order differential operation, we need to import the first-order differential API at the beginning of the code.
Add a function to calculate the differential label value.
Modify the interior_constraint to constrain the "first-order derivative of the model output with respect to the input" rather than the output itself.
Since differential equations typically involve undetermined coefficients resolved by definite conditions (initial or boundary values), we add a boundary condition constraint, bc_constraint, following the interior_constraint.
- Corresponding boundary condition \(u(x_0)=sin(x_0)+2\)
Then add the boundary constraint bc_constraint to constraint.
Similarly, modify the standard solution drawn by Visualizer to \(sin(x)+2\).
Execute training after modification.
The training log is displayed below.
...
...
ppsci INFO: [Train][Epoch 9/10][Iter 90/100] lr: 0.00200, loss: 0.00176, EQ: 0.00087, BC: 0.00088, batch_cost: 0.00346s, reader_cost: 0.00024s, ips: 9527.80, eta: 0:00:00
ppsci INFO: [Train][Epoch 9/10][Iter 100/100] lr: 0.00200, loss: 0.00170, EQ: 0.00087, BC: 0.00083, batch_cost: 0.00349s, reader_cost: 0.00024s, ips: 9452.07, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 10/100] lr: 0.00200, loss: 0.00107, EQ: 0.00072, BC: 0.00035, batch_cost: 0.00350s, reader_cost: 0.00025s, ips: 9424.75, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 20/100] lr: 0.00200, loss: 0.00116, EQ: 0.00083, BC: 0.00033, batch_cost: 0.00350s, reader_cost: 0.00025s, ips: 9441.33, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 30/100] lr: 0.00200, loss: 0.00103, EQ: 0.00079, BC: 0.00024, batch_cost: 0.00355s, reader_cost: 0.00025s, ips: 9291.90, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 40/100] lr: 0.00200, loss: 0.00108, EQ: 0.00078, BC: 0.00030, batch_cost: 0.00353s, reader_cost: 0.00025s, ips: 9348.09, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 50/100] lr: 0.00200, loss: 0.00163, EQ: 0.00082, BC: 0.00082, batch_cost: 0.00350s, reader_cost: 0.00024s, ips: 9416.24, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 60/100] lr: 0.00200, loss: 0.00160, EQ: 0.00083, BC: 0.00077, batch_cost: 0.00353s, reader_cost: 0.00024s, ips: 9345.73, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 70/100] lr: 0.00200, loss: 0.00150, EQ: 0.00082, BC: 0.00068, batch_cost: 0.00351s, reader_cost: 0.00024s, ips: 9393.89, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 80/100] lr: 0.00200, loss: 0.00146, EQ: 0.00081, BC: 0.00064, batch_cost: 0.00350s, reader_cost: 0.00024s, ips: 9424.81, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 90/100] lr: 0.00200, loss: 0.00138, EQ: 0.00081, BC: 0.00058, batch_cost: 0.00349s, reader_cost: 0.00024s, ips: 9444.12, eta: 0:00:00
ppsci INFO: [Train][Epoch 10/10][Iter 100/100] lr: 0.00200, loss: 0.00133, EQ: 0.00079, BC: 0.00054, batch_cost: 0.00349s, reader_cost: 0.00024s, ips: 9461.54, eta: 0:00:00
After training, calculate the L2-relative error with the standard solution using the 1000 points just taken.
The model, trained using the differential equation, exhibits strong predictive capability relative to the analytical solution, achieving an L2 relative error of 0.00564.
The prediction result visualization is shown below.
This demonstrates that training with differential relationships, combined with boundary conditions, allows the model to effectively learn the correct solution satisfying both the physics (equation) and the constraints.
The complete code for Scenario 2 is shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |

