reductions
Module for tail-sensitive tensor reductions.
A plain mean is the wrong reduction whenever a loss must react to a small subset of positions.
Averaging over N positions divides any single position's contribution by N, so a handful of
positions that behave badly are indistinguishable from none at all. tail_mean replaces the mean
with the mean of the worst fraction of positions, which keeps the loss sensitive to that subset while
staying smoother than a hard max.
Functions:
| Name | Description |
|---|---|
tail_mean |
Average the largest |
top_k_mean |
Average the |
tail_mean
¶
Average the largest tail_fraction of values along dim.
The average top-k reduction: the mean of the ceil(tail_fraction * N) largest entries (delegating to
top_k_mean). tail_fraction interpolates between the two familiar reductions —
1.0 is the plain mean (short-circuited to values.mean(dim), so bit-for-bit identical rather than merely close), while a
tail_fraction small enough that k == 1 is values.max(dim). A fraction rather than an explicit k is the right parameterization
when the offending subset scales with N — "the worst 5%" then means the same thing at 196 or 1024 positions; prefer top_k_mean when
the subset has a fixed absolute size instead.
Gradients reach only the selected positions, each with weight 1 / k; the rest receive exactly zero. The result is never below
values.mean(dim), and how far above depends on how concentrated values is, so a loss weight calibrated at one tail_fraction does
not necessarily transfer to another.
Padding: the reduction ranges over every position along dim — both the count k and the selected set include padding — so a caller
with padded rows must exclude the padding upstream (e.g. flatten to the valid positions) before reducing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
Tensor to reduce. Larger entries are treated as worse. |
required |
|
float
|
Fraction of positions to retain, in |
required |
|
int
|
Dimension to reduce. Required rather than defaulted: which axis carries the positions is a property of the caller's layout, and a wrong-axis reduction returns a plausible number rather than an error. |
required |
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
Tensor with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
values = torch.tensor([[0.0, 0.0, 0.0, 0.4, 0.8]])
1.0is the plain mean.¶tail_mean(values, 1.0, dim=-1) tensor([0.2400])
The worst 40% of five positions is the top two: (0.8 + 0.4) / 2.¶
tail_mean(values, 0.4, dim=-1) tensor([0.6000])
Small enough that
k == 1, so the reduction is the maximum.¶tail_mean(values, 0.1, dim=-1) tensor([0.8000])
Reduction is per-row, so one row cannot absorb another row's tail.¶
tail_mean(torch.tensor([[0.0, 1.0], [0.2, 0.2]]), 0.5, dim=-1) tensor([1.0000, 0.2000])
Added in version v3.71.0. Tail-sensitive replacement for `mean`: averages the worst `tail_fraction` of positions along a dimension.
top_k_mean
¶
Average the k largest entries of values along dim.
The count-parameterized form of tail_mean and the primitive it delegates to.
Prefer this when the offending subset has a fixed absolute size rather than a fixed share of the positions — the count is then the
invariant, and going through tail_mean's k / N does not round-trip in binary floating point. k is clamped to the number of
positions, so k >= N selects everything (the plain mean) and short rows need no special-casing. See tail_mean for the reduction's
properties (the equal 1 / k gradient share) and the same padding caveat: the top-k ranges over every position along dim, so a
caller with padded rows must exclude the padding upstream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
Tensor to reduce. Larger entries are treated as worse. |
required |
|
int
|
Number of largest entries to average, clamped to |
required |
|
int
|
Dimension to reduce. |
required |
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
Tensor with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
values = torch.tensor([[0.0, 0.0, 0.0, 0.4, 0.8]]) top_k_mean(values, 2, dim=-1) tensor([0.6000])
kat or above the position count selects everything.¶top_k_mean(values, 99, dim=-1) tensor([0.2400])
Reduction is per-row, so one row cannot absorb another row's tail.¶
top_k_mean(torch.tensor([[0.0, 1.0], [0.2, 0.2]]), 1, dim=-1) tensor([1.0000, 0.2000])
Added in version v3.71.0. Count-parameterized form of `tail_mean`, for callers whose offending subset has a fixed absolute size rather than a fixed share of the positions.