Skip to main content

EmlModel

Struct EmlModel 

Source
pub struct EmlModel { /* private fields */ }
Expand description

Multi-head EML model for O(1) function approximation.

§Architecture

The model uses a shared trunk of EML operators that feeds into multiple output heads. Each head produces one scalar prediction.

Level 0: 8 affine combinations of input features (24 params)
Level 1: 4 EML nodes (no params — pure EML pairing)
Level 2: mixing + EML (depth-dependent params)
...
Level D: multi-head output (2 params per head)

Supported depths: 2, 3, 4, 5.

§Training

Training uses gradient-free random restart + coordinate descent, suitable for the modest parameter counts (typically 30-80 params). Call [record] to accumulate training data, then [train] to optimize parameters.

Implementations§

Source§

impl EmlModel

Source

pub fn new(depth: usize, input_count: usize, head_count: usize) -> Self

Create a new untrained EML model.

§Arguments
  • depth: Tree depth (2, 3, 4, or 5).
  • input_count: Number of input features.
  • head_count: Number of output heads (>= 1).
§Panics

Panics if depth is not in {2, 3, 4, 5} or head_count is 0.

Source

pub fn param_count(&self) -> usize

Total number of trainable parameters.

Source

pub fn params_slice(&self) -> &[f64]

Read-only view of the trainable parameters.

Intended for composed models (e.g., [crate::ToyEmlAttention]) that need to run coordinate descent over the union of several EmlModels’ parameters. Prefer Self::train for single-model training.

Source

pub fn params_slice_mut(&mut self) -> &mut [f64]

Mutable view of the trainable parameters.

Intended for composed models running joint coordinate descent. Callers are responsible for restoring parameters they perturb if a candidate is rejected.

Source

pub fn mark_trained(&mut self, trained: bool)

Mark the model as trained (or not). Used by composed models after joint coordinate descent converges.

Source

pub fn is_trained(&self) -> bool

Whether the model has been trained to convergence.

Source

pub fn training_sample_count(&self) -> usize

Number of training samples collected so far.

Source

pub fn depth(&self) -> usize

Tree depth.

Source

pub fn input_count(&self) -> usize

Number of input features.

Source

pub fn head_count(&self) -> usize

Number of output heads.

Source

pub fn set_model_name(&mut self, name: impl Into<String>)

Set the model name used in emitted events.

Should be called once by the domain-specific wrapper after creation.

Source

pub fn model_name(&self) -> &str

Get the model name.

Source

pub fn drain_events(&mut self) -> Vec<EmlEvent>

Drain all accumulated lifecycle events, returning them.

The caller is responsible for forwarding these to the ExoChain or other audit sinks.

Source

pub fn push_event(&mut self, event: EmlEvent)

Push a custom event into the event log.

Source

pub fn pending_event_count(&self) -> usize

Number of pending (undrained) events.

Source

pub fn predict(&self, inputs: &[f64]) -> Vec<f64>

Predict all heads from input features.

Returns a Vec with one f64 per head. Values are clamped to be non-negative.

Source

pub fn predict_primary(&self, inputs: &[f64]) -> f64

Predict only the primary (first) head.

Source

pub fn record(&mut self, inputs: &[f64], targets: &[Option<f64>])

Record a training sample.

§Arguments
  • inputs: Input feature values.
  • targets: Target values for each head. Use None for heads without ground truth in this sample (they are skipped in the loss function).
Source

pub fn train(&mut self) -> bool

Train the model using random restart + coordinate descent.

Requires at least 50 training samples. Returns true if the model converged (MSE < 0.01).

Source

pub fn distill(&self, target_depth: usize, num_samples: usize) -> EmlModel

Distill this (teacher) model to a shallower student model.

Creates a new EmlModel with target_depth (must be less than the teacher’s depth) and trains it to mimic the teacher’s outputs on num_samples synthetic inputs drawn uniformly from [0, 1].

The student learns from the teacher’s predictions, not from the original training data. This preserves accuracy while reducing computation for constrained devices (WASM, ESP32).

§Panics

Panics if target_depth >= self.depth or target_depth is not in {2, 3, 4, 5}.

Source

pub fn to_json(&self) -> String

Serialize the model to a JSON string.

Source

pub fn from_json(json: &str) -> Option<Self>

Deserialize a model from a JSON string.

Returns None if the JSON is invalid.

Trait Implementations§

Source§

impl Clone for EmlModel

Source§

fn clone(&self) -> EmlModel

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EmlModel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for EmlModel

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for EmlModel

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,