Skip to content

Commit 5e78c13

Browse files
【PPSCI Doc No.75, 77-85】 (#703)
* update docs * update func * Update ppsci/loss/func.py Co-authored-by: HydrogenSulfate <490868991@qq.com> --------- Co-authored-by: HydrogenSulfate <490868991@qq.com>
1 parent 0309d51 commit 5e78c13

File tree

6 files changed

+249
-33
lines changed

6 files changed

+249
-33
lines changed

ppsci/loss/func.py

+27-17
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,54 @@
1919
from typing import Optional
2020
from typing import Union
2121

22-
from typing_extensions import Literal
23-
2422
from ppsci.loss import base
2523

2624

2725
class FunctionalLoss(base.Loss):
2826
r"""Functional loss class, which allows to use custom loss computing function from given loss_expr for complex computation cases.
2927
28+
$$
29+
L = f(\mathbf{x}, \mathbf{y})
30+
$$
31+
32+
$$
33+
\mathbf{x}, \mathbf{y} \in \mathcal{R}^{N}
34+
$$
35+
3036
Args:
3137
loss_expr (Callable): expression of loss calculation.
32-
reduction (Literal["mean", "sum"], optional): Reduction method. Defaults to "mean".
3338
weight (Optional[Union[float, Dict[str, float]]]): Weight for loss. Defaults to None.
3439
3540
Examples:
36-
>>> import ppsci
41+
>>> import paddle
42+
>>> from ppsci.loss import FunctionalLoss
3743
>>> import paddle.nn.functional as F
38-
>>> def loss_expr(output_dict, *args):
44+
>>> def mse_sum_loss(output_dict, label_dict, weight_dict=None):
3945
... losses = 0
40-
... for key in output_dict:
41-
... length = int(len(output_dict[key])/2)
42-
... out_dict = {key: output_dict[key][:length]}
43-
... label_dict = {key: output_dict[key][length:]}
44-
... losses += F.mse_loss(out_dict, label_dict, "sum")
46+
... for key in output_dict.keys():
47+
... loss = F.mse_loss(output_dict[key], label_dict[key], "sum")
48+
... if weight_dict:
49+
... loss *= weight_dict[key]
50+
... losses += loss
4551
... return losses
46-
>>> loss = ppsci.loss.FunctionalLoss(loss_expr)
52+
>>> loss = FunctionalLoss(mse_sum_loss)
53+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
54+
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
55+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
56+
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
57+
>>> weight_dict = {'u': 0.8, 'v': 0.2}
58+
>>> result = loss(output_dict, label_dict, weight_dict)
59+
>>> print(result)
60+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
61+
17.89600182)
4762
"""
4863

4964
def __init__(
5065
self,
5166
loss_expr: Callable,
52-
reduction: Literal["mean", "sum"] = "mean",
5367
weight: Optional[Union[float, Dict[str, float]]] = None,
5468
):
55-
if reduction not in ["mean", "sum"]:
56-
raise ValueError(
57-
f"reduction should be 'mean' or 'sum', but got {reduction}"
58-
)
59-
super().__init__(reduction, weight)
69+
super().__init__(None, weight)
6070
self.loss_expr = loss_expr
6171

6272
def forward(self, output_dict, label_dict=None, weight_dict=None):

ppsci/loss/integral.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,26 @@ class IntegralLoss(base.Loss):
4444
weight (Optional[Union[float, Dict[str, float]]]): Weight for loss. Defaults to None.
4545
4646
Examples:
47-
>>> import ppsci
48-
>>> loss = ppsci.loss.IntegralLoss("mean")
47+
>>> import paddle
48+
>>> from ppsci.loss import IntegralLoss
49+
50+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 2.2, 0.9], [1.1, 0.8, -1.3]]),
51+
... 'v': paddle.to_tensor([[0.5, 2.2, 0.9], [1.1, 0.8, -1.3]]),
52+
... 'area': paddle.to_tensor([[0.01, 0.02, 0.03], [0.01, 0.02, 0.03]])}
53+
>>> label_dict = {'u': paddle.to_tensor([-1.8, 0.0]),
54+
... 'v': paddle.to_tensor([0.1, 0.1])}
55+
>>> weight = {'u': 0.8, 'v': 0.2}
56+
>>> loss = IntegralLoss(weight=weight)
57+
>>> result = loss(output_dict, label_dict)
58+
>>> print(result)
59+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
60+
1.40911996)
61+
62+
>>> loss = IntegralLoss(reduction="sum", weight=weight)
63+
>>> result = loss(output_dict, label_dict)
64+
>>> print(result)
65+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
66+
2.81823993)
4967
"""
5068

5169
def __init__(

ppsci/loss/l1.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,42 @@ class PeriodicL1Loss(base.Loss):
119119
$\mathbf{x_l} \in \mathcal{R}^{N}$ is the first half of batch output,
120120
$\mathbf{x_r} \in \mathcal{R}^{N}$ is the second half of batch output.
121121
122+
when `reduction` is set to "mean"
123+
124+
$$
125+
L = MEAN \left( \Vert \mathbf{x_l}-\mathbf{x_r} \Vert_1 \right)
126+
$$
127+
128+
when `reduction` is set to "sum"
129+
130+
$$
131+
L = SUM \left( \Vert \mathbf{x_l}-\mathbf{x_r} \Vert_1 \right)
132+
$$
133+
122134
Args:
123135
reduction (Literal["mean", "sum"], optional): Reduction method. Defaults to "mean".
124136
weight (Optional[Union[float, Dict[str, float]]]): Weight for loss. Defaults to None.
125137
126138
Examples:
127-
>>> import ppsci
128-
>>> loss = ppsci.loss.PeriodicL1Loss("mean")
139+
>>> import paddle
140+
>>> from ppsci.loss import PeriodicL1Loss
141+
142+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 2.2, 0.9], [1.1, 0.8, -1.3]]),
143+
... 'v': paddle.to_tensor([[0.5, 2.2, 0.9], [1.1, 0.8, -1.3]])}
144+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 0.0, 1.0], [-0.2, 0.2, 2.5]]),
145+
... 'v': paddle.to_tensor([[0.1, 0.1, 0.1], [0.1, 0.1, 0.1]])}
146+
>>> weight = {'u': 0.8, 'v': 0.2}
147+
>>> loss = PeriodicL1Loss(weight=weight)
148+
>>> result = loss(output_dict, label_dict)
149+
>>> print(result)
150+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
151+
4.19999981)
152+
153+
>>> loss = PeriodicL1Loss(reduction="sum", weight=weight)
154+
>>> result = loss(output_dict, label_dict)
155+
>>> print(result)
156+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
157+
4.19999981)
129158
"""
130159

