Skip to content

Commit

Permalink
update docs (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
NKNaN authored Dec 27, 2023
1 parent 60723a0 commit 6285a5f
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 15 deletions.
16 changes: 11 additions & 5 deletions ppsci/metric/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,23 @@ class FunctionalMetric(base.Metric):
keep_batch (bool, optional): Whether keep batch axis. Defaults to False.
Examples:
>>> import ppsci
>>> import paddle
>>> from ppsci.metric import FunctionalMetric
>>> def metric_expr(output_dict, *args):
... rel_l2 = 0
... for key in output_dict:
... length = int(len(output_dict[key])/2)
... out_dict = {key: output_dict[key][:length]}
... label_dict = {key: output_dict[key][length:]}
... out_dict = output_dict[key][:length]
... label_dict = output_dict[key][length:]
... rel_l2 += paddle.norm(out_dict - label_dict) / paddle.norm(label_dict)
... return {"l2": rel_l2}
>>> metric_dict = ppsci.metric.FunctionalMetric(metric_expr)
... return {"rel_l2": rel_l2}
>>> metric_dict = FunctionalMetric(metric_expr)
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3], [-0.2, 1.5], [-0.1, -0.3]]),
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3], [-1.8, 1.0], [-0.2, 2.5]])}
>>> result = metric_dict(output_dict)
>>> print(result)
{'rel_l2': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
2.59985542)}
"""

def __init__(
Expand Down
34 changes: 30 additions & 4 deletions ppsci/metric/l2_rel.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,18 @@ class L2Rel(base.Metric):
keep_batch (bool, optional): Whether keep batch axis. Defaults to False.
Examples:
>>> import ppsci
>>> metric = ppsci.metric.L2Rel()
>>> import paddle
>>> from ppsci.metric import L2Rel
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
>>> loss = L2Rel()
>>> result = loss(output_dict, label_dict)
>>> print(result)
{'u': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
1.42658269), 'v': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
9.69535923)}
"""

# NOTE: Avoid divide by zero in result
Expand Down Expand Up @@ -85,8 +95,24 @@ class MeanL2Rel(base.Metric):
keep_batch (bool, optional): Whether keep batch axis. Defaults to False.
Examples:
>>> import ppsci
>>> metric = ppsci.metric.MeanL2Rel()
>>> import paddle
>>> from ppsci.metric import MeanL2Rel
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
>>> loss = MeanL2Rel()
>>> result = loss(output_dict, label_dict)
>>> print(result)
{'u': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
1.35970235), 'v': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
9.24504089)}
>>> loss = MeanL2Rel(keep_batch=True)
>>> result = loss(output_dict, label_dict)
>>> print(result)
{'u': Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[1.11803389, 1.60137081]), 'v': Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[6.32455540 , 12.16552544])}
"""

# NOTE: Avoid divide by zero in result
Expand Down
20 changes: 18 additions & 2 deletions ppsci/metric/mae.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,24 @@ class MAE(base.Metric):
keep_batch (bool, optional): Whether keep batch axis. Defaults to False.
Examples:
>>> import ppsci
>>> metric = ppsci.metric.MAE()
>>> import paddle
>>> from ppsci.metric import MAE
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
>>> loss = MAE()
>>> result = loss(output_dict, label_dict)
>>> print(result)
{'u': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
1.87500000), 'v': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
0.89999998)}
>>> loss = MAE(keep_batch=True)
>>> result = loss(output_dict, label_dict)
>>> print(result)
{'u': Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[1.20000005, 2.54999995]), 'v': Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.59999996, 1.20000005])}
"""

def __init__(self, keep_batch: bool = False):
Expand Down
20 changes: 18 additions & 2 deletions ppsci/metric/mse.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,24 @@ class MSE(base.Metric):
keep_batch (bool, optional): Whether keep batch axis. Defaults to False.
Examples:
>>> import ppsci
>>> metric = ppsci.metric.MSE()
>>> import paddle
>>> from ppsci.metric import MSE
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
>>> loss = MSE()
>>> result = loss(output_dict, label_dict)
>>> print(result)
{'u': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
5.35750008), 'v': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
0.94000000)}
>>> loss = MSE(keep_batch=True)
>>> result = loss(output_dict, label_dict)
>>> print(result)
{'u': Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[2.65000010, 8.06499958]), 'v': Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.39999998, 1.48000002])}
"""

def __init__(self, keep_batch: bool = False):
Expand Down
14 changes: 12 additions & 2 deletions ppsci/metric/rmse.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,18 @@ class RMSE(base.Metric):
keep_batch (bool, optional): Whether keep batch axis. Defaults to False.
Examples:
>>> import ppsci
>>> metric = ppsci.metric.RMSE()
>>> import paddle
>>> from ppsci.metric import RMSE
>>> output_dict = {'u': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]]),
... 'v': paddle.to_tensor([[0.5, 0.9], [1.1, -1.3]])}
>>> label_dict = {'u': paddle.to_tensor([[-1.8, 1.0], [-0.2, 2.5]]),
... 'v': paddle.to_tensor([[0.1, 0.1], [0.1, 0.1]])}
>>> loss = RMSE()
>>> result = loss(output_dict, label_dict)
>>> print(result)
{'u': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
2.31462741), 'v': Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
0.96953595)}
"""

def __init__(self, keep_batch: bool = False):
Expand Down
15 changes: 15 additions & 0 deletions ppsci/solver/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,21 @@ def predict(
Returns:
Dict[str, Union[paddle.Tensor, np.ndarray]]: Prediction in dict.
Examples:
>>> import paddle
>>> import ppsci
>>> paddle.seed(42) # doctest: +SKIP
>>> model = ppsci.arch.MLP(('x', 'y'), ('u', 'v'), num_layers=None, hidden_size=[32, 8])
>>> solver = ppsci.solver.Solver(model) # doctest: +SKIP
>>> input_dict = {'x': paddle.rand((2, 1)),
... 'y': paddle.rand((2, 1))}
>>> solver.predict(input_dict) # doctest: +SKIP
{'u': Tensor(shape=[2, 1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[-0.17509711],
[-0.03884222]]), 'v': Tensor(shape=[2, 1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[0.27433380],
[0.42387512]])}
"""
num_samples = len(next(iter(input_dict.values())))
num_pad = (self.world_size - num_samples % self.world_size) % self.world_size
Expand Down

0 comments on commit 6285a5f

Please sign in to comment.