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

Add logsigmoid (numerically stable) and softshrink #4663

Merged
merged 5 commits into from
Oct 11, 2017

Conversation

sidgoyal78
Copy link
Contributor

@sidgoyal78 sidgoyal78 commented Oct 10, 2017

This closes #4622, by adding 2 basic activation functions: logsigmoid and softshrink

  • Logsigmoid is defined as:
y = log ( 1 / ( 1 + exp(-x)))

However, to make the computation numerically stable for large negative values of x, the well-known "log-sum-exp" trick is employed. (https://hips.seas.harvard.edu/blog/2013/01/09/computing-log-sum-exp/).

  • softshrink is defined as follows (for a non-negative lambda):
y = x - lambda, if x > lambda
    x + lambda, if x < -lambda
    0, otherwise

Copy link
Contributor

@dzhwinter dzhwinter left a comment

Choose a reason for hiding this comment

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

LGTM! I will approve it after fixing the comment. Otherwise, the approval will be dismissed.

@@ -95,6 +95,31 @@ struct SigmoidGradFunctor : public BaseActivationFunctor<T> {
}
};

// Originally: logsigmoid(x) = -log (1 + exp(-x))
// For numerical stability: logsigmoid(x) = - (max(-x, 0) + log(exp(-max(-x, 0))
// + exp(-x - max(-x, 0))))
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we can put the source URL into the comment? (https://hips.seas.harvard.edu/blog/2013/01/09/computing-log-sum-exp/).
For the follow-up maintainers, he may not know the trick, he even cannot check the formula correctness.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I added more detailed comments. Please let me know if this seems okay now.

dzhwinter
dzhwinter previously approved these changes Oct 10, 2017
Copy link
Contributor

@dzhwinter dzhwinter left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@dzhwinter dzhwinter left a comment

Choose a reason for hiding this comment

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

LGTM!

@sidgoyal78 sidgoyal78 merged commit 6604d7c into PaddlePaddle:develop Oct 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Logsigmoid (numerically stable) and softshrink activations
3 participants