You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/02/11 18:10:20 UTC
[GitHub] szha closed pull request #9759: add metrics tests for #9640
szha closed pull request #9759: add metrics tests for #9640
URL: https://github.com/apache/incubator-mxnet/pull/9759
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md
index bd5a9b4674..a4221c1f26 100644
--- a/CONTRIBUTORS.md
+++ b/CONTRIBUTORS.md
@@ -155,3 +155,4 @@ List of Contributors
* [Julian Salazar](https://github.com/JulianSlzr)
* [Meghna Baijal](https://github.com/mbaijal)
* [Tao Hu](https://github.com/dongzhuoyao)
+* [Sorokin Evgeniy](https://github.com/TheTweak)
diff --git a/tests/python/unittest/test_metric.py b/tests/python/unittest/test_metric.py
index 31f31e6e62..0f2f27f9eb 100644
--- a/tests/python/unittest/test_metric.py
+++ b/tests/python/unittest/test_metric.py
@@ -46,6 +46,48 @@ def test_nll_loss():
expected_loss = -(np.log(pred[0][2].asscalar()) + np.log(pred[1][1].asscalar())) / 2
assert loss == expected_loss
+def test_acc():
+ pred = mx.nd.array([[0.3, 0.7], [0, 1.], [0.4, 0.6]])
+ label = mx.nd.array([0, 1, 1])
+ metric = mx.metric.create('acc')
+ metric.update([label], [pred])
+ _, acc = metric.get()
+ expected_acc = (np.argmax(pred, axis=1) == label).sum().asscalar() / label.size
+ assert acc == expected_acc
+
+def test_f1():
+ pred = mx.nd.array([[0.3, 0.7], [1., 0], [0.4, 0.6], [0.6, 0.4], [0.9, 0.1]])
+ label = mx.nd.array([0, 1, 1, 1, 1])
+ positives = np.argmax(pred, axis=1).sum().asscalar()
+ true_positives = (np.argmax(pred, axis=1) == label).sum().asscalar()
+ precision = true_positives / positives
+ overall_positives = label.sum().asscalar()
+ recall = true_positives / overall_positives
+ f1_expected = 2 * (precision * recall) / (precision + recall)
+ metric = mx.metric.create('f1')
+ metric.update([label], [pred])
+ _, f1 = metric.get()
+ assert f1 == f1_expected
+
+def test_perplexity():
+ pred = mx.nd.array([[0.8, 0.2], [0.2, 0.8], [0, 1.]])
+ label = mx.nd.array([0, 1, 1])
+ p = pred.asnumpy()[np.arange(label.size), label.asnumpy().astype('int32')]
+ perplexity_expected = np.exp(-np.log(p).sum()/label.size)
+ metric = mx.metric.create('perplexity', -1)
+ metric.update([label], [pred])
+ _, perplexity = metric.get()
+ assert perplexity == perplexity_expected
+
+def test_pearsonr():
+ pred = mx.nd.array([[0.7, 0.3], [0.1, 0.9], [1., 0]])
+ label = mx.nd.array([[0, 1], [1, 0], [1, 0]])
+ pearsonr_expected = np.corrcoef(pred.asnumpy().ravel(), label.asnumpy().ravel())[0, 1]
+ metric = mx.metric.create('pearsonr')
+ metric.update([label], [pred])
+ _, pearsonr = metric.get()
+ assert pearsonr == pearsonr_expected
+
if __name__ == '__main__':
import nose
nose.runmodule()
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services