@seanhogg/builderforce-memory-engine 2026.9.33 → 2026.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/evermind/delta_wire.d.ts +10 -1
- package/dist/evermind/delta_wire.d.ts.map +1 -1
- package/dist/evermind/delta_wire.js +25 -3
- package/dist/evermind/delta_wire.js.map +1 -1
- package/dist/evermind/index.d.ts +4 -4
- package/dist/evermind/index.d.ts.map +1 -1
- package/dist/evermind/index.js +2 -2
- package/dist/evermind/index.js.map +1 -1
- package/dist/evermind/merge.d.ts +25 -0
- package/dist/evermind/merge.d.ts.map +1 -1
- package/dist/evermind/merge.js +30 -0
- package/dist/evermind/merge.js.map +1 -1
- package/dist/evermind/package.d.ts +9 -0
- package/dist/evermind/package.d.ts.map +1 -1
- package/dist/evermind/package.js +20 -0
- package/dist/evermind/package.js.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/limbic/index.d.ts +3 -0
- package/dist/limbic/index.d.ts.map +1 -1
- package/dist/limbic/index.js +3 -0
- package/dist/limbic/index.js.map +1 -1
- package/dist/limbic/limbic_task.d.ts +22 -0
- package/dist/limbic/limbic_task.d.ts.map +1 -0
- package/dist/limbic/limbic_task.js +45 -0
- package/dist/limbic/limbic_task.js.map +1 -0
- package/dist/limbic/limbic_trainer.d.ts +17 -30
- package/dist/limbic/limbic_trainer.d.ts.map +1 -1
- package/dist/limbic/limbic_trainer.js +38 -144
- package/dist/limbic/limbic_trainer.js.map +1 -1
- package/dist/ml/classical/ewma.d.ts +32 -0
- package/dist/ml/classical/ewma.d.ts.map +1 -0
- package/dist/ml/classical/ewma.js +46 -0
- package/dist/ml/classical/ewma.js.map +1 -0
- package/dist/ml/classical/gbm.d.ts +42 -0
- package/dist/ml/classical/gbm.d.ts.map +1 -0
- package/dist/ml/classical/gbm.js +116 -0
- package/dist/ml/classical/gbm.js.map +1 -0
- package/dist/ml/classical/index.d.ts +48 -0
- package/dist/ml/classical/index.d.ts.map +1 -0
- package/dist/ml/classical/index.js +45 -0
- package/dist/ml/classical/index.js.map +1 -0
- package/dist/ml/classical/isolation_forest.d.ts +37 -0
- package/dist/ml/classical/isolation_forest.d.ts.map +1 -0
- package/dist/ml/classical/isolation_forest.js +72 -0
- package/dist/ml/classical/isolation_forest.js.map +1 -0
- package/dist/ml/classical/kmeans.d.ts +31 -0
- package/dist/ml/classical/kmeans.d.ts.map +1 -0
- package/dist/ml/classical/kmeans.js +93 -0
- package/dist/ml/classical/kmeans.js.map +1 -0
- package/dist/ml/classical/knn.d.ts +48 -0
- package/dist/ml/classical/knn.d.ts.map +1 -0
- package/dist/ml/classical/knn.js +68 -0
- package/dist/ml/classical/knn.js.map +1 -0
- package/dist/ml/classical/linear.d.ts +29 -0
- package/dist/ml/classical/linear.d.ts.map +1 -0
- package/dist/ml/classical/linear.js +114 -0
- package/dist/ml/classical/linear.js.map +1 -0
- package/dist/ml/classical/logistic.d.ts +39 -0
- package/dist/ml/classical/logistic.d.ts.map +1 -0
- package/dist/ml/classical/logistic.js +103 -0
- package/dist/ml/classical/logistic.js.map +1 -0
- package/dist/ml/classical/math.d.ts +26 -0
- package/dist/ml/classical/math.d.ts.map +1 -0
- package/dist/ml/classical/math.js +90 -0
- package/dist/ml/classical/math.js.map +1 -0
- package/dist/ml/classical/naive_bayes.d.ts +24 -0
- package/dist/ml/classical/naive_bayes.d.ts.map +1 -0
- package/dist/ml/classical/naive_bayes.js +67 -0
- package/dist/ml/classical/naive_bayes.js.map +1 -0
- package/dist/ml/classical/robust_z.d.ts +30 -0
- package/dist/ml/classical/robust_z.d.ts.map +1 -0
- package/dist/ml/classical/robust_z.js +46 -0
- package/dist/ml/classical/robust_z.js.map +1 -0
- package/dist/ml/dense_head.d.ts +33 -0
- package/dist/ml/dense_head.d.ts.map +1 -0
- package/dist/ml/dense_head.js +140 -0
- package/dist/ml/dense_head.js.map +1 -0
- package/dist/ml/features.d.ts +25 -0
- package/dist/ml/features.d.ts.map +1 -0
- package/dist/ml/features.js +46 -0
- package/dist/ml/features.js.map +1 -0
- package/dist/ml/federate.d.ts +70 -0
- package/dist/ml/federate.d.ts.map +1 -0
- package/dist/ml/federate.js +122 -0
- package/dist/ml/federate.js.map +1 -0
- package/dist/ml/head_trainer.d.ts +76 -0
- package/dist/ml/head_trainer.d.ts.map +1 -0
- package/dist/ml/head_trainer.js +182 -0
- package/dist/ml/head_trainer.js.map +1 -0
- package/dist/ml/index.d.ts +23 -0
- package/dist/ml/index.d.ts.map +1 -0
- package/dist/ml/index.js +11 -0
- package/dist/ml/index.js.map +1 -0
- package/dist/ml/registry.d.ts +20 -0
- package/dist/ml/registry.d.ts.map +1 -0
- package/dist/ml/registry.js +43 -0
- package/dist/ml/registry.js.map +1 -0
- package/dist/ml/run.d.ts +94 -0
- package/dist/ml/run.d.ts.map +1 -0
- package/dist/ml/run.js +130 -0
- package/dist/ml/run.js.map +1 -0
- package/dist/ml/tasks/experience.d.ts +90 -0
- package/dist/ml/tasks/experience.d.ts.map +1 -0
- package/dist/ml/tasks/experience.js +156 -0
- package/dist/ml/tasks/experience.js.map +1 -0
- package/dist/ml/tasks/index.d.ts +10 -0
- package/dist/ml/tasks/index.d.ts.map +1 -0
- package/dist/ml/tasks/index.js +28 -0
- package/dist/ml/tasks/index.js.map +1 -0
- package/dist/ml/tasks/local_success.d.ts +55 -0
- package/dist/ml/tasks/local_success.d.ts.map +1 -0
- package/dist/ml/tasks/local_success.js +77 -0
- package/dist/ml/tasks/local_success.js.map +1 -0
- package/dist/ml/tasks/run_anomaly.d.ts +51 -0
- package/dist/ml/tasks/run_anomaly.d.ts.map +1 -0
- package/dist/ml/tasks/run_anomaly.js +60 -0
- package/dist/ml/tasks/run_anomaly.js.map +1 -0
- package/dist/ml/train.d.ts +64 -0
- package/dist/ml/train.d.ts.map +1 -0
- package/dist/ml/train.js +101 -0
- package/dist/ml/train.js.map +1 -0
- package/dist/ml/types.d.ts +112 -0
- package/dist/ml/types.d.ts.map +1 -0
- package/dist/ml/types.js +23 -0
- package/dist/ml/types.js.map +1 -0
- package/dist/moe/moe_model.d.ts.map +1 -1
- package/dist/moe/moe_model.js +1 -16
- package/dist/moe/moe_model.js.map +1 -1
- package/dist/training/cpu_ops.d.ts +2 -0
- package/dist/training/cpu_ops.d.ts.map +1 -1
- package/dist/training/cpu_ops.js +16 -0
- package/dist/training/cpu_ops.js.map +1 -1
- package/package.json +1 -1
- package/src/evermind/delta_wire.ts +148 -114
- package/src/evermind/index.ts +35 -32
- package/src/evermind/merge.ts +53 -0
- package/src/evermind/package.ts +488 -464
- package/src/index.ts +15 -1
- package/src/limbic/index.ts +5 -0
- package/src/limbic/limbic_task.ts +62 -0
- package/src/limbic/limbic_trainer.ts +91 -258
- package/src/ml/classical/ewma.ts +68 -0
- package/src/ml/classical/gbm.ts +143 -0
- package/src/ml/classical/index.ts +76 -0
- package/src/ml/classical/isolation_forest.ts +88 -0
- package/src/ml/classical/kmeans.ts +102 -0
- package/src/ml/classical/knn.ts +98 -0
- package/src/ml/classical/linear.ts +119 -0
- package/src/ml/classical/logistic.ts +125 -0
- package/src/ml/classical/math.ts +93 -0
- package/src/ml/classical/naive_bayes.ts +77 -0
- package/src/ml/classical/robust_z.ts +65 -0
- package/src/ml/dense_head.ts +157 -0
- package/src/ml/features.ts +61 -0
- package/src/ml/federate.ts +160 -0
- package/src/ml/head_trainer.ts +268 -0
- package/src/ml/index.ts +57 -0
- package/src/ml/registry.ts +44 -0
- package/src/ml/run.ts +178 -0
- package/src/ml/tasks/experience.ts +214 -0
- package/src/ml/tasks/index.ts +30 -0
- package/src/ml/tasks/local_success.ts +118 -0
- package/src/ml/tasks/run_anomaly.ts +90 -0
- package/src/ml/train.ts +136 -0
- package/src/ml/types.ts +132 -0
- package/src/moe/moe_model.ts +1 -13
- package/src/training/cpu_ops.ts +141 -127
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { MlPredictor, MlTask } from "../ml/types.js";
|
|
2
|
+
import type { LimbicModel } from "./limbic_model.js";
|
|
3
|
+
export interface LimbicAffectInput {
|
|
4
|
+
/** Experience embedding (length = model.inputDim). */
|
|
5
|
+
embedding: ArrayLike<number>;
|
|
6
|
+
/** Current affective state (length = model.stateDim). */
|
|
7
|
+
state: ArrayLike<number>;
|
|
8
|
+
}
|
|
9
|
+
export interface LimbicAffectOutput {
|
|
10
|
+
/** Bounded affect delta in (-1, 1) per state dim. */
|
|
11
|
+
delta: Float32Array;
|
|
12
|
+
/** Reward prediction. */
|
|
13
|
+
reward: number;
|
|
14
|
+
}
|
|
15
|
+
export declare const LIMBIC_AFFECT_TASK: MlTask<LimbicAffectInput, LimbicAffectOutput>;
|
|
16
|
+
/**
|
|
17
|
+
* Answer `limbic.affect` with `model`. The feature vector is `[embedding, state]`;
|
|
18
|
+
* the hidden scratch resets per call, exactly as the trainer treats each sample.
|
|
19
|
+
* Confidence is 1: a deterministic regression head has no calibrated uncertainty.
|
|
20
|
+
*/
|
|
21
|
+
export declare function limbicAffectPredictor(model: LimbicModel): MlPredictor;
|
|
22
|
+
//# sourceMappingURL=limbic_task.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limbic_task.d.ts","sourceRoot":"","sources":["../../src/limbic/limbic_task.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAgB,WAAW,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,MAAM,WAAW,iBAAiB;IAChC,sDAAsD;IACtD,SAAS,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IAC7B,yDAAyD;IACzD,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;CAC1B;AAED,MAAM,WAAW,kBAAkB;IACjC,qDAAqD;IACrD,KAAK,EAAE,YAAY,CAAC;IACpB,yBAAyB;IACzB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,eAAO,MAAM,kBAAkB,EAAE,MAAM,CAAC,iBAAiB,EAAE,kBAAkB,CAa5E,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,WAAW,GAAG,WAAW,CAarE"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* limbic/limbic_task.ts — `limbic.affect`, the ML catalog's first row, registered
|
|
3
|
+
* by limbic/ itself.
|
|
4
|
+
*
|
|
5
|
+
* Nothing about the affect head changes: the row describes it (input = experience
|
|
6
|
+
* embedding + affective state, output = bounded affect delta + reward), the
|
|
7
|
+
* predictor adapter lets the ONE runner answer with an existing `LimbicModel`, and
|
|
8
|
+
* training stays `LimbicTrainer` (itself a thin registration over `HeadTrainer`).
|
|
9
|
+
* Its flat parameters are what federate when the head travels with the model.
|
|
10
|
+
*/
|
|
11
|
+
import { assignParams, flattenParams } from "../ml/head_trainer.js";
|
|
12
|
+
export const LIMBIC_AFFECT_TASK = {
|
|
13
|
+
kind: "regress",
|
|
14
|
+
id: "limbic.affect",
|
|
15
|
+
description: "How an experience moves the agent's affective state, and how rewarding it was.",
|
|
16
|
+
features: { numeric: (i) => [...Array.from(i.embedding), ...Array.from(i.state)] },
|
|
17
|
+
engines: [{ tier: "local", engine: "head", head: "mlp" }],
|
|
18
|
+
train: { minSamples: 1, trainedBy: "LimbicTrainer" },
|
|
19
|
+
output: {
|
|
20
|
+
decode: (p) => ({
|
|
21
|
+
delta: Float32Array.from(p.scores.slice(0, -1)),
|
|
22
|
+
reward: p.scores[p.scores.length - 1] ?? 0,
|
|
23
|
+
}),
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Answer `limbic.affect` with `model`. The feature vector is `[embedding, state]`;
|
|
28
|
+
* the hidden scratch resets per call, exactly as the trainer treats each sample.
|
|
29
|
+
* Confidence is 1: a deterministic regression head has no calibrated uncertainty.
|
|
30
|
+
*/
|
|
31
|
+
export function limbicAffectPredictor(model) {
|
|
32
|
+
const { inputDim, stateDim } = model.config;
|
|
33
|
+
return {
|
|
34
|
+
predict(x) {
|
|
35
|
+
if (x.length !== inputDim + stateDim) {
|
|
36
|
+
throw new Error(`limbic.affect: expected ${inputDim + stateDim} features (embedding ${inputDim} + state ${stateDim}), got ${x.length}`);
|
|
37
|
+
}
|
|
38
|
+
const f = model.forward(x.subarray(0, inputDim), model.initHidden(), x.subarray(inputDim));
|
|
39
|
+
return { scores: [...Array.from(f.delta), f.reward], confidence: 1 };
|
|
40
|
+
},
|
|
41
|
+
exportParams: () => flattenParams(model.parameters()),
|
|
42
|
+
importParams: (params) => assignParams(model.parameters(), params),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=limbic_task.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limbic_task.js","sourceRoot":"","sources":["../../src/limbic/limbic_task.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAkBpE,MAAM,CAAC,MAAM,kBAAkB,GAAkD;IAC/E,IAAI,EAAE,SAAS;IACf,EAAE,EAAE,eAAe;IACnB,WAAW,EAAE,gFAAgF;IAC7F,QAAQ,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE;IAClF,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;IACzD,KAAK,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE,SAAS,EAAE,eAAe,EAAE;IACpD,MAAM,EAAE;QACN,MAAM,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@@ -3,15 +3,16 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Trains the affect head to predict affect deltas and reward from
|
|
5
5
|
* (experience embedding, current state) pairs, using full-batch gradient
|
|
6
|
-
* descent with AdamW
|
|
7
|
-
* the
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* descent with AdamW — through the shared {@link HeadTrainer} (`ml/head_trainer.ts`),
|
|
7
|
+
* which runs the optimiser step on the GPU via WEIGHT_UPDATE_WGSL when a device is
|
|
8
|
+
* supplied and on a numerically-equivalent CPU AdamW otherwise. This file is only
|
|
9
|
+
* the limbic registration: the head adapter and the public trainer surface.
|
|
10
10
|
*
|
|
11
11
|
* The objective is MSE (regression), not the cross-entropy used by the
|
|
12
12
|
* language-model {@link MambaTrainer} — a limbic experience has continuous
|
|
13
13
|
* targets, not a next-token distribution.
|
|
14
14
|
*/
|
|
15
|
+
import { type HeadTrainOptions, type TrainableHead } from "../ml/head_trainer.js";
|
|
15
16
|
import type { LimbicModel } from "./limbic_model.js";
|
|
16
17
|
/** One training example: an experience and the affect change it should produce. */
|
|
17
18
|
export interface LimbicSample {
|
|
@@ -24,43 +25,29 @@ export interface LimbicSample {
|
|
|
24
25
|
/** Observed scalar reward for the experience. */
|
|
25
26
|
reward: number;
|
|
26
27
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
* instability early-warning) — computed CPU-side so it's always available, unlike
|
|
38
|
-
* the GPU trainer where it's opt-in. */
|
|
39
|
-
onEpochEnd?: ((epoch: number, loss: number, gradNorm?: number) => void) | null;
|
|
40
|
-
}
|
|
28
|
+
/** The limbic trainer's options are the shared head trainer's. */
|
|
29
|
+
export type LimbicTrainOptions = HeadTrainOptions;
|
|
30
|
+
/**
|
|
31
|
+
* The limbic model as a {@link TrainableHead}. Each sample is one experience
|
|
32
|
+
* appraisal: cross-experience memory flows through the affective state `s` (fed
|
|
33
|
+
* back by the runtime), not through the hidden scratch `h`, so the hidden state
|
|
34
|
+
* resets per sample. This also makes BPTT(1) exact — there is no truncated carry.
|
|
35
|
+
*/
|
|
36
|
+
export declare function limbicHead(model: LimbicModel): TrainableHead<LimbicSample>;
|
|
37
|
+
/** Thin registration of the limbic head over the shared {@link HeadTrainer}. */
|
|
41
38
|
export declare class LimbicTrainer {
|
|
42
39
|
readonly model: LimbicModel;
|
|
43
40
|
readonly device: GPUDevice | null;
|
|
44
|
-
private
|
|
45
|
-
private _step;
|
|
46
|
-
private readonly _adamwPipeline;
|
|
41
|
+
private readonly _trainer;
|
|
47
42
|
constructor(model: LimbicModel, device?: GPUDevice | null);
|
|
48
43
|
/** Whether the optimiser step runs on the GPU. */
|
|
49
44
|
get gpuTraining(): boolean;
|
|
50
|
-
private _initMoments;
|
|
51
45
|
/**
|
|
52
46
|
* Train on a batch of samples for `epochs` passes. Returns the per-epoch mean
|
|
53
|
-
* loss (monotonically decreasing on a learnable mapping).
|
|
54
|
-
* accumulate across the whole sequence (recurrent hidden carried, reset per
|
|
55
|
-
* epoch), are averaged, clipped, then applied once per epoch.
|
|
47
|
+
* loss (monotonically decreasing on a learnable mapping).
|
|
56
48
|
*/
|
|
57
49
|
train(samples: LimbicSample[], opts?: LimbicTrainOptions): Promise<number[]>;
|
|
58
50
|
/** Mean MSE loss over samples (no weight update). Hidden resets per sample. */
|
|
59
51
|
evaluate(samples: LimbicSample[]): number;
|
|
60
|
-
/** Clip grads to `maxNorm` in place; returns the PRE-clip L2 norm (the signal). */
|
|
61
|
-
private _clipGradients;
|
|
62
|
-
private _adamwStepCpu;
|
|
63
|
-
/** AdamW on the GPU via the shared WEIGHT_UPDATE_WGSL kernel. Awaited per step. */
|
|
64
|
-
private _adamwStepGpu;
|
|
65
52
|
}
|
|
66
53
|
//# sourceMappingURL=limbic_trainer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"limbic_trainer.d.ts","sourceRoot":"","sources":["../../src/limbic/limbic_trainer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;
|
|
1
|
+
{"version":3,"file":"limbic_trainer.d.ts","sourceRoot":"","sources":["../../src/limbic/limbic_trainer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAe,KAAK,gBAAgB,EAAE,KAAK,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC/F,OAAO,KAAK,EAAE,WAAW,EAAe,MAAM,mBAAmB,CAAC;AAElE,mFAAmF;AACnF,MAAM,WAAW,YAAY;IAC3B,sDAAsD;IACtD,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IACzB,+EAA+E;IAC/E,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IACzB,uFAAuF;IACvF,WAAW,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IAC/B,iDAAiD;IACjD,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,kEAAkE;AAClE,MAAM,MAAM,kBAAkB,GAAG,gBAAgB,CAAC;AAElD;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,WAAW,GAAG,aAAa,CAAC,YAAY,CAAC,CAoB1E;AAED,gFAAgF;AAChF,qBAAa,aAAa;IACxB,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAAC;IAClC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA4B;gBAEzC,KAAK,EAAE,WAAW,EAAE,MAAM,GAAE,SAAS,GAAG,IAAW;IAM/D,kDAAkD;IAClD,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED;;;OAGG;IACH,KAAK,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,IAAI,GAAE,kBAAuB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAIhF,+EAA+E;IAC/E,QAAQ,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,MAAM;CAG1C"}
|
|
@@ -3,172 +3,66 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Trains the affect head to predict affect deltas and reward from
|
|
5
5
|
* (experience embedding, current state) pairs, using full-batch gradient
|
|
6
|
-
* descent with AdamW
|
|
7
|
-
* the
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* descent with AdamW — through the shared {@link HeadTrainer} (`ml/head_trainer.ts`),
|
|
7
|
+
* which runs the optimiser step on the GPU via WEIGHT_UPDATE_WGSL when a device is
|
|
8
|
+
* supplied and on a numerically-equivalent CPU AdamW otherwise. This file is only
|
|
9
|
+
* the limbic registration: the head adapter and the public trainer surface.
|
|
10
10
|
*
|
|
11
11
|
* The objective is MSE (regression), not the cross-entropy used by the
|
|
12
12
|
* language-model {@link MambaTrainer} — a limbic experience has continuous
|
|
13
13
|
* targets, not a next-token distribution.
|
|
14
14
|
*/
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
import { HeadTrainer } from "../ml/head_trainer.js";
|
|
16
|
+
/**
|
|
17
|
+
* The limbic model as a {@link TrainableHead}. Each sample is one experience
|
|
18
|
+
* appraisal: cross-experience memory flows through the affective state `s` (fed
|
|
19
|
+
* back by the runtime), not through the hidden scratch `h`, so the hidden state
|
|
20
|
+
* resets per sample. This also makes BPTT(1) exact — there is no truncated carry.
|
|
21
|
+
*/
|
|
22
|
+
export function limbicHead(model) {
|
|
23
|
+
return {
|
|
24
|
+
parameters: () => model.parameters(),
|
|
25
|
+
gradients: () => model.gradients(),
|
|
26
|
+
zeroGrad: () => model.zeroGrad(),
|
|
27
|
+
accumulate: (s) => model.backwardStep(s.input, model.initHidden(), s.state, s.deltaTarget, s.reward).loss,
|
|
28
|
+
loss: (s) => {
|
|
29
|
+
const { rewardWeight } = model.config;
|
|
30
|
+
const f = model.forward(s.input, model.initHidden(), s.state);
|
|
31
|
+
let loss = 0;
|
|
32
|
+
for (let k = 0; k < f.delta.length; k++) {
|
|
33
|
+
const d = f.delta[k] - (s.deltaTarget[k] ?? 0);
|
|
34
|
+
loss += 0.5 * d * d;
|
|
35
|
+
}
|
|
36
|
+
const rd = f.reward - s.reward;
|
|
37
|
+
loss += 0.5 * rewardWeight * rd * rd;
|
|
38
|
+
return loss;
|
|
39
|
+
},
|
|
40
|
+
};
|
|
22
41
|
}
|
|
42
|
+
/** Thin registration of the limbic head over the shared {@link HeadTrainer}. */
|
|
23
43
|
export class LimbicTrainer {
|
|
24
44
|
model;
|
|
25
45
|
device;
|
|
26
|
-
|
|
27
|
-
_step = 0;
|
|
28
|
-
_adamwPipeline;
|
|
46
|
+
_trainer;
|
|
29
47
|
constructor(model, device = null) {
|
|
30
48
|
this.model = model;
|
|
31
49
|
this.device = device;
|
|
32
|
-
this.
|
|
33
|
-
? createComputePipeline(device, WEIGHT_UPDATE_WGSL, "adamw_update")
|
|
34
|
-
: null;
|
|
50
|
+
this._trainer = new HeadTrainer(limbicHead(model), device, "LimbicTrainer");
|
|
35
51
|
}
|
|
36
52
|
/** Whether the optimiser step runs on the GPU. */
|
|
37
53
|
get gpuTraining() {
|
|
38
|
-
return this.
|
|
39
|
-
}
|
|
40
|
-
_initMoments() {
|
|
41
|
-
if (this._moments)
|
|
42
|
-
return;
|
|
43
|
-
this._moments = this.model.parameters().map((p) => ({
|
|
44
|
-
m: new Float32Array(p.numel),
|
|
45
|
-
v: new Float32Array(p.numel),
|
|
46
|
-
}));
|
|
54
|
+
return this._trainer.gpuTraining;
|
|
47
55
|
}
|
|
48
56
|
/**
|
|
49
57
|
* Train on a batch of samples for `epochs` passes. Returns the per-epoch mean
|
|
50
|
-
* loss (monotonically decreasing on a learnable mapping).
|
|
51
|
-
* accumulate across the whole sequence (recurrent hidden carried, reset per
|
|
52
|
-
* epoch), are averaged, clipped, then applied once per epoch.
|
|
58
|
+
* loss (monotonically decreasing on a learnable mapping).
|
|
53
59
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
throw new Error("LimbicTrainer.train: no samples");
|
|
57
|
-
const { learningRate = 0.05, epochs = 50, weightDecay = 0.0, beta1 = 0.9, beta2 = 0.999, eps = 1e-8, maxGradNorm = 1.0, onEpochEnd = null, } = opts;
|
|
58
|
-
this._initMoments();
|
|
59
|
-
const losses = [];
|
|
60
|
-
for (let epoch = 0; epoch < epochs; epoch++) {
|
|
61
|
-
this.model.zeroGrad();
|
|
62
|
-
let epochLoss = 0;
|
|
63
|
-
// Each sample is one experience appraisal: cross-experience memory flows
|
|
64
|
-
// through the affective state `s` (fed back by the runtime), not through
|
|
65
|
-
// the hidden scratch `h`, so the hidden state resets per sample. This also
|
|
66
|
-
// makes BPTT(1) exact — there is no truncated carry.
|
|
67
|
-
for (const s of samples) {
|
|
68
|
-
const { loss } = this.model.backwardStep(s.input, this.model.initHidden(), s.state, s.deltaTarget, s.reward);
|
|
69
|
-
epochLoss += loss;
|
|
70
|
-
}
|
|
71
|
-
// Average gradients over the batch.
|
|
72
|
-
const grads = this.model.gradients();
|
|
73
|
-
const invN = 1 / samples.length;
|
|
74
|
-
for (const g of grads) {
|
|
75
|
-
for (let i = 0; i < g.data.length; i++)
|
|
76
|
-
g.data[i] *= invN;
|
|
77
|
-
}
|
|
78
|
-
const gradNorm = this._clipGradients(grads, maxGradNorm);
|
|
79
|
-
this._step++;
|
|
80
|
-
const beta1_t = Math.pow(beta1, this._step);
|
|
81
|
-
const beta2_t = Math.pow(beta2, this._step);
|
|
82
|
-
const hp = { learningRate, weightDecay, beta1, beta2, eps, beta1_t, beta2_t };
|
|
83
|
-
if (this.gpuTraining) {
|
|
84
|
-
await this._adamwStepGpu(grads, hp);
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
this._adamwStepCpu(grads, hp);
|
|
88
|
-
}
|
|
89
|
-
const avg = epochLoss / samples.length;
|
|
90
|
-
losses.push(avg);
|
|
91
|
-
if (onEpochEnd)
|
|
92
|
-
onEpochEnd(epoch + 1, avg, gradNorm);
|
|
93
|
-
}
|
|
94
|
-
return losses;
|
|
60
|
+
train(samples, opts = {}) {
|
|
61
|
+
return this._trainer.train(samples, opts);
|
|
95
62
|
}
|
|
96
63
|
/** Mean MSE loss over samples (no weight update). Hidden resets per sample. */
|
|
97
64
|
evaluate(samples) {
|
|
98
|
-
|
|
99
|
-
return 0;
|
|
100
|
-
let total = 0;
|
|
101
|
-
const { rewardWeight } = this.model.config;
|
|
102
|
-
for (const s of samples) {
|
|
103
|
-
const f = this.model.forward(s.input, this.model.initHidden(), s.state);
|
|
104
|
-
let loss = 0;
|
|
105
|
-
for (let k = 0; k < f.delta.length; k++) {
|
|
106
|
-
const d = f.delta[k] - (s.deltaTarget[k] ?? 0);
|
|
107
|
-
loss += 0.5 * d * d;
|
|
108
|
-
}
|
|
109
|
-
const rd = f.reward - s.reward;
|
|
110
|
-
loss += 0.5 * rewardWeight * rd * rd;
|
|
111
|
-
total += loss;
|
|
112
|
-
}
|
|
113
|
-
return total / samples.length;
|
|
114
|
-
}
|
|
115
|
-
/** Clip grads to `maxNorm` in place; returns the PRE-clip L2 norm (the signal). */
|
|
116
|
-
_clipGradients(grads, maxNorm) {
|
|
117
|
-
let normSq = 0;
|
|
118
|
-
for (const g of grads)
|
|
119
|
-
for (let i = 0; i < g.data.length; i++)
|
|
120
|
-
normSq += g.data[i] * g.data[i];
|
|
121
|
-
const norm = Math.sqrt(normSq);
|
|
122
|
-
if (norm > maxNorm && norm > 0) {
|
|
123
|
-
const scale = maxNorm / norm;
|
|
124
|
-
for (const g of grads)
|
|
125
|
-
for (let i = 0; i < g.data.length; i++)
|
|
126
|
-
g.data[i] *= scale;
|
|
127
|
-
}
|
|
128
|
-
return norm;
|
|
129
|
-
}
|
|
130
|
-
_adamwStepCpu(grads, hp) {
|
|
131
|
-
const params = this.model.parameters();
|
|
132
|
-
const { learningRate: lr, weightDecay: wd, beta1, beta2, eps, beta1_t, beta2_t } = hp;
|
|
133
|
-
for (let pi = 0; pi < params.length; pi++) {
|
|
134
|
-
const p = params[pi].data;
|
|
135
|
-
const g = grads[pi].data;
|
|
136
|
-
const mom = this._moments[pi];
|
|
137
|
-
for (let i = 0; i < p.length; i++) {
|
|
138
|
-
const gi = g[i];
|
|
139
|
-
mom.m[i] = beta1 * mom.m[i] + (1 - beta1) * gi;
|
|
140
|
-
mom.v[i] = beta2 * mom.v[i] + (1 - beta2) * gi * gi;
|
|
141
|
-
const mHat = mom.m[i] / (1 - beta1_t);
|
|
142
|
-
const vHat = mom.v[i] / (1 - beta2_t);
|
|
143
|
-
p[i] = p[i] * (1 - lr * wd) - (lr * mHat) / (Math.sqrt(vHat) + eps);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
/** AdamW on the GPU via the shared WEIGHT_UPDATE_WGSL kernel. Awaited per step. */
|
|
148
|
-
async _adamwStepGpu(grads, hp) {
|
|
149
|
-
const device = this.device;
|
|
150
|
-
const pipeline = this._adamwPipeline;
|
|
151
|
-
const params = this.model.parameters();
|
|
152
|
-
const { learningRate: lr, weightDecay: wd, beta1, beta2, eps, beta1_t, beta2_t } = hp;
|
|
153
|
-
for (let pi = 0; pi < params.length; pi++) {
|
|
154
|
-
const p = params[pi];
|
|
155
|
-
const mom = this._moments[pi];
|
|
156
|
-
const paramBuf = createStorageBuffer(device, p.data, true);
|
|
157
|
-
const gradBuf = createStorageBuffer(device, grads[pi].data, false);
|
|
158
|
-
const mBuf = createStorageBuffer(device, mom.m, true);
|
|
159
|
-
const vBuf = createStorageBuffer(device, mom.v, true);
|
|
160
|
-
const uni = createUniformBuffer(device, packAdamParams(p.numel, lr, beta1, beta2, eps, wd, beta1_t, beta2_t));
|
|
161
|
-
const bg = createBindGroup(device, pipeline, [uni, paramBuf, gradBuf, mBuf, vBuf]);
|
|
162
|
-
dispatchKernel(device, pipeline, bg, [cdiv(p.numel, 256), 1, 1]);
|
|
163
|
-
p.data.set((await readBuffer(device, paramBuf, p.numel * 4)).subarray(0, p.numel));
|
|
164
|
-
mom.m.set((await readBuffer(device, mBuf, p.numel * 4)).subarray(0, p.numel));
|
|
165
|
-
mom.v.set((await readBuffer(device, vBuf, p.numel * 4)).subarray(0, p.numel));
|
|
166
|
-
paramBuf.destroy();
|
|
167
|
-
gradBuf.destroy();
|
|
168
|
-
mBuf.destroy();
|
|
169
|
-
vBuf.destroy();
|
|
170
|
-
uni.destroy();
|
|
171
|
-
}
|
|
65
|
+
return this._trainer.evaluate(samples);
|
|
172
66
|
}
|
|
173
67
|
}
|
|
174
68
|
//# sourceMappingURL=limbic_trainer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"limbic_trainer.js","sourceRoot":"","sources":["../../src/limbic/limbic_trainer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"limbic_trainer.js","sourceRoot":"","sources":["../../src/limbic/limbic_trainer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,WAAW,EAA6C,MAAM,uBAAuB,CAAC;AAkB/F;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAC,KAAkB;IAC3C,OAAO;QACL,UAAU,EAAE,GAAkB,EAAE,CAAC,KAAK,CAAC,UAAU,EAAE;QACnD,SAAS,EAAE,GAAkB,EAAE,CAAC,KAAK,CAAC,SAAS,EAAE;QACjD,QAAQ,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,QAAQ,EAAE;QAChC,UAAU,EAAE,CAAC,CAAC,EAAE,EAAE,CAChB,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,KAAK,EAAE,KAAK,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI;QACxF,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE;YACV,MAAM,EAA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CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAC5C,CAAC;IAED,+EAA+E;IAC/E,QAAQ,CAAC,OAAuB;QAC9B,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACzC,CAAC;CACF"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ml/classical/ewma.ts — exponentially-weighted moving average + variance, for
|
|
3
|
+
* drift anomalies in an ordered series (a run's cost creeping up over the week).
|
|
4
|
+
*
|
|
5
|
+
* Per feature, over the history IN ORDER: diff = x − μ; μ += α·diff;
|
|
6
|
+
* σ² = (1 − α)(σ² + α·diff²). A new point scores z = |x − μ| / σ against the
|
|
7
|
+
* state BEFORE it is folded in. Prediction: `scores[0] = max_i z_i / threshold`.
|
|
8
|
+
*/
|
|
9
|
+
import type { MlPrediction, MlPredictor, MlSample } from "../types.js";
|
|
10
|
+
export interface EwmaOptions {
|
|
11
|
+
/** Smoothing factor in (0, 1]. Default 0.3. */
|
|
12
|
+
alpha?: number;
|
|
13
|
+
/** z at which a feature counts as anomalous. Default 3. */
|
|
14
|
+
threshold?: number;
|
|
15
|
+
/** Smallest σ per feature, as a fraction of |μ| (plus 1e-9). Default 0.01. */
|
|
16
|
+
minScaleFraction?: number;
|
|
17
|
+
}
|
|
18
|
+
export declare class EwmaModel implements MlPredictor {
|
|
19
|
+
readonly mean: Float64Array;
|
|
20
|
+
readonly variance: Float64Array;
|
|
21
|
+
readonly alpha: number;
|
|
22
|
+
readonly threshold: number;
|
|
23
|
+
readonly minScaleFraction: number;
|
|
24
|
+
constructor(mean: Float64Array, variance: Float64Array, alpha: number, threshold: number, minScaleFraction: number);
|
|
25
|
+
zScores(x: ArrayLike<number>): number[];
|
|
26
|
+
/** Fold one observation into the running state. */
|
|
27
|
+
update(x: ArrayLike<number>): void;
|
|
28
|
+
predict(x: Float32Array): MlPrediction;
|
|
29
|
+
}
|
|
30
|
+
/** Fit over `samples` in the given (chronological) order. */
|
|
31
|
+
export declare function fitEwma(samples: readonly MlSample[], opts?: EwmaOptions): EwmaModel;
|
|
32
|
+
//# sourceMappingURL=ewma.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ewma.d.ts","sourceRoot":"","sources":["../../../src/ml/classical/ewma.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAIvE,MAAM,WAAW,WAAW;IAC1B,+CAA+C;IAC/C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,2DAA2D;IAC3D,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,8EAA8E;IAC9E,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,qBAAa,SAAU,YAAW,WAAW;IAEzC,QAAQ,CAAC,IAAI,EAAE,YAAY;IAC3B,QAAQ,CAAC,QAAQ,EAAE,YAAY;IAC/B,QAAQ,CAAC,KAAK,EAAE,MAAM;IACtB,QAAQ,CAAC,SAAS,EAAE,MAAM;IAC1B,QAAQ,CAAC,gBAAgB,EAAE,MAAM;gBAJxB,IAAI,EAAE,YAAY,EAClB,QAAQ,EAAE,YAAY,EACtB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,gBAAgB,EAAE,MAAM;IAKnC,OAAO,CAAC,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,MAAM,EAAE;IAOvC,mDAAmD;IACnD,MAAM,CAAC,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,IAAI;IASlC,OAAO,CAAC,CAAC,EAAE,YAAY,GAAG,YAAY;CAIvC;AAED,6DAA6D;AAC7D,wBAAgB,OAAO,CAAC,OAAO,EAAE,SAAS,QAAQ,EAAE,EAAE,IAAI,GAAE,WAAgB,GAAG,SAAS,CAWvF"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { featureDim } from "./math.js";
|
|
2
|
+
import { anomalyConfidence } from "./robust_z.js";
|
|
3
|
+
export class EwmaModel {
|
|
4
|
+
mean;
|
|
5
|
+
variance;
|
|
6
|
+
alpha;
|
|
7
|
+
threshold;
|
|
8
|
+
minScaleFraction;
|
|
9
|
+
constructor(mean, variance, alpha, threshold, minScaleFraction) {
|
|
10
|
+
this.mean = mean;
|
|
11
|
+
this.variance = variance;
|
|
12
|
+
this.alpha = alpha;
|
|
13
|
+
this.threshold = threshold;
|
|
14
|
+
this.minScaleFraction = minScaleFraction;
|
|
15
|
+
if (!(alpha > 0 && alpha <= 1))
|
|
16
|
+
throw new Error(`EwmaModel: alpha must be in (0, 1] (got ${alpha})`);
|
|
17
|
+
}
|
|
18
|
+
zScores(x) {
|
|
19
|
+
return Array.from(this.mean, (m, i) => {
|
|
20
|
+
const sd = Math.max(Math.sqrt(this.variance[i]), this.minScaleFraction * Math.abs(m) + 1e-9);
|
|
21
|
+
return Math.abs((x[i] ?? 0) - m) / sd;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/** Fold one observation into the running state. */
|
|
25
|
+
update(x) {
|
|
26
|
+
for (let i = 0; i < this.mean.length; i++) {
|
|
27
|
+
const diff = (x[i] ?? 0) - this.mean[i];
|
|
28
|
+
const incr = this.alpha * diff;
|
|
29
|
+
this.mean[i] = this.mean[i] + incr;
|
|
30
|
+
this.variance[i] = (1 - this.alpha) * (this.variance[i] + diff * incr);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
predict(x) {
|
|
34
|
+
const score = Math.max(0, ...this.zScores(x)) / this.threshold;
|
|
35
|
+
return { scores: [score], confidence: anomalyConfidence(score) };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** Fit over `samples` in the given (chronological) order. */
|
|
39
|
+
export function fitEwma(samples, opts = {}) {
|
|
40
|
+
const d = featureDim(samples.map((s) => s.x));
|
|
41
|
+
const model = new EwmaModel(Float64Array.from(samples[0].x), new Float64Array(d), opts.alpha ?? 0.3, opts.threshold ?? 3, opts.minScaleFraction ?? 0.01);
|
|
42
|
+
for (let i = 1; i < samples.length; i++)
|
|
43
|
+
model.update(samples[i].x);
|
|
44
|
+
return model;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=ewma.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ewma.js","sourceRoot":"","sources":["../../../src/ml/classical/ewma.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAWlD,MAAM,OAAO,SAAS;IAET;IACA;IACA;IACA;IACA;IALX,YACW,IAAkB,EAClB,QAAsB,EACtB,KAAa,EACb,SAAiB,EACjB,gBAAwB;QAJxB,SAAI,GAAJ,IAAI,CAAc;QAClB,aAAQ,GAAR,QAAQ,CAAc;QACtB,UAAK,GAAL,KAAK,CAAQ;QACb,cAAS,GAAT,SAAS,CAAQ;QACjB,qBAAgB,GAAhB,gBAAgB,CAAQ;QAEjC,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,2CAA2C,KAAK,GAAG,CAAC,CAAC;IACvG,CAAC;IAED,OAAO,CAAC,CAAoB;QAC1B,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;YACpC,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAE,CAAC,EAAE,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;YAC9F,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC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AAK,GAAG,IAAI,SAAS,CACzB,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAE,CAAC,CAAC,CAAC,EAChC,IAAI,YAAY,CAAC,CAAC,CAAC,EACnB,IAAI,CAAC,KAAK,IAAI,GAAG,EACjB,IAAI,CAAC,SAAS,IAAI,CAAC,EACnB,IAAI,CAAC,gBAAgB,IAAI,IAAI,CAC9B,CAAC;IACF,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE;QAAE,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAE,CAAC,CAAC,CAAC,CAAC;IACrE,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ml/classical/gbm.ts — a small gradient-boosted tree ensemble.
|
|
3
|
+
*
|
|
4
|
+
* Depth-limited regression trees fitted to the loss gradient, one after another,
|
|
5
|
+
* shrunk by a learning rate. Objectives: `binary` (log-loss, Newton leaf values)
|
|
6
|
+
* and `regression` (squared error). Exhaustive split search over midpoints —
|
|
7
|
+
* fine for the catalog's small numeric feature sets, not for wide embeddings.
|
|
8
|
+
*/
|
|
9
|
+
import type { MlPrediction, MlPredictor, MlSample } from "../types.js";
|
|
10
|
+
import { type Vec } from "./math.js";
|
|
11
|
+
export type GbmObjective = "binary" | "regression";
|
|
12
|
+
export interface GbmOptions {
|
|
13
|
+
objective?: GbmObjective;
|
|
14
|
+
trees?: number;
|
|
15
|
+
maxDepth?: number;
|
|
16
|
+
learningRate?: number;
|
|
17
|
+
/** Minimum summed sample weight in a leaf. Default 1. */
|
|
18
|
+
minLeafWeight?: number;
|
|
19
|
+
/** L2 on leaf values (Newton denominator). Default 1. */
|
|
20
|
+
lambda?: number;
|
|
21
|
+
}
|
|
22
|
+
type RTree = {
|
|
23
|
+
value: number;
|
|
24
|
+
} | {
|
|
25
|
+
feature: number;
|
|
26
|
+
split: number;
|
|
27
|
+
left: RTree;
|
|
28
|
+
right: RTree;
|
|
29
|
+
};
|
|
30
|
+
export declare class GbmModel implements MlPredictor {
|
|
31
|
+
readonly objective: GbmObjective;
|
|
32
|
+
readonly base: number;
|
|
33
|
+
readonly trees: readonly RTree[];
|
|
34
|
+
readonly learningRate: number;
|
|
35
|
+
constructor(objective: GbmObjective, base: number, trees: readonly RTree[], learningRate: number);
|
|
36
|
+
/** The raw additive score (log-odds for `binary`). */
|
|
37
|
+
margin(x: Vec): number;
|
|
38
|
+
predict(x: Float32Array): MlPrediction;
|
|
39
|
+
}
|
|
40
|
+
export declare function fitGbm(samples: readonly MlSample[], opts?: GbmOptions): GbmModel;
|
|
41
|
+
export {};
|
|
42
|
+
//# sourceMappingURL=gbm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gbm.d.ts","sourceRoot":"","sources":["../../../src/ml/classical/gbm.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAuB,KAAK,GAAG,EAAE,MAAM,WAAW,CAAC;AAE1D,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,YAAY,CAAC;AAEnD,MAAM,WAAW,UAAU;IACzB,SAAS,CAAC,EAAE,YAAY,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,yDAAyD;IACzD,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,yDAAyD;IACzD,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,KAAK,KAAK,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,K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