Skip to content

Commit

Permalink
[MXNET-173]fix acc metric shape miss match (apache#10446)
Browse files Browse the repository at this point in the history
* fix acc metric shape miss match

* add unit test

* fix style

* fix python2 division
  • Loading branch information
roywei authored and hetong007 committed Apr 11, 2018
1 parent fea2548 commit d0ab1aa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions python/mxnet/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/python/unittest/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit d0ab1aa

Please sign in to comment.