Skip to content

Instantly share code, notes, and snippets.

@aufrank
Created March 23, 2009 14:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save aufrank/83572 to your computer and use it in GitHub Desktop.
Save aufrank/83572 to your computer and use it in GitHub Desktop.
## from http://tr.im/hH5A
logsumexp <- function (x) {
y = max(x)
y + log(sum(exp(x - y)))
}
softmax <- function (x) {
exp(x - logsumexp(x))
}
@tmalsburg
Copy link

Why is this used instead of the more readable exp(x) / sum(exp(x))?

@ben519
Copy link

ben519 commented Feb 8, 2017

@tmalsburg Numerical stability. Consider

softmax2 <- function(x) exp(x) / sum(exp(x))

softmax(c(1, 2, 3) * 1000)  # NaN NaN NaN
softmax2(c(1, 2, 3) * 1000)  # 0 0 1

@Achab94
Copy link

Achab94 commented Oct 12, 2019

@ben519 maybe the opposite?
softmax2 <- function(x) exp(x) / sum(exp(x))
softmax2(c(1, 2, 3) * 1000) # NaN NaN NaN
softmax(c(1, 2, 3) * 1000) # 0 0 1

@DusanRandD
Copy link

If the exponent exceeds a size of the defined variable the specified error occurs. I noticed that the number x=90 and larger leads to error for type of variable=float that holds 4B. So I'm doing the cutting to 80 and all going nice. This Sofmax code that clarifies this written in c is on link:
https://github.com/DusanRandD/Softmax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment