premiers-mots / src /sampling.js
thomwolf's picture
thomwolf HF Staff
English UI, inspectable datasets, two model sizes and labeled loss axes
249a0e9 verified
Raw History Blame Contribute Delete
888 Bytes
// Relative probability threshold, applied AFTER temperature. No word rules.
export function sampleCharacter(values, temperature, random, minP = 0) {
if (!(temperature > 0) || !Number.isFinite(temperature)) throw new Error('Invalid temperature.');
if (!(minP >= 0 && minP <= 1)) throw new Error('Seuil de tirage invalide.');
const max = Math.max(...values);
if (!Number.isFinite(max)) throw new Error('Logits non finis.');
const probs = Array.from(values, v => Math.exp((v - max) / temperature));
if (minP > 0) for (let j = 0; j < probs.length; j++) if (probs[j] < minP) probs[j] = 0;
let r = random() * probs.reduce((a, b) => a + b, 0), choice = 0;
for (let j = 0; j < probs.length; j++) {
if (probs[j] === 0) continue;
choice = j; // Rounding at the upper boundary must still choose a retained token.
r -= probs[j]; if (r <= 0) break;
}
return choice;
}