131160
def __init__(

ppsci/loss/l2.py

+91-6
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,40 @@ class L2Loss(base.Loss):
3636
\mathbf{x}, \mathbf{y} \in \mathcal{R}^{N}
3737
$$
3838
39+
when `reduction` is set to "mean"
40+
41+
$$
42+
L = MEAN \left( \Vert \mathbf{x} - \mathbf{y} \Vert_2 \right)
43+
$$
44+
45+
when `reduction` is set to "sum"
46+
47+
$$
48+
L = SUM \left( \Vert \mathbf{x} - \mathbf{y} \Vert_2 \right)
49+
$$
50+
3951
Args:
4052
reduction (Literal["mean", "sum"], optional): Reduction method. Defaults to "mean".
4153
weight (Optional[Union[float, Dict[str, float]]]): Weight for loss. Defaults to None.
4254
4355
Examples:
44-
>>> import ppsci
45-
>>> loss = ppsci.loss.L2Loss()
56+
>>> import paddle
57+
>>> from ppsci.loss import L2Loss
58+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
59+
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
60+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
61+
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
62+
>>> weight = {'u': 0.8, 'v': 0.2}
63+
>>> loss = L2Loss(weight=weight)
64+
>>> result = loss(output_dict, label_dict)
65+
>>> print(result)
66+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
67+
2.78884506)
68+
>>> loss = L2Loss(reduction="sum", weight=weight)
69+
>>> result = loss(output_dict, label_dict)
70+
>>> print(result)
71+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
72+
5.57769012)
4673
"""
4774

4875
def __init__(
@@ -92,13 +119,42 @@ class PeriodicL2Loss(base.Loss):
92119
$\mathbf{x_l} \in \mathcal{R}^{N}$ is the first half of batch output,
93120
$\mathbf{x_r} \in \mathcal{R}^{N}$ is the second half of batch output.
94121
122+
when `reduction` is set to "mean"
123+
124+
$$
125+
L = MEAN \left( \Vert \mathbf{x_l}-\mathbf{x_r} \Vert_2 \right)
126+
$$
127+
128+
when `reduction` is set to "sum"
129+
130+
$$
131+
L = SUM \left( \Vert \mathbf{x_l}-\mathbf{x_r} \Vert_2 \right)
132+
$$
133+
95134
Args:
96135
reduction (Literal["mean", "sum"], optional): Reduction method. Defaults to "mean".
97136
weight (Optional[Union[float, Dict[str, float]]]): Weight for loss. Defaults to None.
98137
99138
Examples:
100-
>>> import ppsci
101-
>>> loss = ppsci.loss.PeriodicL2Loss()
139+
>>> import paddle
140+
>>> from ppsci.loss import PeriodicL2Loss
141+
142+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 2.2, 0.9], [1.1, 0.8, -1.3]]),
143+
... 'v': paddle.to_tensor([[0.5, 2.2, 0.9], [1.1, 0.8, -1.3]])}
144+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 0.0, 1.0], [-0.2, 0.2, 2.5]]),
145+
... 'v': paddle.to_tensor([[0.1, 0.1, 0.1], [0.1, 0.1, 0.1]])}
146+
>>> weight = {'u': 0.8, 'v': 0.2}
147+
>>> loss = PeriodicL2Loss(weight=weight)
148+
>>> result = loss(output_dict, label_dict)
149+
>>> print(result)
150+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
151+
2.67581749)
152+
153+
>>> loss = PeriodicL2Loss(reduction="sum", weight=weight)
154+
>>> result = loss(output_dict, label_dict)
155+
>>> print(result)
156+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
157+
2.67581749)
102158
"""
103159

