Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions python/mxnet/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from .base import numeric_types, string_types
from . import ndarray
from . import registry
from .context import cpu


def check_label_shapes(labels, preds, shape=0):
Expand Down Expand Up @@ -388,6 +389,7 @@ def update(self, labels, preds):
"""
check_label_shapes(labels, preds)

results = []
for label, pred_label in zip(labels, preds):
if pred_label.shape != label.shape:
pred_label = ndarray.argmax(pred_label, axis=self.axis)
Expand All @@ -399,8 +401,10 @@ def update(self, labels, preds):
if pred_label.context != label.context:
pred_label = pred_label.as_in_context(label.context)

self.sum_metric += (pred_label.reshape((-1,)) == label.reshape((-1,))).sum().asscalar()
self.num_inst += numpy.prod(pred_label.shape)
self.num_inst += pred_label.size
results.append((pred_label.reshape((-1,)) == label.reshape((-1,)))
Copy link
Copy Markdown
Contributor

@piiswrong piiswrong Jan 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use add_n at the end instead of += on each iteration?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is for less kernel launch

.sum().as_in_context(cpu()))
self.sum_metric += ndarray.add_n(*results).asscalar()


@register
Expand Down