Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
FluidDoc
提交
c5661840
F
FluidDoc
项目概览
PaddlePaddle
/
FluidDoc
通知
10
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
23
列表
看板
标记
里程碑
合并请求
111
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
FluidDoc
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
23
Issue
23
列表
看板
标记
里程碑
合并请求
111
合并请求
111
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c5661840
编写于
6月 27, 2019
作者:
L
liuwei1031
提交者:
xsrobin
6月 27, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
improve the doc of metrics introduction (#881)
* improve the doc of metrics introduction * tweak the doc of metrics introduction
上级
42d84a8d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
44 addition
and
37 deletion
+44
-37
doc/fluid/user_guides/howto/evaluation_and_debugging/evaluation/metrics.rst
...des/howto/evaluation_and_debugging/evaluation/metrics.rst
+44
-37
未找到文件。
doc/fluid/user_guides/howto/evaluation_and_debugging/evaluation/metrics.rst
浏览文件 @
c5661840
...
...
@@ -2,58 +2,65 @@
模型评估
############
模型评估是指用评价
指标(metrics)反映模型在预期目标下的精度,可作为在训练中调整超参数、评估模型效果的重要依据。其中,评价指标根据模型任务决定,也被称为评价函数。评价函数和loss函数非常相似,但不参与模型的训练优化
。
模型评估是指用评价
函数(metrics)来评估模型的好坏,可作为在训练中调整超参数、评估模型效果的重要依据。不同类型的模型任务会选取不同评价函数,常见的如回归类任务会用均方差(MSE),二分类任务会用AUC (Area Under Curve)值等
。
评价函数的输入为模型的预测值(preds)和真实值(labels),返回评价值。
paddle.fluid.metrics模块提供了一系列常用的模型评价指标; 用户也可以方便的通过Python定制评价指标,或者是通过定制C++ Operator的方式,在GPU上加速评价指标的计算。
评价函数和loss函数非常相似,但不参与模型的训练优化。
评价函数的输入为模型的预测值(preds)和标注值(labels),并返回计算后的评价指标。
paddle.fluid.metrics模块提供了一系列常用的模型评价指标; 用户也可以通过Python接口定制评价指标,或者通过定制C++ Operator的方式,在GPU上加速评价指标的计算。
常用指标
############
根据不同的任务,会选用不同的评价指标。
不同类型的任务,会选用不同的评价指标。
回归问题通常会用RMSE(均方根误差),MAE(平均绝对误差),R-Square(R平方)等
AUC(Area Under Cure)指标则常被用在分类任务(classification)上
目标检测任务(Object Detection)则经常会用到mAP(Mean Average Precision)
paddle.fluid.metrics中包含了一些常用分类指标,例如Precision, Recall, Accuracy等
回归任务labels是实数,评价指标可参考 MSE (Mean Squared Error) 方法。
分类任务常用指标为分类指标(classification metrics),本文提到的一般是二分类指标,多分类(multi-category)和多标签(multi-label)任务的评价指标需要查看对应的API文档。例如排序指标AUC可以同时用在二分类和多分类任务中,因为多分类任务可以转化为二分类任务。
Fluid中包含了常用分类指标,例如Precision, Recall, Accuracy等,更多请阅读API文档。以 :ref:`Precision` 为例,具体方法为
下面是使用Precision指标的示例:
.. code-block:: python
>>> import paddle.fluid as fluid
>>> label = fluid.layers.data(name="label", shape=[1], dtype="int32")
>>> data = fluid.layers.data(name="data", shape=[32, 32], dtype="int32")
>>> pred = fluid.layers.fc(input=data, size=1000, act="tanh")
>>> acc = fluid.metrics.Precision()
>>> for pass_iter in range(PASSES):
>>> acc.reset()
>>> for data in train_reader():
>>> loss, preds, labels = exe.run(fetch_list=[cost, pred, label])
>>> acc.update(preds=preds, labels=labels)
>>> numpy_acc = acc.eval()
其他任务例如MultiTask Learning,Metric Learning,Learning To Rank各种指标构造方法请参考API文档。
import paddle.fluid as fluid
label = fluid.layers.data(name="label", shape=[1], dtype="int32")
data = fluid.layers.data(name="data", shape=[32, 32], dtype="int32")
pred = fluid.layers.fc(input=data, size=1000, act="tanh")
acc = fluid.metrics.Precision()
for pass_iter in range(PASSES):
acc.reset()
for data in train_reader():
loss, preds, labels = exe.run(fetch_list=[cost, pred, label])
acc.update(preds=preds, labels=labels)
numpy_acc = acc.eval()
自定义指标
############
Fluid支持自定义指标,灵活支持各类计算任务。下文通过一个简单的计数器metric函数,实现对模型的评估。
其中preds是模型预测值,labels是给定的标签。
Fluid支持自定义指标,可灵活支持各类计算任务。下面是一个自定义的简单计数器评价函数示例:
其中preds是模型预测值,labels是标注值。
.. code-block:: python
>>>
class MyMetric(MetricBase):
>>>
def __init__(self, name=None):
>>>
super(MyMetric, self).__init__(name)
>>>
self.counter = 0 # simple counter
class MyMetric(MetricBase):
def __init__(self, name=None):
super(MyMetric, self).__init__(name)
self.counter = 0 # simple counter
>>>
def reset(self):
>>>
self.counter = 0
def reset(self):
self.counter = 0
>>>
def update(self, preds, labels):
>>>
if not _is_numpy_(preds):
>>>
raise ValueError("The 'preds' must be a numpy ndarray.")
>>>
if not _is_numpy_(labels):
>>>
raise ValueError("The 'labels' must be a numpy ndarray.")
>>>
self.counter += sum(preds == labels)
def update(self, preds, labels):
if not _is_numpy_(preds):
raise ValueError("The 'preds' must be a numpy ndarray.")
if not _is_numpy_(labels):
raise ValueError("The 'labels' must be a numpy ndarray.")
self.counter += sum(preds == labels)
>>>
def eval(self):
>>>
return self.counter
def eval(self):
return self.counter
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录