diff options
| author | Jonas Knobloch <jonas.knobloch@t-online.de> | 2026-03-17 20:16:39 +0100 |
|---|---|---|
| committer | Jonas Knobloch <jonas.knobloch@t-online.de> | 2026-03-17 20:16:39 +0100 |
| commit | 57fb765bdc5819f8a367ad75c731a50da68b8d85 (patch) | |
| tree | 730647fc08847624c138a83f3e8329b6f4755ea6 /llm/stat.go | |
| parent | 035eef3308edd4ec581061440337aaa10a37f573 (diff) | |
Update perplexity evaluator
* Introduce results channel
* Introduce jobs channel
* Generalize evaluation via callback
Diffstat (limited to 'llm/stat.go')
| -rw-r--r-- | llm/stat.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/llm/stat.go b/llm/stat.go new file mode 100644 index 0000000..228e9f2 --- /dev/null +++ b/llm/stat.go @@ -0,0 +1,34 @@ +package llm + +import ( + "math" + "slices" +) + +func NegLogLikelihood(logits [][]float32, targets []int) (float64, int) { + if len(logits) != len(targets) { + panic("mismatched input lengths") + } + + total := float64(0) + + for i, target := range targets { + maxLogit := float64(slices.Max(logits[i])) + + sumExp := float64(0) + + for _, v := range logits[i] { + sumExp += math.Exp(float64(v) - maxLogit) + } + + logSumExp := maxLogit + math.Log(sumExp) + + targetLogit := float64(logits[i][target]) + + logProb := targetLogit - logSumExp + + total -= logProb + } + + return total, len(targets) +} |
