stable_learning_control.utils.log_utils
Several utilities and helper functions used for logging.
Note
This module was based on spinningup repository.
Submodules
Classes
A variant of |
Functions
|
Colorize a string. |
|
Convert a dictionary to a markdown table. |
|
Print a colorized message to stdout. |
|
Sets up the output_dir for a logger and returns a dict for logger kwargs. |
Package Contents
- 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:
- Returns:
Colorized string.
- Return type:
- stable_learning_control.utils.log_utils.dict_to_mdtable(d, key='Name', val='Value')[source]
Convert a dictionary to a markdown table.
- 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
anderror
. Defaults toNone
.*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
instable_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) andhigh
(logs at every SGD update ” batch). Defaults tolow
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 andline
which prints them in one line. Defaults toline
.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
instable_learning_control/user_config.py
. Defaults toNone
.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:
- 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:
Initialise a EpochLogger.
- epoch_dict
- _tb_index_dict
- _n_table_dumps = 0
- 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 aval
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.
- _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:
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.