MLPConditional#
- class hybrid_aif.MLPConditional[source]#
Bases:
ModuleMLP over concatenated parents → diagonal-Gaussian child (see module docstring).
The family contract is
log_prob(),sample()andmoments(), all throughcondition_on(), the plainGaussianthe parents select.stats()(one tick’s gradient of the NLL) andabsorb()(the optax step) learn it. It is not aLearnable(there is no posterior over the weights):isinstance(decl, Learnable)isFalse, so the conjugate paths leave it alone.Construct with
from_shapes(); the field constructor stays the dataclass default sodataclasses.replaceandeqx.tree_atwork. Freeze withdataclasses.replace(decl, frozen=True).The final layer emits
2 * child_dimnumbers, the mean and then the scale pre-activation, read asheadsays.- head: str = 'softplus'#
How the final layer is read.
"softplus": mean raw, scalemin_scale + softplus(raw)."bounded": meantanh(raw), scaleminstd + (maxstd - minstd) * sigmoid(raw + 2);minstd == maxstdis a fixed-scale head whose scale parameters get zero gradient. Seehybrid_aif.distributions.neural.
- propagation: str = 'first_order'#
How a Gaussian belief on the one continuous parent is read through the network when the conditional is a likelihood on the analytic path:
"first_order", a tangent at the mean, or"unscented", the statistical linearisation over the sigma points (linearise()).
- classmethod from_shapes(
- parent_dims,
- child_dim,
- *,
- key,
- width=64,
- depth=2,
- activation='tanh',
- min_scale=0.001,
- init_scale=1.0,
- frozen=False,
- norm=False,
- head='softplus',
- minstd=0.1,
- maxstd=1.0,
- init='default',
- propagation='first_order',
A randomly-initialized net:
depthhidden layers ofwidthunits.init_scalecentres a softplus head’s initial standard deviation (the scale-half bias) and must exceedmin_scale; a bounded head ignores it. Options, all off by default:norm=True: a LayerNorm between each hidden linear and its activation, hidden biases dropped;head="bounded"withminstd/maxstd(minstd == maxstdis the fixed-σ head);init="trunc_normal_avg":trunc_normal_avg()on the hidden layers,uniform_avg()on the fused head layer (each half at its own(in, child_dim)fan), every bias zero.init="default"iseqx.nn.Linear’s initializer throughout.
propagationsets the static field of that name:"first_order"(default) or"unscented".
- log_prob(child, parents)[source]#
log p(child | parents)(scalar), computed on the diagonal (diag_nll()).
- sample(key, parents)[source]#
One ancestral draw of the child given its parents, shape
(child_dim,).
- moments(parents)[source]#
(mean, cov)given the parents;covis the full, diagonal-valued(child_dim, child_dim)matrix.
- diag_nll(child, parents)[source]#
Negative log-density on the diagonal:
0.5·Σ((y−μ)/σ)² + Σ log σ + 0.5·d·log 2π.Exactly
−log_probfor this head, without building or solving a(d, d)Cholesky.Design: docs/design/amortization.md chunk 2c note §(e) item 1
- nll(child, parents)[source]#
Negative conditional log-density: the gradient loss on one evidence pair.
- stats(child, parents)[source]#
The per-tick statistic
∇ nll(child, parents), as a same-structured pytree.Nonesits at every non-trainable position. RaisesValueErroron a frozen declaration.
- absorb(stats, opt_state, lr=1.0, decay=1.0)[source]#
One optax step: returns
(declaration, opt_state).The optimizer state is passed in and out.
lrscales the step and may be traced;decayother than1.0raisesNotImplementedError.
- __init__(
- layers,
- parent_dims,
- child_dim,
- norms=(),
- activation='tanh',
- min_scale=0.001,
- frozen=False,
- head='softplus',
- minstd=0.1,
- maxstd=1.0,
- propagation='first_order',