Public Tensor Reductions #
Shape-polymorphic scalar reductions for ordinary tensor programs. Empty means use denominator one
(meanDenominator), the same totalized convention as meanSpec and the loss layer.
Traverse entries in row-major order without converting the buffer to an array.
The accumulator may have a different type from the entries and does not require a Storage
instance. For a configured binary tensor, we can accumulate in native binary32 without allocating
a converted tensor:
let total := tensor.foldl
(fun (total : Float32) value => total + FloatLib.Floats.ExecFloat.Binary.toFloat32 value) 0
let average := total / (Tensor.meanDenominator shape : Float32)
The conversion, additions, and final division use the chosen accumulator's arithmetic. Widening does not make those operations exact or prevent overflow for every input.
Instances For
Arithmetic mean of all entries, using denominator one for an empty tensor.
The sum and element-count conversion both use the tensor's scalar type. Either can overflow in a
low-precision format even when the exact mean is representable. To select a wider accumulator,
convert the entries with Tensor.map before calling mean, or convert them inside Tensor.foldl
to accumulate in one pass. The latter also avoids allocating a wider tensor.