ELM engine: export/import weights for offline training

loadWeights/exportWeights move the three matrices in and out of a live ELM so a
model can be fitted offline and shipped as a data file.
This commit is contained in:
2026-10-05 23:48:58 +02:00
parent 16c71c527a
commit a94c6131fd
@@ -333,5 +333,30 @@ proc reset*(e: Elm) =
for i in 0 ..< e.beta.len: e.beta[i] = 0.0
e.diag = ElmDiag()
# ── offline weight export / load ────────────────────────────────────────────
# An ELM trained offline has no use for its sample store: only the three
# matrices matter (hidden W, hidden biases hs, output beta). These two procs
# move them in and out of a live model so a trained model can be shipped as a
# data file and loaded at bot startup instead of being retrained in-game.
type ElmWeights* = object
W*: seq[float64] ## hiddenDim * inputDim, row-major
hs*: seq[float64] ## hiddenDim
beta*: seq[float64] ## (hiddenDim+1) * outputs
proc exportWeights*(e: Elm): ElmWeights =
ElmWeights(W: e.W, hs: e.hs, beta: e.beta)
proc loadWeights*(e: Elm, w: ElmWeights) =
## Overwrite the matrices. Sizes must match the config exactly.
doAssert w.W.len == e.cfg.hiddenDim * e.cfg.inputDim
doAssert w.hs.len == e.cfg.hiddenDim
doAssert w.beta.len == (e.cfg.hiddenDim + 1) * e.cfg.outputs
e.W = w.W
e.hs = w.hs
e.beta = w.beta
e.diag.trainRmse = NaN # stale: the model is no longer the one that was fitted
e.diag.lastBetaNorm = NaN
proc diagnostics*(e: Elm): ElmDiag =
e.diag