Skip to content

Instantly share code, notes, and snippets.

@aufrank
Created March 23, 2009 14:48
Show Gist options
  • 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))
}
@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