未验证 提交 2b1efc35 编写于 作者: W wawltor 提交者: GitHub

transfer the static.accurcay to v2 op (#35494)

* transfer the static.accurcay to v2 api

* remove the unused code
上级 28b64075
...@@ -84,7 +84,9 @@ def accuracy(input, label, k=1, correct=None, total=None): ...@@ -84,7 +84,9 @@ def accuracy(input, label, k=1, correct=None, total=None):
if total is None: if total is None:
total = _varbase_creator(dtype="int32") total = _varbase_creator(dtype="int32")
topk_out, topk_indices = nn.topk(input, k=k) _k = k.numpy().item(0) if isinstance(k, Variable) else k
topk_out, topk_indices = _C_ops.top_k_v2(input, 'k', _k, 'sorted',
False)
_acc, _, _ = _C_ops.accuracy(topk_out, topk_indices, label, correct, _acc, _, _ = _C_ops.accuracy(topk_out, topk_indices, label, correct,
total) total)
return _acc return _acc
...@@ -92,7 +94,20 @@ def accuracy(input, label, k=1, correct=None, total=None): ...@@ -92,7 +94,20 @@ def accuracy(input, label, k=1, correct=None, total=None):
helper = LayerHelper("accuracy", **locals()) helper = LayerHelper("accuracy", **locals())
check_variable_and_dtype(input, 'input', ['float16', 'float32', 'float64'], check_variable_and_dtype(input, 'input', ['float16', 'float32', 'float64'],
'accuracy') 'accuracy')
topk_out, topk_indices = nn.topk(input, k=k) topk_out = helper.create_variable_for_type_inference(dtype=input.dtype)
topk_indices = helper.create_variable_for_type_inference(dtype="int64")
inputs = {"X": [input]}
if isinstance(k, Variable):
inputs['K'] = [k]
else:
attrs = {'k': k}
attrs['sorted'] = False
helper.append_op(
type="top_k_v2",
inputs=inputs,
attrs=attrs,
outputs={"Out": [topk_out],
"Indices": [topk_indices]})
acc_out = helper.create_variable_for_type_inference(dtype="float32") acc_out = helper.create_variable_for_type_inference(dtype="float32")
if correct is None: if correct is None:
correct = helper.create_variable_for_type_inference(dtype="int32") correct = helper.create_variable_for_type_inference(dtype="int32")
......
...@@ -78,6 +78,42 @@ class TestAccuracyOpError(unittest.TestCase): ...@@ -78,6 +78,42 @@ class TestAccuracyOpError(unittest.TestCase):
paddle.metric.accuracy(input=x3, label=label) paddle.metric.accuracy(input=x3, label=label)
class TestAccuracyAPI1(unittest.TestCase):
def setUp(self):
self.predictions = paddle.static.data(
shape=[2, 5], name="predictions", dtype="float32")
self.label = paddle.static.data(
shape=[2, 1], name="labels", dtype="int64")
self.result = paddle.static.accuracy(
input=self.predictions, label=self.label, k=1)
self.input_predictions = np.array(
[[0.2, 0.1, 0.4, 0.1, 0.1], [0.2, 0.3, 0.1, 0.15, 0.25]],
dtype="float32")
self.input_labels = np.array([[2], [0]], dtype="int64")
self.expect_value = np.array([0.5], dtype='float32')
def test_api(self):
exe = paddle.static.Executor()
result, = exe.run(feed={
"predictions": self.input_predictions,
'labels': self.input_labels
},
fetch_list=[self.result.name])
self.assertEqual((result == self.expect_value).all(), True)
class TestAccuracyAPI2(unittest.TestCase):
def test_api(self):
with fluid.dygraph.guard():
predictions = paddle.to_tensor(
[[0.2, 0.1, 0.4, 0.1, 0.1], [0.2, 0.3, 0.1, 0.15, 0.25]],
dtype='float32')
label = paddle.to_tensor([[2], [0]], dtype="int64")
result = paddle.static.accuracy(input=predictions, label=label, k=1)
expect_value = np.array([0.5], dtype='float32')
self.assertEqual((result.numpy() == expect_value).all(), True)
class TestAccuracyAPI(unittest.TestCase): class TestAccuracyAPI(unittest.TestCase):
def test_api(self): def test_api(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册