Metric(评价指标) 模块¶
ppsci.metric
¶
Metric
¶
FunctionalMetric
¶
Bases: Metric
Functional metric class, which allows to use custom metric computing function from given metric_expr for complex computation cases.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric_expr |
Callable
|
Expression of metric calculation. |
required |
keep_batch |
bool
|
Whether keep batch axis. Defaults to False. |
False
|
Examples:
>>> 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 = output_dict[key][:length]
... label_dict = output_dict[key][length:]
... rel_l2 += paddle.norm(out_dict - label_dict) / paddle.norm(label_dict)
... 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)}
Source code in ppsci/metric/func.py
MAE
¶
Bases: Metric
Mean absolute error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keep_batch |
bool
|
Whether keep batch axis. Defaults to False. |
False
|
Examples:
>>> 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])}
Source code in ppsci/metric/mae.py
MSE
¶
Bases: Metric
Mean square error
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keep_batch |
bool
|
Whether keep batch axis. Defaults to False. |
False
|
Examples:
>>> 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])}
Source code in ppsci/metric/mse.py
RMSE
¶
Bases: Metric
Root mean square error
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keep_batch |
bool
|
Whether keep batch axis. Defaults to False. |
False
|
Examples:
>>> 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)}
Source code in ppsci/metric/rmse.py
L2Rel
¶
Bases: Metric
Class for l2 relative error.
NOTE: This metric API is slightly different from MeanL2Rel
, difference is as below:
L2Rel
regards the input sample as a whole and calculates the l2 relative error of the whole;MeanL2Rel
will calculate L2Rel separately for each input sample and return the average of l2 relative error for all samples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keep_batch |
bool
|
Whether keep batch axis. Defaults to False. |
False
|
Examples:
>>> 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)}
Source code in ppsci/metric/l2_rel.py
MeanL2Rel
¶
Bases: Metric
Class for mean l2 relative error.
NOTE: This metric API is slightly different from L2Rel
, difference is as below:
MeanL2Rel
will calculate L2Rel separately for each input sample and return the average of l2 relative error for all samples.L2Rel
regards the input sample as a whole and calculates the l2 relative error of the whole;
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keep_batch |
bool
|
Whether keep batch axis. Defaults to False. |
False
|
Examples:
>>> 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])}
Source code in ppsci/metric/l2_rel.py
LatitudeWeightedACC
¶
Bases: Metric
Latitude weighted anomaly correlation coefficient.
\(lat_m\) is the latitude at m.
\(N_{lat}\) is the number of latitude set by num_lat
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_lat |
int
|
Number of latitude. |
required |
mean |
Optional[Union[array, Tuple[float, ...]]]
|
Mean of training data. Defaults to None. |
required |
keep_batch |
bool
|
Whether keep batch axis. Defaults to False. |
False
|
variable_dict |
Optional[Dict[str, int]]
|
Variable dictionary, the key is the name of a variable and the value is its index. Defaults to None. |
None
|
unlog |
bool
|
Whether calculate expm1 for all elements in the array. Defaults to False. |
False
|
scale |
float
|
The scale value used after expm1. Defaults to 1e-5. |
1e-05
|
Examples:
>>> import numpy as np
>>> import ppsci
>>> mean = np.random.randn(20, 720, 1440)
>>> metric = ppsci.metric.LatitudeWeightedACC(720, mean=mean)
Source code in ppsci/metric/anomaly_coef.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
LatitudeWeightedRMSE
¶
Bases: Metric
Latitude weighted root mean square error.
\(lat_m\) is the latitude at m.
\(N_{lat}\) is the number of latitude set by num_lat
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_lat |
int
|
Number of latitude. |
required |
std |
Optional[Union[array, Tuple[float, ...]]]
|
Standard Deviation of training dataset. Defaults to None. |
None
|
keep_batch |
bool
|
Whether keep batch axis. Defaults to False. |
False
|
variable_dict |
Optional[Dict[str, int]]
|
Variable dictionary, the key is the name of a variable and the value is its index. Defaults to None. |
None
|
unlog |
bool
|
Whether calculate expm1 for all elements in the array. Defaults to False. |
False
|
scale |
float
|
The scale value used after expm1. Defaults to 1e-5. |
1e-05
|
Examples:
>>> import numpy as np
>>> import ppsci
>>> std = np.random.randn(20, 1, 1)
>>> metric = ppsci.metric.LatitudeWeightedRMSE(720, std=std)
Source code in ppsci/metric/rmse.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|