Skip to main content

OutputFactors

OutputFactors(vectors, signs=None)

Vectors at the model output whose signed outer products sum to the output Hessian.

Attributes

Name Type Description
vectors Tensor [E, N, *output_shape], E factors for each of the N examples.
signs Tensor | None [E, N] of +1 or -1, or None when every sign is +1. Signs below zero come from an indefinite Hessian (see from_dense).

Methods

Name Description
dense The matrix the factors stand for, per example: sum_e s_e v_e v_e^T.
masked Zero the factors where mask is False.

dense

OutputFactors.dense()

The matrix the factors stand for, per example: sum_e s_e v_e v_e^T.

Returns

Name Type Description
Tensor [N, K, K], with K the number of entries of one example’s output, in row-major order.

Examples

>>> import torch, hessiankit as hk
>>> fac = hk.categorical(torch.zeros(2, 3), kind="exact")
>>> fac.dense().shape
torch.Size([2, 3, 3])

masked

OutputFactors.masked(mask)

Zero the factors where mask is False.

This zeroes output-side factors only. It is not the padding mask of the reductions: a masked position still counts toward the number of positions and the total weight. Padding and void outputs go to compute(masks=..., output_mask=...).

Parameters

Name Type Description Default
mask Tensor Boolean, broadcastable to [N, *output_shape]. required

Returns

Name Type Description
OutputFactors New factors with the same signs.

Raises

Name Type Description
FactorError The mask is not boolean or does not broadcast to [N, *output_shape].