@@ -994,10 +994,13 @@ best_value: 0.02460772916674614
994994
995995接下来以 ` examples/pipe/poiseuille_flow.py` 为例,介绍如何正确使用 PaddleScience 的数据并行功能进行训练。分布式训练细节可以参考:[Paddle-使用指南-分布式训练-快速开始-数据并行](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/guides/06_distributed_training/cluster_quick_start_collective_cn.html)。
996996
997- 1. 在 constraint 实例化完毕后,将 ` ITERS_PER_EPOCH` 重新赋值为经过自动多卡数据切分后的 ` dataloader` 的长度(一般情况下其长度等于单卡 dataloader 的长度除以卡数,向上取整),如代码中高亮行所示。
997+ 1. 在 constraint 实例化完毕后,将 ` ITERS_PER_EPOCH` 重新赋值为经过自动多卡数据切分后的 ` dataloader` 的长度,再作为参数传递给 ` Solver ` (一般情况下其长度等于单卡 dataloader 的长度除以卡数,向上取整),如代码中高亮行所示。
998998
999- ` ` ` py linenums=" 146" title=" examples/pipe/poiseuille_flow.py" hl_lines=" 22"
1000- ITERS_PER_EPOCH = int(( N_x * N_y * N_p) / BATCH_SIZE)
999+ ` ` ` py linenums=" 146" title=" examples/pipe/poiseuille_flow.py" hl_lines=" 28 37"
1000+ # set constraint
1001+ ITERS_PER_EPOCH = int(
1002+ (cfg.N_x * cfg.N_y * cfg.N_p) / cfg.TRAIN.batch_size.pde_constraint
1003+ )
10011004
10021005 pde_constraint = ppsci.constraint.InteriorConstraint(
10031006 equation[" NavierStokes" ].equations,
@@ -1006,22 +1009,36 @@ best_value: 0.02460772916674614
10061009 dataloader_cfg={
10071010 " dataset" : " NamedArrayDataset" ,
10081011 " num_workers" : 1,
1009- "batch_size": BATCH_SIZE ,
1012+ " batch_size" : cfg.TRAIN.batch_size.pde_constraint ,
10101013 " iters_per_epoch" : ITERS_PER_EPOCH,
10111014 " sampler" : {
10121015 " name" : " BatchSampler" ,
1016+ " shuffle" : False,
1017+ " drop_last" : False,
10131018 },
10141019 },
10151020 loss=ppsci.loss.MSELoss(" mean" ),
10161021 evenly=True,
10171022 name=" EQ" ,
10181023 )
1019- ITERS_PER_EPOCH = len(pde_constraint.data_loader) # re-assign to ITERS_PER_EPOCH
1020-
10211024 # wrap constraints together
10221025 constraint = {pde_constraint.name: pde_constraint}
1023- ...
1024- ...
1026+
1027+ ITERS_PER_EPOCH = len(pde_constraint.data_loader) # re-assign to ITERS_PER_EPOCH
1028+
1029+ # initialize solver
1030+ solver = ppsci.solver.Solver(
1031+ model,
1032+ constraint,
1033+ cfg.output_dir,
1034+ optimizer,
1035+ epochs=cfg.TRAIN.epochs,
1036+ iters_per_epoch=ITERS_PER_EPOCH,
1037+ eval_during_train=cfg.TRAIN.eval_during_train,
1038+ save_freq=cfg.TRAIN.save_freq,
1039+ equation=equation,
1040+ )
1041+ solver.train ()
10251042 ` ` `
10261043
102710442. 使用分布式训练命令启动训练,以 4 卡数据并行训练为例
0 commit comments