-
Notifications
You must be signed in to change notification settings - Fork 102
Add tensorflow adapted ASL #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mooster1
wants to merge
1
commit into
Alibaba-MIIL:main
Choose a base branch
from
Mooster1:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import tensorflow as tf | ||
|
|
||
| def AsymmetricLoss(gamma_neg=4.0, gamma_pos=1.0): # Wrapper for ASL function | ||
| """" | ||
| Tensorflow adaptation of "Official Pytorch Implementation of: 'Asymmetric Loss For Multi-Label Classification'(ICCV, 2021) paper" --> https://github.com/Alibaba-MIIL/ASL/blob/main/src/loss_functions/losses.py | ||
| Returns a loss function with asymmetric, specifiable emphases for false negatives & false positives. Output can be passed in as loss function for model.compile(). | ||
| ---------- | ||
| Parameters | ||
| ---------- | ||
| gamma_neg: asymmetric emphasis on false negatives | ||
| gamma_pos: assymetric emphasis on false positives | ||
| """ | ||
|
|
||
| # Return ASL function with custom emphases | ||
| def ASL_func(y, x): | ||
| """" | ||
| Parameters | ||
| ---------- | ||
| x: input logits (y hat) | ||
| y: targets (multi-label binarized vector) | ||
| """ | ||
|
|
||
| # Calculating Probabilities | ||
| xs_pos = x | ||
| xs_neg = 1 - x | ||
|
|
||
| # Basic CE calculation | ||
| los_pos = y * tf.math.log(xs_pos) | ||
| los_neg = (1 - y) * tf.math.log(xs_neg) | ||
| loss = los_pos + los_neg | ||
|
Comment on lines
+23
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1. tf.math.log() can hit -inf The loss computes tf.math.log(xs_pos) and tf.math.log(xs_neg) without clipping/validating that x is in (0,1), which can produce -inf/NaN and break training. This is missing boundary/edge-case handling and input validation. Agent Prompt
|
||
|
|
||
| # Asymmetric Focusing | ||
| if gamma_neg > 0 or gamma_pos > 0: | ||
| pt0 = xs_pos * y | ||
| pt1 = xs_neg * (1 - y) # pt = p if t > 0 else 1-p | ||
| pt = pt0 + pt1 | ||
| one_sided_gamma = gamma_pos * y + gamma_neg * (1 - y) | ||
| one_sided_w = tf.math.pow(1 - pt, one_sided_gamma) | ||
| loss *= one_sided_w | ||
|
|
||
| return -tf.math.reduce_sum(loss) | ||
| return ASL_func | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2. Logits used as probs
🐞 Bug✓ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools