mixle.reason.encoder module

Amortized modality encoders: raw features -> a Gaussian expert about the latent.

A generative proxy models p(x | z) and is exact but expensive for high-dimensional modalities (images, spectra, long series). The scalable alternative is amortized: learn a network enc(x) -> N(mu(x), diag(sigma^2(x))) that maps a modality’s features directly to a Gaussian belief about the shared latent – a soft observation, fused with other modalities as a product of experts (mixle.inference.belief.GaussianBelief.fuse()) and consumed by mixle.reason.reason() as evidence.

The encoder is heteroscedastic: it learns to report a smaller variance on informative inputs and a larger one on ambiguous inputs, so product-of-experts fusion automatically down-weights a modality exactly where it does not know – the behavior a fixed-noise (homoscedastic) head cannot express. Training is amortized probabilistic regression (Gaussian negative log-likelihood over (x, z) pairs); at inference, encoding is a single forward pass.

Torch is imported lazily inside this module, so the encoder’s network is only built when an encoder is actually constructed (mixle.reason exposes it via a deferred attribute). Domain-neutral: application-specific encoders (a seismic-trace encoder, a spectra encoder) subclass or configure this in the mixle_pde layer, not here.

class AmortizedEncoder(in_dim, latent_dim, *, hidden=(64,), min_sd=1e-3, seed=0)[source]

Bases: object

A learned encoder mapping modality features to a diagonal-Gaussian belief about the latent.

Parameters:
  • in_dim (int) – width of the input feature vector.

  • latent_dim (int) – dimension of the (sub-)latent this encoder informs.

  • hidden (tuple[int, ...]) – hidden-layer widths of the MLP trunk.

  • min_sd (float) – floor on the predicted standard deviation (in latent units), preventing an over-confident zero-variance expert.

  • seed (int) – torch RNG seed for reproducible initialization/training.

fit(X, Z, *, epochs=300, lr=1e-2, weight_decay=0.0)[source]

Train the encoder on (X, Z) pairs by heteroscedastic Gaussian negative log-likelihood.

X is (n, in_dim) modality features, Z is (n, latent_dim) latent targets. Inputs and targets are standardized internally for stable optimization.

Parameters:
Return type:

AmortizedEncoder

encode(x)[source]

Encode one input into a diagonal-Gaussian belief N(mu(x), diag(sigma^2(x))).

Parameters:

x (Any)

Return type:

GaussianBelief

encode_batch(X)[source]

Encode a batch -> (means (n, d), variances (n, d)) in latent units.

Parameters:

X (Any)

Return type:

tuple[ndarray, ndarray]

evidence(x, *, onto=None, name='')[source]

A LinearGaussianEvidence for mixle.reason.reason() from encoding x.

The encoder’s Gaussian output N(mu, diag(var)) is a direct observation of its target latent. onto optionally maps a larger shared latent onto this encoder’s target space (a readout / selector matrix, shape (latent_dim, full_dim)); by default H = I (the encoder targets the whole latent).

Parameters:
Return type:

LinearGaussianEvidence