normalizer
normalizer
¶
Linear normalization module (min-max scaling, z-score, demean). Inspired by https://github.com/real-stanford/diffusion_policy/blob/main/diffusion_policy/model/common/normalizer.py
LinearNormalizer
¶
Bases: DictOfTensorMixin
Multi-key normalizer that stores per-key normalization parameters.
Supports dict input (per-key normalization) and tensor input (single-key '_default').
Source code in src/versatil/common/dict_of_tensor_mixin.py
fit
¶
fit(data, last_n_dims=1, dtype=float32, mode=value, output_max=1.0, output_min=-1.0, range_eps=0.0001, fit_offset=True, device=None, clamp_range=True, min_std=0.02, min_range=0.04, sample_size=0)
Fit normalization parameters to data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict | Tensor | ndarray
|
Dict of tensors (per-key fit) or a single tensor (stored under '_default'). |
required |
last_n_dims
|
int
|
Number of trailing dimensions to treat as features. |
1
|
dtype
|
dtype
|
Data type for the normalizer parameters. |
float32
|
mode
|
str
|
Normalization mode ('min_max', 'gaussian', or 'demean'). |
value
|
output_max
|
float
|
Maximum output value for min_max mode. |
1.0
|
output_min
|
float
|
Minimum output value for min_max mode. |
-1.0
|
range_eps
|
float
|
Epsilon for detecting constant dimensions. |
0.0001
|
fit_offset
|
bool
|
Whether to fit an offset (centering). |
True
|
device
|
device | str | None
|
Device to place parameters on. |
None
|
clamp_range
|
bool
|
Whether to clamp std/range to minimum values. |
True
|
min_std
|
float
|
Minimum std value when clamp_range=True and mode='gaussian'. |
0.02
|
min_range
|
float
|
Minimum range value when clamp_range=True and mode='min_max'. |
0.04
|
sample_size
|
int | dict[str, int]
|
Number of data rows to store as an empirical sample under
each key. Pass an int to use the same budget for every key, or a
|
0
|
Source code in src/versatil/data/normalization/normalizer.py
normalize
¶
Normalize input data. Dict keys without parameters pass through unchanged.
unnormalize
¶
Unnormalize input data back to original scale.
get_input_stats
¶
Get input statistics (min, max, mean, std) for all keys.
Source code in src/versatil/data/normalization/normalizer.py
get_output_stats
¶
Get normalized output statistics for a specific key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Parameter key. Use '_default' for tensor-fitted normalizers, or a specific key for dict-fitted normalizers. |
'_default'
|
Returns:
| Type | Description |
|---|---|
dict
|
Dict with normalized min, max, mean, and scaled std. |
Source code in src/versatil/data/normalization/normalizer.py
SingleFieldLinearNormalizer
¶
Bases: DictOfTensorMixin
Single-field normalizer with scale and offset parameters.
Source code in src/versatil/common/dict_of_tensor_mixin.py
fit
¶
fit(data, last_n_dims=1, dtype=float32, mode=value, output_max=1.0, output_min=-1.0, range_eps=1e-10, fit_offset=True, device='cpu', clamp_range=True, min_std=0.02, min_range=0.04, sample_size=0)
Fit scale and offset for one field from data statistics.
Source code in src/versatil/data/normalization/normalizer.py
create_fit
classmethod
¶
create_fit(data, last_n_dims=1, dtype=float32, mode=value, output_max=1.0, output_min=-1.0, range_eps=1e-10, fit_offset=True, device='cpu', clamp_range=True, min_std=0.02, min_range=0.04)
Create a fitted normalizer in one step.
Source code in src/versatil/data/normalization/normalizer.py
create_manual
classmethod
¶
Create a normalizer from explicit scale, offset, and input stats.
Source code in src/versatil/data/normalization/normalizer.py
create_identity
classmethod
¶
Create an identity normalizer (scale=1, offset=0).
Source code in src/versatil/data/normalization/normalizer.py
normalize
¶
unnormalize
¶
Apply inverse normalization: (x - offset) / scale.
get_input_stats
¶
get_output_stats
¶
Get normalized output statistics.
Source code in src/versatil/data/normalization/normalizer.py
get_input_sample
¶
Return the raw subsample stored at fit time, or None if absent.
Present only when fit(..., sample_size>0) was used.
Source code in src/versatil/data/normalization/normalizer.py
get_output_sample
¶
Return the stored subsample in normalized output space, or None.
Source code in src/versatil/data/normalization/normalizer.py
SequentialNormalizer
¶
Bases: SingleFieldLinearNormalizer
Applies a sequence of already-fitted normalizers.
Source code in src/versatil/data/normalization/normalizer.py
fit
¶
fit(data=None, last_n_dims=1, dtype=float32, mode=value, output_max=1.0, output_min=-1.0, range_eps=1e-10, fit_offset=True, device='cpu', clamp_range=True, min_std=0.02, min_range=0.04)
Fit per-key normalizers over a dict of arrays.
Source code in src/versatil/data/normalization/normalizer.py
normalize
¶
unnormalize
¶
Apply all normalizers in reverse sequence.
get_input_stats
¶
get_output_stats
¶
Get output stats after propagating through all normalizers.