reduce(tensor: Tensor, pattern: str, reduction: str, axes_lengths: int = {})Reduce tensor along axes implied by an einops pattern.
Generalises standard sum / mean / max / min /
prod reductions to arbitrary axes selected by symbolic pattern.
Any axis present on the left-hand side of -> but absent on the
right-hand side is collapsed by the chosen reduction.
Parameters
tensorTensorInput tensor to reduce.
patternstrEinops pattern such as
"b h w -> b w". A number on the left
is an unnamed axis of that length, so "(h 2) (w 2) -> h w"
pools 2x2 windows.reductionstrReduction operation; one of
"sum", "mean", "max",
"min", "prod", "any", "all".**axes_lengthsint= {}Axis lengths, needed to split a dimension into a group and checked
against the input wherever the input already fixes the axis.
Returns
TensorThe reduced tensor.
Raises
lucid._C.engine.InvalidArgumentThe pattern is malformed, the reduction is unknown, an axis
appears only on the right, or a keyword names no axis.
lucid._C.engine.ShapeMismatchThe input's shape does not fit the pattern or the keyword lengths.
lucid._C.engine.NotImplementedErrorreduction="mean" over a tensor that is not floating point.Notes
Conceptually equivalent to a permutation followed by a reduction
along contiguous trailing axes. For reduction="mean" the result
is
Examples
>>> import lucid
>>> x = lucid.randn(2, 3, 4)
>>> lucid.einops.reduce(x, "b h w -> b w", "mean").shape
(2, 4)
>>> lucid.einops.reduce(lucid.randn(2, 4, 6), "b (h 2) (w 3) -> b h w", "max").shape
(2, 2, 2)