Skip to content

Commit 6285a5f

Browse files
authored
update docs (#723)
1 parent 60723a0 commit 6285a5f

File tree

6 files changed

+104
-15
lines changed

6 files changed

+104
-15
lines changed

ppsci/metric/func.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,23 @@ class FunctionalMetric(base.Metric):
2727
keep_batch (bool, optional): Whether keep batch axis. Defaults to False.
2828
2929
Examples:
30-
>>> import ppsci
3130
>>> import paddle
31+
>>> from ppsci.metric import FunctionalMetric
3232
>>> def metric_expr(output_dict, *args):
3333
... rel_l2 = 0
3434
... for key in output_dict:
3535
... length = int(len(output_dict[key])/2)
36-
... out_dict = {key: output_dict[key][:length]}
37-
... label_dict = {key: output_dict[key][length:]}
36+
... out_dict = output_dict[key][:length]
37+
... label_dict = output_dict[key][length:]
3838
... rel_l2 += paddle.norm(out_dict - label_dict) / paddle.norm(label_dict)
39-
... return {"l2": rel_l2}
40-
>>> metric_dict = ppsci.metric.FunctionalMetric(metric_expr)
39+
... return {"rel_l2": rel_l2}
40+
>>> metric_dict = FunctionalMetric(metric_expr)
41+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3], [-0.2, 1.5], [-0.1, -0.3]]),
42+
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3], [-1.8, 1.0], [-0.2, 2.5]])}
43+
>>> result = metric_dict(output_dict)
44+
>>> print(result)
45+
{'rel_l2': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
46+
2.59985542)}
4147
"""
4248

4349
def __init__(

ppsci/metric/l2_rel.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,18 @@ class L2Rel(base.Metric):
4040
keep_batch (bool, optional): Whether keep batch axis. Defaults to False.
4141
4242
Examples:
43-
>>> import ppsci
44-
>>> metric = ppsci.metric.L2Rel()
43+
>>> import paddle
44+
>>> from ppsci.metric import L2Rel
45+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
46+
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
47+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
48+
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
49+
>>> loss = L2Rel()
50+
>>> result = loss(output_dict, label_dict)
51+
>>> print(result)
52+
{'u': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
53+
1.42658269), 'v': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
54+
9.69535923)}
4555
"""
4656

4757
# NOTE: Avoid divide by zero in result
@@ -85,8 +95,24 @@ class MeanL2Rel(base.Metric):
8595
keep_batch (bool, optional): Whether keep batch axis. Defaults to False.
8696
8797
Examples:
88-
>>> import ppsci
89-
>>> metric = ppsci.metric.MeanL2Rel()
98+
>>> import paddle
99+
>>> from ppsci.metric import MeanL2Rel
100+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
101+
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
102+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
103+
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
104+
>>> loss = MeanL2Rel()
105+
>>> result = loss(output_dict, label_dict)
106+
>>> print(result)
107+
{'u': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
108+
1.35970235), 'v': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
109+
9.24504089)}
110+
>>> loss = MeanL2Rel(keep_batch=True)
111+
>>> result = loss(output_dict, label_dict)
112+
>>> print(result)
113+
{'u': Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
114+
[1.11803389, 1.60137081]), 'v': Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
115+
[6.32455540 , 12.16552544])}
90116
"""
91117

92118
# NOTE: Avoid divide by zero in result

ppsci/metric/mae.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,24 @@ class MAE(base.Metric):
3535
keep_batch (bool, optional): Whether keep batch axis. Defaults to False.
3636
3737
Examples:
38-
>>> import ppsci
39-
>>> metric = ppsci.metric.MAE()
38+
>>> import paddle
39+
>>> from ppsci.metric import MAE
40+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
41+
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
42+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
43+
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
44+
>>> loss = MAE()
45+
>>> result = loss(output_dict, label_dict)
46+
>>> print(result)
47+
{'u': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
48+
1.87500000), 'v': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
49+
0.89999998)}
50+
>>> loss = MAE(keep_batch=True)
51+
>>> result = loss(output_dict, label_dict)
52+
>>> print(result)
53+
{'u': Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
54+
[1.20000005, 2.54999995]), 'v': Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
55+
[0.59999996, 1.20000005])}
4056
"""
4157

4258
def __init__(self, keep_batch: bool = False):

ppsci/metric/mse.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,24 @@ class MSE(base.Metric):
3535
keep_batch (bool, optional): Whether keep batch axis. Defaults to False.
3636
3737
Examples:
38-
>>> import ppsci
39-
>>> metric = ppsci.metric.MSE()
38+
>>> import paddle
39+
>>> from ppsci.metric import MSE
40+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
41+
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
42+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
43+
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
44+
>>> loss = MSE()
45+
>>> result = loss(output_dict, label_dict)
46+
>>> print(result)
47+
{'u': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
48+
5.35750008), 'v': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
49+
0.94000000)}
50+
>>> loss = MSE(keep_batch=True)
51+
>>> result = loss(output_dict, label_dict)
52+
>>> print(result)
53+
{'u': Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
54+
[2.65000010, 8.06499958]), 'v': Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
55+
[0.39999998, 1.48000002])}
4056
"""
4157

4258
def __init__(self, keep_batch: bool = False):

ppsci/metric/rmse.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,18 @@ class RMSE(base.Metric):
4141
keep_batch (bool, optional): Whether keep batch axis. Defaults to False.
4242
4343
Examples:
44-
>>> import ppsci
45-
>>> metric = ppsci.metric.RMSE()
44+
>>> import paddle
45+
>>> from ppsci.metric import RMSE
46+
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
47+
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
48+
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
49+
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
50+
>>> loss = RMSE()
51+
>>> result = loss(output_dict, label_dict)
52+
>>> print(result)
53+
{'u': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
54+
2.31462741), 'v': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
55+
0.96953595)}
4656
"""
4757

4858
def __init__(self, keep_batch: bool = False):

ppsci/solver/solver.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,21 @@ def predict(
520520
521521
Returns:
522522
Dict[str, Union[paddle.Tensor, np.ndarray]]: Prediction in dict.
523+
524+
Examples:
525+
>>> import paddle
526+
>>> import ppsci
527+
>>> paddle.seed(42) # doctest: +SKIP
528+
>>> model = ppsci.arch.MLP(('x', 'y'), ('u', 'v'), num_layers=None, hidden_size=[32, 8])
529+
>>> solver = ppsci.solver.Solver(model) # doctest: +SKIP
530+
>>> input_dict = {'x': paddle.rand((2, 1)),
531+
... 'y': paddle.rand((2, 1))}
532+
>>> solver.predict(input_dict) # doctest: +SKIP
533+
{'u': Tensor(shape=[2, 1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
534+
[[-0.17509711],
535+
[-0.03884222]]), 'v': Tensor(shape=[2, 1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
536+
[[0.27433380],
537+
[0.42387512]])}
523538
"""
524539
num_samples = len(next(iter(input_dict.values())))
525540
num_pad = (self.world_size - num_samples % self.world_size) % self.world_size

0 commit comments

Comments
 (0)