Repository navigation
Improved numerical accuracy for log_sum_exp, softmax, and log_softmax #3371
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
helske
wants to merge
9
commits into
stan-dev:develop
Choose a base branch
from
helske:softmax
base: develop
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
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3cc61f5
additional tests for log_softmax and log_sum_exp
helske 873012f
fix fwd tests (remove const)
helske 5af9ced
TEST to TEST_F
helske b99d75a
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot 1411777
improve accuracy and consistency of softmax et al
helske 47d29a7
Merge branch 'softmax' of https://github.com/helske/math into softmax
helske 47e34d1
fixes based on review, helper for sofmax and lse
helske 95c34fe
Merge commit '579253050c2302549e9ca2fb6c893d5a3c96dec1' into HEAD
yashikno 6623432
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot 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 |
|---|---|---|
|
|
@@ -106,3 +106,4 @@ lib/tbb/** | |
|
|
||
| # gdb debug history | ||
| .gdb_history | ||
| .positai | ||
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
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
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
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
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
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
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,69 @@ | ||
| #ifndef STAN_MATH_PRIM_FUN_SOFTMAX_LOG_SUM_EXP_HPP | ||
| #define STAN_MATH_PRIM_FUN_SOFTMAX_LOG_SUM_EXP_HPP | ||
|
|
||
| #include <stan/math/prim/meta.hpp> | ||
| #include <stan/math/prim/fun/constants.hpp> | ||
| #include <stan/math/prim/fun/Eigen.hpp> | ||
| #include <stan/math/prim/fun/log_sum_exp.hpp> | ||
| #include <stan/math/prim/fun/softmax.hpp> | ||
| #include <stan/math/prim/fun/to_ref.hpp> | ||
| #include <cmath> | ||
| #include <utility> | ||
|
|
||
| namespace stan { | ||
| namespace math { | ||
| namespace internal { | ||
|
|
||
| /** | ||
| * Return the softmax and the log sum of exponentials of the specified | ||
| * vector or matrix, using a single evaluation of the exponential per | ||
| * element. For a matrix, both are taken over all elements. | ||
| * | ||
| * With \f$m = \max(x)\f$ and \f$\theta = \exp(x - m)\f$, | ||
| * | ||
| * \f$\mbox{softmax}(x) = \theta / \sum_{n=1}^N \theta_n\f$ and | ||
| * \f$\mbox{log\_sum\_exp}(x) = m + \log \sum_{n=1}^N \theta_n\f$. | ||
| * | ||
| * The results match `softmax(x)` and `log_sum_exp(x)`, including for | ||
| * non-finite input: if \f$m\f$ is not finite, the log sum of exponentials | ||
| * is \f$m\f$ and the softmax is NaN. | ||
| * | ||
| * @tparam T type of Eigen vector or matrix | ||
| * @param x Eigen vector or matrix | ||
| * @return pair of the softmax of `x` and the log sum of exponentials of | ||
| * `x`. For empty input, returns an empty softmax and negative infinity | ||
| */ | ||
| template <typename T, require_eigen_vt<std::is_arithmetic, T>* = nullptr> | ||
| inline std::pair<plain_type_t<T>, double> softmax_log_sum_exp(const T& x) { | ||
| if (x.size() == 0) { | ||
| return {x, NEGATIVE_INFTY}; | ||
| } | ||
| const auto& x_ref = to_ref(x); | ||
| const double max = x_ref.maxCoeff(); | ||
| const auto theta = (x_ref.array() - max).exp().eval(); | ||
| const double theta_sum = theta.sum(); | ||
| const double lse = std::isfinite(max) ? max + std::log(theta_sum) : max; | ||
| return {plain_type_t<T>((theta / theta_sum).matrix()), lse}; | ||
| } | ||
|
|
||
| /** | ||
| * Return the softmax and the log sum of exponentials of the specified | ||
| * vector or matrix of autodiff values. This happens with nested autodiff, | ||
| * e.g. the values of an `fvar<var>` vector, and falls back to the | ||
| * autodiff specializations of `softmax()` and `log_sum_exp()`. | ||
| * | ||
| * @tparam T type of Eigen vector or matrix with autodiff scalar | ||
| * @param x vector or matrix | ||
| * @return pair of the softmax of `x` and the log sum of exponentials of `x` | ||
| */ | ||
| template <typename T, require_eigen_vt<is_autodiff, T>* = nullptr> | ||
| inline std::pair<plain_type_t<T>, value_type_t<T>> softmax_log_sum_exp( | ||
| const T& x) { | ||
| return {plain_type_t<T>(softmax(x)), log_sum_exp(x)}; | ||
| } | ||
|
|
||
| } // namespace internal | ||
| } // namespace math | ||
| } // namespace stan | ||
|
|
||
| #endif |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.