Skip to content

Commit

Permalink
【hydra No.3】Adapt VIV to hydra (PaddlePaddle#622)
Browse files Browse the repository at this point in the history
* 【hydra No.4】Adapt VIV to hydra

* fix bug

* update
  • Loading branch information
GreatV authored Nov 3, 2023
1 parent 708f0a0 commit b3728fb
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 52 deletions.
46 changes: 26 additions & 20 deletions docs/zh/examples/viv.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/6160556?contributionType=1&sUid=438690&shared=1&ts=1683961088129" class="md-button md-button--primary" style>AI Studio快速体验</a>

=== "模型训练命令"

``` sh
python viv.py
```

## 1. 背景简介

涡激振动(Vortex-Induced Vibration,VIV)是一种流固耦合振动现象,主要发生在流体绕过柱体或管体时。在海洋工程和风工程中,这种振动现象具有重要应用。
Expand Down Expand Up @@ -41,9 +47,9 @@ $$

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

``` py linenums="28"
``` py linenums="31"
--8<--
examples/fsi/viv.py:28:29
examples/fsi/viv.py:31:32
--8<--
```

Expand All @@ -56,9 +62,9 @@ examples/fsi/viv.py:28:29

由于 VIV 使用的是 VIV 方程,因此可以直接使用 PaddleScience 内置的 `VIV`

``` py linenums="31"
``` py linenums="34"
--8<--
examples/fsi/viv.py:31:32
examples/fsi/viv.py:34:35
--8<--
```

Expand All @@ -76,19 +82,19 @@ examples/fsi/viv.py:31:32

在定义约束之前,需要给监督约束指定文件路径等数据读取配置。

``` py linenums="34"
``` py linenums="37"
--8<--
examples/fsi/viv.py:34:50
examples/fsi/viv.py:37:52
--8<--
```

#### 3.4.1 监督约束

由于我们以监督学习方式进行训练,此处采用监督约束 `SupervisedConstraint`

``` py linenums="51"
``` py linenums="54"
--8<--
examples/fsi/viv.py:51:57
examples/fsi/viv.py:54:60
--8<--
```

Expand All @@ -102,29 +108,29 @@ examples/fsi/viv.py:51:57

在监督约束构建完毕之后,以我们刚才的命名为关键字,封装到一个字典中,方便后续访问。

``` py linenums="58"
``` py linenums="61"
--8<--
examples/fsi/viv.py:58:61
examples/fsi/viv.py:61:64
--8<--
```

### 3.5 超参数设定

接下来我们需要指定训练轮数和学习率,此处我们按实验经验,使用十万轮训练轮数,并每隔1000个epochs评估一次模型精度。

``` py linenums="63"
``` yaml linenums="38"
--8<--
examples/fsi/viv.py:63:65
examples/fsi/conf/viv.yaml:38:51
--8<--
```

### 3.6 优化器构建

训练过程会调用优化器来更新模型参数,此处选择较为常用的 `Adam` 优化器和 `Step` 间隔衰减学习率。

``` py linenums="67"
``` py linenums="66"
--8<--
examples/fsi/viv.py:67:71
examples/fsi/viv.py:66:68
--8<--
```

Expand All @@ -136,9 +142,9 @@ examples/fsi/viv.py:67:71

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

``` py linenums="73"
``` py linenums="70"
--8<--
examples/fsi/viv.py:73:95
examples/fsi/viv.py:70:92
--8<--
```

Expand All @@ -152,19 +158,19 @@ examples/fsi/viv.py:73:95

本文需要可视化的数据是 $t-\eta$ 和 $t-f$ 两组关系图,假设每个时刻 $t$ 的坐标是 $t_i$,则对应网络输出为 $\eta_i$,升力为 $f_i$,因此我们只需要将评估过程中产生的所有 $(t_i, \eta_i, f_i)$ 保存成图片即可。代码如下:

``` py linenums="97"
``` py linenums="94"
--8<--
examples/fsi/viv.py:97:116
examples/fsi/viv.py:94:113
--8<--
```

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

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

``` py linenums="118"
``` py linenums="115"
--8<--
examples/fsi/viv.py:118:
examples/fsi/viv.py:115:136
--8<--
```

Expand Down
58 changes: 58 additions & 0 deletions examples/fsi/conf/viv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
hydra:
run:
# dynamic output directory according to running time and override name
dir: outputs_VIV/${now:%Y-%m-%d}/${now:%H-%M-%S}/${hydra.job.override_dirname}
job:
name: ${mode} # name of logfile
chdir: false # keep current working direcotry unchaned
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}
log_freq: 20

VIV_DATA_PATH: "./VIV_Training_Neta100.mat"

# model settings
MODEL:
input_keys: ["t_f"]
output_keys: ["eta"]
num_layers: 5
hidden_size: 50
activation: "tanh"

# training settings
TRAIN:
epochs: 100000
iters_per_epoch: 1
save_freq: 1
eval_during_train: true
eval_freq: 1000
batch_size: 100
lr_scheduler:
epochs: ${TRAIN.epochs}
iters_per_epoch: ${TRAIN.iters_per_epoch}
learning_rate: 0.001
step_size: 20000
gamma: 0.9
pretrained_model_path: null
checkpoint_path: null

# evaluation settings
EVAL:
pretrained_model_path: null
batch_size: 32
Loading

0 comments on commit b3728fb

Please sign in to comment.