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/01/12 20:21:27 UTC

[GitHub] sxjscience opened a new pull request #9404: Use a better formula to calculate sigmoid_bce and logistic_loss

sxjscience opened a new pull request #9404: Use a better formula to calculate sigmoid_bce and logistic_loss
URL: https://github.com/apache/incubator-mxnet/pull/9404
 
 
   ## Description ##
   Use the same formula to calculate the BCE as TF https://github.com/tensorflow/tensorflow/blob/r1.1/tensorflow/python/ops/nn_impl.py#L128
   
   The formula is simpler than the old one and is more accurate.
   
   ```python
   import mxnet as mx
   import mxnet.ndarray as nd
   
   def sigmoid_bce1(pred, label):
       max_val = nd.relu(-pred)
       loss = pred - pred*label + max_val + nd.log(nd.exp(-max_val)+nd.exp(-pred-max_val))
       return loss.asscalar()
   
   def sigmoid_bce2(pred, label):
       loss = nd.relu(pred) - pred * label + nd.Activation(-nd.abs(pred), act_type='softrelu')
       return loss.asscalar()
   
   for x, label in [(-100, 0), (-50, 0), (-20, 0), (-10, 0), (-1, 0), (1, 1), (10, 1), (20, 1), (50, 1), (100, 1)]:
       x_nd = nd.array([x], ctx=mx.gpu())
       label_nd = nd.array([label], ctx=mx.gpu())
       val1 = sigmoid_bce1(x_nd, label_nd)
       val2 = sigmoid_bce2(x_nd, label_nd)
       print("Formula1=", val1, "Formula2=", val2)
   ```
   
   ## Checklist ##
   ### Essentials ###
   - [x] Passed code style checking (`make lint`)
   - [x] Changes are complete (i.e. I finished coding on this PR)
   - [x] All changes have test coverage:
   - [x] Code is well-documented: 
   - For user-facing API changes, API doc string has been updated. 
   - For new C++ functions in header files, their functionalities and arguments are documented. 
   - For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
   - [x] To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change
   
   ### Changes ###
   - [x] Use `max(x, 0) - x * z + log(1 + exp(-abs(x)))` to calculate BCE + Test
   

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