Skip to content

Commit cd21d80

Browse files
remove seed and directory code to slim laplace2d exmaples (#824)
1 parent 65f4447 commit cd21d80

File tree

3 files changed

+28
-39
lines changed

3 files changed

+28
-39
lines changed

docs/zh/examples/laplace2d.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ $$
6060

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

63-
``` py linenums="32"
63+
``` py linenums="23"
6464
--8<--
65-
examples/laplace/laplace2d.py:32:33
65+
examples/laplace/laplace2d.py:23:24
6666
--8<--
6767
```
6868

@@ -74,9 +74,9 @@ examples/laplace/laplace2d.py:32:33
7474

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

77-
``` py linenums="35"
77+
``` py linenums="26"
7878
--8<--
79-
examples/laplace/laplace2d.py:35:36
79+
examples/laplace/laplace2d.py:26:27
8080
--8<--
8181
```
8282

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

88-
``` py linenums="38"
88+
``` py linenums="29"
8989
--8<--
90-
examples/laplace/laplace2d.py:38:43
90+
examples/laplace/laplace2d.py:29:34
9191
--8<--
9292
```
9393

@@ -97,19 +97,19 @@ examples/laplace/laplace2d.py:38:43
9797

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

100-
``` yaml linenums="26"
100+
``` yaml linenums="30"
101101
--8<--
102-
examples/laplace/conf/laplace2d.yaml:26:27
102+
examples/laplace/conf/laplace2d.yaml:30:31
103103
--8<--
104104
```
105105

106106
#### 3.4.1 内部点约束
107107

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

110-
``` py linenums="60"
110+
``` py linenums="50"
111111
--8<--
112-
examples/laplace/laplace2d.py:60:68
112+
examples/laplace/laplace2d.py:50:59
113113
--8<--
114114
```
115115

@@ -131,19 +131,19 @@ examples/laplace/laplace2d.py:60:68
131131

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

134-
``` py linenums="69"
134+
``` py linenums="60"
135135
--8<--
136-
examples/laplace/laplace2d.py:69:76
136+
examples/laplace/laplace2d.py:60:72
137137
--8<--
138138
```
139139

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

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

144-
``` py linenums="45"
144+
``` py linenums="36"
145145
--8<--
146-
examples/laplace/laplace2d.py:45:49
146+
examples/laplace/laplace2d.py:36:40
147147
--8<--
148148
```
149149

@@ -153,29 +153,29 @@ examples/laplace/laplace2d.py:45:49
153153

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

156-
``` yaml linenums="41"
156+
``` yaml linenums="45"
157157
--8<--
158-
examples/laplace/conf/laplace2d.yaml:41:46
158+
examples/laplace/conf/laplace2d.yaml:45:50
159159
--8<--
160160
```
161161

162162
### 3.6 优化器构建
163163

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

166-
``` py linenums="83"
166+
``` py linenums="74"
167167
--8<--
168-
examples/laplace/laplace2d.py:83:84
168+
examples/laplace/laplace2d.py:74:75
169169
--8<--
170170
```
171171

172172
### 3.7 评估器构建
173173

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

176-
``` py linenums="86"
176+
``` py linenums="77"
177177
--8<--
178-
examples/laplace/laplace2d.py:86:100
178+
examples/laplace/laplace2d.py:77:92
179179
--8<--
180180
```
181181

@@ -185,19 +185,19 @@ examples/laplace/laplace2d.py:86:100
185185

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

188-
``` py linenums="103"
188+
``` py linenums="94"
189189
--8<--
190-
examples/laplace/laplace2d.py:103:112
190+
examples/laplace/laplace2d.py:94:103
191191
--8<--
192192
```
193193

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

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

198-
``` py linenums="114"
198+
``` py linenums="105"
199199
--8<--
200-
examples/laplace/laplace2d.py:114:134
200+
examples/laplace/laplace2d.py:105:125
201201
--8<--
202202
```
203203

examples/laplace/conf/laplace2d.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ hydra:
1414
- mode
1515
- output_dir
1616
- log_freq
17+
callbacks:
18+
init_callback:
19+
_target_: ppsci.utils.callbacks.InitCallback
1720
sweep:
1821
# output directory for multirun
1922
dir: ${hydra.run.dir}
2023
subdir: ./
2124

2225
# general settings
2326
mode: train # running mode: train/eval
24-
seed: 42
27+
seed: 2024
2528
log_freq: 20
2629
output_dir: ${hydra:run.dir}
2730
NPOINT_INTERIOR: 9801

examples/laplace/laplace2d.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from os import path as osp
16-
1715
import hydra
1816
import numpy as np
1917
from omegaconf import DictConfig
2018

2119
import ppsci
22-
from ppsci.utils import logger
2320

2421

2522
def train(cfg: DictConfig):
26-
# set random seed for reproducibility
27-
ppsci.utils.misc.set_random_seed(cfg.seed)
28-
29-
# # set output directory
30-
logger.init_logger("ppsci", osp.join(cfg.output_dir, "train.log"), "info")
31-
3223
# set model
3324
model = ppsci.arch.MLP(**cfg.MODEL)
3425

@@ -135,11 +126,6 @@ def u_solution_func(out):
135126

136127

137128
def evaluate(cfg: DictConfig):
138-
# set random seed for reproducibility
139-
ppsci.utils.misc.set_random_seed(cfg.seed)
140-
# set output directory
141-
logger.init_logger("ppsci", osp.join(cfg.output_dir, "eval.log"), "info")
142-
143129
# set model
144130
model = ppsci.arch.MLP(**cfg.MODEL)
145131

0 commit comments

Comments
 (0)