normalizers
normalizers
¶
Online feature normalization for continual learning.
Implements online (streaming) normalization that updates estimates of mean and variance at every time step, following the principle of temporal uniformity.
Two normalizer variants are provided:
-
EMANormalizer: Exponential moving average estimates of mean and variance. Suitable for non-stationary distributions where recent observations should be weighted more heavily. -
WelfordNormalizer: Welford's online algorithm for numerically stable estimation of cumulative sample mean and variance with Bessel's correction. Suitable for stationary distributions.
EMANormalizerState
¶
State for EMA-based online feature normalization.
Uses exponential moving average to estimate running mean and variance, suitable for non-stationary distributions.
Attributes: mean: Running mean estimate per feature var: Running variance estimate per feature sample_count: Number of samples seen decay: Exponential decay factor for estimates (1.0 = no decay, pure online)
WelfordNormalizerState
¶
State for Welford's online normalization algorithm.
Uses Welford's algorithm for numerically stable estimation of cumulative sample mean and variance with Bessel's correction.
Attributes: mean: Running mean estimate per feature var: Running variance estimate per feature (Bessel-corrected) sample_count: Number of samples seen p: Sum of squared deviations from the current mean (M2 accumulator)
Normalizer(epsilon=1e-08)
¶
Bases: ABC
Abstract base class for online feature normalizers.
Normalizes features using running estimates of mean and standard deviation:
x_normalized = (x - mean) / (std + epsilon)
The normalizer updates its estimates at every time step, following temporal uniformity.
Subclasses must implement init and normalize. The normalize_only
and update_only methods have default implementations.
Attributes: epsilon: Small constant for numerical stability
Args: epsilon: Small constant added to std for numerical stability
Source code in src/alberta_framework/core/normalizers.py
to_config()
abstractmethod
¶
init(feature_dim)
abstractmethod
¶
Initialize normalizer state.
Args: feature_dim: Dimension of feature vectors
Returns: Initial normalizer state with zero mean and unit variance
Source code in src/alberta_framework/core/normalizers.py
normalize(state, observation)
abstractmethod
¶
Normalize observation and update running statistics.
This method both normalizes the current observation AND updates the running statistics, maintaining temporal uniformity.
Args: state: Current normalizer state observation: Raw feature vector
Returns: Tuple of (normalized_observation, new_state)
Source code in src/alberta_framework/core/normalizers.py
normalize_only(state, observation)
¶
Normalize observation without updating statistics.
Useful for inference or when you want to normalize multiple observations with the same statistics.
Args: state: Current normalizer state observation: Raw feature vector
Returns: Normalized observation
Source code in src/alberta_framework/core/normalizers.py
update_only(state, observation)
¶
Update statistics without returning normalized observation.
Args: state: Current normalizer state observation: Raw feature vector
Returns: Updated normalizer state
Source code in src/alberta_framework/core/normalizers.py
EMANormalizer(epsilon=1e-08, decay=0.99)
¶
Bases: Normalizer[EMANormalizerState]
Online feature normalizer using exponential moving average.
Estimates mean and variance via EMA, suitable for non-stationary environments where recent observations should be weighted more heavily.
The effective decay ramps up from 0 to the target decay over early steps to prevent instability.
Attributes: epsilon: Small constant for numerical stability decay: Exponential decay for running estimates (0.99 = slower adaptation)
Args: epsilon: Small constant added to std for numerical stability decay: Exponential decay factor for running estimates. Lower values adapt faster to changes. 1.0 means pure online average (no decay).
Source code in src/alberta_framework/core/normalizers.py
to_config()
¶
init(feature_dim)
¶
Initialize EMA normalizer state.
Args: feature_dim: Dimension of feature vectors
Returns: Initial normalizer state with zero mean and unit variance
Source code in src/alberta_framework/core/normalizers.py
normalize(state, observation)
¶
Normalize observation and update EMA running statistics.
Args: state: Current EMA normalizer state observation: Raw feature vector
Returns: Tuple of (normalized_observation, new_state)
Source code in src/alberta_framework/core/normalizers.py
normalize_only(state, observation)
¶
Normalize observation without updating statistics.
Useful for inference or when you want to normalize multiple observations with the same statistics.
Args: state: Current normalizer state observation: Raw feature vector
Returns: Normalized observation
Source code in src/alberta_framework/core/normalizers.py
update_only(state, observation)
¶
Update statistics without returning normalized observation.
Args: state: Current normalizer state observation: Raw feature vector
Returns: Updated normalizer state
Source code in src/alberta_framework/core/normalizers.py
WelfordNormalizer(epsilon=1e-08)
¶
Bases: Normalizer[WelfordNormalizerState]
Online feature normalizer using Welford's algorithm.
Computes cumulative sample mean and variance with Bessel's correction, suitable for stationary distributions. Numerically stable for large sample counts.
Reference: Welford 1962, "Note on a Method for Calculating Corrected Sums of Squares and Products"
Attributes: epsilon: Small constant for numerical stability
Args: epsilon: Small constant added to std for numerical stability
Source code in src/alberta_framework/core/normalizers.py
to_config()
¶
init(feature_dim)
¶
Initialize Welford normalizer state.
Args: feature_dim: Dimension of feature vectors
Returns: Initial normalizer state with zero mean and unit variance
Source code in src/alberta_framework/core/normalizers.py
normalize(state, observation)
¶
Normalize observation and update Welford running statistics.
Uses Welford's online algorithm: 1. Increment count 2. Update mean incrementally 3. Update sum of squared deviations (p / M2) 4. Compute variance with Bessel's correction when count >= 2
Args: state: Current Welford normalizer state observation: Raw feature vector
Returns: Tuple of (normalized_observation, new_state)
Source code in src/alberta_framework/core/normalizers.py
normalize_only(state, observation)
¶
Normalize observation without updating statistics.
Useful for inference or when you want to normalize multiple observations with the same statistics.
Args: state: Current normalizer state observation: Raw feature vector
Returns: Normalized observation
Source code in src/alberta_framework/core/normalizers.py
update_only(state, observation)
¶
Update statistics without returning normalized observation.
Args: state: Current normalizer state observation: Raw feature vector
Returns: Updated normalizer state
Source code in src/alberta_framework/core/normalizers.py
normalizer_from_config(config)
¶
Reconstruct a normalizer from a config dict.
Args:
config: Dict with "type" key and constructor kwargs
Returns: Reconstructed normalizer instance
Raises: ValueError: If the normalizer type is unknown