mixle.models.softmax_leaf module¶
A neural classifier as a Mixle conditional-density leaf: p(y | x) = softmax(module(x)).
The discriminative sibling of NeuralGaussian. NeuralCategorical(module) wraps
a Torch module that emits k logits as a mixle distribution over observations (x, y) with y an integer
class index. It implements the full SequenceEncodableProbabilityDistribution contract, so it drops into
MixtureDistribution / CompositeDistribution / HMM emissions like any leaf – and its EM M-step is a
responsibility-weighted cross-entropy gradient step on the module (warm-started across EM iterations =>
generalized EM). The model’s seq_log_density IS -cross_entropy(module(x), y): the objective is the
leaf’s log-density, never a user-supplied loss closure.
This is the leaf that the declarative Categorical(logits=Net(...)) PPL slot lowers to, and the component
that makes a Mix([Categorical(logits=Net(...)), ...]) a mixture of neural classifiers fit by ordinary EM.
Requires torch. The leaf is conditional: predict(x) and sampler().sample_given(x) work; sample() raises
because the model has no marginal p(x). This is the same conditional contract used by NeuralGaussian and
RandomForestConditional.
- 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 NeuralCategoricalSampler(dist, seed=None)[source]
Bases:
DistributionSamplerConditional sampler over class labels for
NeuralCategorical.- Parameters:
dist (NeuralCategorical)
seed (int | None)
- sample(size=None, *, batched=True)[source]
Raise because the leaf defines
p(y | x)and has no marginalp(x).
- class NeuralCategoricalEncoder[source]
Bases:
DataSequenceEncoderEncode feature/class pairs for neural-categorical scoring and fitting.
- class NeuralCategoricalAccumulator[source]
Bases:
SequenceEncodableStatisticAccumulatorBuffer weighted feature/class batches for the neural-categorical M-step.
- update(xy, weight, estimate)[source]
Add one weighted feature/class pair to the accumulator.
- seq_update(enc, weights, estimate)[source]
Add an encoded batch and responsibility weights to the accumulator.
- 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 categorical accumulator.
- Parameters:
other (Any)
- Return type:
NeuralCategoricalAccumulator
- from_value(value)[source]
Restore accumulator buffers from a value tuple.
- Parameters:
value (tuple)
- Return type:
NeuralCategoricalAccumulator
- acc_to_encoder()[source]
Return the encoder expected by this accumulator.
- Return type:
NeuralCategoricalEncoder
- class NeuralCategoricalAccumulatorFactory[source]
Bases:
StatisticAccumulatorFactoryFactory for neural-categorical accumulators.
- make()[source]
Create a fresh accumulator.
- Return type:
NeuralCategoricalAccumulator
- class NeuralCategoricalEstimator(module, m_steps=40, lr=0.01, name=None, batch_size=None, device='cpu', ewc=None)[source]
Bases:
ParameterEstimatorEM estimator for a
NeuralCategorical: the M-step ism_stepsof responsibility-weighted cross-entropy gradient on the module (the module is warm-started across EM iterations => generalized EM).The weighted CE is normalized by the responsibility mass
sum(w)so the M-step is scale-invariant to the responsibility magnitude (the easy bug: an unnormalized weighted loss makes the step size track cluster size).- Parameters:
- accumulator_factory()[source]
Return an accumulator factory for weighted classification batches.
- Return type:
NeuralCategoricalAccumulatorFactory
- SoftmaxNeuralLeaf
alias of
NeuralCategorical
- SoftmaxNeuralLeafEstimator
alias of
NeuralCategoricalEstimator