ray.rllib.utils.metrics.metrics_logger.MetricsLogger.reduce#

MetricsLogger.reduce() Dict[source]#

Reduces all logged values based on their settings and returns a result dict.

DO NOT CALL THIS METHOD under normal circumstances! RLlib’s components call it right before a distinct step has been completed and the (MetricsLogger-based) results of that step need to be passed upstream to other components for further processing.

The returned result dict has the exact same structure as the logged keys (or nested key sequences) combined. Values are Stats objects if this MetricsLogger is not a root logger. If this MetricsLogger is a root logger, the values are the actual reduced values.

For example, imagine component A (e.g. an Algorithm) containing a MetricsLogger and n remote components (e.g. n EnvRunners), each with their own MetricsLogger object. Component A calls its n remote components, each of which returns an equivalent, reduced dict with values at the leafs. Component A can then further log these n result dicts through its own MetricsLogger through: logger.aggregate([n returned result dicts from n subcomponents]).

# Log some values under different keys.
logger.log_value("loss", 0.1, window=2)
logger.log_value("loss", 0.2, window=2)
logger.log_value("min_loss", 0.3, reduce="min", window=2)
logger.log_value("min_loss", 0.1, reduce="min", window=2)
# reduce() returns the reduced values.
results = logger.reduce()
check(results["loss"], 0.15)  # mean of [0.1, 0.2]
check(results["min_loss"], 0.1)  # min of [0.3, 0.1]
# We can also reduce a specific key using indexing.
check(logger.reduce()["loss"], 0.15)  # mean of [0.1, 0.2]
# Or reduce a nested key structure.
logger.log_value(("nested", "key"), 1.0)
check(logger.reduce()["nested"]["key"], 1.0)
Returns:

A (nested) dict matching the structure of self.stats (contains all ever logged keys to this MetricsLogger) with the leafs being (reduced) Stats objects if this MetricsLogger is not a root logger. If this MetricsLogger is a root logger, the leafs are the actual (reduced) values.