mxnet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] hetong007 closed pull request #10446: [MXNET-173]fix acc metric shape miss match
Date Wed, 11 Apr 2018 00:27:38 GMT
hetong007 closed pull request #10446: [MXNET-173]fix acc metric shape miss match
URL: https://github.com/apache/incubator-mxnet/pull/10446
 
 
   

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/python/mxnet/metric.py b/python/mxnet/metric.py
index d8facbc1bb4..152cbc1bb42 100644
--- a/python/mxnet/metric.py
+++ b/python/mxnet/metric.py
@@ -417,11 +417,14 @@ def update(self, labels, preds):
                 pred_label = ndarray.argmax(pred_label, axis=self.axis)
             pred_label = pred_label.asnumpy().astype('int32')
             label = label.asnumpy().astype('int32')
+            # flatten before checking shapes to avoid shape miss match
+            label = label.flat
+            pred_label = pred_label.flat
 
             labels, preds = check_label_shapes(label, pred_label)
 
-            self.sum_metric += (pred_label.flat == label.flat).sum()
-            self.num_inst += len(pred_label.flat)
+            self.sum_metric += (pred_label == label).sum()
+            self.num_inst += len(pred_label)
 
 
 @register
diff --git a/tests/python/unittest/test_metric.py b/tests/python/unittest/test_metric.py
index bcb0e2d9bf8..1571a0b2428 100644
--- a/tests/python/unittest/test_metric.py
+++ b/tests/python/unittest/test_metric.py
@@ -54,6 +54,17 @@ def test_acc():
     expected_acc = (np.argmax(pred, axis=1) == label).sum().asscalar() / label.size
     assert acc == expected_acc
 
+def test_acc_2d_label():
+    # label maybe provided in 2d arrays in custom data iterator
+    pred = mx.nd.array([[0.3, 0.7], [0, 1.], [0.4, 0.6], [0.8, 0.2], [0.3, 0.5], [0.6, 0.4]])
+    label = mx.nd.array([[0, 1, 1], [1, 0, 1]])
+    metric = mx.metric.create('acc')
+    metric.update([label], [pred])
+    _, acc = metric.get()
+    expected_acc = (np.argmax(pred, axis=1).asnumpy() == label.asnumpy().ravel()).sum() /
\
+                   float(label.asnumpy().ravel().size)
+    assert acc == expected_acc
+
 def test_f1():
     microF1 = mx.metric.create("f1", average="micro")
     macroF1 = mx.metric.F1(average="macro")


 

----------------------------------------------------------------
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

Mime
View raw message