Skip to content

Instantly share code, notes, and snippets.

@menduz
Created June 2, 2017 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save menduz/d197acbd6ee8ac53d07cc1fa99662492 to your computer and use it in GitHub Desktop.
Save menduz/d197acbd6ee8ac53d07cc1fa99662492 to your computer and use it in GitHub Desktop.
Softmax
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