A Reduction operation returns a tensor with fewer dimensions than the original by applying a reduction operator to slices of values. You specify the dimensions to reduce using an array of integers (the "reduction dimensions").
Key Rules for Reduction Dimensions:
- Dimensions are indices of the input tensor.
- Each dimension index must be less than the tensor rank.
- Each dimension should occur at most once in the reduction dimensions array.
- Listing dimensions in increasing order may improve performance.
- Special Case: If no parameter is passed to the reduction operation, the tensor is reduced along all dimensions, resulting in a zero-dimension tensor (a scalar).
Common Reduction Operators:
sum(): Sum of reduced values.mean(): Mean of reduced values.maximum(): Largest of the reduced values.minimum(): Smallest of the reduced values.prod(): Product of the reduced values.all(): Checks if all elements are true (after casting to bool).any(): Checks if any element is true (after casting to bool).reduce(dims, reducer): Uses a user-defined reduction operator.
// Example: Reduction along one dimension
Eigen::Tensor<int, 2> a(2, 3);
a.setValues({{1, 2, 3}, {6, 5, 4}});
Eigen::array<int, 1> dims({1}); // Reduce along the second dimension
Eigen::Tensor<int, 1> b = a.maximum(dims);
// b will be: 3, 6
// Example: Reduction along all dimensions (returns a scalar/0-dim tensor)
Eigen::Tensor<float, 3> a(2, 3, 4);
Eigen::Tensor<float, 0> b = a.sum();