104160
def __init__(
@@ -158,13 +214,42 @@ class L2RelLoss(base.Loss):
158214
\mathbf{x}, \mathbf{y} \in \mathcal{R}^{N}
159215
$$
160216
217+
when `reduction` is set to "mean"
218+
219+
$$
220+
L = MEAN \left( \dfrac{\Vert \mathbf{x} - \mathbf{y} \Vert_2}{\Vert \mathbf{y} \Vert_2} \right)
221+
$$
222+
223+
when `reduction` is set to "sum"
224+
225+
$$
226+
L = SUM \left( \dfrac{\Vert \mathbf{x} - \mathbf{y} \Vert_2}{\Vert \mathbf{y} \Vert_2} \right)
227+
$$
228+
161229
Args:
162230
reduction (Literal["mean", "sum"], optional): Specifies the reduction to apply to the output: 'mean' | 'sum'. Defaults to "mean".
163231
weight (Optional[Union[float, Dict[str, float]]]): Weight for loss. Defaults to None.
164232
165233
Examples:
166-
>>> import ppsci
167-
>>> loss = ppsci.loss.L2RelLoss()
234+
>>> import paddle
235+
>>> from ppsci.loss import L2RelLoss
236+
237+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
238+
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
239+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
240+
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
241+
>>> weight = {'u': 0.8, 'v': 0.2}
242+
>>> loss = L2RelLoss(weight=weight)
243+
>>> result = loss(output_dict, label_dict)
244+
>>> print(result)
245+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
246+
2.93676996)
247+
248+
>>> loss = L2RelLoss(reduction="sum", weight=weight)
249+
>>> result = loss(output_dict, label_dict)
250+
>>> print(result)
251+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
252+
5.87353992)
168253
"""
169254

170255
def __init__(

ppsci/loss/mae.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,25 @@ class MAELoss(base.Loss):
4444
weight (Optional[Union[float, Dict[str, float]]]): Weight for loss. Defaults to None.
4545
4646
Examples:
47-
>>> import ppsci
48-
>>> loss = ppsci.loss.MAELoss("mean")
47+
>>> import paddle
48+
>>> from ppsci.loss import MAELoss
49+
50+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
51+
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
52+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
53+
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
54+
>>> weight = {'u': 0.8, 'v': 0.2}
55+
>>> loss = MAELoss(weight=weight)
56+
>>> result = loss(output_dict, label_dict)
57+
>>> print(result)
58+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
59+
1.67999995)
60+
61+
>>> loss = MAELoss(reduction="sum", weight=weight)
62+
>>> result = loss(output_dict, label_dict)
63+
>>> print(result)
64+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
65+
6.71999979)
4966
"""
5067

