stable_learning_control.utils.log_utils

Several utilities and helper functions used for logging.

Note

This module was based on spinningup repository.

Submodules

Package Contents

Classes

EpochLogger

A variant of Logger tailored for tracking average values over epochs.

Functions

colorize(string, color[, bold, highlight])

Colorize a string.

dict_to_mdtable(d[, key, val])

Convert a dictionary to a markdown table.

log_to_std_out(msg[, color, bold, highlight, type])

Print a colorized message to stdout.

setup_logger_kwargs(exp_name[, seed, ...])

Sets up the output_dir for a logger and returns a dict for logger kwargs.

stable_learning_control.utils.log_utils.colorize(string, color, bold=False, highlight=False)[source]

Colorize a string.

See also

This function wraps the gym.utils.colorize() function to make sure that it also works with empty color strings.

Parameters:
  • string (str) – The string you want to colorize.

  • color (str) – The color you want to use.

  • bold (bool, optional) – Whether you want the text to be bold. Defaults to False.

  • highlight (bool, optional) – Whether you want to highlight the text. Defaults to False.

Returns:

Colorized string.

Return type:

str

stable_learning_control.utils.log_utils.dict_to_mdtable(d, key='Name', val='Value')[source]

Convert a dictionary to a markdown table.

Parameters:
  • d (dict) – The dictionary you want to convert.

  • key (str, optional) – The name of the key column. Defaults to "Name".

  • val (str, optional) – The name of the value column. Defaults to "Value".

Returns:

The markdown table.

Return type:

str

stable_learning_control.utils.log_utils.log_to_std_out(msg, color='', bold=False, highlight=False, type=None, *args, **kwargs)[source]

Print a colorized message to stdout.

Parameters:
  • msg (str) – Message you want to log.

  • color (str, optional) – Color you want the message to have. Defaults to "".

  • bold (bool, optional) – Whether you want the text to be bold text has to be bold.

  • highlight (bool, optional) – Whether you want to highlight the text. Defaults to False.

  • type (str, optional) – The log message type. Options are: info, warning and error. Defaults to None.

  • *args – All args to pass to the print function.

  • **kwargs – All kwargs to pass to the print function.

stable_learning_control.utils.log_utils.setup_logger_kwargs(exp_name, seed=None, save_checkpoints=False, use_tensorboard=False, tb_log_freq='low', use_wandb=False, wandb_job_type=DEFAULT_WANDB_JOB_TYPE, wandb_project=DEFAULT_WANDB_PROJECT, wandb_group=None, wandb_run_name=None, quiet=False, verbose_fmt=DEFAULT_STD_OUT_TYPE, verbose_vars=[], data_dir=None, datestamp=False)[source]

Sets up the output_dir for a logger and returns a dict for logger kwargs.

If no seed is given and datestamp is false,

output_dir = data_dir/exp_name

If a seed is given and datestamp is false,

output_dir = data_dir/exp_name/exp_name_s[seed]

If datestamp is true, amend to

output_dir = data_dir/YY-MM-DD_exp_name/YY-MM-DD_HH-MM-SS_exp_name_s[seed]

You can force datestamp=True by setting FORCE_DATESTAMP=True in stable_learning_control/user_config.py.

Parameters:
  • exp_name (str) – Name for experiment.

  • seed (int, optional) – Seed for random number generators used by experiment.

  • save_checkpoints (bool, optional) – Save checkpoints during training. Defaults to False.

  • use_tensorboard (bool, optional) – Whether you want to use TensorBoard. Defaults to True.

  • tb_log_freq (str, optional) – The TensorBoard log frequency. Options are low (Recommended: logs at every epoch) and high (logs at every SGD update ” batch). Defaults to low since this is less resource intensive.

  • use_wandb (bool, optional) – Whether you want to use Weights & Biases. Defaults to False.

  • wandb_job_type (str, optional) – The Weights & Biases job type. Defaults to None.

  • wandb_project (str, optional) – The name of the Weights & Biases project you want to log to. Defaults to None.

  • wandb_group (str, optional) – The name of the Weights & Biases group you want to assign the run to. Defaults to None.

  • wandb_run_name (str, optional) – The name of the Weights & Biases run. Defaults to None which means that the run name is automatically generated.

  • quiet (bool, optional) – Whether you want to suppress logging of the diagnostics to the stdout. Defaults to False.

  • verbose_fmt (str, optional) – The format in which the diagnostics are displayed to the terminal. Options are table which supplies them as a table and line which prints them in one line. Defaults to line.

  • verbose_vars (list, optional) – A list of variables you want to log to the stdout. By default all variables are logged.

  • data_dir (str, optional) – Path to folder where results should be saved. Default is the DEFAULT_DATA_DIR in stable_learning_control/user_config.py. Defaults to None.

  • datestamp (bool, optional) – Whether to include a date and timestamp in the name of the save directory. Defaults to False.

Returns:

