Skip to content

Commit c4bcd67

Browse files
[Fix] Adapt preprocess transforms to multiple arguments (#572)
* adapt preprocess transforms to multiple arguments * update code * update docs * fix document * refine code in IDE and FPDE
1 parent c20e44e commit c4bcd67

File tree

9 files changed

+317
-113
lines changed

9 files changed

+317
-113
lines changed

docs/zh/examples/volterra_ide.md

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22

33
<a href="https://aistudio.baidu.com/aistudio/projectdetail/6622866?sUid=438690&shared=1&ts=1691582831601" class="md-button md-button--primary" style>AI Studio快速体验</a>
44

5+
=== "模型训练命令"
6+
7+
``` sh
8+
python volterra_ide.py
9+
```
10+
11+
=== "模型评估命令"
12+
13+
``` sh
14+
python volterra_ide.py mode=eval EVAL.pretrained_model_path=https://paddle-org.bj.bcebos.com/paddlescience/models/volterra_ide/volterra_ide_pretrained.pdparams
15+
```
16+
17+
| 预训练模型 | 指标 |
18+
|:--| :--|
19+
| [volterra_ide_pretrained.pdparams](https://paddle-org.bj.bcebos.com/paddlescience/models/volterra_ide/volterra_ide_pretrained.pdparams) | loss(L2Rel_Validator): 0.00023 <br> L2Rel.u(L2Rel_Validator): 0.00023 |
20+
521
## 1. 背景简介
622

723
Volterra integral equation(沃尔泰拉积分方程)是一种积分方程,即方程中含有对待求解函数的积分运算,其有两种形式,如下所示
@@ -45,9 +61,9 @@ $$
4561

4662
在上述问题中,我们确定了输入为 $x$,输出为 $u(x)$,因此我们使用,用 PaddleScience 代码表示如下:
4763

48-
``` py linenums="37"
64+
``` py linenums="39"
4965
--8<--
50-
examples/ide/volterra_ide.py:37:38
66+
examples/ide/volterra_ide.py:39:40
5167
--8<--
5268
```
5369

@@ -57,19 +73,19 @@ examples/ide/volterra_ide.py:37:38
5773

5874
Volterra_IDE 问题的积分域是 $a$ ~ $t$,其中 `a` 为固定常数 0,`t` 的范围为 0 ~ 5,因此可以使用PaddleScience 内置的一维几何 `TimeDomain` 作为计算域。
5975

60-
``` py linenums="40"
76+
``` py linenums="42"
6177
--8<--
62-
examples/ide/volterra_ide.py:40:42
78+
examples/ide/volterra_ide.py:42:43
6379
--8<--
6480
```
6581

6682
### 3.3 方程构建
6783

6884
由于 Volterra_IDE 使用的是积分方程,因此可以直接使用 PaddleScience 内置的 `ppsci.equation.Volterra`,并指定所需的参数:积分下限 `a``t` 的离散取值点数 `num_points`、一维高斯积分点的个数 `quad_deg`、$K(t,s)$ 核函数 `kernel_func`、$u(t) - f(t)$ 等式右侧表达式 `func`
6985

70-
``` py linenums="44"
86+
``` py linenums="45"
7187
--8<--
72-
examples/ide/volterra_ide.py:44:64
88+
examples/ide/volterra_ide.py:45:61
7389
--8<--
7490
```
7591

@@ -81,9 +97,9 @@ examples/ide/volterra_ide.py:44:64
8197

8298
由于等式左侧涉及到积分计算(实际采用高斯积分近似计算),因此在 0 ~ 5 区间内采样出多个 `t_i` 点后,还需要计算其用于高斯积分的点集,即对每一个 `(0,t_i)` 区间,都计算出一一对应的高斯积分点集 `quad_i` 和点权 `weight_i`。PaddleScience 将这一步作为输入数据的预处理,加入到代码中,如下所示
8399

84-
``` py linenums="66"
100+
``` py linenums="63"
85101
--8<--
86-
examples/ide/volterra_ide.py:66:108
102+
examples/ide/volterra_ide.py:63:117
87103
--8<--
88104
```
89105

@@ -97,47 +113,47 @@ $$
97113

98114
因此可以加入 `t=0` 时的初值条件,代码如下所示
99115

100-
``` py linenums="110"
116+
``` py linenums="119"
101117
--8<--
102-
examples/ide/volterra_ide.py:110:128
118+
examples/ide/volterra_ide.py:119:137
103119
--8<--
104120
```
105121

106122
在微分方程约束、初值约束构建完毕之后,以我们刚才的命名为关键字,封装到一个字典中,方便后续访问。
107123

108-
``` py linenums="129"
124+
``` py linenums="138"
109125
--8<--
110-
examples/ide/volterra_ide.py:129:133
126+
examples/ide/volterra_ide.py:138:142
111127
--8<--
112128
```
113129

114130
### 3.5 超参数设定
115131

116132
接下来我们需要指定训练轮数和学习率,此处我们按实验经验,让 `L-BFGS` 优化器进行一轮优化即可,但一轮优化内的 `max_iters` 数可以设置为一个较大的一个数 `15000`
117133

118-
``` py linenums="135"
134+
``` yaml linenums="39"
119135
--8<--
120-
examples/ide/volterra_ide.py:135:136
136+
examples/ide/conf/volterra_ide.yaml:39:57
121137
--8<--
122138
```
123139

124140
### 3.6 优化器构建
125141

126142
训练过程会调用优化器来更新模型参数,此处选择较为常用的 `LBFGS` 优化器。
127143

128-
``` py linenums="138"
144+
``` py linenums="144"
129145
--8<--
130-
examples/ide/volterra_ide.py:138:146
146+
examples/ide/volterra_ide.py:144:145
131147
--8<--
132148
```
133149

134150
### 3.7 评估器构建
135151

136152
在训练过程中通常会按一定轮数间隔,用验证集(测试集)评估当前模型的训练情况,因此使用 `ppsci.validate.GeometryValidator` 构建评估器。
137153

138-
``` py linenums="148"
154+
``` py linenums="147"
139155
--8<--
140-
examples/ide/volterra_ide.py:148:163
156+
examples/ide/volterra_ide.py:147:161
141157
--8<--
142158
```
143159

@@ -149,9 +165,9 @@ examples/ide/volterra_ide.py:148:163
149165

150166
完成上述设置之后,只需要将上述实例化的对象按顺序传递给 `ppsci.solver.Solver`,然后启动训练。
151167

152-
``` py linenums="165"
168+
``` py linenums="163"
153169
--8<--
154-
examples/ide/volterra_ide.py:165:181
170+
examples/ide/volterra_ide.py:163:181
155171
--8<--
156172
```
157173

@@ -161,7 +177,7 @@ examples/ide/volterra_ide.py:165:181
161177

162178
``` py linenums="183"
163179
--8<--
164-
examples/ide/volterra_ide.py:183:
180+
examples/ide/volterra_ide.py:183:194
165181
--8<--
166182
```
167183

examples/fpde/fractional_poisson_2d.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import math
1818
from typing import Dict
19+
from typing import Tuple
1920
from typing import Union
2021

2122
import numpy as np
@@ -68,22 +69,35 @@ def u_solution_func(
6869
return paddle.abs(1 - (out["x"] ** 2 + out["y"] ** 2)) ** (1 + ALPHA / 2)
6970
return np.abs(1 - (out["x"] ** 2 + out["y"] ** 2)) ** (1 + ALPHA / 2)
7071

71-
# set input transform
72-
def fpde_transform(in_: Dict[str, np.ndarray]) -> Dict[str, np.ndarray]:
72+
# set transform for input data
73+
def input_data_fpde_transform(
74+
input: Dict[str, np.ndarray],
75+
weight: Dict[str, np.ndarray],
76+
label: Dict[str, np.ndarray],
77+
) -> Tuple[
78+
Dict[str, paddle.Tensor], Dict[str, paddle.Tensor], Dict[str, paddle.Tensor]
79+
]:
7380
"""Get sampling points for integral.
7481
7582
Args:
76-
in_ (Dict[str, np.ndarray]): Raw input dict.
83+
input (Dict[str, paddle.Tensor]): Raw input dict.
84+
weight (Dict[str, paddle.Tensor]): Raw weight dict.
85+
label (Dict[str, paddle.Tensor]): Raw label dict.
7786
7887
Returns:
79-
Dict[str, np.ndarray]: Input dict contained sampling points.
88+
Tuple[ Dict[str, paddle.Tensor], Dict[str, paddle.Tensor], Dict[str, paddle.Tensor] ]:
89+
Input dict contained sampling points, weight dict and label dict.
8090
"""
81-
points = np.concatenate((in_["x"].numpy(), in_["y"].numpy()), axis=1)
91+
points = np.concatenate((input["x"].numpy(), input["y"].numpy()), axis=1)
8292
x = equation["fpde"].get_x(points)
83-
return {
84-
**in_,
85-
**{k: paddle.to_tensor(v) for k, v in x.items()},
86-
}
93+
return (
94+
{
95+
**input,
96+
**{k: paddle.to_tensor(v) for k, v in x.items()},
97+
},
98+
weight,
99+
label,
100+
)
87101

88102
fpde_constraint = ppsci.constraint.InteriorConstraint(
89103
equation["fpde"].equations,
@@ -95,7 +109,7 @@ def fpde_transform(in_: Dict[str, np.ndarray]) -> Dict[str, np.ndarray]:
95109
"transforms": (
96110
{
97111
"FunctionalTransform": {
98-
"transform_func": fpde_transform,
112+
"transform_func": input_data_fpde_transform,
99113
},
100114
},
101115
),

examples/ide/conf/volterra_ide.yaml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
hydra:
2+
run:
3+
# dynamic output directory according to running time and override name
4+
dir: outputs_volterra_IDE/${now:%Y-%m-%d}/${now:%H-%M-%S}/${hydra.job.override_dirname}
5+
job:
6+
name: ${mode} # name of logfile
7+
chdir: false # keep current working direcotry unchaned
8+
config:
9+
override_dirname:
10+
exclude_keys:
11+
- TRAIN.checkpoint_path
12+
- TRAIN.pretrained_model_path
13+
- EVAL.pretrained_model_path
14+
- mode
15+
- output_dir
16+
- log_freq
17+
sweep:
18+
# output directory for multirun
19+
dir: ${hydra.run.dir}
20+
subdir: ./
21+
22+
# general settings
23+
mode: train # running mode: train/eval
24+
seed: 42
25+
output_dir: ${hydra:run.dir}
26+
log_freq: 20
27+
28+
# set geometry
29+
BOUNDS: [0, 5]
30+
31+
# model settings
32+
MODEL:
33+
input_keys: ["x"]
34+
output_keys: ["u"]
35+
num_layers: 3
36+
hidden_size: 20
37+
activation: "tanh"
38+
39+
# training settings
40+
TRAIN:
41+
epochs: 1
42+
iters_per_epoch: 1
43+
save_freq: 1
44+
eval_during_train: true
45+
eval_freq: 1
46+
optimizer:
47+
learning_rate: 1
48+
max_iter: 15000
49+
max_eval: 1250
50+
tolerance_grad: 1.0e-8
51+
tolerance_change: 0
52+
history_size: 100
53+
quad_deg: 20
54+
npoint_interior: 12
55+
npoint_ic: 1
56+
pretrained_model_path: null
57+
checkpoint_path: null
58+
59+
# evaluation settings
60+
EVAL:
61+
pretrained_model_path: null
62+
eval_with_no_grad: true
63+
npoint_eval: 100

0 commit comments

Comments
 (0)