Categorical, Bernoulli, Gaussian, FromHessian
The built-in likelihoods.
Categorical
Categorical(class_dim=-1, ignore_index=None)Softmax cross-entropy, the class axis at class_dim.
Attributes
| Name | Type | Description |
|---|---|---|
| class_dim | int | The class axis of the logits: -1 for [N, C] and [N, T, C], 1 for images [N, C, H, W]. |
| ignore_index | int | None | The void label (-100 for padding tokens, 255 for void pixels). Positions with it get zero factors and False in the output mask. None when no label is void. |
Examples
>>> import torch, hessiankit as hk
>>> lik = hk.Categorical(class_dim=1, ignore_index=255)
>>> f = torch.randn(2, 5, 4, 4) # logits [N, C, H, W]
>>> y = torch.randint(0, 5, (2, 4, 4))
>>> y[0, 0, 0] = 255 # a void pixel
>>> lik.event_axes(f)
(1,)
>>> lik.output_mask(f, y).shape
torch.Size([2, 1, 4, 4])
>>> lik.factors(f, y, fisher="empirical").num
1Methods
| Name | Description |
|---|---|
| event_axes | The class axis, as a non-negative axis of f. |
| factors | Factors from categorical, zero at void positions. |
| fishers | All three kinds. |
| output_mask | False where the target is ignore_index, with the class axis of size 1. |
event_axes
Categorical.event_axes(f, /)The class axis, as a non-negative axis of f.
factors
Categorical.factors(
f,
targets,
/,
*,
fisher,
mc_samples=1,
generator=None,
grouping='full',
)Factors from categorical, zero at void positions.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| f | Tensor | Logits [N, ...]. |
required |
| targets | Tensor | None | Class indices of f’s shape without the class axis. Needed for "empirical" and to find void labels. |
required |
| fisher | Fisher | "exact", "mc" or "empirical". |
required |
| mc_samples | int | Number of draws for "mc". |
1 |
| generator | torch.Generator | None | Random generator for "mc". |
None |
| grouping | Grouping | How exact factors of several positions are grouped. | 'full' |
fishers
Categorical.fishers()All three kinds.
output_mask
Categorical.output_mask(f, targets, /)False where the target is ignore_index, with the class axis of size 1.
Returns
| Name | Type | Description |
|---|---|---|
| Tensor | None | None without ignore_index or targets. |
Raises
| Name | Type | Description |
|---|---|---|
| FactorError | The targets are not of f’s shape without the class axis. |
Bernoulli
Bernoulli(ignore_index=None)Sigmoid binary cross-entropy, elementwise.
There are no event axes: every entry after the batch axis is a position with one column ([N, 1] for a binary classifier, [N, H, W] for a binary mask). Soft labels in [0, 1] are fine.
Attributes
| Name | Type | Description |
|---|---|---|
| ignore_index | float | None | The void label. Entries with it get zero factors and False in the output mask. None when no label is void. |
Examples
>>> import torch, hessiankit as hk
>>> lik = hk.Bernoulli(ignore_index=-100)
>>> f = torch.randn(4, 1)
>>> y = torch.tensor([[1.0], [0.0], [-100.0], [1.0]])
>>> lik.event_axes(f)
()
>>> lik.output_mask(f, y).squeeze(1).tolist()
[True, True, False, True]
>>> lik.factors(f, y, fisher="exact").vectors.shape
torch.Size([1, 4, 1])Methods
| Name | Description |
|---|---|
| event_axes | No axis: every entry is a position. |
| factors | Factors from bernoulli, zero at void entries. |
| fishers | All three kinds. |
| output_mask | False where the target is ignore_index, of f’s shape. |
event_axes
Bernoulli.event_axes(f, /)No axis: every entry is a position.
factors
Bernoulli.factors(
f,
targets,
/,
*,
fisher,
mc_samples=1,
generator=None,
grouping='full',
)Factors from bernoulli, zero at void entries.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| f | Tensor | Logits [N, ...]. |
required |
| targets | Tensor | None | Labels in [0, 1] of f’s shape. Needed for "empirical" and to find void labels. |
required |
| fisher | Fisher | "exact", "mc" or "empirical". |
required |
| mc_samples | int | Number of draws for "mc". |
1 |
| generator | torch.Generator | None | Random generator for "mc". |
None |
| grouping | Grouping | How exact factors of several positions are grouped; "columns" gives one factor. |
'full' |
fishers
Bernoulli.fishers()All three kinds.
output_mask
Bernoulli.output_mask(f, targets, /)False where the target is ignore_index, of f’s shape.
Returns
| Name | Type | Description |
|---|---|---|
| Tensor | None | None without ignore_index or targets. |
Raises
| Name | Type | Description |
|---|---|---|
| FactorError | The targets are not of f’s shape. |
Gaussian
Gaussian(sigma=1.0, event_dim=-1)Gaussian negative log-likelihood with standard deviation sigma; f is the mean.
Attributes
| Name | Type | Description |
|---|---|---|
| sigma | float | Tensor | Positive: a number, or a tensor broadcastable to f (one per channel of images [N, C, H, W] is [C, 1, 1]). |
| event_dim | int | The event axis: -1 for [N, K] and [N, T, K], 1 for the channels of images [N, C, H, W]. |
Examples
>>> import torch, hessiankit as hk
>>> lik = hk.Gaussian(sigma=0.1, event_dim=1)
>>> f = torch.randn(2, 3, 8, 8) # predicted noise [N, C, H, W]
>>> lik.event_axes(f)
(1,)
>>> lik.factors(f, None, fisher="exact", grouping="columns").num
3Methods
| Name | Description |
|---|---|
| event_axes | The event axis, as a non-negative axis of f. |
| factors | Factors from gaussian, with the event axis moved last for it and back. |
| fishers | All three kinds. |
| output_mask | Always None: nothing is void. |
event_axes
Gaussian.event_axes(f, /)The event axis, as a non-negative axis of f.
factors
Gaussian.factors(
f,
targets,
/,
*,
fisher,
mc_samples=1,
generator=None,
grouping='full',
)Factors from gaussian, with the event axis moved last for it and back.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| f | Tensor | Means [N, ...]. |
required |
| targets | Tensor | None | Targets of f’s shape. Needed for "empirical". |
required |
| fisher | Fisher | "exact", "mc" or "empirical". |
required |
| mc_samples | int | Number of draws for "mc". |
1 |
| generator | torch.Generator | None | Random generator for "mc". MC draws follow the layout with the event axis last. |
None |
| grouping | Grouping | How exact factors of several positions are grouped. | 'full' |
fishers
Gaussian.fishers()All three kinds.
output_mask
Gaussian.output_mask(f, targets, /)Always None: nothing is void.
FromHessian
FromHessian(hessian, event_dims=1)Any likelihood, given its Hessian in the output.
hessian(f, targets) returns the Hessian of the negative log-likelihood in the last event_dims axes of f, flattened row-major to K entries: [N, K, K], or [N, *positions, K, K] when f has position axes between the batch and the event. It may be indefinite: the factors come from its eigendecomposition (from_dense), and negative eigenvalues give factors with sign -1. Only fisher="exact" is supported.
Attributes
| Name | Type | Description |
|---|---|---|
| hessian | Callable[[Tensor, Tensor | None], Tensor] | The per-example (per-position) Hessian, a function of f and the targets. |
| event_dims | int | How many trailing axes of f form one event. |
Examples
>>> import torch, hessiankit as hk
>>> def bradley_terry(f, y): # f = [r_a, r_b], -log sigmoid(+-(r_a - r_b))
... p = torch.sigmoid(f[:, 0] - f[:, 1])
... a = torch.tensor([[1.0, -1.0], [-1.0, 1.0]])
... return (p * (1 - p))[:, None, None] * a
>>> lik = hk.FromHessian(bradley_terry)
>>> lik.factors(torch.randn(4, 2), None, fisher="exact").dense().shape
torch.Size([4, 2, 2])Methods
| Name | Description |
|---|---|
| event_axes | The last event_dims axes of f. |
| factors | Signed factors of hessian(f, targets). |
| fishers | Only "exact". |
| output_mask | Always None: nothing is void. |
event_axes
FromHessian.event_axes(f, /)The last event_dims axes of f.
factors
FromHessian.factors(
f,
targets,
/,
*,
fisher,
mc_samples=1,
generator=None,
grouping='full',
)Signed factors of hessian(f, targets).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| f | Tensor | The model output [N, ...]. |
required |
| targets | Tensor | None | Passed on to hessian. |
required |
| fisher | Fisher | Must be "exact". |
required |
| mc_samples | int | Unused. | 1 |
| generator | torch.Generator | None | Unused. | None |
| grouping | Grouping | How the factors of several positions are grouped. With "columns", column k of every position goes in one factor per sign, so E is K, or 2 K when some eigenvalue is negative. |
'full' |
Raises
| Name | Type | Description |
|---|---|---|
| FactorError | fisher is not "exact", or the Hessian has the wrong shape. |
fishers
FromHessian.fishers()Only "exact".
output_mask
FromHessian.output_mask(f, targets, /)Always None: nothing is void.