mixle.inference.objectives module¶
Generic objective optimization and variational projection utilities.
This module is intentionally model-facing rather than engine-facing: callers provide objective functions, distributions provide their own scoring math, and compute engines provide only tensor arithmetic/autograd.
- class ObjectiveFitResult(model, value, iterations, history=(), converged=False, initial_value=None, final_delta=None, maximize=True, best_value=None, best_iteration=None, final_gradient_norm=None)[source]
Bases:
objectOptimization result for arbitrary differentiable objectives.
By default objective helpers restore the best trainable state seen during optimization. In that mode
valueandmodelrefer tobest_value/best_iterationwhilehistorystill records every attempted iterate andfinal_deltadescribes the last attempted step.- Parameters:
- model: Any
- value: float
- iterations: int
- converged: bool = False
- maximize: bool = True
- as_tuple()[source]
Return the historical
(model, value)shape used by fit helpers.
- property objective_change: float | None
Return the signed objective change from the start of optimization.
- class ObjectiveParameter(name, value, constraint='real')[source]
Bases:
objectNamed trainable parameter for arbitrary differentiable objectives.
Supported constraints are
real,positive/positive_vector/positive_matrix,unit_interval,simplex/simplex_vector,row_simplex_matrix,column_simplex_matrix,greater_than:<name>, andless_than:<name>. Coupled bound constraints refer to an earlier parameter in the same set. The optimizer stores unconstrained raw tensors and presents constrained values to the objective callable.- name: str
- value: Any
- constraint: str = 'real'
- class ObjectiveParameterSet(parameters, engine=None, precision=None, torch=None)[source]
Bases:
objectEngine-backed named parameters for user-supplied objectives.
- Parameters:
parameters (Any)
engine (Any | None)
precision (Any | None)
torch (Any | None)
- class ExpectedLogDensity(weights=None, normalize=False)[source]
Bases:
objectObjective
sum_i w_i log q_model(x_i)for encoded observations.
- class ObjectiveSum(*objectives)[source]
Bases:
objectAdd several model objectives into one scalar objective.
- Parameters:
objectives (ObjectiveCallable)
- class UnnormalizedLogLikelihood(log_unnormalized, log_partition=None, partition_enc=None, reference_log_density=None, weights=None, normalize=False)[source]
Bases:
objectObjective for models specified by unnormalized log likelihoods.
The objective is
sum_i w_i * log f_theta(x_i) - sum_i w_i * log Z(theta)
where
log f_thetais supplied by the caller.log Z(theta)can be supplied exactly withlog_partitionor estimated from reference samples using self-normalized importance formlogmeanexp(log f_theta(y_j) - log q(y_j)).
- class CallableObjective(fn, name='callable_objective')[source]
Bases:
objectSmall adapter naming arbitrary objective callables.
- Parameters:
fn (ObjectiveCallable)
name (str)
- fit_objective(enc, model, objective, engine=None, max_its=500, lr=0.05, optimizer='adam', tol=1.0e-7, maximize=True, out=None, print_iter=100, return_result=False, precision=None, restore_best=True)[source]
Optimize a user-supplied differentiable objective over a distribution tree.
The objective is called as
objective(shadow_model, enc, engine)and must return a scalar engine tensor. Distribution parameters are obtained from the same declaration-driven raw state used byfit_mle/fit_map. Whenrestore_bestis true, the returned model/value correspond to the best objective value seen, not necessarily the last attempted optimizer step.- Parameters:
- Return type:
- variational_projection(source, target, data=None, enc=None, sample_size=1000, seed=None, engine=None, max_its=500, lr=0.05, optimizer='adam', tol=1.0e-7, out=None, print_iter=100, return_result=False, precision=None, restore_best=True)[source]
Project
sourceonto the family represented bytarget.This minimizes a Monte-Carlo estimate of the forward KL
KL(source || target)by maximizingE_source[log target(X)]. Passdata/encto reuse common random numbers or a curated design set; otherwise samples are drawn fromsource.- Parameters:
source (SequenceEncodableProbabilityDistribution)
target (SequenceEncodableProbabilityDistribution)
enc (Any | None)
sample_size (int)
seed (int | None)
engine (Any | None)
max_its (int)
lr (float)
optimizer (str)
tol (float)
out (Any | None)
print_iter (int)
return_result (bool)
precision (Any | None)
restore_best (bool)
- Return type:
- optimize_torch_objective(parameters, objective, engine=None, max_its=500, lr=0.05, optimizer='adam', tol=1.0e-7, maximize=True, out=None, print_iter=100, precision=None, return_result=False, restore_best=True)[source]
Optimize an arbitrary Torch objective over supplied tensor parameters.
This is the escape hatch for models whose likelihood is not an iid distribution score, such as Gaussian-process marginal likelihoods or supervised neural-network losses. When
restore_bestis true, supplied tensors are copied back to their best seen values before returning.
- fit_parameter_objective(parameters, objective, enc=None, engine=None, max_its=500, lr=0.05, optimizer='adam', tol=1.0e-7, maximize=True, out=None, print_iter=100, precision=None, return_result=False, restore_best=True)[source]
Optimize an arbitrary objective over named constrained parameters.
parametersmay be a mapping ofname -> initial_valuefor real parameters, a sequence ofObjectiveParameterobjects, or a sequence of(name, initial_value, constraint)tuples.objectiveis called asobjective(params, enc, engine), whereparamsis a mapping of constrained engine tensors. Whenrestore_bestis true, returned detached values are the best seen named parameters.- Parameters:
- Return type: