| 635 | } |
| 636 | |
| 637 | int sample_mult(float* probabilities, int n, float coin) { |
| 638 | // sample index from probabilities (they must sum to 1!) |
| 639 | // coin is a random number in [0, 1), usually from random_f32() |
| 640 | float cdf = 0.0f; |
| 641 | for (int i = 0; i < n; i++) { |
| 642 | cdf += probabilities[i]; |
| 643 | if (coin < cdf) { |
| 644 | return i; |
| 645 | } |
| 646 | } |
| 647 | return n - 1; // in case of rounding errors |
| 648 | } |
| 649 | |
| 650 | // ---------------------------------------------------------------------------- |
| 651 | // main training loop |