mixle.models.neural_leaf module¶
A neural network as a mixle conditional-density leaf – the bridge that makes nets generative components.
NeuralGaussian(module) wraps a Torch module as a mixle distribution p(y | x) = N(y; module(x), noise^2 I)
over observations (x, y). It implements the full SequenceEncodableProbabilityDistribution contract, so
it drops into MixtureDistribution / CompositeDistribution / HMM emissions like any leaf – but its EM
M-step is weighted-NLL gradient descent on the module (warm-started across EM iterations => generalized EM).
A MixtureDistribution of NeuralGaussian components is therefore a mixture of neural experts: the E-step
computes responsibilities, the M-step trains each expert by responsibility-weighted regression. Combined with
the em move in mixle.experimental.program, the same model fits with EM where conjugate and gradient where neural:
from mixle.stats import MixtureEstimator
experts = MixtureEstimator([NeuralGaussian(mlp_a).estimator(), NeuralGaussian(mlp_b).estimator()])
# ... run EM (estimate loop) -> each expert specializes, gated by the responsibilities.
Requires torch (the module). The leaf is conditional: sampler().sample_given(x) draws y; sample()
raises (there is no p(x)).
- class NeuralGaussian(module, noise=1.0, m_steps=40, lr=0.01, name=None, device=None)[source]
Bases:
SequenceEncodableProbabilityDistributionp(y | x) = N(y; module(x), noise^2 I)as a mixle leaf. Observation is the pair(x, y).- log_density(xy)[source]
Return the log-density or log-mass at a single observation.
- seq_log_density(enc)[source]
Return vectorized log-density values for sequence-encoded observations.
- classmethod compute_capabilities()[source]
- backend_seq_log_density(enc, engine)[source]
Engine-neutral vectorized log-density for encoded
(x, y)pairs.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
NeuralGaussianSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
NeuralGaussianEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
NeuralGaussianEncoder
- to_dict()[source]
Return a safe JSON-compatible representation of this distribution.
- class NeuralGaussianSampler(dist, seed=None)[source]
Bases:
DistributionSampler- Parameters:
dist (NeuralGaussian)
seed (int | None)
- sample(size=None, *, batched=True)[source]
Draw observations.
Combinator samplers (mixture/sequence/…) accept
batched. Withbatched=True(the default) each child stream is drawn in one vectorized call instead of a per-draw Python loop – far faster. Because every child sampler owns an independentRandomState, batching consumes each stream in the same order as the loop, so the draws are identical to the legacy path.batched=Falseforces that legacy per-draw loop as a guaranteed- stable reference. Leaf samplers are already vectorized and ignore the flag.
- class NeuralGaussianEncoder[source]
Bases:
DataSequenceEncoder
- class NeuralGaussianAccumulator[source]
Bases:
SequenceEncodableStatisticAccumulator- update(xy, weight, estimate)[source]
- seq_update(enc, weights, estimate)[source]
- seq_initialize(enc, weights, rng)[source]
- acc_to_encoder()[source]
- Return type:
NeuralGaussianEncoder
- class NeuralGaussianAccumulatorFactory[source]
Bases:
StatisticAccumulatorFactory- make()[source]
- Return type:
NeuralGaussianAccumulator
- class NeuralGaussianEstimator(module, noise=1.0, m_steps=40, lr=0.01, name=None, device=None)[source]
Bases:
ParameterEstimatorEM estimator for a
NeuralGaussian: the M-step ism_stepsof weighted-NLL gradient on the module.The module is held (and warm-started) across EM iterations, so each M-step is a partial maximization (generalized EM). The accumulator buffers responsibility-weighted
(x, y)observations.- accumulator_factory()[source]
- Return type:
NeuralGaussianAccumulatorFactory
- NeuralLeaf
alias of
NeuralGaussian
- NeuralLeafEstimator
alias of
NeuralGaussianEstimator