logger_kwargs

A dict containing output_dir and exp_name.

Return type:

dict

class stable_learning_control.utils.log_utils.EpochLogger(*args, **kwargs)[source]

Bases: Logger

A variant of Logger tailored for tracking average values over epochs.

Typical use case: there is some quantity which is calculated many times throughout an epoch, and at the end of the epoch, you would like to report the average/std/min/max value of that quantity.

With an EpochLogger, each time the quantity is calculated, you would use

epoch_logger.store(NameOfQuantity=quantity_value)

to load it into the EpochLogger’s state. Then at the end of the epoch, you would use

epoch_logger.log_tabular(NameOfQuantity, **options)

to record the desired values.

epoch_dict

Dictionary used to store variables you want to log into the EpochLogger current state.

Type:

dict

Initialise a EpochLogger.

store(tb_write=False, tb_aliases=dict(), extend=False, global_step=None, **kwargs)[source]

Save something into the EpochLogger’s current state.

Provide an arbitrary number of keyword arguments with numerical values.

Parameters:
  • tb_write (Union[bool, dict], optional) – Boolean or dict of key boolean pairs specifying whether you also want to write the value to the TensorBoard logfile. Defaults to False.

  • tb_aliases (dict, optional) – Dictionary that can be used to set aliases for the variables you want to store. Defaults to empty dict.

  • extend (bool, optional) – Boolean specifying whether you want to extend the values to the log buffer. By default False meaning the values are appended to the buffer.

  • global_step (int, optional) – Global step value to record. Uses internal step counter if global step is not supplied.

log_to_tb(keys, val=None, with_min_and_max=False, average_only=False, tb_prefix=None, tb_alias=None, global_step=None)[source]

Log a diagnostic to TensorBoard. This function takes or a list of keys or a key-value pair. If only keys are supplied, averages will be calculated using the new data found in the Loggers internal storage. If a key-value pair is supplied, this pair will be directly logged to TensorBoard.

Parameters:
  • keys (Union[list[str], str]) – The name(s) of the diagnostic.

  • val – A value for the diagnostic.

  • with_min_and_max (bool) – If True, log min and max values of the diagnostic.

  • average_only (bool) – If True, do not log the standard deviation of the diagnostic.

  • tb_prefix (str, optional) – A prefix which can be added to group the variables.

  • tb_alias (str, optional) – A tb alias for the variable you want to store store. Defaults to empty dict. If not supplied the variable name is used.

  • global_step (int, optional) – Global step value to record. Uses internal step counter if global step is not supplied.

log_tabular(key, val=None, with_min_and_max=False, average_only=False, tb_write=False, tb_prefix=None, tb_alias=None)[source]

Log a value or possibly the mean/std/min/max values of a diagnostic.

Parameters:
  • key (str) – The name of the diagnostic. If you are logging a diagnostic whose state has previously been saved with store(), the key here has to match the key you used there.

  • val – A value for the diagnostic. If you have previously saved values for this key via store(), do not provide a val here.

  • with_min_and_max (bool) – If True, log min and max values of the diagnostic over the epoch.

  • average_only (bool) – If True, do not log the standard deviation of the diagnostic over the epoch.

  • tb_write (bool, optional) – Boolean specifying whether you also want to write the value to the TensorBoard logfile. Defaults to False.

  • tb_prefix (str, optional) – A prefix which can be added to group the variables.

  • tb_alias (str, optional) – A tb alias for the variable you want to store store. Defaults to empty dict. If not supplied the variable name is used.

dump_tabular(*args, **kwargs)[source]

Small wrapper around the Logger.dump_tabular() method which makes sure that the TensorBoard index track dictionary is reset after the table is dumped.

Parameters:
  • *args – All args to pass to parent method.

  • **kwargs – All kwargs to pass to parent method.

get_stats(key)[source]

Lets an algorithm ask the logger for mean/std/min/max of a diagnostic.

Parameters:

key (str) – The key for which you want to get the stats.

Returns:

tuple containing:

  • mean(float): The current mean value.

  • std(float): The current mean standard deviation.

  • min(float): The current mean value.

  • max(float): The current mean value.

Return type:

(tuple)

_log_tb_diagnostics(key, with_min_and_max=False, average_only=False, tb_prefix=None, tb_alias=None, global_step=None)[source]

Calculates the diagnostics of a given key from all the new data found in the Loggers internal storage.

Parameters:
  • key (Union[list[str], str]) – The name of the diagnostic.

  • with_min_and_max (bool) – If True, log min and max values of the diagnostic.

  • average_only (bool) – If True, do not log the standard deviation of the diagnostic.

  • tb_prefix (str, optional) – A prefix which can be added to group the variables.

  • tb_alias (str, optional) – A tb alias for the variable you want to store store. Defaults to empty dict. If not supplied the variable name is used.

  • global_step (int, optional) – Global step value to record. Uses internal step counter if global step is not supplied.