Spaces:
Running
Running
Download src/sampling.js from thomwolf/premiers-mots: direct link, hf CLI and curl.
- Browser
- Download file 888 Bytes
-
https://huggingface.co/spaces/thomwolf/premiers-mots/resolve/main/src/sampling.js
- Command line
-
hf download hf://spaces/thomwolf/premiers-mots/src/sampling.js
-
curl -L -o sampling.js https://huggingface.co/spaces/thomwolf/premiers-mots/resolve/main/src/sampling.js
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; | |
| } | |