Stats Mean#
mean
#
| CLASS | DESCRIPTION |
|---|---|
RunningMean |
Numerically stable running mean computation. |
Classes#
RunningMean(state: dict | None = None)
#
Bases: Stats
Numerically stable running mean computation.
Uses the Chan-Golub-LeVeque algorithm for incremental mean updates, which avoids catastrophic cancellation and maintains numerical precision and stability even with large numbers of samples.
The mean is updated incrementally using:
\(\mu_{new} = \mu_{old} + \frac{n_{batch}}{n_{total}} (\mu_{batch} - \mu_{old})\)
where:
n_{batch}is the size of the current batchn_{total}is the cumulative count of all samplesmu_{batch}is the mean of the current batchmu_{old}is the previous running mean
| PARAMETER | DESCRIPTION |
|---|---|
state
|
Previous state to restore from.
TYPE:
|
Properties
count : int Total number of samples processed.
batch_count : int Number of batches processed.
data_shape : tuple | None Original shape of feature dimensions.
mean : torch.Tensor | None Current mean estimate.
weight_sum : float Cumulative sum of weights (for weighted samples).
Examples:
Basic usage:
>>> from spectre.stats import RunningMean
>>> import torch
>>>
>>> stat = RunningMean()
>>> for _ in range(10):
... batch = torch.randn(100, 5) # 100 samples, 5 features
... stat.add(batch)
>>>
>>> mean = stat.mean()
>>> mean.shape
torch.Size([5])
Higher precision computation:
>>> stat = RunningMean()
>>> for _ in range(10):
... batch = torch.randn(100, 5, dtype=torch.float64)
... stat.add(batch)
>>> mean = stat.mean() # float64 precision maintained
Multi-dimensional features:
>>> stat = RunningMean()
>>> for _ in range(10):
... batch = torch.randn(100, 3, 32, 32) # Image-like data
... stat.add(batch)
>>> mean = stat.mean()
>>> mean.shape
torch.Size([3, 32, 32])
| METHOD | DESCRIPTION |
|---|---|
add |
Add batch of samples to running mean. |
mean |
Get current mean estimate. |
load_state_dict |
Load state from dictionary. |
state_dict |
Save state to dictionary. |
to |
Move internal tensors to specified device. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
count |
Get total number of samples processed.
TYPE:
|
batch_count |
Get number of batches processed.
TYPE:
|
data_shape |
Get original feature shape (or None if unknown).
TYPE:
|
weight_sum |
Get cumulative sum of weights.
TYPE:
|
Source code in spectre/stats/mean.py
Attributes#
count: int
property
writable
#
Get total number of samples processed.
batch_count: int
property
writable
#
Get number of batches processed.
data_shape: tuple | None
property
writable
#
Get original feature shape (or None if unknown).
weight_sum: float
property
writable
#
Get cumulative sum of weights.
Functions#
add(X: torch.Tensor, weights: torch.Tensor | None = None, *args, **kwargs) -> None
#
Add batch of samples to running mean.
Updates the mean estimate incrementally using the Chan algorithm. Handles multi-dimensional features by flattening and restoring shapes. Supports weighted samples for robust statistics.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Batch of data with shape (n_samples, n_features, ...). First dimension is batch, remaining dimensions are features.
TYPE:
|
weights
|
Sample weights with shape (n_samples,). If None, all samples have equal weight (equivalent to weights=1). By default None.
TYPE:
|
Source code in spectre/stats/mean.py
mean() -> torch.Tensor
#
Get current mean estimate.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Mean with original feature shape. |
load_state_dict(state: dict) -> None
#
Load state from dictionary.
Restores the running mean computation from a saved state, enabling resumption of computation.
| PARAMETER | DESCRIPTION |
|---|---|
state
|
State dictionary from
TYPE:
|
Source code in spectre/stats/mean.py
state_dict() -> dict
#
Save state to dictionary.
Returns a dictionary containing all internal state that can be
saved to disk and later restored via load_state_dict().
| RETURNS | DESCRIPTION |
|---|---|
dict
|
State dictionary containing:
|
Source code in spectre/stats/mean.py
to(device: str = 'cpu') -> None
#
Move internal tensors to specified device.
| PARAMETER | DESCRIPTION |
|---|---|
device
|
Target device ("cpu", "cuda", "cuda:0", etc.).
TYPE:
|