mixle.models.neural module

Torch neural-network wrappers trained through Mixle objective utilities.

The wrappers expose Gaussian regression and categorical classification models with consistent log-likelihood objectives, convergence diagnostics, precision handling, and prediction helpers.

class GaussianRegressionNeuralNetwork(module, noise=1.0, engine=None, precision=None)[source]

Bases: object

A 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.

Return type:

Iterable[Any]

property noise: float

Return the fitted observation standard deviation.

predict_tensor(x)[source]

Return module predictions as a Torch tensor on the configured engine.

Parameters:

x (Any)

Return type:

Any

log_likelihood(x, y)[source]

Return the summed Gaussian regression log likelihood.

Parameters:
Return type:

Any

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. Set return_result=True for the full objective diagnostics.

Parameters:
Return type:

Any

predict(x)[source]

Return mean predictions as a NumPy array.

Parameters:

x (Any)

Return type:

ndarray

class CategoricalClassificationNeuralNetwork(module, engine=None, precision=None)[source]

Bases: object

A 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_objective so 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.

Return type:

Iterable[Any]

logits_tensor(x)[source]

Return raw class logits for x as a Torch tensor.

Parameters:

x (Any)

Return type:

Any

log_likelihood(x, y)[source]

Return the summed categorical log likelihood for integer labels.

Parameters:
Return type:

Any

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.

Parameters:
Return type:

Any

predict_proba_tensor(x)[source]

Return class probabilities for x as a Torch tensor.

Parameters:

x (Any)

Return type:

Any

predict_proba(x)[source]

Return class probabilities for x as a NumPy array.

Parameters:

x (Any)

Return type:

ndarray

predict(x)[source]

Return maximum-probability class labels for x.

Parameters:

x (Any)

Return type:

ndarray

class PoissonRegressionNeuralNetwork(module, engine=None, precision=None)[source]

Bases: object

A 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.

Return type:

Iterable[Any]

log_rate_tensor(x)[source]

Return predicted log rates as a Torch tensor.

Parameters:

x (Any)

Return type:

Any

log_likelihood(x, y)[source]

Return the summed Poisson count log likelihood.

Parameters:
Return type:

Any

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.

Parameters:
Return type:

Any

predict_rate_tensor(x)[source]

Return predicted Poisson rates as a Torch tensor.

Parameters:

x (Any)

Return type:

Any

predict_rate(x)[source]

Return predicted Poisson rates as a NumPy array.

Parameters:

x (Any)

Return type:

ndarray

predict(x)[source]

Return rounded count predictions as integer NumPy values.

Parameters:

x (Any)

Return type:

ndarray

make_mlp(input_dim, hidden_dims, output_dim=1, activation='tanh')[source]

Create a simple fully connected Torch MLP.

Parameters:
Return type:

Any

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 softplus before use, so every weight is strictly non-negative; composed with the (smooth, strictly increasing) Softplus activation, 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=False negates 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 as make_mlp()NeuralGaussian for regression, NeuralCategorical for 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.

Parameters:
Return type:

Any

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 MLP phi (shared weights, applied identically to every element – torch.nn.Linear already broadcasts over all leading dims, so reusing make_mlp() for phi gives exactly that) maps each element to a latent_dim code; a permutation-invariant pool (pooling="mean"/"sum"/"max", taken over the set axis) aggregates the codes into one order-independent summary; a second MLP rho maps the summary to the output. Because phi is 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 as make_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.

Parameters:
Return type:

Any