@@ -112,19 +112,19 @@ $$Re \sim(100, 750)$$
112
112
113
113
首先展示代码中定义的各个参数变量,每个参数的具体含义会在下面使用到时进行解释。
114
114
115
- ``` py linenums="51 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
115
+ ``` py linenums="50 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
116
116
-- 8 < --
117
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:51 : 62
117
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:50 : 65
118
118
-- 8 < --
119
119
```
120
120
121
121
#### 3.2.1 约束构建
122
122
123
123
本案例基于数据驱动的方法求解问题,因此需要使用 PaddleScience 内置的 ` SupervisedConstraint ` 构建监督约束。在定义约束之前,需要首先指定监督约束中用于数据加载的各个参数,代码如下:
124
124
125
- ``` py linenums="67 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
125
+ ``` py linenums="70 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
126
126
-- 8 < --
127
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:67 : 84
127
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:70 : 87
128
128
-- 8 < --
129
129
```
130
130
@@ -143,9 +143,9 @@ examples/cylinder/2d_unsteady/transformer_physx/train_enn.py:67:84
143
143
144
144
定义监督约束的代码如下:
145
145
146
- ``` py linenums="86 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
146
+ ``` py linenums="89 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
147
147
-- 8 < --
148
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:86 : 94
148
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:89 : 97
149
149
-- 8 < --
150
150
```
151
151
@@ -168,37 +168,37 @@ examples/cylinder/2d_unsteady/transformer_physx/train_enn.py:86:94
168
168
169
169
用 PaddleScience 代码表示如下:
170
170
171
- ``` py linenums="99 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
171
+ ``` py linenums="102 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
172
172
-- 8 < --
173
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:99 : 105
173
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:102 : 108
174
174
-- 8 < --
175
175
```
176
176
177
177
其中,` CylinderEmbedding ` 的前两个参数在前文中已有描述,这里不再赘述,网络模型的第三、四个参数是训练数据集的均值和方差,用于归一化输入数据。计算均值、方差的的代码表示如下:
178
178
179
- ``` py linenums="28 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
179
+ ``` py linenums="29 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
180
180
-- 8 < --
181
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:28 : 45
181
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:29 : 46
182
182
-- 8 < --
183
183
```
184
184
185
185
#### 3.2.3 学习率与优化器构建
186
186
187
187
本案例中使用的学习率方法为 ` ExponentialDecay ` ,学习率大小设置为0.001。优化器使用 ` Adam ` ,梯度裁剪使用了 Paddle 内置的 ` ClipGradByGlobalNorm ` 方法。用 PaddleScience 代码表示如下:
188
188
189
- ``` py linenums="107 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
189
+ ``` py linenums="110 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
190
190
-- 8 < --
191
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:107 : 121
191
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:110 : 124
192
192
-- 8 < --
193
193
```
194
194
195
195
#### 3.2.4 评估器构建
196
196
197
197
本案例训练过程中会按照一定的训练轮数间隔,使用验证集评估当前模型的训练情况,需要使用 ` SupervisedValidator ` 构建评估器。代码如下:
198
198
199
- ``` py linenums="123 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
199
+ ``` py linenums="126 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
200
200
-- 8 < --
201
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:123 : 150
201
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:126 : 153
202
202
-- 8 < --
203
203
```
204
204
@@ -208,39 +208,39 @@ examples/cylinder/2d_unsteady/transformer_physx/train_enn.py:123:150
208
208
209
209
完成上述设置之后,只需要将上述实例化的对象按顺序传递给 ` ppsci.solver.Solver ` ,然后启动训练、评估。
210
210
211
- ``` py linenums="153 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
211
+ ``` py linenums="156 " title="examples/cylinder/2d_unsteady/transformer_physx/train_enn.py"
212
212
-- 8 < --
213
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:153 :
213
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_enn.py:156 :
214
214
-- 8 < --
215
215
```
216
216
217
217
### 3.3 Transformer 模型
218
218
219
219
上文介绍了如何构建 Embedding 模型的训练、评估,在本节中将介绍如何使用训练好的 Embedding 模型训练 Transformer 模型。因为训练 Transformer 模型的步骤与训练 Embedding 模型的步骤基本相似,因此本节在两者的重复部分的各个参数不再详细介绍。首先将代码中定义的各个参数变量展示如下,每个参数的具体含义会在下面使用到时进行解释。
220
220
221
- ``` py linenums="58 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
221
+ ``` py linenums="57 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
222
222
-- 8 < --
223
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:58 : 74
223
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:57 : 79
224
224
-- 8 < --
225
225
```
226
226
227
227
#### 3.3.1 约束构建
228
228
229
229
Transformer 模型同样基于数据驱动的方法求解问题,因此需要使用 PaddleScience 内置的 ` SupervisedConstraint ` 构建监督约束。在定义约束之前,需要首先指定监督约束中用于数据加载的各个参数,代码如下:
230
230
231
- ``` py linenums="82 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
231
+ ``` py linenums="87 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
232
232
-- 8 < --
233
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:82 : 99
233
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:87 : 104
234
234
-- 8 < --
235
235
```
236
236
237
237
数据加载的各个参数与 Embedding 模型中的基本一致,不再赘述。需要说明的是由于 Transformer 模型训练的输入数据是 Embedding 模型 Encoder 模块的输出数据,因此我们将训练好的 Embedding 模型作为 ` CylinderDataset ` 的一个参数,在初始化时首先将训练数据映射到编码空间。
238
238
239
239
定义监督约束的代码如下:
240
240
241
- ``` py linenums="101 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
241
+ ``` py linenums="106 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
242
242
-- 8 < --
243
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:101 : 106
243
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:106 : 111
244
244
-- 8 < --
245
245
```
246
246
@@ -255,9 +255,9 @@ examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py:101:106
255
255
256
256
用 PaddleScience 代码表示如下:
257
257
258
- ``` py linenums="111 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
258
+ ``` py linenums="116 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
259
259
-- 8 < --
260
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:111 : 119
260
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:116 : 124
261
261
-- 8 < --
262
262
```
263
263
@@ -267,19 +267,19 @@ examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py:111:119
267
267
268
268
本案例中使用的学习率方法为 ` CosineWarmRestarts ` ,学习率大小设置为0.001。优化器使用 ` Adam ` ,梯度裁剪使用了 Paddle 内置的 ` ClipGradByGlobalNorm ` 方法。用 PaddleScience 代码表示如下:
269
269
270
- ``` py linenums="121 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
270
+ ``` py linenums="126 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
271
271
-- 8 < --
272
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:121 : 135
272
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:126 : 140
273
273
-- 8 < --
274
274
```
275
275
276
276
#### 3.3.4 评估器构建
277
277
278
278
训练过程中会按照一定的训练轮数间隔,使用验证集评估当前模型的训练情况,需要使用 ` SupervisedValidator ` 构建评估器。用 PaddleScience 代码表示如下:
279
279
280
- ``` py linenums="137 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
280
+ ``` py linenums="142 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
281
281
-- 8 < --
282
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:137 : 163
282
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:142 : 168
283
283
-- 8 < --
284
284
```
285
285
@@ -289,25 +289,25 @@ examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py:137:163
289
289
290
290
在本文中首先定义了对 Transformer 模型输出数据变换到物理状态空间的代码:
291
291
292
- ``` py linenums="32 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
292
+ ``` py linenums="33 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
293
293
-- 8 < --
294
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:32 : 52
294
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:33 : 53
295
295
-- 8 < --
296
296
```
297
297
298
- ``` py linenums="78 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
298
+ ``` py linenums="83 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
299
299
-- 8 < --
300
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:78 : 79
300
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:83 : 84
301
301
-- 8 < --
302
302
```
303
303
304
304
可以看到,程序首先载入了训练好的 Embedding 模型,然后在 ` OutputTransform ` 的 ` __call__ ` 函数内实现了编码向量到物理状态空间的变换。
305
305
306
306
在定义好了以上代码之后,就可以实现可视化器代码的构建了:
307
307
308
- ``` py linenums="165 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
308
+ ``` py linenums="170 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
309
309
-- 8 < --
310
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:165 : 193
310
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:170 : 197
311
311
-- 8 < --
312
312
```
313
313
@@ -317,9 +317,9 @@ examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py:165:193
317
317
318
318
完成上述设置之后,只需要将上述实例化的对象按顺序传递给 ` ppsci.solver.Solver ` ,然后启动训练、评估。
319
319
320
- ``` py linenums="193 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
320
+ ``` py linenums="199 " title="examples/cylinder/2d_unsteady/transformer_physx/train_transformer.py"
321
321
-- 8 < --
322
- examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:193 :
322
+ examples/ cylinder/ 2d_unsteady / transformer_physx/ train_transformer.py:199 :
323
323
-- 8 < --
324
324
```
325
325
0 commit comments