Skip to content

Commit

Permalink
modify laplace2d to hydra style (#575)
Browse files Browse the repository at this point in the history
* modify laplace2d to hydra style

* modify laplace2d to hydra style

* modify laplace2d to hydra style

* modify laplace2d to hydra style

* modify laplace2d to hydra style

* modify laplace2d to hydra style

* fix
  • Loading branch information
xusuyong authored Oct 19, 2023
1 parent a993261 commit 1f3955f
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 56 deletions.
56 changes: 31 additions & 25 deletions docs/zh/examples/laplace2d.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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

=== "模型训练命令"

``` sh
python laplace2d.py
```

## 1. 背景简介

拉普拉斯方程由法国数学家拉普拉斯首先提出而得名,该方程在许多领域都有重要应用,例如电磁学、天文学和流体力学等。在实际应用中,拉普拉斯方程的求解往往是一个复杂的数学问题。对于一些具有特定边界条件和初始条件的实际问题,可以通过特定的数值方法(如有限元方法、有限差分方法等)来求解拉普拉斯方程。对于一些复杂的问题,可能需要采用更高级的数值方法或者借助高性能计算机进行计算。
Expand Down Expand Up @@ -32,9 +38,9 @@ $$

上式中 $f$ 即为 MLP 模型本身,用 PaddleScience 代码表示如下

``` py linenums="35"
``` py linenums="32"
--8<--
examples/laplace/laplace2d.py:35:36
examples/laplace/laplace2d.py:32:33
--8<--
```

Expand All @@ -46,9 +52,9 @@ examples/laplace/laplace2d.py:35:36

由于 2D-Laplace 使用的是 Laplace 方程的2维形式,因此可以直接使用 PaddleScience 内置的 `Laplace`,指定该类的参数 `dim` 为2。

``` py linenums="38"
``` py linenums="35"
--8<--
examples/laplace/laplace2d.py:38:39
examples/laplace/laplace2d.py:35:36
--8<--
```

Expand All @@ -57,9 +63,9 @@ examples/laplace/laplace2d.py:38:39
本文中 2D Laplace 问题作用在以 (0.0, 0.0), (1.0, 1.0) 为对角线的二维矩形区域,
因此可以直接使用 PaddleScience 内置的空间几何 `Rectangle` 作为计算域。

``` py linenums="41"
``` py linenums="38"
--8<--
examples/laplace/laplace2d.py:41:42
examples/laplace/laplace2d.py:38:43
--8<--
```

Expand All @@ -69,19 +75,19 @@ examples/laplace/laplace2d.py:41:42

在定义约束之前,需要给每一种约束指定采样点个数,表示每一种约束在其对应计算域内采样数据的数量,以及通用的采样配置。

``` py linenums="56"
``` py linenums="26"
--8<--
examples/laplace/laplace2d.py:56:58
examples/laplace/conf/laplace2d.yaml:26:27
--8<--
```

#### 3.4.1 内部点约束

以作用在内部点上的 `InteriorConstraint` 为例,代码如下:

``` py linenums="61"
``` py linenums="60"
--8<--
examples/laplace/laplace2d.py:61:69
examples/laplace/laplace2d.py:60:68
--8<--
```

Expand All @@ -103,51 +109,51 @@ examples/laplace/laplace2d.py:61:69

同理,我们还需要构建矩形的四个边界的约束。但与构建 `InteriorConstraint` 约束不同的是,由于作用区域是边界,因此我们使用 `BoundaryConstraint` 类,代码如下:

``` py linenums="70"
``` py linenums="69"
--8<--
examples/laplace/laplace2d.py:70:77
examples/laplace/laplace2d.py:69:76
--8<--
```

`BoundaryConstraint` 类第一个参数表示我们直接对网络模型的输出结果 `out["u"]` 作为程序运行时的约束对象;

第二个参数是指我们约束对象的真值如何获得,这里我们直接通过其解析解进行计算,定义解析解的代码如下:

``` py linenums="44"
``` py linenums="45"
--8<--
examples/laplace/laplace2d.py:44:48
examples/laplace/laplace2d.py:45:49
--8<--
```

`BoundaryConstraint` 类其他参数的含义与 `InteriorConstraint` 基本一致,这里不再介绍。

### 3.5 超参数设定

接下来我们需要指定训练轮数和学习率,此处我们按实验经验,使用两万轮训练轮数,评估间隔为两百轮。
接下来我们需要在配置文件中指定训练轮数,此处我们按实验经验,使用两万轮训练轮数,评估间隔为两百轮。

``` py linenums="26"
``` py linenums="41"
--8<--
examples/laplace/laplace2d.py:26:29
examples/laplace/conf/laplace2d.yaml:41:46
--8<--
```

### 3.6 优化器构建

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

``` py linenums="84"
``` py linenums="83"
--8<--
examples/laplace/laplace2d.py:84:85
examples/laplace/laplace2d.py:83:84
--8<--
```

### 3.7 评估器构建

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

``` py linenums="87"
``` py linenums="86"
--8<--
examples/laplace/laplace2d.py:87:102
examples/laplace/laplace2d.py:86:100
--8<--
```

Expand All @@ -157,19 +163,19 @@ examples/laplace/laplace2d.py:87:102

本文中的输出数据是一个区域内的二维点集,因此我们只需要将评估的输出数据保存成 **vtu格式** 文件,最后用可视化软件打开查看即可。代码如下:

``` py linenums="104"
``` py linenums="103"
--8<--
examples/laplace/laplace2d.py:104:113
examples/laplace/laplace2d.py:103:112
--8<--
```

### 3.9 模型训练、评估与可视化

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

``` py linenums="115"
``` py linenums="114"
--8<--
examples/laplace/laplace2d.py:115:
examples/laplace/laplace2d.py:114:134
--8<--
```

Expand Down
53 changes: 53 additions & 0 deletions examples/laplace/conf/laplace2d.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
hydra:
run:
# dynamic output directory according to running time and override name
dir: output_laplace2d/${now:%Y-%m-%d}/${now:%H-%M-%S}/${hydra.job.override_dirname}
job:
name: ${mode} # name of logfile
chdir: false # keep current working directory unchanged
config:
override_dirname:
exclude_keys:
- TRAIN.checkpoint_path
- TRAIN.pretrained_model_path
- EVAL.pretrained_model_path
- mode
- output_dir
- log_freq
sweep:
# output directory for multirun
dir: ${hydra.run.dir}
subdir: ./

# general settings
mode: train # running mode: train/eval
seed: 42
output_dir: ${hydra:run.dir}
NPOINT_INTERIOR: 9801
NPOINT_BC: 400

# set geometry
DIAGONAL_COORD:
xmin: [0.0, 0.0]
xmax: [1.0, 1.0]

# model settings
MODEL:
input_keys: ["x", "y"]
output_keys: ["u"]
num_layers: 5
hidden_size: 20

# training settings
TRAIN:
epochs: 20000
iters_per_epoch: 1
eval_during_train: true
eval_freq: 200
learning_rate: 0.001

# evaluation settings
EVAL:
pretrained_model_path: null
eval_with_no_grad: true

Loading

0 comments on commit 1f3955f

Please sign in to comment.