@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
24 lines (18 loc) • 725 B
JavaScript
/**
* Normalized exponential function, converts vector of size K into probability distribution of K possible outcomes
* Useful in neural network implementations
* @see https://en.wikipedia.org/wiki/Softmax_function
* @param {number[]|Float32Array} z input vector
* @param {number} i Element in vector Z to compute softmax for
* @param {number} k Should be >= 1, typically dimension of Z
* @returns {number}
*/
export function softmax(z, i, k) {
const ezi = Math.exp(z[i]);
// NOTE that if we want to compute softmax for an entire vector, it could be far more efficient
let sum = 0;
for (let j = 1; j < k; j++) {
sum += Math.exp(z[j]);
}
return ezi / sum;
}