qfeval_functions.functions.ema
- ema(x, alpha, dim=-1)[source]
Compute exponential moving average along a specified dimension.
This function calculates the exponential moving average (EMA) of a tensor along the specified dimension. EMA is a type of weighted moving average where more recent values have higher weights. The weight of each value decreases exponentially as you go back in time.
For each position \(i\) along the specified dimension, the EMA is computed as:
\[\text{EMA}[i] = \frac{\sum_{j=0}^{i} x[j] \cdot (1-\alpha)^{i-j}} {\sum_{j=0}^{i} (1-\alpha)^{i-j}}\]where \(\alpha\) is the smoothing factor (0 < \(\alpha\) < 1). Smaller values of \(\alpha\) give more weight to recent observations.
- Parameters:
x (
Tensor) – The input tensor containing values to be averaged.alpha (
float) – The smoothing factor, must be in the range (0, 1). Smaller values result in more smoothing (slower decay).dim (
int) – The dimension along which to compute the exponential moving average. Default is -1 (the last dimension).
- Returns:
A tensor of the same shape as the input, containing the exponential moving average values.
- Return type:
Example
>>> # Simple 1D example >>> x = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0]) >>> ema_result = QF.ema(x, alpha=0.5) >>> ema_result tensor([1.0000, 1.6667, 2.4286, 3.2667, 4.1613])
>>> # 2D example with dim=1 >>> x = torch.tensor([[1.0, 2.0, 3.0, 4.0], ... [4.0, 3.0, 2.0, 1.0]]) >>> ema_result = QF.ema(x, alpha=0.3, dim=1) >>> ema_result tensor([[1.0000, 1.5882, 2.2329, 2.9305], [4.0000, 3.4118, 2.7671, 2.0695]])
Note
The implementation uses an efficient \(O(\log n)\) algorithm based on geometric progression properties, making it suitable for long sequences.