Skip to content

Instantly share code, notes, and snippets.

@enobufs
Created July 28, 2018 03:02
Show Gist options
  • Save enobufs/33a0efd21426df618e4f7c65a6937cbc to your computer and use it in GitHub Desktop.
Save enobufs/33a0efd21426df618e4f7c65a6937cbc to your computer and use it in GitHub Desktop.
Softmax in JS using ndarray
const ndarray = require("ndarray");
const ops = require("ndarray-ops");
// Softmax in-place
const softmax = (() => {
function denom(arr, C) {
const tmp = ndarray(new Float32Array(arr.size));
ops.assign(tmp, arr);
ops.addseq(tmp, -C);
ops.expeq(tmp);
return ops.sum(tmp);
}
return (arr) => {
const C = ops.sup(arr);
const d = denom(arr, C);
ops.addseq(arr, -C);
ops.expeq(arr);
ops.divseq(arr, d);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment