Metrics¶
Per-agent metric accumulators used internally by
Population to track training progress.
Base Class¶
- class agilerl.metrics.BaseMetrics(*, fitness_window: int = 100, nonscalar_window: int = 100000)¶
Base class for per-agent metrics.
- Parameters:
- add_fitness(value: float) None¶
Add a fitness value to the fitness history.
- Parameters:
value (float) – Fitness value to add.
- finalize_training_step(num_steps: int) None¶
Compute fps from the delta since
init_training_step().- Parameters:
num_steps (int) – Number of steps taken during the training step.
- abstract get_histogram(name: str, *args: str) ndarray | None¶
Return accumulated raw values for a histogram metric.
- increment_steps(n: int) None¶
Increment the cumulative environment step count.
- Parameters:
n – Number of steps to add.
- abstract log_histogram(name: str, values: ndarray) None¶
Extend the accumulator with raw sample values for a histogram metric.
- register(name: str) None¶
Register a named scalar metric to be tracked.
- Parameters:
name (str) – Unique metric name (e.g.
"loss","entropy").- Raises:
ValueError – If name is already registered.
- register_histogram(name: str) None¶
Register a named non-scalar (histogram) metric.
Non-scalar metrics store array data (e.g. action distributions) and are only reported by loggers that support them (TensorBoard).
- Parameters:
name (str) – Unique metric name (e.g.
"action_distribution").- Raises:
ValueError – If name is already registered.
Single-Agent Metrics¶
- class agilerl.metrics.AgentMetrics(*, fitness_window: int = 100, nonscalar_window: int = 100000)¶
Tracks training metrics for a single-agent RL algorithm instance.
- Parameters:
- get_histogram(name: str) ndarray | None¶
Return the accumulated raw values for a histogram metric.
- Parameters:
name (str) – Previously registered non-scalar metric name.
- Returns:
Array of all accumulated values, or
Noneif empty.- Return type:
numpy.ndarray | None
- log(name: str, value: float) None¶
Append a value to the accumulator for a registered metric.
Example: >>> metrics = AgentMetrics() >>> metrics.log(“loss”, 0.1) >>> metrics.log(“loss”, 0.2) >>> metrics.get_mean(“loss”)
- log_histogram(name: str, values: ndarray) None¶
Extend the accumulator with raw sample values for a histogram metric.
Values are accumulated across calls so that
get_histogram()returns the full distribution for the entire evo step.- Parameters:
name (str) – Previously registered non-scalar metric name.
values (numpy.ndarray) – Array of raw sample values (e.g. action indices).
Multi-Agent Metrics¶
- class agilerl.metrics.MultiAgentMetrics(agent_ids: list[str], *, fitness_window: int = 100, nonscalar_window: int = 100000)¶
Tracks training metrics for multi-agent RL algorithms.
Assumes that we log metrics for each sub-agent separately. For settings where we have homogeneous/grouped agents (e.g. speaker_0, speaker_1, listener_0, listener_1), we assume metrics are logged for the group as a whole.
- Parameters:
- get_histogram(name: str, agent_id: str) ndarray | None¶
Return accumulated raw values for a histogram metric and sub-agent.
- get_mean(name: str, agent_id: str) float¶
Return the mean of accumulated values for a registered metric and sub-agent.
- log(name: str, value: float, agent_id: str | None = None) None¶
Append a value to the accumulator for a registered metric and sub-agent.
Example: >>> metrics = MultiAgentMetrics([“speaker_0”, “speaker_1”, “listener_0”, “listener_1”]) >>> metrics.log(“loss”, 0.1, “speaker_0”) >>> metrics.log(“loss”, 0.2, “speaker_0”) >>> metrics.get_mean(“loss”, “speaker_0”)