logger module

Logging for quantitative metrics and free-form text. Code adopted from https://github.com/HumanCompatibleAI/imitation.git

class logger.HierarchicalLogger(default_logger: stable_baselines3.common.logger.Logger, format_strs: Sequence[str] = ('stdout', 'log', 'csv'))

Bases: stable_baselines3.common.logger.Logger

A logger supporting contexts for accumulating mean values.

self.accumulate_means creates a context manager. While in this context, values are loggged to a sub-logger, with only mean values recorded in the top-level (root) logger.

accumulate_means(subdir: Union[str, bytes, os.PathLike]) Generator[None, None, None]

Temporarily modifies this HierarchicalLogger to accumulate means values.

During this context, self.record(key, value) writes the “raw” values in “{self.default_logger.log_dir}/{subdir}” under the key “raw/{subdir}/{key}”. At the same time, any call to self.record will also accumulate mean values on the default logger by calling self.default_logger.record_mean(f”mean/{subdir}/{key}”, value).

During the context, self.record(key, value) will write the “raw” values in “{self.default_logger.log_dir}/subdir” under the key “raw/{subdir}/key”.

After the context exits, calling self.dump() will write the means of all the “raw” values accumulated during this context to self.default_logger under keys with the prefix mean/{subdir}/

Note that the behavior of other logging methods, log and record_mean are unmodified and will go straight to the default logger.

Args:
subdir: A string key which determines the folder where raw data is

written and temporary logging prefixes for raw and mean data. Entering an accumulate_means context in the future with the same subdir will safely append to logs written in this folder rather than overwrite.

Yields:

None when the context is entered.

Raises:
RuntimeError: If this context is entered into while already in

an accumulate_means context.

close()

closes the file

dump(step=0)

Write all of the diagnostics from the current iteration

get_dir() str

Get directory that log files are being written to. will be None if there is no output directory (i.e., if you didn’t call start)

Returns

the logging directory

log(*args, **kwargs)

Write the sequence of args, with no separators, to the console and output files (if you’ve configured an output file).

level: int. (see logger.py docs) If the global logger level is higher than

the level argument here, don’t print to stdout.

Parameters
  • args – log the arguments

  • level – the logging level (can be DEBUG=10, INFO=20, WARN=30, ERROR=40, DISABLED=50)

record(key, val, exclude=None)

Log a value of some diagnostic Call this once for each diagnostic quantity, each iteration If called many times, last value will be used.

Parameters
  • key – save to log this key

  • value – save to log this value

  • exclude – outputs to be excluded

record_mean(key, val, exclude=None)

The same as record(), but if called many times, values averaged.

Parameters
  • key – save to log this key

  • value – save to log this value

  • exclude – outputs to be excluded

set_level(level: int) None

Set logging threshold on current logger.

Parameters

level – the logging level (can be DEBUG=10, INFO=20, WARN=30, ERROR=40, DISABLED=50)

class logger.WandbOutputFormat

Bases: stable_baselines3.common.logger.KVWriter

A stable-baseline logger that writes to wandb.

Users need to call wandb.init() before initializing WandbOutputFormat.

close() None

Close owned resources

write(key_values: Dict[str, Any], key_excluded: Dict[str, Union[str, Tuple[str, ...]]], step: int = 0) None

Write a dictionary to file

Parameters
  • key_values

  • key_excluded

  • step

logger.configure(folder: Optional[Union[str, bytes, os.PathLike]] = None, format_strs: Optional[Sequence[str]] = None) logger.HierarchicalLogger

Configure Stable Baselines logger to be accumulate_means()-compatible.

After this function is called, stable_baselines3.logger.{configure,reset}() are replaced with stubs that raise RuntimeError.

Args:

folder: Argument from stable_baselines3.logger.configure. format_strs: An list of output format strings. For details on available

output formats see stable_baselines3.logger.make_output_format.

Returns:

The configured HierarchicalLogger instance.