Created
March 23, 2009 14:48
-
-
Save aufrank/83572 to your computer and use it in GitHub Desktop.
This file contains 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
## from http://tr.im/hH5A | |
logsumexp <- function (x) { | |
y = max(x) | |
y + log(sum(exp(x - y))) | |
} | |
softmax <- function (x) { | |
exp(x - logsumexp(x)) | |
} |
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
@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