5168
def __init__(

ppsci/loss/mse.py

+61-4
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,25 @@ class MSELoss(base.Loss):
4444
weight (Optional[Union[float, Dict[str, float]]]): Weight for loss. Defaults to None.
4545
4646
Examples:
47-
>>> import ppsci
48-
>>> loss = ppsci.loss.MSELoss("mean")
47+
>>> import paddle
48+
>>> from ppsci.loss import MSELoss
49+
50+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
51+
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
52+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
53+
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
54+
>>> weight = {'u': 0.8, 'v': 0.2}
55+
>>> loss = MSELoss(weight=weight)
56+
>>> result = loss(output_dict, label_dict)
57+
>>> print(result)
58+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
59+
4.47400045)
60+
61+
>>> loss = MSELoss(reduction="sum", weight=weight)
62+
>>> result = loss(output_dict, label_dict)
63+
>>> print(result)
64+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
65+
17.89600182)
4966
"""
5067

5168
def __init__(
@@ -108,8 +125,27 @@ class MSELossWithL2Decay(MSELoss):
108125
ValueError: reduction should be 'mean' or 'sum'.
109126
110127
Examples:
111-
>>> import ppsci
112-
>>> loss = ppsci.loss.MSELossWithL2Decay("mean", {"k_matrix": 2.0})
128+
>>> import paddle
129+
>>> from ppsci.loss import MSELossWithL2Decay
130+
131+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
132+
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
133+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
134+
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
135+
>>> weight = {'u': 0.8, 'v': 0.2}
136+
>>> regularization_dict = {'u': 2.0}
137+
>>> loss = MSELossWithL2Decay(regularization_dict=regularization_dict, weight=weight)
138+
>>> result = loss(output_dict, label_dict)
139+
>>> print(result)
140+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
141+
12.39400005)
142+
143+
>>> regularization_dict = {'v': 1.0}
144+
>>> loss = MSELossWithL2Decay(reduction="sum", regularization_dict=regularization_dict, weight=weight)
145+
>>> result = loss(output_dict, label_dict)
146+
>>> print(result)
147+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
148+
21.85600090)
113149
"""
114150

115151
def __init__(
@@ -152,6 +188,27 @@ class PeriodicMSELoss(base.Loss):
152188
Args:
153189
reduction (Literal["mean", "sum"], optional): Reduction method. Defaults to "mean".
154190
weight (Optional[Union[float, Dict[str, float]]]): Weight for loss. Defaults to None.
191+
192+
Examples:
193+
>>> import paddle
194+
>>> from ppsci.loss import PeriodicMSELoss
195+
196+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
197+
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
198+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
199+
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
200+
>>> weight = {'u': 0.8, 'v': 0.2}
201+
>>> loss = PeriodicMSELoss(weight=weight)
202+
>>> result = loss(output_dict, label_dict)
203+
>>> print(result)
204+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
205+
2.59999967)
206+
207+
>>> loss = PeriodicMSELoss(reduction="sum", weight=weight)
208+
>>> result = loss(output_dict, label_dict)
209+
>>> print(result)
210+
Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
211+
5.19999933)
155212
"""
156213

157214
def __init__(

0 commit comments

Comments
 (0)