Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

add pos_weight for SigmoidBinaryCrossEntropyLoss #13612

Merged
merged 25 commits into from
Mar 8, 2019
Merged

Conversation

eureka7mt
Copy link
Contributor

@eureka7mt eureka7mt commented Dec 11, 2018

Description

Add pos_weight for SigmoidBinaryCrossEntropyLoss.
A value pos_weights > 1 decreases the false negative count, hence increasing the recall.
Conversely setting pos_weights < 1 decreases the false positive count and increases the precision.
This can be seen from the fact that pos_weight is introduced as a multiplicative coefficient for the positive targets term in the loss expression:
label * -log(sigmoid(pred)) * pos_weight + (1 - label) * -log(1 - sigmoid(pred))

It's adopted from tensorflow's implementation

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • 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
  • Check the API doc at http:https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

Comments

  • If this change is a backward incompatible change, why must this change be made.
  • Interesting edge cases to note here

@eureka7mt eureka7mt requested a review from szha as a code owner December 11, 2018 08:03
@roywei
Copy link
Member

roywei commented Dec 12, 2018

@eureka7mt Thanks for the contribution, could you add a unit test for this case?

@roywei
Copy link
Member

roywei commented Dec 12, 2018

@mxnet-label-bot add[Gluon, pr-awaiting-review]

@marcoabreu marcoabreu added Gluon pr-awaiting-review PR is waiting for code review labels Dec 12, 2018
add test
python/mxnet/gluon/loss.py Outdated Show resolved Hide resolved
python/mxnet/gluon/loss.py Show resolved Hide resolved
Copy link
Member

@anirudhacharya anirudhacharya left a comment

Choose a reason for hiding this comment

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

minor comment on the unit test and fix the CI failure.

Rest LGTM.

tests/python/unittest/test_loss.py Outdated Show resolved Hide resolved
@stu1130
Copy link
Contributor

stu1130 commented Jan 16, 2019

@eureka7mt Could you fix the Trailing whitespace issue?

@eureka7mt
Copy link
Contributor Author

Don't know why it failed in test_multinomial_generator() which is in the file '/work/mxnet/tests/python/gpu/../unittest/test_random.py' with unix-gpu.

@vandanavk
Copy link
Contributor

@eureka7mt could you re-trigger the CI?

@ankkhedia
Copy link
Contributor

@eureka7mt Could you please look into the CI failures?

@anirudhacharya
Copy link
Member

@mxnet-label-bot update [pr-awaiting-merge]

@marcoabreu marcoabreu added pr-awaiting-merge Review and CI is complete. Ready to Merge and removed Gluon pr-awaiting-review PR is waiting for code review labels Mar 1, 2019
python/mxnet/gluon/loss.py Outdated Show resolved Hide resolved
python/mxnet/gluon/loss.py Outdated Show resolved Hide resolved
CONTRIBUTORS.md Outdated Show resolved Hide resolved
Copy link
Member

@wkcn wkcn left a comment

Choose a reason for hiding this comment

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

We will merge it after the CI passes.
Thanks for your contribution!

python/mxnet/gluon/loss.py Outdated Show resolved Hide resolved
CONTRIBUTORS.md Outdated Show resolved Hide resolved
CONTRIBUTORS.md Outdated Show resolved Hide resolved
@wkcn wkcn added pr-awaiting-testing PR is reviewed and waiting CI build and test and removed pr-awaiting-merge Review and CI is complete. Ready to Merge labels Mar 7, 2019
@eureka7mt
Copy link
Contributor Author

eureka7mt commented Mar 7, 2019

Adding the if-else statement make an error.Though the default value of pos_weight is set to be 1,the pos_weight is usually an (1,N) NDArray.And it seems that an error happen in if-else statement when input is a symbol

@wkcn
Copy link
Member

wkcn commented Mar 7, 2019

@eureka7mt
I see. Since pos_weight is a tensor, it is better to default pos_weight=None as you wrote before. I will update it. Thanks!

