You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by gi...@git.apache.org on 2017/10/09 22:08:55 UTC
[GitHub] szha commented on a change in pull request #8193: add center loss
szha commented on a change in pull request #8193: add center loss
URL: https://github.com/apache/incubator-mxnet/pull/8193#discussion_r143588676
##########
File path: python/mxnet/gluon/loss.py
##########
@@ -696,3 +696,35 @@ def hybrid_forward(self, F, pred, positive, negative):
axis=self._batch_axis, exclude=True)
loss = F.relu(loss + self._margin)
return _apply_weighting(F, loss, self._weight, None)
+
+
+class CenterLoss(Loss):
+ r""" Center Loss
+ """
+ def __init__(self, num_classes, feature_size, ctx, alpha=0.5, lmbd=0.01, weight=1., batch_axis=0, **kwargs):
+ super(CenterLoss, self).__init__(weight*lmbd, batch_axis, **kwargs)
+ self._alpha = alpha
+ self._lmbd = lmbd
+ self._num_classes = num_classes
+ self._feature_size = feature_size
+ self._ctx = ctx
+ self._centers = nd.zeros((num_classes, feature_size), ctx=ctx)
+
+ def hybrid_forward(self, F, output, label, sample_weight=None):
+ selected_centers = F.take(self._centers, label)
+ diff = output - selected_centers
+ loss = F.sum(F.square(diff), axis=1)
+
+ loss = _apply_weighting(F, loss, self._weight, sample_weight)
+ # Update the centers
+ counts = nd.zeros((self._num_classes, ), ctx=self._ctx)
Review comment:
What are you referring to? Here if you explicitly use ndarray, the hybridized version or the symbolic version of training won't work properly, hence it's discouraged to use ndarray-specific functionality in HybridBlocks. Loss class is a child class of HybridBlock.
----------------------------------------------------------------
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