Skip to main content

categorical

categorical(
    f,
    *,
    kind,
    targets=None,
    generator=None,
    mc_samples=1,
    class_dim=-1,
    grouping='full',
)

Factors of the softmax cross-entropy loss.

The exact square root has column c equal to sqrt(p_c) (e_c - p). An MC draw is (onehot(y) - p) / sqrt(S) with y drawn from p, and the empirical factor is onehot(y) - p at the targets.

Parameters

Name Type Description Default
f Tensor Logits [N, ...], the class axis at class_dim, any other axes positions. required
kind Kind "exact", "mc" or "empirical". required
targets Tensor | None Class indices of f’s shape without the class axis. Needed for "empirical". None
generator torch.Generator | None Random generator for "mc". Labels are drawn with torch.multinomial over the logits flattened to [(N, *positions), C], in that order. None
mc_samples int Number of draws S for "mc". 1
class_dim int The class axis of f. -1
grouping Grouping How exact factors of several positions are grouped. 'full'

Returns

Name Type Description
OutputFactors Factors [E, N, *f.shape[1:]]: E = positions x C for exact "full", E = C for exact "columns", E = S for "mc", E = 1 for "empirical".

Raises

Name Type Description
ValueError Unknown kind or grouping, or mc_samples < 1.
FactorError f has no output axis, class_dim is out of range or the batch axis, or the targets are missing, of the wrong shape or type, or out of range.