ratiopath.ray.aggregate.TensorMean
Bases: AggregateFnV2[dict, ndarray | float]
Calculates the mean (average) of a column containing Tensors.
This aggregator treats the data column as a high-dimensional array where axis 0 represents the batch dimension. To satisfy the requirements of a reduction and prevent memory growth proportional to the number of rows, axis 0 must be included in the aggregation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
on
|
str
|
The name of the column containing tensors or numbers. |
required |
axis
|
int | tuple[int, ...] | None
|
The axis or axes along which the reduction is computed.
- |
None
|
ignore_nulls
|
bool
|
Whether to ignore null values. Defaults to True. |
True
|
alias_name
|
str | None
|
Optional name for the resulting column. Defaults to "mean( |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Note
This aggregator is designed for "reduction" operations. If you wish to
calculate statistics per-row without collapsing the batch dimension,
use .map() instead.
Example
import ray import numpy as np from ratiopath.ray.aggregate import TensorMean
Dataset with 2x2 matrices: total shape (Batch=2, Dim1=2, Dim2=2)
ds = ray.data.from_items( ... [ ... {"m": np.array([[1, 1], [1, 1]])}, ... {"m": np.array([[3, 3], [3, 3]])}, ... ] ... )
1. Global Mean (axis=None) -> Result: 2.0
ds.aggregate(TensorMean(on="m", axis=None))
2. Batch Mean (axis=0) -> Result: np.array([[2, 2], [2, 2]])
ds.aggregate(TensorMean(on="m", axis=0))
3. Mean across Batch and Rows (axis=(0, 1)) -> Result: np.array([2, 2])
ds.aggregate(TensorMean(on="m", axis=(0, 1)))
Source code in ratiopath/ray/aggregate/tensor_mean.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
__init__(on, axis=None, ignore_nulls=True, alias_name=None)
Source code in ratiopath/ray/aggregate/tensor_mean.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
aggregate_block(block)
Source code in ratiopath/ray/aggregate/tensor_mean.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
combine(current_accumulator, new)
Source code in ratiopath/ray/aggregate/tensor_mean.py
130 131 132 133 134 135 | |
finalize(accumulator)
Source code in ratiopath/ray/aggregate/tensor_mean.py
137 138 139 140 141 142 143 144 | |
zero_factory()
staticmethod
Source code in ratiopath/ray/aggregate/tensor_mean.py
86 87 88 | |