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/03/09 21:10:40 UTC

[GitHub] fhieber closed pull request #8027: Optional reshape of predictions in Perplexity metric

fhieber closed pull request #8027: Optional reshape of predictions in Perplexity metric
URL: https://github.com/apache/incubator-mxnet/pull/8027
 
 
   

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 1b192f233f6..51f52ab2476 100644
--- a/python/mxnet/metric.py
+++ b/python/mxnet/metric.py
@@ -646,9 +646,21 @@ def update(self, labels, preds):
         loss = 0.
         num = 0
         for label, pred in zip(labels, preds):
-            assert label.size == pred.size/pred.shape[-1], \
+            k = pred.shape[self.axis]
+            n = int(pred.size/k)
+            assert len(pred.shape) >= 2, \
+                "predictions must have at least 2 dimensions, got %s" % pred.shape
+            assert label.size == n, \
                 "shape mismatch: %s vs. %s"%(label.shape, pred.shape)
             label = label.as_in_context(pred.context).reshape((label.size,))
+
+            axis = self.axis if self.axis >= 0 else len(pred.shape) + self.axis
+            if axis != len(pred.shape) - 1: # if not last axis, swap:
+                # move softmax axis to last axis
+                pred = pred.swapaxes(pred, dim1=axis, dim2=len(pred.shape) - 1)
+
+            if len(pred.shape) > 2:
+                pred = pred.reshape((n, k))
             pred = ndarray.pick(pred, label.astype(dtype='int32'), axis=self.axis)
             if self.ignore_label is not None:
                 ignore = (label == self.ignore_label).astype(pred.dtype)


 

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