File size: 639 Bytes
ed9f15f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/**
* Math Utilities - Mathematical helper functions
*/
/**
* Softmax function for converting logits to probabilities
*/
export function softmax(logits) {
const maxLogit = Math.max(...logits);
const scores = logits.map(l => Math.exp(l - maxLogit));
const sum = scores.reduce((a, b) => a + b);
return scores.map(s => s / sum);
}
/**
* Calculate mean of array
*/
export function mean(array) {
return array.reduce((a, b) => a + b, 0) / array.length;
}
/**
* Clamp value between min and max
*/
export function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
|