torchcell.losses.distributional.DistHead
- class DistHead(mode: str, quantiles: Tensor | list[float] | None = None, sigma_floor: float = 0.001, num_features: int | None = None, rank: int = 32, num_samples: int = 128, v_init: float = 0.1)[source]
Bases:
ModulePluggable distributional readout: interpret a head’s raw params, score them, pool.
Stateless except for the quantile grid (
quantilemode) and the global low-rank factorV(energymode). Given a structural head’s outputparams–[..., F](point) or[..., F, P](P=2gaussian / laplace / nll_gaussian / energy,P=Kquantile) – it exposes:point()– the point estimate[..., F]used by the (loss-agnostic) metric and any downstream reporting: identity (point),mu(gaussian / nll_gaussian / energy), the predictive MEDIANmu(laplace), or the median quantile (quantile);loss()– a masked scalar training loss: MSE (point), mean Gaussian CRPS (gaussian), mean pinball (quantile), mean Laplace CRPS (laplace), mean Gaussian NLL (nll_gaussian), or mean energy score (energy);pit()– per-feature PIT values[B, F]for the calibration diagnostic (every mode butpoint).
energyis the one mode with learnable state of its own:Vis annn.Parameterof shape[num_features, rank]that is GLOBAL (shared across the batch), because a correlation basis over features is a property of the phenotype space, not of an individual strain. It therefore does NOT widenparam_dim– the structural head still emits(mu, sigma), i.e.P=2, and only the head ownsV.- property taus: Tensor
The quantile grid (empty for non-quantile modes).
The buffer is stored as
_tausand surfaced through this property becausenn.Module.__getattr__is typedTensor | Module; casting once here keeps every caller (and mypy –strict) working with a plainTensor.
- property v: Tensor | None
The global low-rank factor
V [F, k](energymode) orNone.Nonefor every non-energymode and forenergywithrank=0(the diagonal-only ablation). Surfaced through a property for the same reason astaus:nn.Module.__getattr__is typedTensor | Module.
- property param_dim: int
1 (point), K (quantile), or 2 (every parametric mode).
energyis 2 as well – its extra state is the GLOBALV, owned by this head, not per-feature output of the structural head.- Type:
Params per feature
- point(params: Tensor) Tensor[source]
Reduce raw params to a point estimate
[..., F](metric / reporting).Identity (point), the median knot (quantile), or
mufor every parametric mode – which is the predictive MEAN for gaussian / nll_gaussian / energy and the predictive MEDIAN for laplace.
- samples(params: Tensor, num_samples: int) Tensor[source]
Draw
energy-mode predictive samples[B, m, F](reparameterized).- Parameters:
params – Head params
[B, F, 2].num_samples –
m, samples per row.
- Returns:
Samples
[B, m, F]fromN(mu, diag(sigma^2) + V V^T).
- loss(params: Tensor, target: Tensor, mask: Tensor | None = None, feature_mask: Tensor | None = None) Tensor[source]
Masked scalar training loss for this head.
- Parameters:
params – Head params
[B, F](point) or[B, F, P](every other mode).target – Point targets
[B, F](same F asparams).mask – Optional bool
[B]selecting supervised rows. When no row is supervised, a graph-connected zero is returned (grads flow as zero).feature_mask – Optional bool
[B, F]selecting which FEATURES to score. Used by the masked-label objective to score only the genes still hidden at the current unmasking step – scoring a revealed gene would let the model copy its own input. Row-masked alongsideparams/targetso the two stay aligned.
- Returns:
Scalar loss.
- pit(params: Tensor, target: Tensor, mask: Tensor | None = None, num_samples: int | None = None) Tensor[source]
Per-feature PIT values
[B, F]for this head’s predictive distribution.The mode-agnostic calibration diagnostic – see
pit_values()for how to read the histogram andcoverage()for the interval summary. Not available inpointmode (no predictive distribution).- Parameters:
params – Head params
[B, F](point) or[B, F, P].target – Point targets
[B, F].mask – Optional bool
[B]selecting rows, as inloss().num_samples –
energymode only – samples used to build the empirical CDF. Defaults toDEFAULT_ENERGY_EVAL_SAMPLES, NOT the head’s trainingm: PIT resolution is1/m, so the (deliberately cheap) trainingmwould quantize the histogram far too coarsely to read. Eval needs no gradients, so the extra samples are nearly free.
- Returns:
PIT values
[B, F]in[0, 1].