Created
June 2, 2017 16:41
-
-
Save menduz/d197acbd6ee8ac53d07cc1fa99662492 to your computer and use it in GitHub Desktop.
Softmax
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
function softmax(output) { | |
var maximum = output.reduce(function(p,c) { return p>c ? p : c; }); | |
var nominators = output.map(function(e) { return Math.exp(e - maximum); }); | |
var denominator = nominators.reduce(function (p, c) { return p + c; }); | |
var softmax = nominators.map(function(e) { return e / denominator; }); | |
var maxIndex = 0; | |
softmax.reduce(function(p,c,i){if(p<c) {maxIndex=i; return c;} else return p;}); | |
var result = []; | |
for (var i=0; i<output.length; i++) | |
{ | |
if (i==maxIndex) | |
result.push(1); | |
else | |
result.push(0); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment