mixle.models package¶
Applied models – richer, domain-specialized families that plug into the same contract as mixle.stats.
Where mixle.stats holds the elementary distributions (a Gaussian, a Poisson, a categorical), this package
holds the models that are more than one elementary density: a neural network, a Gaussian process, a random
forest, a knowledge graph, a grammar, a decision process, a causal skeleton. Each is exposed through the same
five-piece Distribution/Estimator/Accumulator/Sampler/Encoder contract (or, for the
supervised/decision/causal ones, a small task-appropriate surface), so it composes with the stats core – a
neural leaf drops into a CompositeDistribution, a GP into a mixture, and so on.
The right mental model is a small catalog of applied model families (mixle/models/README.md maps every
module to one):
neural & deep – neural nets, transformers/LMs, embeddings, and their training utilities;
non-parametric – Gaussian processes and random forests as
p(y | x)leaves;relational / structured – knowledge graphs, random graphs, grammars;
latent-variable – Bayesian-nonparametric mixtures (Dirichlet process);
decision & control – partially observable Markov decision processes;
causal discovery – constraint-based structure learning.
(The imports below stay alphabetical – the ruff import sorter enforces that – so use the families above, not import order, as the map.)
These surfaces vary in maturity (see the Project status table in the top-level README); treat them as specialist adapters composable with the stable stats spine, not as the spine itself.
- class LM(vocab, *, d_model=256, n_layer=6, n_head=8, block=128, device='cpu', embedding=None)[source]
Bases:
objectA causal-Transformer language model with a small declarative surface:
fit/generate/nll.- Parameters:
- to_dict()[source]
Serialize the hyperparameters + trained weights so the LM survives a process boundary.
The token embedding may be tied across LMs (
embedding=);from_dictrebuilds an untied module and loads the savedstate_dictinto it, so a round-tripped LM is standalone (any external tie is dropped).- Return type:
- classmethod from_dict(payload)[source]
Rebuild an LM from
to_dict()output (fresh module, saved weights loaded in).- Parameters:
payload (dict)
- Return type:
LM
- save(path)[source]
Persist the trained LM to
pathviatorch.save(hyperparameters + weights).- Parameters:
path (str)
- Return type:
None
- classmethod load(path)[source]
Load an LM previously written by
save().- Parameters:
path (str)
- Return type:
LM
- fit(token_ids, *, epochs=1, batch_size=64, lr=3e-3, distributed=False, precision='fp32', shuffle=True, tp_size=1, pp_size=1, cp_size=1)[source]
Pretrain (or continue) on a token-id array via the streaming estimator; the corpus is never buffered.
tp_size/pp_size/cp_size(only meaningful withdistributed=True) are the F1 N-D parallelism dimensions – tensor/pipeline/context parallel, ORTHOGONAL to the data-parallel axis (DDP on CPU, FSDP2/ZeRO-3 on CUDA) this handle already runs. They default to 1 (off): the plan is validated against the model’s real dimensions (n_head % tp_size,pp_size <= n_layer,block % cp_size) so a bad plan fails fast, using the sharding/reconstruction mechanism inmixle.utils.parallel.tensor_pipeline_context_parallel(tested there at small scale against the dense forward). Composing the validated plan into real per-axis multi-GPU process groups is the piece that needs actual hardware this environment does not have – see that module’s docstring.
- fit_pairs(pairs, *, epochs=1, batch_size=32, lr=3e-3, mask_prompt=True, pad_id=0, seed=0, log=None)[source]
Supervised fine-tuning on
(prompt_ids, completion_ids)pairs with a dense per-position loss.The streaming
fitpath scores ONE next-token target per window (the right shape for an unbounded pretraining stream); for a pair corpus that wastes a factor ofblockin compute. Here every position of every pair contributes cross-entropy in a single forward, andmask_promptrestricts the loss to completion positions – the standard SFT objective. Sequences longer thanblockkeep the completion and drop the oldest prompt tokens; shorter ones are left-padded withpad_id(excluded from the loss). Include your end-of-sequence token in each completion sogenerate(stop_id=...)knows where to stop.
- generate(prompt_ids, n=200, *, temperature=1.0, greedy=False, seed=0, stop_id=None)[source]
Autoregressively extend
prompt_idsbyntokens (greedy, or temperature-sampled).stop_idends generation early when that token is produced (it is included in the return value, so callers can strip it – and its presence distinguishes ‘finished’ from ‘ran out of budget’).
- class CategoricalEmbedding(num_categories, dim, *, name=None)[source]
Bases:
objectA lazily-built learned embedding of shape
(num_categories, dim); every consumer gets the same module.
- class CausalSkeleton(edges, separating_sets, variable_names)[source]
Bases:
objectUndirected skeleton plus separating sets from a PC-style search.
- Parameters:
- class CategoricalClassificationNeuralNetwork(module, engine=None, precision=None)[source]
Bases:
objectA Torch classifier wrapper optimized by summed categorical log likelihood.
The wrapped module must return one logits row per observation. Fitting is delegated to
optimize_torch_objectiveso classification examples get the same convergence diagnostics and best-state restoration as distribution objectives.- Parameters:
module (Any)
engine (Any | None)
precision (Any | None)
- parameters()[source]
Return trainable parameters of the wrapped classification module.
- logits_tensor(x)[source]
Return raw class logits for
xas a Torch tensor.
- log_likelihood(x, y)[source]
Return the summed categorical log likelihood for integer labels.
- fit(x, y, max_its=500, lr=0.01, optimizer='adam', tol=1.0e-7, out=None, print_iter=100, return_result=False, restore_best=True)[source]
Maximize the categorical classification log likelihood.
- predict_proba_tensor(x)[source]
Return class probabilities for
xas a Torch tensor.
- predict_proba(x)[source]
Return class probabilities for
xas a NumPy array.
- class ConditionalIndependenceResult(measure, statistic, p_value, independent)[source]
Bases:
objectResult from a conditional independence calculation.
- DPOLeaf
alias of
DPOModel
- class ErdosRenyiGraphModel(p, directed=False, self_loops=False, name=None)[source]
Bases:
objectIndependent Bernoulli edge model for directed or undirected graphs.
- classmethod fit_mle(adjacency, directed=False, self_loops=False, pseudo_count=0.0, prior_p=0.5, name=None)[source]
Thin shim delegating to
fit_erdos_renyi_mle(kept for the classmethod-fit call API).
- log_likelihood(adjacency)[source]
Return the Bernoulli graph log likelihood.
- sample(num_nodes, seed=None)[source]
Draw one binary adjacency matrix.
- class GaussianProcessRegressor(lengthscale=1.0, amplitude=1.0, noise=0.1, mean=0.0, jitter=1.0e-6, kernel='rbf', engine=None, precision=None)[source]
Bases:
objectExact GP regression with a stationary kernel and Gaussian observation noise.
The kernel is RBF (squared-exponential) by default;
kernel="matern32"or"matern52"selects the Matern-3/2 or Matern-5/2 covariance, whose less smooth sample paths often fit physical responses better than the very smooth RBF.- Parameters:
- parameters()[source]
Return trainable raw kernel/noise parameters and the mean.
- property lengthscale: float
Return the fitted kernel lengthscale.
- property amplitude: float
Return the fitted kernel amplitude.
- property noise: float
Return the fitted Gaussian observation-noise standard deviation.
- kernel(x1, x2)[source]
Return the covariance matrix between two input arrays under the configured kernel.
- log_marginal_likelihood(x, y)[source]
Return the exact GP log marginal likelihood for training data.
- fit(x, y, max_its=500, lr=0.05, optimizer='adam', tol=1.0e-7, out=None, print_iter=100, return_result=False, restore_best=True)[source]
Maximize the GP log marginal likelihood.
The default return shape is the historical
(value, iterations)tuple. Setreturn_result=Truefor the full objective diagnostics.
- predict(x_train, y_train, x_new, return_cov=False)[source]
Return posterior predictive mean, and optionally covariance.
- predict_monotone(x_train, y_train, x_new, increasing=True)[source]
Return the posterior-mean prediction projected to be monotone in scalar
x_new.Predicts the GP posterior mean at
x_newand projects it onto the monotone cone (non-decreasing ifincreasingelse non-increasing) by pool-adjacent-violators inx_neworder – the L2-closest monotone curve to the GP mean. Intended for scalar (1-D) inputs (e.g. monotone age-depth / dose-response fits); reduces topredict()when the posterior mean is already monotone.
- class GaussianRegressionNeuralNetwork(module, noise=1.0, engine=None, precision=None)[source]
Bases:
objectA Torch module trained with a Gaussian regression log likelihood.
The wrapped module predicts the response mean and this helper learns a scalar observation noise alongside module weights. It uses the same generic Torch objective optimizer as the distribution objective helpers.
- Parameters:
module (Any)
noise (float)
engine (Any | None)
precision (Any | None)
- parameters()[source]
Return trainable module parameters plus the raw noise parameter.
- property noise: float
Return the fitted observation standard deviation.
- predict_tensor(x)[source]
Return module predictions as a Torch tensor on the configured engine.
- log_likelihood(x, y)[source]
Return the summed Gaussian regression log likelihood.
- fit(x, y, max_its=500, lr=0.01, optimizer='adam', tol=1.0e-7, out=None, print_iter=100, return_result=False, restore_best=True)[source]
Maximize the Gaussian regression log likelihood.
The default return shape is the historical
(value, iterations)tuple. Setreturn_result=Truefor the full objective diagnostics.
- class NeuralCategorical(module, m_steps=40, lr=0.01, name=None, batch_size=None, device='cpu')[source]
Bases:
SequenceEncodableProbabilityDistributionp(y | x) = softmax(module(x))as a mixle leaf. Observation is the pair(x, y),yan int class.batch_size(None = full batch) makes the M-step minibatch SGD overm_stepspasses – needed to train a real conv net on a large image set;device(e.g."mps"/"cuda") runs it on the GPU.- Parameters:
- log_density(xy)[source]
Return
log p(y | x)for one feature/class observation pair.
- seq_log_density(enc)[source]
Return per-row categorical conditional log probabilities for encoded pairs.
- predict(x)[source]
Return maximum-probability class predictions for one or more inputs.
- sampler(seed=None)[source]
Return a conditional sampler over labels given features.
- Parameters:
seed (int | None)
- Return type:
NeuralCategoricalSampler
- estimator(pseudo_count=None)[source]
Return the generalized-EM estimator for weighted cross-entropy training.
- Parameters:
pseudo_count (float | None)
- Return type:
NeuralCategoricalEstimator
- dist_to_encoder()[source]
Return the encoder for
(x, class)observation pairs.- Return type:
NeuralCategoricalEncoder
- to_dict()[source]
Serialize hyperparameters and module bytes for registry-based round trips.
- class NeuralConditionalDensity(module, *, m_steps=60, lr=5e-3, device='cpu', name=None)[source]
Bases:
SequenceEncodableProbabilityDistributionWrap a torch conditional-density
module(module.log_density(x, y) -> (n,)) as a mixle leaf.Observations are pairs
(x, y). The module must also exposesample_given(x) -> (n, d)to drawy.- log_density(xy)[source]
Return
log p(y | x)for one observation pair(x, y).
- seq_log_density(enc)[source]
Return per-row conditional log densities for encoded
(x, y)arrays.
- sampler(seed=None)[source]
Return a conditional sampler for drawing
ygivenx.- Parameters:
seed (int | None)
- Return type:
NeuralConditionalDensitySampler
- estimator(pseudo_count=None)[source]
Return the generalized-EM estimator for weighted conditional-density training.
- Parameters:
pseudo_count (float | None)
- Return type:
NeuralConditionalDensityEstimator
- dist_to_encoder()[source]
Return the encoder for
(x, y)observation pairs.- Return type:
NeuralConditionalDensityEncoder
- to_dict()[source]
Serialize hyperparameters and module bytes for registry-based round trips.
- class GradEstimator(module, *, m_steps=60, lr=5e-3, device=None, batch_size=None, precision='fp32', name=None, loss=None, optimizer=None)[source]
Bases:
ParameterEstimatorM-step: responsibility-weighted MLE –
max sum_i w_i log p(x_i)by gradient ascent on the module (warm-started across EM iterations).loss/optimizerare the caller’s hooks; the optimizer only ever sees trainable parameters, so frozen submodules stay frozen and a fully frozen module makes the M-step a no-op (a fixed distribution).- Parameters:
- accumulator_factory()[source]
Return the accumulator factory used to collect this estimator’s sufficient statistics.
- Return type:
DataBufferAccumulatorFactory
- class GradLeaf(module, *, m_steps=60, lr=5e-3, device=None, batch_size=None, precision='fp32', name=None, loss=None, optimizer=None)[source]
Bases:
SequenceEncodableProbabilityDistributionWrap a torch density
module(module.log_density(x) -> (n,)) as a composable mixle distribution (see the module docstring).lossandoptimizerare the M-step hooks.- Parameters:
- log_density(x)[source]
Return the log-density or log-mass at a single observation.
- seq_log_density(x)[source]
Return vectorized log-density values for sequence-encoded observations.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
GradLeafSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
GradEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
GradLeafEncoder
- class NeuralDensity(module, *, m_steps=60, lr=5e-3, device='cpu', name=None)[source]
Bases:
GradLeafWrap a torch density
module(module.log_density(x) -> (n,)) as a composable mixle distribution.A thin named subclass of
GradLeaf– the generic bridge owns the manufactured contract (buffer accumulator, array encoder, gradient M-step, sampler); this class owns only its name, its JSON payload, and its ready-module builders below.loss/optimizerhooks pass through (see the grad_leaf module docstring for the control story).- log_density(x)[source]
Return
log p(x)for one observation under the wrapped density module.
- seq_log_density(x)[source]
Return per-row log densities for encoded observations.
- sampler(seed=None)[source]
Return a sampler delegating to the wrapped module’s
samplemethod.- Parameters:
seed (int | None)
- Return type:
NeuralDensitySampler
- estimator(pseudo_count=None)[source]
Return the generalized-EM estimator for weighted neural-density training.
- Parameters:
pseudo_count (float | None)
- Return type:
NeuralDensityEstimator
- dist_to_encoder()[source]
Return the encoder for vectorized neural-density scoring and fitting.
- Return type:
NeuralDensityEncoder
- to_dict()[source]
Serialize hyperparameters and module bytes for registry-based round trips.
- class NeuralDensityEstimator(module, *, m_steps=60, lr=5e-3, device='cpu', name=None)[source]
Bases:
ParameterEstimatorM-step: responsibility-weighted MLE –
max sum_i w_i log p(x_i)by gradient ascent on the module (warm).- accumulator_factory()[source]
Return an accumulator factory for weighted neural-density batches.
- Return type:
NeuralDensityAccumulatorFactory
- 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 VAE(dim, *, latent=2, hidden=32, m_steps=120, lr=5e-3, device='cpu', name=None)[source]
Bases:
_NeuralFamilyA latent-variable
p(x)overR^dimvia a variational autoencoder.log_densityis the ELBO – a lower bound onlog p(x)– evaluated deterministically at the encoder mean so an EM log-likelihood stays monotone. Compare it with other bounded leaves whenever possible; mixing it with an exact-density leaf, such as a Gaussian or flow, compares a bound against an exact value and can under-weight the VAE. Seebuild_vae()for the full statement.
- class Flow(dim, *, hidden=32, layers=4, m_steps=80, lr=5e-3, device='cpu', name=None)[source]
Bases:
_NeuralFamilyAn exact
p(x)overR^dimvia a RealNVP coupling flow (invertible map to a standard-normal base).
- class MAF(dim, *, hidden=64, blocks=3, m_steps=80, lr=5e-3, device='cpu', name=None)[source]
Bases:
_NeuralFamilyAn exact
p(x)overR^dimvia a masked autoregressive flow (richer autoregressive dependence).
- class DiscreteAR(dim, cats, *, hidden=64, m_steps=100, lr=5e-3, device='cpu', name=None)[source]
Bases:
_NeuralFamilyAn exact, normalized
p(x)over discrete vectorsx in {0..cats-1}^dim(autoregressive, MADE-masked).
- class DPOModel(policy, ref, beta=0.1, m_steps=100, lr=1e-3, device='cpu')[source]
Bases:
SequenceEncodableProbabilityDistributionDPO over
(x, chosen, rejected)preference triples.policyis trained,refis frozen.- seq_log_density(enc)[source]
Return per-row DPO preference log likelihoods for encoded triples.
- log_density(xcr)[source]
Return the DPO log likelihood for one
(x, chosen, rejected)triple.
- prefers(x)[source]
The policy’s argmax action at
x– what the aligned policy now picks.
- sampler(seed=None)[source]
Return the sampler for the preference-scoring leaf.
- Parameters:
seed (int | None)
- Return type:
DPOModelSampler
- estimator(pseudo_count=None)[source]
Return the DPO estimator that trains the policy while keeping the reference fixed.
- Parameters:
pseudo_count (float | None)
- Return type:
DPOModelEstimator
- dist_to_encoder()[source]
Return the encoder for preference triples.
- Return type:
DPOEncoder
- to_dict()[source]
Serialize policy/reference modules and DPO hyperparameters.
- class EnergyModel(module, *, m_steps=200, lr=5e-3, noise_ratio=1, langevin_steps=40, langevin_step=0.05, device='cpu', name=None)[source]
Bases:
SequenceEncodableProbabilityDistributionlog p(x) ≈ -E(x) + cfor an energy module (module.energy(x) -> (n,)and a learned scalarlog_norm).Approximately normalized (trained by NCE);
log_densityreturns-E(x) + c. Composes like any leaf.- Parameters:
- log_density(x)[source]
Return the approximate normalized log density for one observation.
- seq_log_density(x)[source]
Return approximate normalized log densities for a batch of observations.
- sampler(seed=None)[source]
Return a Langevin sampler for the learned energy model.
- Parameters:
seed (int | None)
- Return type:
EnergyModelSampler
- estimator(pseudo_count=None)[source]
Return the NCE estimator used as the model’s M-step.
- Parameters:
pseudo_count (float | None)
- Return type:
EnergyModelEstimator
- dist_to_encoder()[source]
Return the encoder for vectorized energy-model scoring.
- Return type:
EnergyModelEncoder
- to_dict()[source]
Serialize hyperparameters and module bytes for registry-based round trips.
- class FeatureMapDensity(feature_name, inner, name=None)[source]
Bases:
SequenceEncodableProbabilityDistributionp(feature_fn(x))for a registered, deterministicfeature_fnand inner distribution.- density(x)[source]
Return the induced feature-space density at raw item
x.
- log_density(x)[source]
Return
log p(feature_fn(x))under the inner distribution.
- seq_log_density(x)[source]
Return inner log densities for an already-featurized batch.
- sampler(seed=None)[source]
Return a sampler for the inner feature-space distribution.
- Parameters:
seed (int | None)
- Return type:
FeatureMapSampler
- estimator(pseudo_count=None)[source]
Return an estimator that fits the inner estimator on registered features.
- Parameters:
pseudo_count (float | None)
- Return type:
FeatureMapEstimator
- dist_to_encoder()[source]
Return the encoder that maps raw items to feature vectors.
- Return type:
FeatureMapEncoder
- class FeatureMapEstimator(feature_name, inner, name=None)[source]
Bases:
ParameterEstimatorFits
inneronfeature_fn(x)for raw itemsx– the estimator side ofFeatureMapDensity.- accumulator_factory()[source]
Return an accumulator factory that feature-maps raw inputs before inner accumulation.
- Return type:
FeatureMapAccumulatorFactory
- feature_fn(name)[source]
Look up a registered feature function by name; raises if it was never registered.
- register_feature_fn(name, fn)[source]
Register
fn(raw item -> fixed-length vector) undernameso a leaf can carry just the name.
- class StreamingTransformer(module, device='cpu')[source]
Bases:
SequenceEncodableProbabilityDistributionWraps a live, persistently-trained module.
seq_log_density= next-tokenlog p(eval/telemetry).- Parameters:
module (Any)
device (str)
- classmethod from_config(vocab, *, d_model=128, n_layer=4, n_head=4, block=64, embedding=None, device='cpu')[source]
Build the leaf from hyperparameters (no hand-built torch module) – the declarative estimator surface.
embeddingoptionally ties a sharedCategoricalEmbeddingacross leaves.
- log_density(xy)[source]
Return the next-token log probability for one
(context, token)pair.
- predict(x)[source]
Return argmax next-token predictions for one or more contexts.
- sampler(seed=None)[source]
Return the sampler for the conditional next-token model.
- Parameters:
seed (int | None)
- Return type:
StreamingTransformerSampler
- seq_log_density(enc)[source]
Return per-row next-token log probabilities for encoded context/token pairs.
- estimator(pseudo_count=None)[source]
Return the streaming estimator that trains the live module in accumulator updates.
- Parameters:
pseudo_count (float | None)
- Return type:
StreamingTransformerEstimator
- dist_to_encoder()[source]
Return the encoder for context/token training pairs.
- Return type:
StreamingTokenEncoder
- to_dict()[source]
Serialize the module bytes and device for registry-based round trips.
- NeuralLeaf
alias of
NeuralGaussian
- SoftmaxNeuralLeaf
alias of
NeuralCategorical
- StreamingTransformerLeaf
alias of
StreamingTransformer
- class TrainingSearchResult(recipe, loss, history=None)[source]
Bases:
objectThe outcome of a multi-fidelity training search: the best recipe, its full-budget loss, and the history.
- class TrainingSpace(d_model_choices=(64, 128, 256, 512), n_layer_range=(2, 12), log10_lr_range=(-4.0, -2.0), batch_choices=(16, 32, 64, 128))[source]
Bases:
objectThe tunable axes of an LM training recipe and how a unit-cube point decodes into concrete knobs.
- Parameters:
- bounds()[source]
Return unit-cube bounds for the DOE optimizer.
- class TransformerLMEstimator(vocab, *, d_model=128, n_layer=4, n_head=4, block=64, embedding=None, lr=3e-3, device='cpu')[source]
Bases:
StreamingTransformerEstimatorA Transformer language model as a fit-ready estimator:
TransformerLMEstimator(vocab, d_model=..., ...).The clean, declarative surface – no hand-built torch module, no
Leaf(...).estimator()two-step. Drops intoMixtureEstimator/CompositeEstimatorlike any other*Estimator.embeddingoptionally ties a sharedCategoricalEmbedding(e.g. one word embedding across a mixture’s experts).TransformerLMEstimator(V, embedding=emb)andStreamingTransformer.from_config(V, embedding=emb).estimator()build the same thing.
- build_causal_lm(vocab, d_model=128, n_layer=3, n_head=4, block=64, embedding=None, gradient_checkpointing=False)[source]
Build a causal decoder-only Transformer LM (token+pos embeddings, pre-norm blocks, weight-tied head).
embeddingoptionally injects a shared tokennn.Embedding(vocab x d_model) to use in place of a fresh one – so several language models can tie the same word embedding and train it jointly (the weight-tied head follows it). Its shape must match(vocab, d_model).gradient_checkpointing=Truerecomputes block activations during backward instead of storing them – identical gradients (pinned by test) for a large activation-memory cut on deep stacks or long blocks. The flag is a plain module attribute, so it can also be toggled on an existing model – including to a per-block list/tuple of bools (one pern_layer) rather than a single all-or-nothing bool, for F6’s cost-model-driven selective policy (mixle.models.memory_efficient_training.SelectiveRecomputePolicy).
- ewc(anchor, fisher, lam=1.0)[source]
Bundle
(anchor, fisher, lambda)for.fit(..., ewc=...)(the EWC anti-forgetting penalty).
- extrapolate_learning_curve(steps, losses, *, at)[source]
Predict the loss at budget/step
atfrom a partial run’s(steps, losses)via a power-law fit.Fits
loss(t) = a + b * t^(-c)and evaluates it atatso a partial run can estimate the full-budget loss for early stopping. Falls back to the last observed loss if the fit fails.
- fisher_diagonal(leaf, x, y, *, samples=512, device='cpu', seed=0)[source]
Diagonal empirical Fisher of a classification leaf’s module on
(x, y): mean of(d log p(y|x)/dtheta)^2.
- lm_train_fn(token_ids, val_ids, *, vocab, block=64, max_epochs=3, device='cpu')[source]
Return a training callback
(recipe, budget) -> held-out nats/tokenforLM.budget in (0, 1]scales the number of epochs. A larger pretraining loop can use the same convention to scale steps or token subsets.
- snapshot(leaf_or_module)[source]
Detached clones of the module’s parameters – the anchor
theta*for an EWC penalty.
- tune_training(train, space=None, *, fidelities=(0.25, 1.0), costs=None, max_cost=20.0, n_init=None, seed=0)[source]
Run multi-fidelity BO over a training recipe.
train(recipe, budget)returns held-out loss, where lower is better.fidelitiesare the training-budget fractions the search may run at. Returns the recipe with the best full-budget loss and the full BO history.
- apply_mup_init(model, *, base_width, base_std=0.02)[source]
Re-initialize
modelin place per the muP init rules, relative tobase_width.base_stdis the hidden-role init std tuned/measured atbase_width(the mixle default,0.02, matches common transformer practice and is a reasonable base-width value on its own).model.d_modelis read as the target width, sowidth_mult = model.d_model / base_width. LayerNorm weight/bias keep their identity init (1/0, unaffected by width, matching the"input"role’s no-rescale treatment); every other bias is zero-initialized (muP does not rescale bias init); every other weight matrix is drawnNormal(0, base_std * init_std_multiplier(role, width_mult)). Also turns on muP attention-logit scaling (enable_mup_attention()) on every block – the1/head_dimQK scaling is as much a part of “the model is parametrized under muP” as the init/lr rules above, and previously being left at the standard1/sqrt(head_dim)scale was an unintentional gap between what this module documented and what it actually configured.
- classify_causal_lm_params(model)[source]
Map every named parameter of a
mixle.models.transformer.CausalLMto its muP role.tok.weight/pos.weight(embeddings) ->"input"– fan-in is the fixed vocab / block length, notd_model.LayerNorm affine params (
blocks.*.ln1/ln2, top-levelln) ->"input"– no fan-in at all, muP leaves them at their standardTheta(1)scale/shift regardless of width.head.weight-> not a separate entry: it is the samenn.Parameterastok.weight(weight tying), somodel.named_parameters()already reports it once, under"tok.weight". See the module docstring for how the muP output role is instead applied at readout time.everything else (the attention qkv/proj and MLP
Linearweights/biases inside each block) ->"hidden"– fan-in scales linearly withd_model.
- init_std_multiplier(role, width_mult)[source]
Return the muP multiplier on init std for a parameter of
roleat the givenwidth_mult.width_mult = target_width / base_width. Multiply the BASE width’s tuned/measured init std by this factor to get the init std to use at the target width:"input"->1(fan-in fixed, e.g. vocab size – no rescale)"hidden"->width_mult ** -0.5(variance1/fan_in, standard, unchanged form)"output"->width_mult ** -1(variance1/fan_in**2– an extra1/width_mult)
- lr_multiplier(role, width_mult)[source]
Return the muP multiplier on learning rate for a parameter of
roleat the givenwidth_mult.width_mult = target_width / base_width. Multiply the BASE width’s tuned lr by this factor to get the transferred lr to use at the target width:"input"->1(constant lr – the muP “don’t touch it” role)"hidden"->width_mult ** -1(the headline muP rule: lr shrinks as the model widens)"output"->width_mult ** -1(same shrink as hidden, for Adam)
- mup_param_groups(model, *, base_width, lr)[source]
Build torch optimizer param groups implementing muP’s per-role lr scaling for
model.lris the BASE (hidden-role) learning rate – the one hyperparameter tuned once, cheaply, atbase_width.model.d_modelis read as the target width. Returns a list of{"params": [...], "lr": ..., "mup_role": ...}dicts suitable fortorch.optim.Adam(groups); the"input"group’s lr is unscaled, the"hidden"/"output"groups getlr * lr_multiplier(role, width_mult)– i.e. passing the same tunedlrat any target width reproduces exactly whattransfer_lr()predicts for that role.transfer_lris the formula; this is the mechanism that applies it to a live model + optimizer.
- output_forward_multiplier(width_mult)[source]
Return the muP readout multiplier (the “c” of abc-parametrization) applied to output-role logits.
Multiply the raw
headoutput by this factor so the readout’s output scale stays width-independent at init, even though (due to weight tying, see the module docstring)head.weightitself is parametrized under the"input"rule rather than a separate"output"init/lr rule.
- transfer_init_std(base_std, base_width, target_width, *, role='hidden')[source]
Rescale
base_std(tuned/measured atbase_width) to the muP init std attarget_width.
- transfer_lr(base_lr, base_width, target_width, *, role='hidden')[source]
Rescale
base_lr(tuned atbase_width) to the muP-predicted optimum attarget_width.This is the deliverable that “collapses the ladder’s tuning bill”: tune
role="hidden"lr once, cheaply, at a smallbase_width, then call this to predict the optimal lr at any largertarget_widthwith (ideally) no further search. Defaults torole="hidden"– the dominant parameter group (attention + MLP) and the one muP’s headline lr-transfer guarantee is about.
- stream_fit(module, token_source, *, lr=3e-3, device='cpu', report_every=200, log=None)[source]
Train
moduleby streaming micro-batches fromtoken_source(a generator). The accumulator holds the PERSISTENT optimizer and trains incrementally; its payload stays(loss_sum, tokens)– the corpus is never buffered. Returns(StreamingTransformer, (loss_sum, tokens)).
- class GrammarLearningResult(model, history, validation_history=None)[source]
Bases:
FitResult[HeterogeneousPCFGDistribution]Fitted PCFG plus training and optional validation log-likelihood history.
- class HamiltonianNet(dim, hidden=(64, 64))[source]
Bases:
objectLearns a scalar
H(q, p)and exposes the symplectic-gradient dynamics it implies.dimis the dimension ofq(and ofp, always equal);hiddensizes the MLP computingHfrom the concatenated(q, p).module(the underlyingtorch.nn.Module) is exposed directly for training with an ordinary optimizer loop against derivative-matching data.- hamiltonian(q, p)[source]
H(q, p), shape(...,)– squeezes the module’s scalar output dimension.
- time_derivative(q, p)[source]
(dq/dt, dp/dt) = (dH/dp, -dH/dq)via autograd – the symplectic gradient ofhamiltonian.q/pmust be leaf tensors (requires_grad_(True)) or already part of an active graph; the returned derivatives carry gradients back throughself.module’s parameters, so this composes into a training loop that fits derivative-matching data end to end.
- class HardEMResult(model, history, validation_history=None)[source]
Bases:
FitResult[StochasticBlockGraphModel]Result from hard-EM fitting of a stochastic block model.
- class KnowledgeGraphFitResult(model, history, validation_history=None)[source]
Bases:
FitResult[TransEKnowledgeGraphModel]Result from TransE margin fitting.
- class PartiallyObservableMarkovDecisionProcessFilterResult(beliefs, log_likelihood, predictive_observation_probs)[source]
Bases:
objectBelief trajectories, log likelihood, and predictive observation terms.
- class PartiallyObservableMarkovDecisionProcessFitResult(model, history, validation_history=None)[source]
Bases:
FitResult[PartiallyObservableMarkovDecisionProcessModel]Baum-Welch style fit result for known-action PartiallyObservableMarkovDecisionProcess sequences.
- class PartiallyObservableMarkovDecisionProcessModel(transition, observation, initial_belief=None, rewards=None, name=None)[source]
Bases:
objectFinite-state PartiallyObservableMarkovDecisionProcess with action-conditioned transitions and observations.
transition[a, i, j]is P(S_t=j | S_{t-1}=i, A_t=a).observation[a, j, o]is P(O_t=o | S_t=j, A_t=a).- Parameters:
transition (Any)
observation (Any)
initial_belief (Any | None)
rewards (Any | None)
name (str | None)
- belief_update(belief, action, observation)[source]
Update a belief after taking
actionand seeingobservation.
- filter(actions, observations, initial_belief=None)[source]
Run the forward filter and return posterior beliefs and log likelihood.
- sequence_log_likelihood(actions, observations, initial_belief=None)[source]
Return log P(observations | actions, model).
- forward_backward(actions, observations, initial_belief=None)[source]
Return state marginals, transition marginals, and sequence log likelihood.
- predict_observation(belief, action)[source]
Return P(O_t | belief, action) before observing O_t.
- expected_reward(belief, action)[source]
Return E[R | belief, action] when rewards were supplied.
- class PartiallyDirectedGraph(directed_edges, undirected_edges, variable_names)[source]
Bases:
objectPartially directed graph after collider orientation.
- class PCFGParseNode(label, span, log_prob, rule_index, rule_type, children=(), value=None)[source]
Bases:
objectNode in a Viterbi parse tree.
- Parameters:
- class PINNRegression(module, residual_fn, domain, *, noise=1.0, residual_weight=1.0, n_collocation=64, m_steps=40, lr=0.01, seed=0, name=None, device=None)[source]
Bases:
NeuralGaussianNeuralGaussianplus a PDE/ODE-residual penalty evaluated on sampled collocation points.domainis a(low, high)pair of per-dimension box bounds for collocation sampling;residual_fncomputes the physics residual (see module docstring);residual_weightscales the penalty relative to the data-fit NLL;n_collocationis how many collocation points are drawn fresh every M-step.- Parameters:
- estimator(pseudo_count=None)[source]
Return the estimator that combines weighted data fit with residual collocation penalties.
- Parameters:
pseudo_count (float | None)
- Return type:
PINNRegressionEstimator
- dist_to_encoder()[source]
Return the neural-Gaussian encoder for
(x, y)observation pairs.- Return type:
NeuralGaussianEncoder
- to_dict()[source]
Serialize the module, residual function reference, domain, and PINN hyperparameters.
- class PINNRegressionEstimator(module, residual_fn, domain, *, noise=1.0, residual_weight=1.0, n_collocation=64, m_steps=40, lr=0.01, seed=0, name=None, device=None)[source]
Bases:
NeuralGaussianEstimatorEM estimator for
PINNRegression: the M-step adds a residual penalty on fresh collocation points to the same weighted-NLL gradient descentNeuralGaussianEstimatorruns.Collocation sampling is deterministic given
seed(a privatenumpy.random.RandomState, advanced once per M-step) – refitting with the same seed draws the same collocation batches.- Parameters:
- accumulator_factory()[source]
Return the neural-Gaussian accumulator factory for weighted observation pairs.
- Return type:
NeuralGaussianAccumulatorFactory
- class QATWrapper(base, *, bits=4, clip_percentile=None, enabled=True)[source]
Bases:
ModuleWrap an
nn.Linearso its weight is straight-through fake-quantized on every forward call: the module computesF.linear(x, fake_quantize(weight), bias)instead ofF.linear(x, weight, bias). Bias stays fp32, matching PTQ’s scheme (mixle.task.quantize.quantize_mlp()) where only weights are quantized.Drop-in composition:
QATWrapper(linear)has the sameforward(x) -> Tensorcontract as theLinearit wraps, so it slots into any module tree (seeapply_qat()) without the surrounding model or training loop changing at all.- forward(x)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- apply_qat(model, *, bits=4, clip_percentile=None)[source]
Replace every
nn.Linearundermodelin place with aQATWrapper, so the whole model trains quantization-aware (straight-through int4 fake-quant on every Linear weight, every forward call). Mirrorsmixle.experimental.program.lora’s wrapping pattern: walknamed_children, swapLinearleaves, recurse into everything else. Weight-tied layers (e.g.CausalLM.headsharingtok.weight) wrap cleanly – only the wrapped module’sforwardchanges, the underlyingnn.Parameter(and anything else pointing at it) is untouched. Returnsmodel(mutated in place) for chaining.
- fake_quantize(x, *, bits=4, clip_percentile=None)[source]
Straight-through fake-quantize
xtobits(int4 or int8, permixle.task.quantize._QMAX): forward returns the real quantize->dequantize round trip, backward passes the gradient through unchanged (STE).
- fake_quantize_int4(x, *, clip_percentile=None)[source]
fake_quantize(x, bits=4)– the int4 case this roadmap item targets.
- set_fake_quant_enabled(model, enabled)[source]
Toggle every
QATWrapperundermodelon/off in place.enabled=Falseruns the model at its real fp32 weights (e.g. to check that QAT training didn’t wreck full-precision quality);enabled=True(the default afterapply_qat()) restores the fake-quant forward. Returnsmodelfor chaining.
- class PoissonRegressionNeuralNetwork(module, engine=None, precision=None)[source]
Bases:
objectA Torch count-regression wrapper optimized by Poisson log likelihood.
The wrapped module predicts log rates. Observed counts must be non-negative and match the module output shape after one-dimensional inputs are promoted to column vectors.
- Parameters:
module (Any)
engine (Any | None)
precision (Any | None)
- parameters()[source]
Return trainable parameters of the wrapped log-rate module.
- log_rate_tensor(x)[source]
Return predicted log rates as a Torch tensor.
- log_likelihood(x, y)[source]
Return the summed Poisson count log likelihood.
- fit(x, y, max_its=500, lr=0.01, optimizer='adam', tol=1.0e-7, out=None, print_iter=100, return_result=False, restore_best=True)[source]
Maximize the Poisson count log likelihood.
- predict_rate_tensor(x)[source]
Return predicted Poisson rates as a Torch tensor.
- predict_rate(x)[source]
Return predicted Poisson rates as a NumPy array.
- class RandomForestConditional(forest, task, sigma=None, n_features=None, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionFitted random forest viewed as a conditional distribution p(y | x).
Observations are (x, y) pairs: x is a feature vector and y is a class label (classification) or a real target (regression). seq_log_density returns log p(y | x) – predict_log_proba for classification, a Gaussian residual density with scale sigma for regression.
- Parameters:
- density(x)[source]
Return
p(y | x)for one feature/target pair.
- log_density(x)[source]
Return
log p(y | x)for one feature/target pair.
- seq_log_density(x)[source]
Return per-row conditional log densities for encoded
(X, y)data.
- sample_y(x, rng)[source]
Draw target values from the fitted conditional forest at feature rows
x.- Parameters:
x (Any)
rng (RandomState)
- Return type:
- sampler(seed=None)[source]
Return a conditional sampler for drawing targets given features.
- Parameters:
seed (int | None)
- Return type:
RandomForestConditionalSampler
- estimator(pseudo_count=None)[source]
Return a fresh estimator with the same task, name, and keyed-accumulation settings.
- Parameters:
pseudo_count (float | None)
- Return type:
RandomForestEstimator
- dist_to_encoder()[source]
Return the encoder for feature/target observation pairs.
- Return type:
RandomForestEncoder
- class RandomForestEstimator(task='auto', n_estimators=100, max_depth=None, min_samples_split=2, min_samples_leaf=1, max_features='auto', random_state=None, min_sigma=1.0e-3, name=None, keys=None)[source]
Bases:
ParameterEstimatorEstimator that fits a native (numpy) random forest as a conditional leaf.
task is ‘classification’, ‘regression’, or ‘auto’ (inferred from the dtype of y). The forest hyperparameters (n_estimators, max_depth, min_samples_split, min_samples_leaf, max_features, random_state) are passed straight to the native ensemble. estimate() trains in one pass on the accumulated weighted data; there is no EM iteration, so drive it with optimize(max_its=1) or call the seq_encode / accumulate / estimate path directly.
- Parameters:
- accumulator_factory()[source]
Return an accumulator factory for weighted feature/target buffers.
- Return type:
RandomForestAccumulatorFactory
- class StochasticBlockGraphModel(block_probs, block_assignments, directed=False, self_loops=False, name=None)[source]
Bases:
objectBernoulli stochastic block model with fixed node assignments.
- Parameters:
- classmethod fit_mle(adjacency, block_assignments, num_blocks=None, directed=False, self_loops=False, pseudo_count=0.0, prior_p=0.5, name=None)[source]
Thin shim delegating to
fit_stochastic_block_mle(kept for the classmethod-fit call API).
- log_likelihood(adjacency)[source]
Return the Bernoulli SBM log likelihood.
- sample(seed=None)[source]
Draw one graph from the block model.
- class TransEKnowledgeGraphModel(entity_embeddings, relation_embeddings, entity_names=None, relation_names=None, name=None)[source]
Bases:
objectDependency-free TransE model with a NumPy margin objective.
- Parameters:
entity_embeddings (Any)
relation_embeddings (Any)
entity_names (Sequence[Any] | None)
relation_names (Sequence[Any] | None)
name (str | None)
- classmethod random(num_entities, num_relations, embedding_dim=16, seed=None, scale=0.01, entity_names=None, relation_names=None, name=None)[source]
Create a randomly initialized model.
- distance_triples(triples)[source]
Return squared TransE distances ||h + r - t||^2.
- score_triples(triples)[source]
Return TransE scores; higher is more plausible.
- margin_loss(positive_triples, negative_triples, margin=1.0)[source]
Return the pairwise TransE ranking loss.
- negative_sample(triples, seed=None, corrupt='tail')[source]
Corrupt heads or tails to produce negative triples.
- fit_margin(positive_triples, negative_triples=None, margin=1.0, lr=0.01, max_its=100, seed=None, normalize_entities=True)[source]
Fit embeddings with simple stochastic subgradient descent.
- class TruncatedDirichletProcessMixtureFitResult(model, responsibilities, history)[source]
Bases:
objectFitted truncated DPM plus variational responsibilities and history.
- class TruncatedDirichletProcessMixtureModel(components, alpha=1.0, gamma=None, weights=None, name=None)[source]
Bases:
objectTruncated stick-breaking mixture over existing mixle component models.
- Parameters:
- property expected_log_weights: ndarray
Return E_q[log pi_k] under the variational stick posteriors.
- component_log_density(x)[source]
Return component log densities for one observation.
- log_density(x)[source]
Return the finite-truncation mixture log density for one observation.
- density(x)[source]
Return the finite-truncation mixture density for one observation.
- responsibilities(data, expected=True)[source]
Return posterior component probabilities for observations.
- effective_components(threshold=0.01)[source]
Count components with posterior mean stick weight above
threshold.
- baum_welch_pomdp(sequences, num_states, num_actions, num_observations, initial_model=None, max_its=50, tol=1.0e-8, pseudo_count=1.0e-3, seed=None)[source]
Fit a known-action finite PartiallyObservableMarkovDecisionProcess by Baum-Welch/EM.
- Parameters:
- Return type:
PartiallyObservableMarkovDecisionProcessFitResult
- discrete_conditional_mutual_information(data, x, y, given=())[source]
Estimate I(X;Y | Z) from categorical samples using empirical counts.
- expected_log_stick_weights(gamma)[source]
Return E_q[log pi_k] for truncated Beta stick posteriors.
- fit_erdos_renyi_mle(adjacency, directed=False, self_loops=False, pseudo_count=0.0, prior_p=0.5, name=None)[source]
Conjugate-Bernoulli MLE of the edge probability (module-level estimation, not a classmethod-fit).
- fit_stochastic_block_mle(adjacency, block_assignments, num_blocks=None, directed=False, self_loops=False, pseudo_count=0.0, prior_p=0.5, name=None)[source]
Conjugate-Bernoulli MLE of block edge probabilities for fixed assignments (module-level estimation).
- fit_truncated_dpm(data, initial_components, component_estimator, alpha=1.0, max_its=50, tol=1.0e-8, sort_components=True, name=None)[source]
Fit a truncated DP mixture by coordinate-ascent variational updates.
The component M-steps are delegated to ordinary
mixle.statsestimators. This keeps component likelihood math and sufficient statistics in their distribution modules.- Parameters:
- Return type:
TruncatedDirichletProcessMixtureFitResult
- fit_induced_pcfg(data, terminal_estimators, max_nonterminals, initial_model=None, vdata=None, max_its=10, init_p=1.0, seed=None, terminal_rule_mass=0.5, rule_pseudo_count=1.0e-3, prune_threshold=0.0, min_rule_prob=0.0, start='S', name=None)[source]
Fit an induced heterogeneous PCFG and track train/validation likelihoods.
- Parameters:
terminal_estimators (Sequence[ParameterEstimator])
max_nonterminals (int)
initial_model (HeterogeneousPCFGDistribution | None)
max_its (int)
init_p (float)
seed (int | None)
terminal_rule_mass (float)
rule_pseudo_count (float | None)
prune_threshold (float)
min_rule_prob (float)
start (Any)
name (str | None)
- Return type:
GrammarLearningResult
- gaussian_conditional_independence(data, x, y, given=(), alpha=0.05, ridge=1.0e-10)[source]
Fisher-z Gaussian conditional independence test.
- gaussian_partial_correlation(data, x, y, given=(), ridge=1.0e-10)[source]
Return partial correlation rho_xy.given for continuous data.
- grammar_rule_table(model)[source]
Return a flat, inspectable rule table for learned PCFGs.
- hard_em_stochastic_block_model(adjacency, num_blocks, max_its=20, restarts=1, seed=None, directed=False, self_loops=False, pseudo_count=1.0, prior_p=0.5)[source]
Classification/hard-EM fit for a stochastic block model.
- leapfrog_rollout(net, q0, p0, dt, n_steps)[source]
Symplectic leapfrog integration of
net’s learned (or untrained) Hamiltonian flow.Returns
(qs, ps), each shaped(n_steps + 1, *q0.shape)– the trajectory including the initial state. A symplectic integrator is the right numerical counterpart to a conservative continuous-time system: unlike a generic (e.g. Euler) integrator, its energy error stays bounded and oscillates rather than drifting monotonically over long rollouts.
- learn_pc_skeleton(data, variable_names=None, alpha=0.05, max_cond_set=2, method='gaussian')[source]
Learn a PC-style undirected skeleton from conditional independences.
- build_autoregressive_categorical(dim, n_categories, *, hidden=64)[source]
An autoregressive neural density over discrete vectors
x in {0..C-1}^dim– exact, normalizedp(x).The continuous flows/VAE above model
R^d; heterogeneous data is also categorical. This factorizesp(x) = prod_i p(x_i | x_{<i})with a MADE-masked network whose per-coordinate softmax is each conditional, so the density is exactly normalized (sums to 1 over the finite space) and can be compared directly with count/categorical families.log_densitysums the picked log-softmax logits;samplefills the vector one coordinate at a time. Another ready module forNeuralDensity; the adapter is unchanged.
- build_conditional_autoregressive_categorical(x_dim, y_dim, n_categories, *, hidden=64)[source]
An autoregressive categorical conditioned on
x: exactp(y | x)over discretey in {0..C-1}^y_dim.The conditional sibling of
build_autoregressive_categorical()and the discrete counterpart tobuild_conditional_flow(). It factorizesp(y | x) = prod_i p(y_i | y_{<i}, x)with a MADE-masked net overyinto whichxis injected unmasked (degree 0, so every coordinate may depend onx). Each per-coordinate softmax is exactly a conditional, so the density is exactly normalized and comparable to other exact discrete conditional leaves. Exposeslog_density(x, y)andsample_given(x)– the contract aNeuralConditionalDensityadapts.
- build_conditional_flow(x_dim, y_dim, *, hidden=32, layers=4)[source]
A conditional coupling flow: an exact
p(y | x)whose transform ofyis conditioned onx.The exact-density counterpart to
build_mdn(). Each affine-coupling layer’s shift/scale networks take both the passed-throughycoordinates andx, so the whole invertibley-transform bends with the input – capturing within-``y`` dependence (e.g.y2a nonlinear function ofy1) that a single-GaussianNeuralGaussian(isotropic mean-only) cannot, while keeping an exact log-density rather than a bound. Needsy_dim >= 2for the coupling to be non-trivial. Exposeslog_density(x, y)andsample_given(x)– the contract aNeuralConditionalDensityadapts.
- build_convex_energy_net(dim, *, hidden=64, layers=3)[source]
An input-convex MLP energy
E(x): R^dim -> R, convex inxBY CONSTRUCTION – ready to wrap in anEnergyModelexactly likebuild_energy_net(). A convex energy gives Langevin sampling (EnergyModelSampler) a unimodal target with no spurious local minima to get stuck in, and gives any consumer of the fitted energy a certified-convex scalar-valued potential (e.g. a verified optimum for a downstreammixle.doesearch over-E(x)). See_convex_energy_net_class()for the construction.
- build_coupling_flow(dim, *, hidden=32, layers=4)[source]
A RealNVP coupling flow over
R^dimwith an exactlog_density(x)andsample(n)– ready to wrap.Alternating affine-coupling layers map data to a standard-normal base;
log_densityis the base log-prob plus the log-determinant of the (triangular) Jacobian. A minimal, correct instance of the density module aNeuralDensityadapts – swap in any other module with the same two methods.
- build_energy_net(dim, *, hidden=64, layers=3)[source]
An MLP energy
E(x): R^dim -> R(plus a learned scalarlog_norm) – ready to wrap in an EnergyModel.Lower energy = higher (unnormalized) density.
log_normis the NCE-learned normalizer, so the pairedEnergyModelscores-E(x) + log_norm. Swap in any module exposingenergy(x) -> (n,), alog_normparameter and adimattribute.
- build_maf(dim, *, hidden=64, blocks=3)[source]
A masked autoregressive flow over
R^dim– an exact density that factorizesp(x)by the chain rule, eachp(x_i | x_{<i})an affine map with autoregressive (MADE-masked) mean and log-scale.Unlike the coupling flow it conditions every coordinate on all earlier ones (a richer autoregressive dependence), and unlike the VAE its
log_densityis exact. It can therefore be compared directly with a Gaussian, a flow, or another exact-density leaf. Sampling is the sequential inverse (one coordinate at a time). Another ready module forNeuralDensity; the adapter is unchanged.
- build_mdn(x_dim, y_dim, *, k=5, hidden=32, layers=2)[source]
A mixture density network:
p(y | x) = sum_k pi_k(x) N(y; mu_k(x), diag sigma_k(x)^2)– ready to wrap.A shared MLP body maps
xto three heads – mixing logits, component means, and (log) component scales – so the entire conditional law is a function ofx: multimodal (severalmu_k) and heteroscedastic (input-dependentsigma_k). Exposeslog_density(x, y)(a log-sum-exp over components) andsample_given(x)(pick a component bypi, then a Gaussian), the contract aNeuralConditionalDensityadapts.
- build_product_energy_net(experts)[source]
Combine expert energy modules multiplicatively: one module with
energy(x) = sum_k experts[k].energy(x).A product of experts,
p(x) ∝ prod_k p_k(x)– a conjunction (each expert a soft constraint, the product their intersection), as opposed to a mixture’s disjunction. This is the ENERGY-BASED, arbitrary-density complement tomixle.ops.product_of_experts(), which pools tractable families (Categorical, Gaussian) in closed form but deliberately raises on the general continuous case because the product normalizer is then intractable. That intractable normalizer is exactly what the energy stack already handles: wrap the result here in anEnergyModelto fit the sharedlog_normby NCE and sample by Langevin, no new machinery.Each expert must expose
energy(x) -> (n,)and adimattribute (e.g. anybuild_energy_net()/build_convex_energy_net()module, all sharing one input dim), and stays individually inspectable via the built module’s.experts/.expert_energies(x). Fit it in one line:model = EnergyModel(build_product_energy_net([expert_a, expert_b]), m_steps=250)
- build_projection_leaf(d_x, d_y, *, encoder_x=None, encoder_y=None, proj_dim=None, hidden=64, freeze_encoders=True, temperature=0.07)[source]
A contrastive (InfoNCE / CLIP-style) conditional
p(y | x)between two embedding spaces – ready to wrap.This is the stage-1 multimodal pattern – frozen encoder -> trainable projection -> frozen encoder – stated with no domain nouns.
encoder_x/encoder_yare any torch module mapping a raw item to ad_x/d_yembedding; both default tonn.Identity(), sox/ymay already BE the embeddings (pass precomputed vectors straight in, no backbone required). Encoders are frozen by default (freeze_encoders=True): their parameters getrequires_grad_(False)and the module is pinned ineval()regardless of the outertrain()/eval()calls the M-step makes, so no dropout/batchnorm noise leaks into a “frozen” backbone and no gradient ever reaches it. The only trainable piece is a small projection head per side (d_x/d_y->hidden->proj_dim, defaultproj_dim = min(d_x, d_y)) mapping BOTH embeddings into one shared, L2-normalized space – the CLIP design (two projections into a shared space), not a single asymmetricx -> yregression – so the same leaf answers “which y matches this x” and “which x matches this y”.log_density(x, y)returns, per row, the (negative) SYMMETRIC INFONCE loss for a batch ofnpaired embeddings: every row’s projected pair is scored against every OTHER row in the batch as a negative, in both directions (x -> yandy -> x), log-softmax-normalized over the batch dimension, then averaged. That is exactly what the sharedNeuralConditionalDensityM-step already does withlog_density– weight it and sum it – so no separate loss path is needed: the M-step’s responsibility-weighted-NLL gradient ascent onlog_densityis InfoNCE training, “for free” from the adapter’s existing contract. As withbuild_vae()’s ELBO, this is an honest score against itself (or another leaf scored the same batch-relative way) rather than a calibratedlog p(y | x)– there is no way to integrate a softmax-over-the-current-batch score to 1 over ally. A batch of a single row has no negatives to contrast against, solog_densityreturns0for it rather than raising.sample_givenis not defined – a contrastive leaf is discriminative (it scores/ranks pairs); it has no generativep(y | x)to draw from. Retrieve a matchingyby comparingmodule.embed_x(x)againstmodule.embed_y(candidates)(cosine similarity in the shared space) instead.
- build_vae(dim, *, latent=2, hidden=32)[source]
Build a variational autoencoder over
R^dim.An amortized encoder
q(z | x)and a decoderp(x | z)(diagonal-Gaussian, learned observation scale) are trained by the ELBO with the reparameterization trick. This is a different family from a flow: structure is represented through a low-dimensional latent rather than an invertible map, while the sameNeuralDensityadapter can still use it because it exposes the same two methods.log_density(x)returns the ELBO, a lower bound onlog p(x), not the exact value. Compare VAE leaves with other bounded leaves whenever possible. Mixing a VAE with an exact-density leaf, such as a Gaussian or flow, compares a bound against an exact value and can under-weight the VAE.log_densityis deterministic: it evaluates the ELBO at the encoder meanz = mu(x)(norandnresample), so repeated scoring of the samexis bit-identical and an EM log-likelihood stays monotone. Training still uses the reparameterized sample (training=True) for an unbiased gradient.
- make_deep_set(element_dim, phi_hidden, latent_dim, rho_hidden, output_dim=1, *, pooling='mean')[source]
A Deep Sets network (Zaheer et al. 2017): invariant to any permutation of the set axis, by construction.
Input shape
(..., set_size, element_dim): a per-element MLPphi(shared weights, applied identically to every element –torch.nn.Linearalready broadcasts over all leading dims, so reusingmake_mlp()forphigives exactly that) maps each element to alatent_dimcode; a permutation-invariant pool (pooling="mean"/"sum"/"max", taken over the set axis) aggregates the codes into one order-independent summary; a second MLPrhomaps the summary to the output. Becausephiis applied identically per element and the pool is a symmetric function, the output is exactly unchanged by any permutation of the set axis – true for any weights, trained or not, unlike e.g. training on many random orderings and hoping the network learns invariance.The returned module is a plain
torch.nn.Module, trainable with any ordinary Torch optimizer loop over(set_size, element_dim)-shaped inputs. Note:NeuralGaussian’s accumulator flattens each observation to a 1-D feature vector (reshape(n, -1)) before the M-step, which destroys the set axis this module needs – so it is not a drop-in wrapper for set-shaped data asmake_mlp()/make_monotonic_mlp()are for flat feature vectors. Use this module directly with a custom training loop (or through a wrapper that preserves the set axis) for a fixed set size.
- make_mlp(input_dim, hidden_dims, output_dim=1, activation='tanh')[source]
Create a simple fully connected Torch MLP.
- make_monotonic_mlp(input_dim, hidden_dims, output_dim=1, *, increasing=True)[source]
A fully connected Torch MLP that is monotonic in every input dimension jointly, BY CONSTRUCTION.
Each layer’s weight matrix is reparameterized through
softplusbefore use, so every weight is strictly non-negative; composed with the (smooth, strictly increasing)Softplusactivation, a non-negative-weight affine map followed by an increasing activation is itself increasing, and that property is closed under composition – so the whole network is provably non-decreasing in every input coordinate, with no penalty term and no post-hoc check needed.increasing=Falsenegates the output, giving a network non-increasing in every coordinate instead.This is a hard architectural constraint (unlike
PINNRegression’s soft residual penalty): the guarantee holds at every point in input space, not just where training data landed. Drops into the same wrappers asmake_mlp()–NeuralGaussianfor regression,NeuralCategoricalfor classification – no other changes needed. Only jointly monotonic in ALL inputs; a network monotonic in some coordinates and free in others needs a two-path (monotonic + unconstrained) variant, not built here.
- mean_stick_weights(gamma)[source]
Return E_q[pi_k] under independent Beta stick posteriors.
- orient_v_structures(skeleton)[source]
Orient unshielded colliders i -> k <- j using separating sets.
- Parameters:
skeleton (CausalSkeleton)
- Return type:
PartiallyDirectedGraph
- pcfg_log_likelihood(model, data)[source]
Return total PCFG log likelihood on raw sequences.
- sample_crp_assignments(num_obs, alpha, seed=None)[source]
Sample Chinese-restaurant-process assignments and table counts.
- stick_breaking_weights(stick_fractions, residual=True)[source]
Convert stick fractions into mixture weights.
When
residualis true, the returned vector has one extra final entry containing the remaining stick mass. This is the usual finite truncation.
- viterbi_parse(model, sequence)[source]
Return the maximum-probability CKY parse under a heterogeneous PCFG.
Submodules¶
- mixle.models._forest module
- mixle.models._kernels module
- mixle.models._neural_serial module
- mixle.models._result module
- mixle.models.coarsening module
- mixle.models.compress module
- mixle.models.continual module
- mixle.models.dependence module
- mixle.models.dirichlet_process_mixture module
- mixle.models.dpo_leaf module
- mixle.models.embedding module
- mixle.models.energy module
- mixle.models.eval_harness module
- mixle.models.feature_map module
- mixle.models.gaussian_process module
- mixle.models.grad_leaf module
- mixle.models.grammar module
- mixle.models.hamiltonian module
- mixle.models.knowledge_graph module
- mixle.models.language_model module
- mixle.models.memory_efficient_training module
- mixle.models.mixture_density module
- mixle.models.moe module
- mixle.models.moment_propagation module
- mixle.models.mup module
- mixle.models.neural module
- mixle.models.neural_density module
- mixle.models.neural_families module
- mixle.models.neural_leaf module
- mixle.models.partially_observable_markov_decision_process module
- mixle.models.pinn module
- mixle.models.qat module
- mixle.models.quotient module
- mixle.models.random_forest module
- mixle.models.random_graph module
- mixle.models.self_distillation module
- mixle.models.sigma_weighted_projection module
- mixle.models.softmax_leaf module
- mixle.models.sorted_profile_quantizer module
- mixle.models.sparse_gaussian_process module
- mixle.models.sparsity_2_4 module
- mixle.models.streaming_transformer_leaf module
- mixle.models.train_search module
- mixle.models.transformer module
- mixle.models.unified_quantizer module