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 2019/03/02 23:36:11 UTC

[GitHub] wkcn commented on a change in pull request #13612: add pos_weight for SigmoidBinaryCrossEntropyLoss

wkcn commented on a change in pull request #13612: add pos_weight for SigmoidBinaryCrossEntropyLoss
URL: https://github.com/apache/incubator-mxnet/pull/13612#discussion_r261846119
 
 

 ##########
 File path: python/mxnet/gluon/loss.py
 ##########
 @@ -227,13 +234,14 @@ def __init__(self, from_sigmoid=False, weight=None, batch_axis=0, **kwargs):
         super(SigmoidBinaryCrossEntropyLoss, self).__init__(weight, batch_axis, **kwargs)
         self._from_sigmoid = from_sigmoid
 
-    def hybrid_forward(self, F, pred, label, sample_weight=None):
+    def hybrid_forward(self, F, pred, label, pos_weight=1, sample_weight=None):
         label = _reshape_like(F, label, pred)
         if not self._from_sigmoid:
             # We use the stable formula: max(x, 0) - x * z + log(1 + exp(-abs(x)))
 
 Review comment:
   pos_weight will drop the performance when it is equal to 1.
   Could you please add the following if-else statement?
   ```python
               if pos_weight == 1:
                   # We use the stable formula: max(x, 0) - x * z + log(1 + exp(-abs(x)))
                   loss = F.relu(pred) - pred * label + F.Activation(-F.abs(pred), act_type='softrelu')
               else:
                   # We use the stable formula: x - x * z + (1 + z * pos_weight - z) * (log(1 + exp(-abs(x))) + max(-x, 0))
                   log_weight = 1 + (pos_weight - 1) * label
                   loss = pred - pred*label + log_weight*(F.Activation(-F.abs(pred), act_type='softrelu') + F.relu(-pred))
   ```
   
   Thanks!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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