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
log p(y | x)for one encoded observation pair(x, y).
- seq_log_density(enc)[source]
Return per-row Gaussian conditional log densities for encoded
(x, y)arrays.
- classmethod compute_capabilities()[source]
Declare engine-ready scoring support for NumPy and Torch execution backends.
- backend_seq_log_density(enc, engine)[source]
Engine-neutral vectorized log-density for encoded
(x, y)pairs.
- sampler(seed=None)[source]
Return a conditional sampler for drawing
ygivenx.- Parameters:
seed (int | None)
- Return type:
NeuralGaussianSampler
- estimator(pseudo_count=None)[source]
Return the generalized-EM estimator for responsibility-weighted neural regression.
- Parameters:
pseudo_count (float | None)
- Return type:
NeuralGaussianEstimator
- dist_to_encoder()[source]
Return the encoder for batches of
(x, y)observation pairs.- Return type:
NeuralGaussianEncoder
- to_dict()[source]
Serialize hyperparameters and module bytes for registry-based round trips.
- class NeuralGaussianSampler(dist, seed=None)[source]
Bases:
DistributionSamplerConditional sampler for
NeuralGaussian; draws responses given covariates.- Parameters:
dist (NeuralGaussian)
seed (int | None)
- sample(size=None, *, batched=True)[source]
Raise because the leaf defines
p(y | x)and has no marginalp(x).
- class NeuralGaussianEncoder[source]
Bases:
DataSequenceEncoderEncode
(x, y)observation pairs for vectorized neural-Gaussian scoring and fitting.
- class NeuralGaussianAccumulator[source]
Bases:
SequenceEncodableStatisticAccumulatorBuffer weighted
(x, y)batches for the neural-Gaussian M-step.- update(xy, weight, estimate)[source]
Add one weighted observation pair to the accumulator.
- seq_update(enc, weights, estimate)[source]
Add a batch of encoded observation pairs and responsibility weights.
- initialize(xy, weight, rng)[source]
Initialize from one observation using the ordinary update path.
- seq_initialize(enc, weights, rng)[source]
Initialize from an encoded batch using the ordinary batch update path.
- combine(other)[source]
Merge the value tuple from another neural-Gaussian accumulator.
- Parameters:
other (Any)
- Return type:
NeuralGaussianAccumulator
- from_value(value)[source]
Restore accumulator buffers from a value tuple.
- Parameters:
value (tuple)
- Return type:
NeuralGaussianAccumulator
- acc_to_encoder()[source]
Return the encoder expected by this accumulator.
- Return type:
NeuralGaussianEncoder
- class NeuralGaussianAccumulatorFactory[source]
Bases:
StatisticAccumulatorFactoryFactory for neural-Gaussian accumulators.
- make()[source]
Create a fresh accumulator.
- 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 an accumulator factory for weighted neural-regression batches.
- Return type:
NeuralGaussianAccumulatorFactory
- NeuralLeaf
alias of
NeuralGaussian
- NeuralLeafEstimator
alias of
NeuralGaussianEstimator