Logger#

Simple Usage#

import safety_gymnasium
from safepo.common.logger import EpochLogger

algo = 'Random'
env_name = 'SafetyPointGoal1-v0'
seed = 0
exp_name = 'example'

log_dir = f'./{algo}/{env_name}/{exp_name}'

logger = EpochLogger(
   log_dir = log_dir,
   seed = str(seed)
)

exp_config = {
   'env_name': env_name,
   'seed': seed,
   'algo': algo,
   'exp_name': exp_name,
}

env = safety_gymnasium.make(env_name)
env.reset(seed=seed)
d = False

ep_ret, ep_cost = 0, 0
while not d:
   a = env.action_space.sample()
   o, r, c, te, tr, info = env.step(a)
   d = te or tr
   ep_ret += r
   ep_cost += c

logger.store(
   **{
      "EpRet": ep_ret,
      "EpCost": ep_cost,
   }
)
logger.log_tabular("EpRet")
logger.log_tabular("EpCost")

logger.dump_tabular()

API Documentation#

class safepo.common.logger.Logger(log_dir, seed=None, output_fname='progress.csv', debug: bool = False, level: int = 1, use_tensorboard=True, verbose=True)#

Bases: object

A class for logging experimental data and managing logging-related functionalities.

Parameters:
  • log_dir (str) – The directory path for storing log files.

  • seed (int or None) – The seed for reproducibility. Default is None.

  • output_fname (str) – The name of the output file. Default is “progress.csv”.

  • debug (bool) – Toggle for debugging mode. Default is False.

  • level (int) – The logging level. Default is 1.

  • use_tensorboard (bool) – Toggle for using TensorBoard logging. Default is True.

  • verbose (bool) – Toggle for verbose output. Default is True.

close()#

Close the output file.

debug(msg, color='yellow')#

Print a colorized message to stdout.

dump_tabular() None#

Write all of the diagnostics from the current iteration.

Writes both to stdout, and to the output file.

log(msg, color='green')#

Print a colorized message to stdout.

log_tabular(key, val)#

Log a key-value pair in a tabular format for subsequent output.

Parameters:
  • key (str) – The key to log.

  • val – The corresponding value to log.

Raises:

AssertionError – If attempting to introduce a new key that was not included in the first iteration, or if the key has already been set in the current iteration.

save_config(config)#

Save the experiment configuration as a JSON file.

Parameters:

config (dict) – The experiment configuration to be saved.

Notes

If exp_name is specified, it will be added to the configuration.

Returns:

None

save_state(state_dict, itr=None)#

Save the state dictionary using joblib’s pickling mechanism.

Parameters:
  • state_dict – The state dictionary to be saved.

  • itr (int or None) – The iteration number. If provided, it’s used in the filename.

Notes

If itr is None, the default filename is “state.pkl”.

Returns:

None

setup_torch_saver(what_to_save)#

Set up easy model saving for a single PyTorch model.

Because PyTorch saving and loading is especially painless, this is very minimal; we just need references to whatever we would like to pickle. This is integrated into the logger because the logger knows where the user would like to save information about this training run.

Parameters:

what_to_save – Any PyTorch model or serializable object containing PyTorch models.

torch_save(itr=None)#

Saves the PyTorch model (or models).

class safepo.common.logger.EpochLogger(log_dir, seed=None, output_fname='progress.csv', debug: bool = False, level: int = 1, use_tensorboard=True, verbose=True)#

Bases: Logger

dump_tabular()#

Write all of the diagnostics from the current iteration.

Writes both to stdout, and to the output file.

get_stats(key)#

Get the values of a diagnostic.

log_tabular(key, val=None, min_and_max=False, std=False)#

Log a key-value pair in a tabular format for subsequent output.

Parameters:
  • key (str) – The key to log.

  • val – The corresponding value to log.

Raises:

AssertionError – If attempting to introduce a new key that was not included in the first iteration, or if the key has already been set in the current iteration.

store(add_value=False, **kwargs)#