From a94c6131fd8fd57357f8c5f6163a01eec02fd862 Mon Sep 17 00:00:00 2001 From: Davide Cappellini Date: Mon, 5 Oct 2026 23:48:58 +0200 Subject: [PATCH] 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. --- .../DevControlBot/modules/ELM/elm.nim | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/DevControlBot_garage/DevControlBot/modules/ELM/elm.nim b/DevControlBot_garage/DevControlBot/modules/ELM/elm.nim index 21d6b57..2fcbef4 100644 --- a/DevControlBot_garage/DevControlBot/modules/ELM/elm.nim +++ b/DevControlBot_garage/DevControlBot/modules/ELM/elm.nim @@ -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