Edit: I think the pos_weight is a scalar, since it is a binary classification loss.

@eureka7mt
Copy link
Contributor Author

@wkcn It could be a scalar for classifying a single class.But for multi-class and multi-label classifying,it should be a tensor.Because in this situation,the number of positive examples and negative examples isn't same for each class.
And in pytorch,it's also defined as a tensor.See the pytorch docs.

@wkcn
Copy link
Member

wkcn commented Mar 7, 2019

I change the order of SigmoidBinaryCrossEntropyLoss inputs from (self, F, pred, label, pos_weight=None, sample_weight=None) to (self, F, pred, label, sample_weight=None, pos_weight=None), since we need the compatibilty for other projects

@wkcn
Copy link
Member

wkcn commented Mar 7, 2019

Sorry that I trigger the sanity problem.
Could someone please help me solve it? Thanks!

@eureka7mt
Copy link
Contributor Author

Maybe the broadcast_mul isn't necessary.I think that a NDArray * a NDArray will do broadcast_mul automatically.

@wkcn
Copy link
Member

wkcn commented Mar 8, 2019

@eureka7mt I think we may pass Symbol into SigmoidBinaryCrossEntropyLoss. Symbol will not broadcast_mul automatically in my test.

import mxnet as mx
from mxnet.gluon import nn

class TestBlock(nn.HybridBlock):
    def __init__(self):
        super(TestBlock, self).__init__()
    def hybrid_forward(self, F, x, y):
        return x * y

block = TestBlock()
block.hybridize()
a = mx.nd.zeros((10, 1))
b = mx.nd.ones((1, 5))
c = block(a, b)
print (c.asnumpy())

@wkcn wkcn added pr-awaiting-merge Review and CI is complete. Ready to Merge and removed pr-awaiting-testing PR is reviewed and waiting CI build and test labels Mar 8, 2019
@wkcn wkcn merged commit ce9e3cf into apache:master Mar 8, 2019
@wkcn
Copy link
Member

wkcn commented Mar 8, 2019

The PR has been merged.
Thanks for your contribution: )

vdantu pushed a commit to vdantu/incubator-mxnet that referenced this pull request Mar 31, 2019
* add pos_weight for SigmoidBinaryCrossEntropyLoss in gluon.loss

* Update loss.py

* add test

add test

* set the default value of pos_weight to be 1

* fix unittest

* set N be a random number

* fix issues

* test without random number

* test with random N

* fix

* fix errors

* fix errors

* fix order

* Update loss.py

* Update loss.py

* fix pylint

* default pos_weight=None

* add broadcast_mul and fix pylint

* fix unittest

* Update loss.py

* Update loss.py

* Update loss.py
nswamy pushed a commit that referenced this pull request Apr 5, 2019
* add pos_weight for SigmoidBinaryCrossEntropyLoss in gluon.loss

* Update loss.py

* add test

add test

* set the default value of pos_weight to be 1

* fix unittest

* set N be a random number

* fix issues

* test without random number

* test with random N

* fix

* fix errors

* fix errors

* fix order

* Update loss.py

* Update loss.py

* fix pylint

* default pos_weight=None

* add broadcast_mul and fix pylint

* fix unittest

* Update loss.py

* Update loss.py

* Update loss.py
haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
* add pos_weight for SigmoidBinaryCrossEntropyLoss in gluon.loss

* Update loss.py

* add test

add test

* set the default value of pos_weight to be 1

* fix unittest

* set N be a random number

* fix issues

* test without random number

* test with random N

* fix

* fix errors

* fix errors

* fix order

* Update loss.py

* Update loss.py

* fix pylint

* default pos_weight=None

* add broadcast_mul and fix pylint

* fix unittest

* Update loss.py

* Update loss.py

* Update loss.py
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pr-awaiting-merge Review and CI is complete. Ready to Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants