未验证 提交 78ca8cf0 编写于 作者: Q qingqing01 提交者: GitHub

Unify the metrics implementation between low-level and high-level API. (#26158)

* Move paddle/incubate/hapi/metrics to paddle/metric
* Add Precision, Recall and Auc metric
上级 da1efe24
......@@ -91,6 +91,7 @@ set(PADDLE_PYTHON_PACKAGE_DIR ${CMAKE_CURRENT_BINARY_DIR}/dist/)
if (WITH_TESTING)
add_subdirectory(paddle/reader/tests)
add_subdirectory(paddle/dataset/tests)
add_subdirectory(paddle/tests)
add_subdirectory(paddle/fluid/tests)
add_subdirectory(paddle/fluid/contrib/tests)
add_subdirectory(paddle/fluid/contrib/slim/tests)
......
......@@ -3682,5 +3682,32 @@ class TestBook(LayerTest):
batch_first=batch_first)
class TestMetricsDetectionMap(unittest.TestCase):
def test_detection_map(self):
program = fluid.Program()
with program_guard(program):
detect_res = fluid.layers.data(
name='detect_res',
shape=[10, 6],
append_batch_size=False,
dtype='float32')
label = fluid.layers.data(
name='label',
shape=[10, 1],
append_batch_size=False,
dtype='float32')
box = fluid.layers.data(
name='bbox',
shape=[10, 4],
append_batch_size=False,
dtype='float32')
map_eval = fluid.metrics.DetectionMAP(
detect_res, label, box, class_num=21)
cur_map, accm_map = map_eval.get_map_var()
self.assertIsNotNone(cur_map)
self.assertIsNotNone(accm_map)
print(str(program))
if __name__ == '__main__':
unittest.main()
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import paddle.fluid as fluid
from paddle.fluid.framework import Program, program_guard
class TestMetricsDetectionMap(unittest.TestCase):
def test_detection_map(self):
program = fluid.Program()
with program_guard(program):
detect_res = fluid.layers.data(
name='detect_res',
shape=[10, 6],
append_batch_size=False,
dtype='float32')
label = fluid.layers.data(
name='label',
shape=[10, 1],
append_batch_size=False,
dtype='float32')
box = fluid.layers.data(
name='bbox',
shape=[10, 4],
append_batch_size=False,
dtype='float32')
map_eval = fluid.metrics.DetectionMAP(
detect_res, label, box, class_num=21)
cur_map, accm_map = map_eval.get_map_var()
self.assertIsNotNone(cur_map)
self.assertIsNotNone(accm_map)
print(str(program))
if __name__ == '__main__':
unittest.main()
......@@ -20,7 +20,6 @@ from . import download
from . import model
from .model import *
from . import metrics
from . import datasets
from . import distributed
from . import vision
......@@ -39,7 +38,6 @@ __all__ = [
'datasets',
'distributed',
'download',
'metrics',
'vision',
'text',
'utils',
......
......@@ -305,8 +305,8 @@ class ProgBarLogger(Callback):
optim = fluid.optimizer.Adam(0.001)
model.prepare(optimizer=optim,
loss_function=paddle.nn.CrossEntropyLoss(),
metrics=hapi.metrics.Accuracy())
loss=paddle.nn.CrossEntropyLoss(),
metrics=paddle.metric.Accuracy())
callback = hapi.callbacks.ProgBarLogger(log_freq=10)
model.fit(train_dataset, batch_size=64, callbacks=callback)
......@@ -441,8 +441,8 @@ class ModelCheckpoint(Callback):
optim = fluid.optimizer.Adam(0.001)
model.prepare(optimizer=optim,
loss_function=paddle.nn.CrossEntropyLoss(),
metrics=hapi.metrics.Accuracy())
loss=paddle.nn.CrossEntropyLoss(),
metrics=paddle.metric.Accuracy())
callback = hapi.callbacks.ModelCheckpoint(save_dir='./temp')
model.fit(train_dataset, batch_size=64, callbacks=callback)
......
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import six
import abc
import numpy as np
import paddle.fluid as fluid
import logging
FORMAT = '%(asctime)s-%(levelname)s: %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT)
logger = logging.getLogger(__name__)
__all__ = ['Metric', 'Accuracy']
@six.add_metaclass(abc.ABCMeta)
class Metric(object):
"""
Base class for metric, encapsulates metric logic and APIs
Usage:
m = SomeMetric()
for prediction, label in ...:
m.update(prediction, label)
m.accumulate()
Advanced usage for :code:`add_metric_op`
Metric calculation can be accelerated by calculating metric states
from model outputs and labels by Paddle OPs in :code:`add_metric_op`,
metric states will be fetch as numpy array and call :code:`update`
with states in numpy format.
Metric calculated as follows (operations in Model and Metric are
indicated with curly brackets, while data nodes not):
inputs & labels || ------------------
| ||
{model} ||
| ||
outputs & labels ||
| || tensor data
{Metric.add_metric_op} ||
| ||
metric states(tensor) ||
| ||
{fetch as numpy} || ------------------
| ||
metric states(numpy) || numpy data
| ||
{Metric.update} \/ ------------------
Examples:
For :code:`Accuracy` metric, which takes :code:`pred` and :code:`label`
as inputs, we can calculate the correct prediction matrix between
:code:`pred` and :code:`label` in :code:`add_metric_op`.
For examples, prediction results contains 10 classes, while :code:`pred`
shape is [N, 10], :code:`label` shape is [N, 1], N is mini-batch size,
and we only need to calculate accurary of top-1 and top-5, we could
calculated the correct prediction matrix of the top-5 scores of the
prediction of each sample like follows, while the correct prediction
matrix shape is [N, 5].
.. code-block:: python
def add_metric_op(pred, label):
# sort prediction and slice the top-5 scores
pred = fluid.layers.argsort(pred, descending=True)[1][:, :5]
# calculate whether the predictions are correct
correct = pred == label
return fluid.layers.cast(correct, dtype='float32')
With the :code:`add_metric_op`, we split some calculations to OPs(which
may run on GPU devices, will be faster), and only fetch 1 tensor with
shape as [N, 5] instead of 2 tensors with shapes as [N, 10] and [N, 1].
:code:`update` can be define as follows:
.. code-block:: python
def update(self, correct):
accs = []
for i, k in enumerate(self.topk):
num_corrects = correct[:, :k].sum()
num_samples = len(correct)
accs.append(float(num_corrects) / num_samples)
self.total[i] += num_corrects
self.count[i] += num_samples
return accs
"""
def __init__(self):
pass
@abc.abstractmethod
def reset(self):
"""
Reset states and result
"""
raise NotImplementedError("function 'reset' not implemented in {}.".
format(self.__class__.__name__))
@abc.abstractmethod
def update(self, *args):
"""
Update states for metric
Inputs of :code:`update` is the outputs of :code:`Metric.add_metric_op`,
if :code:`add_metric_op` is not defined, the inputs of :code:`update`
will be flatten arguments of **output** of mode and **label** from data:
:code:`update(output1, output2, ..., label1, label2,...)`
see :code:`Metric.add_metric_op`
"""
raise NotImplementedError("function 'update' not implemented in {}.".
format(self.__class__.__name__))
@abc.abstractmethod
def accumulate(self):
"""
Accumulates statistics, computes and returns the metric value
"""
raise NotImplementedError(
"function 'accumulate' not implemented in {}.".format(
self.__class__.__name__))
@abc.abstractmethod
def name(self):
"""
Returns metric name
"""
raise NotImplementedError("function 'name' not implemented in {}.".
format(self.__class__.__name__))
def add_metric_op(self, *args):
"""
This API is advanced usage to accelerate metric calculating, calulations
from outputs of model to the states which should be updated by Metric can
be defined here, where Paddle OPs is also supported. Outputs of this API
will be the inputs of "Metric.update".
If :code:`add_metric_op` is defined, it will be called with **outputs**
of model and **labels** from data as arguments, all outputs and labels
will be concatenated and flatten and each filed as a separate argument
as follows:
:code:`add_metric_op(output1, output2, ..., label1, label2,...)`
If :code:`add_metric_op` is not defined, default behaviour is to pass
input to output, so output format will be:
:code:`return output1, output2, ..., label1, label2,...`
see :code:`Metric.update`
"""
return args
class Accuracy(Metric):
"""
Encapsulates accuracy metric logic
Examples:
.. code-block:: python
import paddle
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
fluid.enable_dygraph()
train_dataset = hapi.datasets.MNIST(mode='train')
model = hapi.Model(hapi.vision.LeNet(classifier_activation=None))
optim = fluid.optimizer.Adam(
learning_rate=0.001, parameter_list=model.parameters())
model.prepare(
optim,
loss_function=paddle.nn.CrossEntropyLoss(),
metrics=hapi.metrics.Accuracy())
model.fit(train_dataset, batch_size=64)
"""
def __init__(self, topk=(1, ), name=None, *args, **kwargs):
super(Accuracy, self).__init__(*args, **kwargs)
self.topk = topk
self.maxk = max(topk)
self._init_name(name)
self.reset()
def add_metric_op(self, pred, label, *args):
pred = fluid.layers.argsort(pred, descending=True)[1][:, :self.maxk]
correct = pred == label
return fluid.layers.cast(correct, dtype='float32')
def update(self, correct, *args):
accs = []
for i, k in enumerate(self.topk):
num_corrects = correct[:, :k].sum()
num_samples = len(correct)
accs.append(float(num_corrects) / num_samples)
self.total[i] += num_corrects
self.count[i] += num_samples
return accs
def reset(self):
self.total = [0.] * len(self.topk)
self.count = [0] * len(self.topk)
def accumulate(self):
res = []
for t, c in zip(self.total, self.count):
res.append(float(t) / c)
return res
def _init_name(self, name):
name = name or 'acc'
if self.maxk != 1:
self._name = ['{}_top{}'.format(name, k) for k in self.topk]
else:
self._name = [name]
def name(self):
return self._name
......@@ -25,7 +25,7 @@ from paddle import fluid
from paddle.incubate.hapi import Model, Input, set_device
from paddle.nn.layer.loss import CrossEntropyLoss
from paddle.incubate.hapi.vision.models import LeNet
from paddle.incubate.hapi.metrics import Accuracy
from paddle.metric import Accuracy
from paddle.incubate.hapi.callbacks import ProgBarLogger
from paddle.incubate.hapi.datasets import MNIST
......
......@@ -25,7 +25,7 @@ from paddle import fluid
from paddle.incubate.hapi import Model, Input, set_device
from paddle.nn.layer.loss import CrossEntropyLoss
from paddle.incubate.hapi.vision.models import LeNet
from paddle.incubate.hapi.metrics import Accuracy
from paddle.metric import Accuracy
from paddle.incubate.hapi.callbacks import ProgBarLogger
from paddle.incubate.hapi.datasets import MNIST
......
......@@ -29,7 +29,7 @@ from paddle.fluid.dygraph.base import to_variable
import paddle.incubate.hapi as hapi
from paddle.incubate.hapi import Model, Input
from paddle.nn.layer.loss import CrossEntropyLoss
from paddle.incubate.hapi.metrics import Accuracy
from paddle.metric import Accuracy
from paddle.incubate.hapi.datasets import MNIST
from paddle.incubate.hapi.vision.models import LeNet
from paddle.incubate.hapi.distributed import DistributedBatchSampler, prepare_distributed_context
......@@ -202,7 +202,7 @@ class TestModel(unittest.TestCase):
model = Model(net, inputs=self.inputs, labels=self.labels)
model.prepare(
optim_new,
loss_function=CrossEntropyLoss(reduction="sum"),
loss=CrossEntropyLoss(reduction="sum"),
metrics=Accuracy())
model.fit(self.train_dataset, batch_size=64, shuffle=False)
......@@ -333,8 +333,7 @@ class TestModelFunction(unittest.TestCase):
inputs = [Input([None, dim], 'float32', 'x')]
labels = [Input([None, 1], 'int64', 'label')]
model = Model(net, inputs, labels)
model.prepare(
optim2, loss_function=CrossEntropyLoss(reduction="sum"))
model.prepare(optim2, loss=CrossEntropyLoss(reduction="sum"))
loss, = model.train_batch([data], [label])
np.testing.assert_allclose(loss.flatten(), ref.flatten())
......@@ -379,8 +378,7 @@ class TestModelFunction(unittest.TestCase):
parameter_list=net.parameters())
model = Model(net, inputs, labels)
model.prepare(
optimizer=optim,
loss_function=CrossEntropyLoss(reduction="sum"))
optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
model.save(path + '/test')
model.load(path + '/test')
shutil.rmtree(path)
......@@ -394,8 +392,7 @@ class TestModelFunction(unittest.TestCase):
model = Model(MyModel(classifier_activation=None))
optim = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=model.parameters())
model.prepare(
optimizer=optim, loss_function=CrossEntropyLoss(reduction="sum"))
model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
model.save(path + '/test')
fluid.disable_dygraph()
......@@ -404,8 +401,7 @@ class TestModelFunction(unittest.TestCase):
model = Model(MyModel(classifier_activation=None), inputs, labels)
optim = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=model.parameters())
model.prepare(
optimizer=optim, loss_function=CrossEntropyLoss(reduction="sum"))
model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
model.load(path + '/test')
shutil.rmtree(path)
......@@ -418,8 +414,7 @@ class TestModelFunction(unittest.TestCase):
optim = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=net.parameters())
model = Model(net, inputs, labels)
model.prepare(
optimizer=optim, loss_function=CrossEntropyLoss(reduction="sum"))
model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
model.save(path + '/test')
device = hapi.set_device('cpu')
......@@ -431,8 +426,7 @@ class TestModelFunction(unittest.TestCase):
optim = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=net.parameters())
model = Model(net, inputs, labels)
model.prepare(
optimizer=optim, loss_function=CrossEntropyLoss(reduction="sum"))
model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
model.load(path + '/test')
shutil.rmtree(path)
fluid.disable_dygraph()
......
......@@ -12,17 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# TODO: define the functions to calculate metric in this directory
__all__ = [
'Accuracy', 'Auc', 'ChunkEvaluator', 'CompositeMetric', 'DetectionMAP',
'EditDistance', 'Precision', 'Recall', 'accuracy', 'auc', 'chunk_eval',
'cos_sim', 'mean_iou'
]
from ..fluid.metrics import Accuracy, Auc, ChunkEvaluator, CompositeMetric, DetectionMAP, EditDistance, \
Precision, Recall
from .metrics import *
from . import metrics
from ..fluid.layers.metric_op import accuracy, auc
from ..fluid.layers.nn import chunk_eval, cos_sim, mean_iou
__all__ = metrics.__all__ + [
'accuracy',
'auc',
'chunk_eval',
'cos_sim',
'mean_iou',
]
此差异已折叠。
file(GLOB TEST_OPS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "test_*.py")
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")
foreach(src ${TEST_OPS})
py_test(${src} SRCS ${src}.py)
endforeach()
......@@ -19,10 +19,9 @@ import os
import unittest
import numpy as np
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.base import to_variable
from paddle.incubate.hapi.metrics import *
from paddle.incubate.hapi.utils import to_list
......@@ -35,7 +34,7 @@ def accuracy(pred, label, topk=(1, )):
res = []
for k in topk:
correct_k = correct[:, :k].sum()
res.append(correct_k / batch_size)
res.append(float(correct_k) / batch_size)
return res
......@@ -47,6 +46,41 @@ def convert_to_one_hot(y, C):
return oh
class TestAccuracy(unittest.TestCase):
def test_acc(self):
paddle.disable_static()
x = paddle.to_tensor(
np.array([[0.1, 0.2, 0.3, 0.4], [0.1, 0.4, 0.3, 0.2],
[0.1, 0.2, 0.4, 0.3], [0.1, 0.2, 0.3, 0.4]]))
y = paddle.to_tensor(np.array([[0], [1], [2], [3]]))
m = paddle.metric.Accuracy(name='my_acc')
# check name
self.assertEqual(m.name(), ['my_acc'])
correct = m.compute(x, y)
# check results
self.assertEqual(m.update(correct), 0.75)
self.assertEqual(m.accumulate(), 0.75)
x = paddle.to_tensor(
np.array([[0.1, 0.2, 0.3, 0.4], [0.1, 0.3, 0.4, 0.2],
[0.1, 0.2, 0.4, 0.3], [0.1, 0.2, 0.3, 0.4]]))
y = paddle.to_tensor(np.array([[0], [1], [2], [3]]))
correct = m.compute(x, y)
# check results
self.assertEqual(m.update(correct), 0.5)
self.assertEqual(m.accumulate(), 0.625)
# check reset
m.reset()
self.assertEqual(m.total[0], 0.0)
self.assertEqual(m.count[0], 0.0)
paddle.enable_static()
class TestAccuracyDynamic(unittest.TestCase):
def setUp(self):
self.topk = (1, )
......@@ -66,17 +100,18 @@ class TestAccuracyDynamic(unittest.TestCase):
def test_main(self):
with fluid.dygraph.guard(fluid.CPUPlace()):
acc = Accuracy(topk=self.topk, name=self.name)
acc = paddle.metric.Accuracy(topk=self.topk, name=self.name)
for _ in range(10):
label, pred = self.random_pred_label()
label_var = to_variable(label)
pred_var = to_variable(pred)
state = to_list(acc.add_metric_op(pred_var, label_var))
label_var = paddle.to_tensor(label)
pred_var = paddle.to_tensor(pred)
state = to_list(acc.compute(pred_var, label_var))
acc.update(* [s.numpy() for s in state])
res_m = acc.accumulate()
res_f = accuracy(pred, label, self.topk)
assert np.all(np.isclose(np.array(res_m, dtype='float64'), np.array(res_f, dtype='float64'), rtol=1e-3)), \
"Accuracy precision error: {} != {}".format(res_m, res_f)
assert np.all(np.isclose(np.array(res_m, dtype='float64'),
np.array(res_f, dtype='float64'), rtol=1e-3)), \
"Accuracy precision error: {} != {}".format(res_m, res_f)
acc.reset()
assert np.sum(acc.total) == 0
assert np.sum(acc.count) == 0
......@@ -94,12 +129,14 @@ class TestAccuracyStatic(TestAccuracyDynamic):
def test_main(self):
main_prog = fluid.Program()
startup_prog = fluid.Program()
main_prog.random_seed = 1024
startup_prog.random_seed = 1024
with fluid.program_guard(main_prog, startup_prog):
pred = fluid.data(
name='pred', shape=[None, self.class_num], dtype='float32')
label = fluid.data(name='label', shape=[None, 1], dtype='int64')
acc = Accuracy(topk=self.topk, name=self.name)
state = acc.add_metric_op(pred, label)
acc = paddle.metric.Accuracy(topk=self.topk, name=self.name)
state = acc.compute(pred, label)
exe = fluid.Executor(fluid.CPUPlace())
compiled_main_prog = fluid.CompiledProgram(main_prog)
......@@ -114,7 +151,7 @@ class TestAccuracyStatic(TestAccuracyDynamic):
acc.update(*state_ret)
res_m = acc.accumulate()
res_f = accuracy(pred, label, self.topk)
assert np.all(np.isclose(np.array(res_m, dtype='float64'), np.array(res_f, dtype='float64'), rtol=1e-3)), \
assert np.all(np.isclose(np.array(res_m), np.array(res_f), rtol=1e-3)), \
"Accuracy precision error: {} != {}".format(res_m, res_f)
acc.reset()
assert np.sum(acc.total) == 0
......@@ -125,9 +162,114 @@ class TestAccuracyStaticMultiTopk(TestAccuracyStatic):
def setUp(self):
self.topk = (1, 5)
self.class_num = 10
self.sample_num = 1000
self.sample_num = 100
self.name = "accuracy"
class TestPrecision(unittest.TestCase):
def test_1d(self):
paddle.disable_static()
x = np.array([0.1, 0.5, 0.6, 0.7])
y = np.array([1, 0, 1, 1])
m = paddle.metric.Precision()
m.update(x, y)
r = m.accumulate()
self.assertAlmostEqual(r, 2. / 3.)
x = paddle.to_tensor(np.array([0.1, 0.5, 0.6, 0.7, 0.2]))
y = paddle.to_tensor(np.array([1, 0, 1, 1, 1]))
m.update(x, y)
r = m.accumulate()
self.assertAlmostEqual(r, 4. / 6.)
paddle.enable_static()
def test_2d(self):
paddle.disable_static()
x = np.array([0.1, 0.5, 0.6, 0.7]).reshape(-1, 1)
y = np.array([1, 0, 1, 1]).reshape(-1, 1)
m = paddle.metric.Precision()
m.update(x, y)
r = m.accumulate()
self.assertAlmostEqual(r, 2. / 3.)
x = np.array([0.1, 0.5, 0.6, 0.7, 0.2]).reshape(-1, 1)
y = np.array([1, 0, 1, 1, 1]).reshape(-1, 1)
m.update(x, y)
r = m.accumulate()
self.assertAlmostEqual(r, 4. / 6.)
# check reset
m.reset()
self.assertEqual(m.tp, 0.0)
self.assertEqual(m.fp, 0.0)
self.assertEqual(m.accumulate(), 0.0)
paddle.enable_static()
class TestRecall(unittest.TestCase):
def test_1d(self):
paddle.disable_static()
x = np.array([0.1, 0.5, 0.6, 0.7])
y = np.array([1, 0, 1, 1])
m = paddle.metric.Recall()
m.update(x, y)
r = m.accumulate()
self.assertAlmostEqual(r, 2. / 3.)
x = paddle.to_tensor(np.array([0.1, 0.5, 0.6, 0.7]))
y = paddle.to_tensor(np.array([1, 0, 0, 1]))
m.update(x, y)
r = m.accumulate()
self.assertAlmostEqual(r, 3. / 5.)
# check reset
m.reset()
self.assertEqual(m.tp, 0.0)
self.assertEqual(m.fn, 0.0)
self.assertEqual(m.accumulate(), 0.0)
paddle.enable_static()
class TestAuc(unittest.TestCase):
def test_auc_numpy(self):
paddle.disable_static()
x = np.array([[0.78, 0.22], [0.62, 0.38], [0.55, 0.45], [0.30, 0.70],
[0.14, 0.86], [0.59, 0.41], [0.91, 0.08], [0.16, 0.84]])
y = np.array([[0], [1], [1], [0], [1], [0], [0], [1]])
m = paddle.metric.Auc()
m.update(x, y)
r = m.accumulate()
self.assertAlmostEqual(r, 0.8125)
m.reset()
self.assertEqual(m.accumulate(), 0.0)
paddle.enable_static()
def test_auc_tensor(self):
paddle.disable_static()
x = paddle.to_tensor(
np.array([[0.78, 0.22], [0.62, 0.38], [0.55, 0.45], [0.30, 0.70],
[0.14, 0.86], [0.59, 0.41], [0.91, 0.08], [0.16, 0.84]]))
y = paddle.to_tensor(np.array([[0], [1], [1], [0], [1], [0], [0], [1]]))
m = paddle.metric.Auc()
m.update(x, y)
r = m.accumulate()
self.assertAlmostEqual(r, 0.8125)
m.reset()
self.assertEqual(m.accumulate(), 0.0)
paddle.enable_static()
if __name__ == '__main__':
unittest.main()
......@@ -106,12 +106,27 @@
"Metric.update",
"Metric.accumulate",
"Metric.name",
"Metric.add_metric_op",
"Metric.compute",
"Accuracy.reset",
"Accuracy.update",
"Accuracy.accumulate",
"Accuracy.name",
"Accuracy.add_metric_op",
"Accuracy.compute",
"Precision.reset",
"Precision.update",
"Precision.accumulate",
"Precision.name",
"Precision.compute",
"Recall.reset",
"Recall.update",
"Recall.accumulate",
"Recall.name",
"Recall.compute",
"Auc.reset",
"Auc.update",
"Auc.accumulate",
"Auc.name",
"Auc.compute",
"Callback.set_params",
"Callback.on_train_begin",
"Callback.on_train_end",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册