Logger

To overwrite the default logger, you can pass one to the algorithm. Available formats are ["stdout", "csv", "log", "tensorboard", "json"].

Warning

When passing a custom logger object, this will overwrite tensorboard_log and verbose settings passed to the constructor.

from stable_baselines3 import A2C
from stable_baselines3.common.logger import configure

tmp_path = "/tmp/sb3_log/"
# set up logger
new_logger = configure(tmp_path, ["stdout", "csv", "tensorboard"])

model = A2C("MlpPolicy", "CartPole-v1", verbose=1)
# Set new logger
model.set_logger(new_logger)
model.learn(10000)
class stable_baselines3.common.logger.CSVOutputFormat(filename)[source]
close()[source]

closes the file

Return type:

None

write(key_values, key_excluded, step=0)[source]

Write a dictionary to file

Parameters:
  • key_values (Dict[str, Any]) –

  • key_excluded (Dict[str, Union[str, Tuple[str, ...]]]) –

  • step (int) –

Return type:

None

class stable_baselines3.common.logger.Figure(figure, close)[source]

Figure data class storing a matplotlib figure and whether to close the figure after logging it

Parameters:
  • figure (figure) – figure to log

  • close (bool) – if true, close the figure after logging it

exception stable_baselines3.common.logger.FormatUnsupportedError(unsupported_formats, value_description)[source]
class stable_baselines3.common.logger.HumanOutputFormat(filename_or_file)[source]
close()[source]

closes the file

Return type:

None

write(key_values, key_excluded, step=0)[source]

Write a dictionary to file

Parameters:
  • key_values (Dict) –

  • key_excluded (Dict) –

  • step (int) –

Return type:

None

write_sequence(sequence)[source]

write_sequence an array to file

Parameters:

sequence (List) –

Return type:

None

class stable_baselines3.common.logger.Image(image, dataformats)[source]

Image data class storing an image and data format

Parameters:
  • image (Union[Tensor, ndarray, str]) – image to log

  • dataformats (str) – Image data format specification of the form NCHW, NHWC, CHW, HWC, HW, WH, etc. More info in add_image method doc at https://pytorch.org/docs/stable/tensorboard.html Gym envs normally use ‘HWC’ (channel last)

class stable_baselines3.common.logger.JSONOutputFormat(filename)[source]
close()[source]

closes the file

Return type:

None

write(key_values, key_excluded, step=0)[source]

Write a dictionary to file

Parameters:
  • key_values (Dict[str, Any]) –

  • key_excluded (Dict[str, Union[str, Tuple[str, ...]]]) –

  • step (int) –

Return type:

None

class stable_baselines3.common.logger.KVWriter[source]

Key Value writer

close()[source]

Close owned resources

Return type:

None

write(key_values, key_excluded, step=0)[source]

Write a dictionary to file

Parameters:
  • key_values (Dict[str, Any]) –

  • key_excluded (Dict[str, Union[str, Tuple[str, ...]]]) –

  • step (int) –

Return type:

None

class stable_baselines3.common.logger.Logger(folder, output_formats)[source]

The logger class.

Parameters:
  • folder (Optional[str]) – the logging location

  • output_formats (List[KVWriter]) – the list of output formats

close()[source]

closes the file

Return type:

None

debug(*args)[source]

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

Parameters:

args – log the arguments

Return type:

None

dump(step=0)[source]

Write all of the diagnostics from the current iteration

Return type:

None

error(*args)[source]

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

Parameters:

args – log the arguments

Return type:

None

get_dir()[source]

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)

Return type:

str

Returns:

the logging directory

info(*args)[source]

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

Parameters:

args – log the arguments

Return type:

None

log(*args, level=20)[source]

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 (int) – the logging level (can be DEBUG=10, INFO=20, WARN=30, ERROR=40, DISABLED=50)

Return type:

None

record(key, value, exclude=None)[source]

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 (str) – save to log this key

  • value (Any) – save to log this value

  • exclude (Union[str, Tuple[str, ...], None]) – outputs to be excluded

Return type:

None

record_mean(key, value, exclude=None)[source]

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

Parameters:
  • key (str) – save to log this key

  • value (Any) – save to log this value

  • exclude (Union[str, Tuple[str, ...], None]) – outputs to be excluded

Return type:

None

set_level(level)[source]

Set logging threshold on current logger.

Parameters:

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

Return type:

None

warn(*args)[source]

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

Parameters:

args – log the arguments

Return type:

None

class stable_baselines3.common.logger.SeqWriter[source]

sequence writer

write_sequence(sequence)[source]

write_sequence an array to file

Parameters:

sequence (List) –

Return type:

None

class stable_baselines3.common.logger.TensorBoardOutputFormat(folder)[source]
close()[source]

closes the file

Return type:

None

write(key_values, key_excluded, step=0)[source]

Write a dictionary to file

Parameters:
  • key_values (Dict[str, Any]) –

  • key_excluded (Dict[str, Union[str, Tuple[str, ...]]]) –

  • step (int) –

Return type:

None

class stable_baselines3.common.logger.Video(frames, fps)[source]

Video data class storing the video frames and the frame per seconds

Parameters:
  • frames (Tensor) – frames to create the video from

  • fps (Union[float, int]) – frames per second

stable_baselines3.common.logger.configure(folder=None, format_strings=None)[source]

Configure the current logger.

Parameters:
  • folder (Optional[str]) – the save location (if None, $SB3_LOGDIR, if still None, tempdir/SB3-[date & time])

  • format_strings (Optional[List[str]]) – the output logging format (if None, $SB3_LOG_FORMAT, if still None, [‘stdout’, ‘log’, ‘csv’])

Return type:

Logger

Returns:

The logger object.

stable_baselines3.common.logger.filter_excluded_keys(key_values, key_excluded, _format)[source]

Filters the keys specified by key_exclude for the specified format

Parameters:
  • key_values (Dict[str, Any]) – log dictionary to be filtered

  • key_excluded (Dict[str, Union[str, Tuple[str, ...]]]) – keys to be excluded per format

  • _format (str) – format for which this filter is run

Return type:

Dict[str, Any]

Returns:

dict without the excluded keys

stable_baselines3.common.logger.make_output_format(_format, log_dir, log_suffix='')[source]

return a logger for the requested format

Parameters:
  • _format (str) – the requested format to log to (‘stdout’, ‘log’, ‘json’ or ‘csv’ or ‘tensorboard’)

  • log_dir (str) – the logging directory

  • log_suffix (str) – the suffix for the log file

Return type:

KVWriter

Returns:

the logger

stable_baselines3.common.logger.read_csv(filename)[source]

read a csv file using pandas

Parameters:

filename (str) – the file path to read

Return type:

DataFrame

Returns:

the data in the csv

stable_baselines3.common.logger.read_json(filename)[source]

read a json file using pandas

Parameters:

filename (str) – the file path to read

Return type:

DataFrame

Returns:

the data in the json