ModelStateRegistry<T>

Models & Types · AiDotNet.Models

The declared home for model state that is not a flat parameter vector.

How It Works

ModelBase persists whatever the model declared through its parameter components, which covers every model whose learned state IS its parameter vector. It is not everything. A k-nearest-neighbours model’s state is the training set; a random forest’s is a list of trees; a GAM’s is its fitted knot vectors; a kernel ridge model’s is its centres and dual coefficients. None of that fits a flat vector, none of it had anywhere to be declared, and so every one of those models hand-wrote a Serialize/Deserialize pair – which is two places to forget the same field.

This is the model-side analogue of LayerBase.RegisterBuffer. A model DECLARES a piece of state once, by name, with a getter and a setter; the base writes and reads it. The author never touches a BinaryWriter, and because both halves are driven by the one registration they cannot drift apart – the failure mode a hand-written pair cannot detect.

Keyed by NAME, and a name in the payload with no matching registration is skipped rather than fatal, so adding state does not invalidate existing checkpoints. Values are written in registration order, but read by name, so re-ordering registrations is also safe.

Constructors

Constructor Summary
ModelStateRegistry()

Properties

Property Summary
int Count { get; } Gets the number of declared state entries.

Methods

Method Summary
void Declare(string name, Func<Dictionary<int, Vector<T>>> get, Action<Dictionary<int, Vector<T>>> set) Declares a double array.
void Declare(string name, Func<Dictionary<string, Vector<T>>> get, Action<Dictionary<string, Vector<T>>> set) Declares a double array.
void Declare(string name, Func<List<Matrix<T>>> get, Action<List<Matrix<T>>> set) Declares a double array.
void Declare(string name, Func<List<Tensor<T>>> get, Action<List<Tensor<T>>> set) Declares a double array.
void Declare(string name, Func<List<Vector<T>>> get, Action<List<Vector<T>>> set) Declares a double array.
void Declare(string name, Func<Matrix<T>> get, Action<Matrix<T>> set) Declares a double array.
void Declare(string name, Func<Matrix<T>[]> get, Action<Matrix<T>[]> set) Declares a double array.
void Declare(string name, Func<Tensor<T>> get, Action<Tensor<T>> set) Declares a double array.
void Declare(string name, Func<Vector<T>> get, Action<Vector<T>> set) Declares a double array.
void Declare(string name, Func<Vector<T>[]> get, Action<Vector<T>[]> set) Declares a double array.
void Declare(string name, Func<Vector<int>> get, Action<Vector<int>> set) Declares a double array.
void Declare(string name, Func<double[]> get, Action<double[]> set) Declares a double array.
void Declare(string name, Func<int[]> get, Action<int[]> set) Declares a double array.
void DeclareAfterParameterRestore(string name, Action restore) Declares a deterministic repair that runs after the flat parameter vector has been restored.
void DeclareAfterRestore(string name, Action restore) Declares a deterministic repair that runs after the ordinary state entries in the payload.
void DeclareArray(string name, Func<T[]> get, Action<T[]> set) Declares an array held as the model’s own numeric type.
void DeclareBoolean(string name, Func<bool> get, Action<bool> set) Declares a boolean, such as a fitted flag or a mode switch.
void DeclareByteVector(string name, Func<Vector<byte>> get, Action<Vector<byte>> set) Declares a byte vector, such as quantized optimizer moments.
void DeclareChild(string name, Func<TChild> get) Declares a single nested model, such as a DQN agent’s target network.
void DeclareChild(string name, Func<TChild> get, Action<TChild> set) Declares an assignable child that may not exist until its fitted state is restored.
void DeclareChild(string name, Func<TChild> get, Action<TChild> set, Func<TChild> create) Declares an assignable fitted child whose parent already owns the canonical construction factory for its abstract or interface-typed slot.
void DeclareChildList(string name, Func<List<TChild>> get) Declares a list of nested models, such as an agent’s per-actor target networks.
void DeclareChildren(string name, Func<IList<TChild>> get) Declares a child model whose own state travels with this one – an ensemble member, a base learner, a per-output head.
void DeclareDouble(string name, Func<double> get, Action<double> set) Declares a double, such as a temperature or a learned threshold.
void DeclareDoubleVector(string name, Func<Vector<double>> get, Action<Vector<double>> set) Declares a double vector held by a model whose primary numeric type may differ.
void DeclareExact(string name, Func<double[]> get, Action<double[]> set) Declares a replaceable jagged double parameter array whose exact payload wins after vector restore.
void DeclareExact(string name, Func<double[][]> get, Action<double[][]> set) Declares a replaceable jagged double parameter array whose exact payload wins after vector restore.
void DeclareExactDouble(string name, Func<double> get, Action<double> set) Declares a double scalar that shadows a flat T parameter slot.
void DeclareExactInPlace(string name, Func<double[]> get) Declares a readonly jagged double parameter array whose exact payload wins after vector restore.
void DeclareExactInPlace(string name, Func<double[][]> get) Declares a readonly jagged double parameter array whose exact payload wins after vector restore.
void DeclareGraph(string name, Func<TNode> getRoot, Action<TNode> setRoot, Action<NodeShape<T, TNode>> describe) Declares a recursive node graph, such as a decision tree.
void DeclareGraphList(string name, Func<List<TNode>> get, Action<List<TNode>> set, Action<NodeShape<T, TNode>> describe) Declares a LIST of node graphs — a forest, where each entry is its own root.
void DeclareInPlace(string name, Func<Dictionary<int, Vector<T>>> get) Declares a readonly jagged double array and restores its contents in place.
void DeclareInPlace(string name, Func<Dictionary<string, Vector<T>>> get) Declares a readonly jagged double array and restores its contents in place.
void DeclareInPlace(string name, Func<List<Matrix<T>>> get) Declares a readonly jagged double array and restores its contents in place.
void DeclareInPlace(string name, Func<List<Tensor<T>>> get) Declares a readonly jagged double array and restores its contents in place.
void DeclareInPlace(string name, Func<List<Vector<T>>> get) Declares a readonly jagged double array and restores its contents in place.
void DeclareInPlace(string name, Func<Matrix<T>> get) Declares a readonly jagged double array and restores its contents in place.
void DeclareInPlace(string name, Func<Tensor<T>> get) Declares a readonly jagged double array and restores its contents in place.
void DeclareInPlace(string name, Func<Vector<T>> get) Declares a readonly jagged double array and restores its contents in place.
void DeclareInPlace(string name, Func<double[]> get) Declares a readonly jagged double array and restores its contents in place.
void DeclareInPlace(string name, Func<double[][]> get) Declares a readonly jagged double array and restores its contents in place.
void DeclareInt32(string name, Func<int> get, Action<int> set) Declares an integer, such as a neighbour count or a tree depth.
void DeclareInt64(string name, Func<long> get, Action<long> set) Declares a 64-bit integer, such as an online model’s sample count.
void DeclareJaggedArray(string name, Func<T[][]> get, Action<T[][]> set) Declares a JAGGED array held as the model’s own numeric type.
void DeclareLayerList(string name, Func<List<TLayer>> get) Declares a list of layers the model owns directly, such as a conv stack.
void DeclareObject(string name, Func<TState> get, Action<TState> set) Declares an assignable fitted object, array, list, or dictionary.
void DeclareObjectInPlace(string name, Func<TState> get) Declares a readonly list or dictionary and restores its contents in place.
void DeclareOptions(string name, Func<TOptions> get) Declares the options object a model predicts with.
void DeclareParameterSource(string name, Func<IParameterSource<T>> get) Declares a nested parameter source, such as a duelling agent’s target network.
void DeclareRandom(string name, Func<Random> get) Declares the exact continuation state of a constructor-owned random generator.
void DeclareRandom(string name, Func<Random> get, Action<Random> set) Declares continuation state for an assignable, lazily-created random generator.
void DeclareScalar(string name, Func<T> get, Action<T> set) Declares a numeric value held as the model’s own numeric type.
void DeclareString(string name, Func<string> get, Action<string> set) Declares a string, such as a fitted category name or a chosen kernel.
void DeclareTree(string name, Func<DecisionTreeNode<T>> get, Action<DecisionTreeNode<T>> set) Declares a decision tree, carried whole.
void ReadAfterParameters(BinaryReader reader) Restores only native-precision parameter shadows.
void ReadAll(BinaryReader reader) Restores every entry the payload and this model have in common.
void ReadBeforeParameters(BinaryReader reader) Restores structural/non-parameter state while leaving exact native-precision parameter shadows for the post-parameter phase.
void WriteAll(BinaryWriter writer) Writes every declared entry, name-tagged and length-prefixed.