forked from PaddlePaddle/PaddleScience
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enh] Refactor sum aggregator (PaddlePaddle#834)
* add Sum loss aggregator * simplify loss aggregation code in train.py and add check for AGDA and PCGrad when used with amp * add check for using L-BFGS with use_amp=True * Refine Relobralo * Fix docstring of timedomain.py * remove unnecessary code in train.py * automatically download *.pdeqn file if available when download pretrained model * wrap func generated by symbolic module with DDP * fix Relobralo * initialize loss with 0.0 instead of first loss
- Loading branch information
1 parent
54f8b8d
commit 17eff79
Showing
11 changed files
with
117 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ | |
- LossAggregator | ||
- PCGrad | ||
- Relobralo | ||
- Sum | ||
show_root_heading: true | ||
heading_level: 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from typing import Sequence | ||
|
||
if TYPE_CHECKING: | ||
import paddle | ||
|
||
from ppsci.loss.mtl.base import LossAggregator | ||
|
||
|
||
class Sum(LossAggregator): | ||
r""" | ||
**Default loss aggregator** which do simple summation for given losses as below. | ||
$$ | ||
loss = \sum_i^N losses_i | ||
$$ | ||
""" | ||
|
||
def __init__(self) -> None: | ||
self.step = 0 | ||
|
||
def __call__( | ||
self, losses: Sequence["paddle.Tensor"], step: int = 0 | ||
) -> paddle.Tensor: | ||
assert ( | ||
len(losses) > 0 | ||
), f"Number of given losses({len(losses)}) can not be empty." | ||
self.step = step | ||
|
||
loss = 0.0 | ||
for i in range(len(losses)): | ||
loss += losses[i] | ||
|
||
return loss |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters