Skip to content
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

∵0**0=1 ∴ gamma != 0 #15

Open
zhangyuqi-1 opened this issue Nov 12, 2020 · 2 comments
Open

∵0**0=1 ∴ gamma != 0 #15

zhangyuqi-1 opened this issue Nov 12, 2020 · 2 comments

Comments

@zhangyuqi-1
Copy link

增加 gamma = 0 的情况的讨论

def focal_loss_sigmoid(prediction_tensor, target_tensor, weights=None, alpha=0.5, gamma=0):
    target_tensor = tf.cast(target_tensor, tf.float32)
    sigmoid_p = tf.nn.sigmoid(prediction_tensor)
    zeros = array_ops.zeros_like(sigmoid_p, dtype=sigmoid_p.dtype)

    pos_p_sub = array_ops.where(target_tensor > zeros, target_tensor - sigmoid_p, zeros)
    neg_p_sub = array_ops.where(target_tensor > zeros, zeros, sigmoid_p)

    if gamma != 0:
        per_entry_cross_ent = - alpha * (pos_p_sub ** gamma) * tf.log(tf.clip_by_value(sigmoid_p, 1e-8, 1.0)) \
                              - (1 - alpha) * (neg_p_sub ** gamma) * tf.log(tf.clip_by_value(1.0 - sigmoid_p, 1e-8, 1.0))
    else:
        # ∵0**0=1 
        per_entry_cross_ent = - alpha * target_tensor * tf.log(tf.clip_by_value(sigmoid_p, 1e-8, 1.0)) \
                              - (1 - alpha) * (1 - target_tensor) * tf.log(tf.clip_by_value(1.0 - sigmoid_p, 1e-8, 1.0))

    return per_entry_cross_ent
@zhangyuqi-1
Copy link
Author

此即为sigmoid_cross_entropy

@marvinquiet
Copy link

Agreed. I also found that when gamma=0, it does not correctly compute the loss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants