Loggers module

This module is a set of loggers. You can either choose one of our built-in loggers or implement your logger with the help of the Custom loggers guide.

LogsObserver

class LogsObserver

Class responsible for managing singleton instances of the loggers, initialization and finalization of the loggers, and passing the logged values to the appropriate loggers and their methods.

add_logger(source: tuple[str, SourceType] | str | None, logger_type: type, logger_params: dict[str, any]) None

Initializes a singleton instance of the logger and connects a given source with that logger.

Parameters:
  • source (Source) – Source to connect.

  • logger_type (type) – Type of the selected loger.

  • logger_params (dict) – Parameters of the selected logger.

init_loggers()

Initializes all loggers by calling their init method.

finish_loggers()

Finalizes the work of all loggers by calling their finish method.

update_observations(observations: any) None

Passes new observations to the loggers.

Parameters:

observations (dicy or any) – Observations received by the agent.

update_agent_state(agent_state: BaseAgent) None

Passes the agent state to the loggers.

Parameters:

agent_state (BaseAgent) – Current agent state.

update_metrics(metric: any, metric_name: str) None

Passes metrics to loggers.

Parameters:
  • metric (any) – Metric value.

  • metric_name (str) – Name of the metric.

update_custom(value: any, name: str) None

Passes values provided by the user to the loggers.

Parameters:
  • value (any) – Value to log.

  • name (str) – Name of the value.

BaseLogger

class BaseLogger(**kwargs)

Base interface for loggers.

init(sources: list[tuple[str, SourceType] | str | None]) None

Initializes the logger given the list of all sources defined by the user.

Parameters:

sources (list[Source]) – List containing the sources to log.

finish() None

Finalizes the loggers work (e.g., closes file or shows plots).

log_scalar(source: tuple[str, SourceType] | str | None, value: float | int, custom: bool) None

Method of the logger interface used for logging scalar values.

Parameters:
  • source (Source) – Source of the logged value.

  • value (float) – Scalar to log.

  • custom (bool) – Whether the source is a custom source.

log_array(source: tuple[str, SourceType] | str | None, value: Array | ndarray | bool | number, custom: bool) None

Method of the logger interface used for logging one-dimensional arrays.

Parameters:
  • source (Source) – Source of the logged value.

  • value (Array) – Array to log.

  • custom (bool) – Whether the source is a custom source.

log_dict(source: tuple[str, SourceType] | str | None, value: dict, custom: bool) None

Method of the logger interface used for logging dictionaries.

Parameters:
  • source (Source) – Source of the logged value.

  • value (dict) – Dictionary to log.

  • custom (bool) – Whether the source is a custom source.

log_other(source: tuple[str, SourceType] | str | None, value: any, custom: bool) None

Method of the logger interface used for logging other values.

Parameters:
  • source (Source) – Source of the logged value.

  • value (any) – Value of any type to log.

  • custom (bool) – Whether the source is a custom source.

static source_to_name(source: tuple[str, SourceType] | str | None) str

Returns a full name of the source. If source is a string itself, returns that string. Otherwise, it returns a string in the format “name-sourcetype” (e.g., “action-metric”).

Parameters:

source (Source) – Source of the logged value.

Returns:

Name of the source.

Return type:

str

CsvLogger

class CsvLogger(csv_path: str = None, **kwargs)

Bases: BaseLogger

Logger that saves values in CSV format. It saves the logged values to the CSV file when the experiment is finished. CsvLogger synchronizes the logged values in time. It means that if the same source is logged twice in a row, the step number will be incremented for all columns and the logger will move to the next row.

Parameters:

csv_path (str, default="~/rlib-logs-[date]-[time].csv") – Path to the output file.

finish() None

Saves the logged values to the CSV file.

log_scalar(source: tuple[str, SourceType] | str | None, value: float | int, *_) None

Logs a scalar as a standard value in a column.

Parameters:
  • source (Source) – Source of the logged value.

  • value (float) – Scalar to log.

log_array(source: tuple[str, SourceType] | str | None, value: Array | ndarray | bool | number, *_) None

Logs an array as a JSON string.

Parameters:
  • source (Source) – Source of the logged value.

  • value (Array) – Array to log.

log_dict(source: tuple[str, SourceType] | str | None, value: dict, *_) None

Logs a dictionary as a JSON string.

Parameters:
  • source (Source) – Source of the logged value.

  • value (dict) – Dictionary to log.

log_other(source: tuple[str, SourceType] | str | None, value: any, *_) None

Logs an object as a JSON string.

Parameters:
  • source (Source) – Source of the logged value.

  • value (any) – Value of any type to log.

StdoutLogger

class StdoutLogger(**kwargs)

Bases: BaseLogger

Logger that writes values to the standard output.

finish() None

Prints the last row if there are any unprinted values left.

log_scalar(source: tuple[str, SourceType] | str | None, value: float | int, custom: bool) None

Logs a scalar as the standard value.

Parameters:
  • source (Source) – Source of the logged value.

  • value (float) – Scalar to log.

  • custom (bool) – Whether the source is a custom source.

log_array(source: tuple[str, SourceType] | str | None, value: Array | ndarray | bool | number, custom: bool) None

Logs an array as a JSON string.

Parameters:
  • source (Source) – Source of the logged value.

  • value (Array) – Array to log.

  • custom (bool) – Whether the source is a custom source.

log_dict(source: tuple[str, SourceType] | str | None, value: dict, custom: bool) None

Logs a dictionary as a JSON string.

Parameters:
  • source (Source) – Source of the logged value.

  • value (dict) – Dictionary to log.

  • custom (bool) – Whether the source is a custom source.

log_other(source: tuple[str, SourceType] | str | None, value: any, custom: bool) None

Logs an object as a JSON string.

Parameters:
  • source (Source) – Source of the logged value.

  • value (any) – Value of any type to log.

  • custom (bool) – Whether the source is a custom source.

PlotsLogger

class PlotsLogger(plots_dir: str = None, plots_ext: str = 'pdf', plots_smoothing: float | int = 0.6, plots_scatter: bool = False, plots_sync_steps: bool = False, **kwargs)

Bases: BaseLogger

Logger that presents and saves values as matplotlib plots. Offers smoothing of the curve, scatter plots, and multiple curves in a single chart (while logging arrays). PlotsLogger is able to synchronize the logged values in time. This means that if the same source is logged less often than other sources, the step will be increased accordingly to maintain the appropriate spacing between the values on the x-axis.

Parameters:
  • plots_dir (str, default="~") – Output directory for the plots.

  • plots_ext (str, default="svg") – Extension of the saved plots.

  • plots_smoothing (float, default=0.6) – Weight of the exponential moving average (EMA/EWMA) [1] used for smoothing. \(\alpha \in [0, 1)\).

  • plots_scatter (bool, default=False) – Set to True if you want to generate a scatter plot instead of a line plot. plots_smoothing parameter does not apply to the scatter plots.

  • plots_sync_steps (bool, default=False) – Set to True if you want to synchronize the logged values in time.

References

finish() None

Shows the generated plots and saves them to the output directory with the specified extension (the names of the files follow the pattern "rlib-plot-[source]-[date]-[time].[ext]").

log_scalar(source: tuple[str, SourceType] | str | None, value: float | int, *_) None

Adds a given scalar to the plot values.

Parameters:
  • source (Source) – Source of the logged value.

  • value (float) – Scalar to log.

log_array(source: tuple[str, SourceType] | str | None, value: Array | ndarray | bool | number, *_) None

Log values from an array to the same plot. Creates multiple line plots for each value in the array.

Parameters:
  • source (Source) – Source of the logged value.

  • value (Array) – Array to log.

TensorboardLogger

class TensorboardLogger(tb_log_dir: str = None, tb_comet_config: dict[str, any] = None, tb_sync_steps: bool = False, **kwargs)

Bases: BaseLogger

Logger that saves values in TensorBoard [2] format. Offers a possibility to log to Comet [3]. TensorboardLogger synchronizes the logged values in time. This means that if the same source is logged less often than other sources, the step will be increased accordingly to maintain the appropriate spacing between the values on the x-axis.

Parameters:
  • tb_log_dir (str, optional) – Path to the output directory. If None, the default directory is used.

  • tb_comet_config (dict, optional) – Configuration for the Comet logger. If None, the logger is disabled.

  • tb_sync_steps (bool, default=False) – Set to True if you want to synchronize the logged values in time.

References

finish() None

Closes the summary writer.

log_scalar(source: tuple[str, SourceType] | str | None, value: float | int, *_) None

Adds a given scalar to the summary writer.

Parameters:
  • source (Source) – Source of the logged value.

  • value (float) – Scalar to log.

log_array(source: tuple[str, SourceType] | str | None, value: Array | ndarray | bool | number, *_) None

Log values from an array to the same plot. Creates multiple line plots for each value in the array.

Parameters:
  • source (Source) – Source of the logged value.

  • value (Array) – Array to log.

log_dict(source: tuple[str, SourceType] | str | None, value: dict, *_) None

Logs a dictionary as a JSON string.

Parameters:
  • source (Source) – Source of the logged value.

  • value (dict) – Dictionary to log.

log_other(source: tuple[str, SourceType] | str | None, value: any, *_) None

Logs an object as a JSON string.

Parameters:
  • source (Source) – Source of the logged value.

  • value (any) – Value of any type to log.

WeightsAndBiasesLogger

class WeightsAndBiasesLogger(wandb_sync_steps: bool = False, wandb_kwargs: dict = None, **kwargs)

Bases: BaseLogger

Logger that saves values to Weights & Biases [4] platform. WeightsAndBiasesLogger synchronizes the logged values in time. This means that if the same source is logged less often than other sources, the step will be increased accordingly to maintain the appropriate spacing between the values on the x-axis.

Note: to use this logger, you need to log into W&B before running the script. The necessary steps are described in the official documentation [4].

Parameters:
  • wandb_sync_steps (bool, default=False) – Set to True if you want to synchronize the logged values in time.

  • wandb_kwargs (dict, optional) – Additional keyword arguments passed to wandb.init function.

References

finish() None

Finishes the W&B run.

log_scalar(source: tuple[str, SourceType] | str | None, value: float | int, *_) None

Logs a scalar value to the W&B logger.

Parameters:
  • source (Source) – Source of the logged value.

  • value (float) – Scalar to log.

log_array(source: tuple[str, SourceType] | str | None, value: Array | ndarray | bool | number, *_) None

Logs an array to the W&B logger.

Parameters:
  • source (Source) – Source of the logged value.

  • value (Array) – Array to log.

log_dict(source: tuple[str, SourceType] | str | None, value: dict, *_) None

Logs a dictionary to the W&B logger.

Parameters:
  • source (Source) – Source of the logged value.

  • value (dict) – Dictionary to log.

log_other(source: tuple[str, SourceType] | str | None, value: any, *_) None

Logs an object to the W&B logger.

Parameters:
  • source (Source) – Source of the logged value.

  • value (any) – Value of any type to log.