Loss Fisher#
fisher
#
| CLASS | DESCRIPTION |
|---|---|
FisherSpectralLoss |
Fisher spectral criterion for automatic spectral gap discovery. |
VariationalSpectralLoss |
Variational spectral loss with prior over number of states. |
| FUNCTION | DESCRIPTION |
|---|---|
fisher_spectral_loss |
Fisher spectral criterion for automatic spectral gap discovery. |
variational_spectral_loss |
Variational spectral loss with prior over number of states. |
Classes#
FisherSpectralLoss(min_states: int = 2, temperature: float = 1.0, var_floor: float = 0.005, sign: float = -1.0, context_prefix: str | None = None, **kwargs)
#
Bases: Loss
Fisher spectral criterion for automatic spectral gap discovery.
Treats the eigenvalue spectrum as a 1D clustering problem: for every
possible split position k, the Fisher discriminant ratio measures how
well the eigenvalues separate into a "signal" group (lambda_1, ...,
lambda_k) and a "noise" group (lambda_{k+1}, ..., lambda_n).
The loss maximizes the best split quality via a smooth logsumexp
approximation over all candidate positions.
Mathematically, for split position \(k\):
where \(\mu_1, \sigma_1^2\) are the mean and variance of the top \(k\) eigenvalues, and \(\mu_2, \sigma_2^2\) of the remaining \(n - k\).
The overall loss is the smooth maximum over all valid splits:
Unlike gap-based losses that only act on the two eigenvalues adjacent to the gap, Fisher shapes the entire spectrum.
| PARAMETER | DESCRIPTION |
|---|---|
min_states
|
Minimum number of states to consider. Splits with fewer than
TYPE:
|
temperature
|
Temperature for
TYPE:
|
var_floor
|
Floor for within-class variance to prevent gradient explosion. When clusters become very tight (variance -> 0), the Fisher ratio diverges. The variance floor caps the ratio and stabilizes gradients. Values in [0.001, 0.01] work well for eigenvalues in [0, 1].
TYPE:
|
sign
|
Loss sign multiplier. -1.0 for maximization (default).
TYPE:
|
**kwargs
|
Additional keyword arguments passed to
DEFAULT:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
allowed_reduce |
Allowed reduction operations (only
TYPE:
|
loss_types |
Loss function categories (
TYPE:
|
Examples:
Automatic gap discovery (no predefined number of states):
>>> import torch
>>> from spectre.loss import FisherSpectralLoss
>>>
>>> loss_fn = FisherSpectralLoss(min_states=2, temperature=1.0)
>>> eigenvalues = torch.tensor([1.0, 0.95, 0.90, 0.30, 0.25, 0.20])
>>> context = {"eigenvalues": eigenvalues}
>>> loss = loss_fn(context)
| METHOD | DESCRIPTION |
|---|---|
forward |
Compute Fisher spectral loss. |
Source code in spectre/loss/fisher.py
Functions#
forward(context: dict, **kwargs) -> torch.Tensor
#
Compute Fisher spectral loss.
| PARAMETER | DESCRIPTION |
|---|---|
context
|
Context dictionary, must contain
TYPE:
|
**kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar Fisher spectral loss. |
Source code in spectre/loss/fisher.py
VariationalSpectralLoss(min_states: int = 2, temperature: float = 1.0, var_floor: float = 0.005, prior: Literal['uniform', 'geometric'] | torch.Tensor = 'uniform', prior_param: float | None = None, prior_weight: float = 0.1, sign: float = -1.0, context_prefix: str | None = None, **kwargs)
#
Bases: Loss
Variational spectral loss with prior over number of states.
Extends FisherSpectralLoss by treating the number of states k as a
discrete latent variable with a learned posterior and a prior. The loss
maximizes a variational lower bound (ELBO):
where:
- \(J(k)\) is the Fisher discriminant ratio at split position \(k\)
- \(q(k | \lambda) = \text{softmax}(J(k) / \tau)\) is the posterior over number of states, derived from the eigenvalue spectrum
- \(p(k)\) is a prior encoding domain knowledge
- \(\tau\) is the temperature and \(\beta\) controls prior strength
The first term is the Fisher spectral loss (data fit). The KL term regularizes the posterior toward the prior, resolving ambiguity when multiple split positions have similar Fisher scores.
The posterior \(q(k)\) can be monitored during training to track how the model's belief about the number of states evolves.
| PARAMETER | DESCRIPTION |
|---|---|
min_states
|
Minimum number of states to consider.
TYPE:
|
temperature
|
Temperature for
TYPE:
|
var_floor
|
Floor for within-class variance (see
TYPE:
|
prior
|
Prior distribution over number of states.
TYPE:
|
prior_param
|
Parameter for parametric priors. For
TYPE:
|
prior_weight
|
Weight \(\beta\) for the KL divergence term.
TYPE:
|
sign
|
Loss sign multiplier. -1.0 for maximization.
TYPE:
|
**kwargs
|
Additional keyword arguments passed to
DEFAULT:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
allowed_reduce |
Allowed reduction operations (only
TYPE:
|
loss_types |
Loss function categories (
TYPE:
|
Examples:
With geometric prior (prefer fewer states):
>>> import torch
>>> from spectre.loss import VariationalSpectralLoss
>>>
>>> loss_fn = VariationalSpectralLoss(
... prior="geometric",
... prior_param=0.5,
... prior_weight=0.5,
... )
>>> eigenvalues = torch.tensor([1.0, 0.9, 0.8, 0.3, 0.2, 0.1])
>>> context = {"eigenvalues": eigenvalues}
>>> loss = loss_fn(context)
With uniform prior (equivalent to Fisher + entropy bonus):
| METHOD | DESCRIPTION |
|---|---|
forward |
Compute variational spectral loss. |
Source code in spectre/loss/fisher.py
Functions#
forward(context: dict, **kwargs) -> torch.Tensor
#
Compute variational spectral loss.
| PARAMETER | DESCRIPTION |
|---|---|
context
|
Context dictionary, must contain
TYPE:
|
**kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar variational spectral loss. |
Source code in spectre/loss/fisher.py
Functions#
fisher_spectral_loss(eigenvalues: torch.Tensor, min_states: int = 2, temperature: float = 1.0, var_floor: float = 0.005, sign: float = -1.0) -> torch.Tensor
#
Fisher spectral criterion for automatic spectral gap discovery.
Evaluates the Fisher discriminant ratio at every possible split position in
the eigenvalue spectrum, then takes a smooth maximum via logsumexp.
For each split at position k, the eigenvalues are partitioned into a
signal group (top k) and a noise group (remaining n - k). The Fisher
ratio measures the quality of this partition:
The loss is:
The gradient with respect to each eigenvalue lambda_i acts on all
eigenvalues simultaneously:
- Signal group eigenvalues are pushed toward their group mean (tightening)
- Noise group eigenvalues are pushed toward their group mean (tightening)
- The gap between group means is widened
This provides a much richer learning signal than gap-based losses, which only act on the two eigenvalues adjacent to the spectral gap.
| PARAMETER | DESCRIPTION |
|---|---|
eigenvalues
|
Eigenvalues in descending order, shape
TYPE:
|
min_states
|
Minimum number of states. Splits below this are excluded.
TYPE:
|
temperature
|
TYPE:
|
var_floor
|
Floor for within-class variance to prevent gradient explosion when clusters become very tight.
TYPE:
|
sign
|
Loss sign (-1 for maximization).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar loss value. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If eigenvalues are not sorted descending or the valid split range is empty. |
Source code in spectre/loss/fisher.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
variational_spectral_loss(eigenvalues: torch.Tensor, min_states: int = 2, temperature: float = 1.0, var_floor: float = 0.005, prior: Literal['uniform', 'geometric'] | torch.Tensor = 'uniform', prior_param: float | None = None, prior_weight: float = 0.1, sign: float = -1.0) -> torch.Tensor
#
Variational spectral loss with prior over number of states.
Combines the Fisher spectral criterion with a KL divergence term that regularizes the posterior \(q(k)\) toward a prior \(p(k)\):
The posterior is \(q(k) = \text{softmax}(J(k) / \tau)\) where \(J(k)\) is the Fisher discriminant ratio at split position \(k\).
When prior_weight=0, this reduces to fisher_spectral_loss.
| PARAMETER | DESCRIPTION |
|---|---|
eigenvalues
|
Eigenvalues in descending order, shape
TYPE:
|
min_states
|
Minimum number of states.
TYPE:
|
temperature
|
Temperature for
TYPE:
|
var_floor
|
Floor for within-class variance.
TYPE:
|
prior
|
Prior distribution. See
TYPE:
|
prior_param
|
Parameter for parametric priors (e.g., decay rate for
TYPE:
|
prior_weight
|
Weight \(\beta\) for the KL term.
TYPE:
|
sign
|
Loss sign (-1 for maximization).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar loss value. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If eigenvalues are not sorted descending or the valid split range is empty. |
Examples:
>>> import torch
>>> eigenvalues = torch.tensor([1.0, 0.9, 0.6, 0.5, 0.2, 0.1])
>>> loss = variational_spectral_loss(
... eigenvalues,
... prior="geometric",
... prior_param=0.5,
... prior_weight=0.5,
... )
Source code in spectre/loss/fisher.py
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 | |