Metrics¶
Overview¶
MASA metrics are designed to make it easy to record and report learning signals at different granularities: single scalars, streaming summary statistics, and approximate distributions. Keeping logging lightweight and backend-agnostic.
- Scalars are plain numeric values (e.g., reward, loss, episode length). Loggers keep a rolling window of recent scalar values and typically report a smoothed mean over that window.
- Summary statistics are handled by
masa.common.metrics.Stats, which performs streaming aggregation over batches of values. It tracks running moments (mean and mean-square) and extrema (min/max/magnitude), and exposes derived quantities like standard deviation: \(\sigma = \sqrt{\max(0, \mathbb{E}[X^2] - \mathbb{E}[X]^2)}\). CallingStats.getreturns a flat dictionary of scalars suitable for logging. - Distributions are handled by
masa.common.metrics.Dist, which maintains a fixed-size reservoir sample of a stream. This gives a compact approximation of the underlying distribution that can be plotted or logged as a histogram. - Logging is performed by logger implementations (see the next page), which accept mixtures of scalars,
Stats, andDistobjects. They aggregate values over a configurable window and can emit to stdout and/or TensorBoard with consistent key prefixing.
API Reference¶
Metrics Core¶
masa.common.metrics.Stats ¶
Streaming scalar summary statistics for an arbitrary batch of values.
This class maintains simple running aggregates over a stream of scalar observations:
- Mean: \(\mu\)
- Mean of squares: \(\mathbb{E}[X^2]\)
- Derived standard deviation: \(\sigma = \sqrt{\max(0, \mathbb{E}[X^2] - \mu^2)}\)
- Extremes: min/max
- Magnitude: \(\max |x|\)
The update method accepts any array-like input, flattens it to a 1D
vector, and updates the aggregates. The get method returns a dict of
scalars suitable for logging; if prefix is non-empty, keys are
prefixed with "{prefix}_".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
Optional key prefix (without trailing underscore). If provided,
keys returned by |
''
|
Attributes:
| Name | Type | Description |
|---|---|---|
n |
int
|
Total number of scalar samples seen so far. |
prefix |
str
|
Prefix used to namespace emitted keys. |
stats |
Optional[Dict[str, float]]
|
Internal aggregate state or |
Source code in masa/common/metrics.py
update ¶
Update aggregates with a batch of scalar values.
The input is converted to np.float32 and flattened. If the resulting
array is empty, the call is a no-op.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Union[ndarray, Iterable[float], float]
|
A scalar or array-like collection of numeric values. |
required |
Source code in masa/common/metrics.py
get ¶
Return the current statistics as a flat dict of scalars.
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
A mapping containing: |
Dict[str, float]
|
|
Dict[str, float]
|
|
Dict[str, float]
|
|
Dict[str, float]
|
|
Dict[str, float]
|
|
Dict[str, float]
|
If |
Dict[str, float]
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called before any data has been observed. |
Source code in masa/common/metrics.py
__add__ ¶
Combine two Stats objects into a new aggregate.
This is useful when you have two independent streams and want the same summary you would have obtained if you had processed all samples in one stream. The combination is exact for the tracked aggregates (first/second moments and extrema).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
'Stats'
|
Another |
required |
Returns:
| Type | Description |
|---|---|
'Stats'
|
A new |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
RuntimeError
|
If either object has not observed any data. |
Source code in masa/common/metrics.py
masa.common.metrics.Dist ¶
Dist(prefix: str = '', reservoir_size: int = 2048, rng: Optional[Union[int, Generator, BitGenerator]] = None)
Reservoir-sampled distribution summary.
This class maintains a fixed-size reservoir sample of a stream of scalar values using reservoir sampling. After processing \(n\) total samples, a reservoir of size \(k\) contains a uniform sample (without replacement) from the observed stream (in expectation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
Optional logical prefix for this distribution (used by loggers). |
''
|
reservoir_size
|
int
|
Maximum number of samples retained in the reservoir. |
2048
|
rng
|
Optional[Union[int, Generator, BitGenerator]]
|
Seed (or seed-like) passed to |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
n |
int
|
Total number of scalar samples seen so far. |
prefix |
str
|
Prefix used by |
reservoir_size |
int
|
Maximum reservoir capacity. |
res |
ndarray
|
Current reservoir buffer of shape |
rng |
Generator
|
Numpy random generator used for reservoir sampling. |
Source code in masa/common/metrics.py
update ¶
Update the reservoir with a batch of values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Union[ndarray, Iterable[float], float]
|
A scalar or array-like collection of numeric values. The input is flattened into a 1D array. |
required |
Source code in masa/common/metrics.py
get ¶
Return a copy of the reservoir buffer.
Returns:
| Type | Description |
|---|---|
ndarray
|
A copy of the internal reservoir array (dtype |
__add__ ¶
Merge two reservoirs into a new reservoir.
The merged reservoir is produced by sampling (without replacement) from
the concatenation of both reservoirs, capped at reservoir_size.
This is a pragmatic merge for logging/visualisation; it does not exactly
reproduce a single-pass reservoir over the full underlying streams.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
'Dist'
|
Another |
required |
Returns:
| Type | Description |
|---|---|
'Dist'
|
A new |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in masa/common/metrics.py
Next Steps¶
- Logging - Learn how logging is handled automatically in MASA.