未验证 提交 79e43913 编写于 作者: L liuwei1031 提交者: GitHub

improve doc of module evaluation and CosineDecay API (#1782)

* improve doc of module evaluation and CosineDecay API

* tweak doc of CosineDecay
上级 440fabb6
...@@ -16,8 +16,9 @@ paddle.fluid.metrics模块提供了一系列常用的模型评价指标; 用户 ...@@ -16,8 +16,9 @@ paddle.fluid.metrics模块提供了一系列常用的模型评价指标; 用户
不同类型的任务,会选用不同的评价指标。 不同类型的任务,会选用不同的评价指标。
回归问题通常会用RMSE(均方根误差),MAE(平均绝对误差),R-Square(R平方)等 回归问题通常会用RMSE(均方根误差)、MAE(平均绝对误差)、R-Square(R平方)等
AUC(Area Under Cure)指标则常被用在分类任务(classification)上 AUC(Area Under Cure)指标则常被用在分类任务(classification)上
目标检测任务(Object Detection)则经常会用到mAP(Mean Average Precision) 目标检测任务(Object Detection)则经常会用到mAP(Mean Average Precision)
paddle.fluid.metrics中包含了一些常用分类指标,例如Precision, Recall, Accuracy等 paddle.fluid.metrics中包含了一些常用分类指标,例如Precision, Recall, Accuracy等
...@@ -27,16 +28,25 @@ paddle.fluid.metrics中包含了一些常用分类指标,例如Precision, Reca ...@@ -27,16 +28,25 @@ paddle.fluid.metrics中包含了一些常用分类指标,例如Precision, Reca
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle.fluid as fluid
label = fluid.layers.data(name="label", shape=[1], dtype="int32") import numpy as np
data = fluid.layers.data(name="data", shape=[32, 32], dtype="int32")
pred = fluid.layers.fc(input=data, size=1000, act="tanh") metric = fluid.metrics.Precision()
acc = fluid.metrics.Precision()
for pass_iter in range(PASSES): # generate the preds and labels
acc.reset()
for data in train_reader(): preds = [[0.1], [0.7], [0.8], [0.9], [0.2],
loss, preds, labels = exe.run(fetch_list=[cost, pred, label]) [0.2], [0.3], [0.5], [0.8], [0.6]]
acc.update(preds=preds, labels=labels)
numpy_acc = acc.eval() labels = [[0], [1], [1], [1], [1],
[0], [0], [0], [0], [0]]
preds = np.array(preds)
labels = np.array(labels)
metric.update(preds=preds, labels=labels)
numpy_precision = metric.eval()
print("expect precision: %.2f and got %.2f" % (3.0 / 5.0, numpy_precision))
自定义指标 自定义指标
......
...@@ -20,18 +20,28 @@ Fluid contains common classification metrics, such as Precision, Recall, Accurac ...@@ -20,18 +20,28 @@ Fluid contains common classification metrics, such as Precision, Recall, Accurac
.. code-block:: python .. code-block:: python
>>> import paddle.fluid as fluid
>>> label = fluid.layers.data(name="label", shape=[1], dtype="int32") import paddle.fluid as fluid
>>> data = fluid.layers.data(name="data", shape=[32, 32], dtype="int32") import numpy as np
>>> pred = fluid.layers.fc(input=data, size=1000, act="tanh")
>>> acc = fluid.metrics.Precision() metric = fluid.metrics.Precision()
>>> for pass_iter in range(PASSES):
>>> acc.reset() # generate the preds and labels
>>> for data in train_reader():
>>> loss, preds, labels = exe.run(fetch_list=[cost, preds, labels]) preds = [[0.1], [0.7], [0.8], [0.9], [0.2],
>>> acc.update(preds=preds, labels=labels) [0.2], [0.3], [0.5], [0.8], [0.6]]
>>> numpy_acc = acc.eval()
labels = [[0], [1], [1], [1], [1],
[0], [0], [0], [0], [0]]
preds = np.array(preds)
labels = np.array(labels)
metric.update(preds=preds, labels=labels)
numpy_precision = metric.eval()
print("expect precision: %.2f and got %.2f" % (3.0 / 5.0, numpy_precision))
As for other tasks such as MultiTask Learning, Metric Learning, and Learning To Rank, please refer to the API documentation for their various metric construction methods. As for other tasks such as MultiTask Learning, Metric Learning, and Learning To Rank, please refer to the API documentation for their various metric construction methods.
...@@ -41,20 +51,20 @@ Fluid supports custom metrics and is flexible enough to support a wide range of ...@@ -41,20 +51,20 @@ Fluid supports custom metrics and is flexible enough to support a wide range of
.. code-block:: python .. code-block:: python
>>> class MyMetric(MetricBase): class MyMetric(MetricBase):
>>> def __init__(self, name=None): def __init__(self, name=None):
>>> super(MyMetric, self).__init__(name) super(MyMetric, self).__init__(name)
>>> self.counter = 0 # simple counter self.counter = 0 # simple counter
>>> def reset(self): def reset(self):
>>> self.counter = 0 self.counter = 0
>>> def update(self, preds, labels): def update(self, preds, labels):
>>> if not _is_numpy_(preds): if not _is_numpy_(preds):
>>> raise ValueError("The 'preds' must be a numpy ndarray.") raise ValueError("The 'preds' must be a numpy ndarray.")
>>> if not _is_numpy_(labels): if not _is_numpy_(labels):
>>> raise ValueError("The 'labels' must be a numpy ndarray.") raise ValueError("The 'labels' must be a numpy ndarray.")
>>> self.counter += sum(preds == labels) self.counter += sum(preds == labels)
>>> def eval(self): def eval(self):
>>> return self.counter return self.counter
...@@ -30,12 +30,13 @@ CosineDecay ...@@ -30,12 +30,13 @@ CosineDecay
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid
base_lr = 0.1 base_lr = 0.1
with fluid.dygraph.guard(): with fluid.dygraph.guard():
optimizer = fluid.optimizer.SGD( gru = fluid.dygraph.GRUUnit(5 * 3)
learning_rate = fluid.dygraph.CosineDecay( optimizer = fluid.optimizer.SGD(
base_lr, 10000, 120) ) learning_rate=fluid.dygraph.CosineDecay(
base_lr, 10000, 120), parameter_list=gru.parameters())
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册