Loggers
Using a Logger
SLC ships with basic logging tools, implemented in the classes
Logger
and EpochLogger
. The Logger
class contains most of the basic functionality for saving diagnostics,
hyperparameter configurations, the state of a training run, and the trained model. The EpochLogger
class adds a thin layer to make it easy to track a diagnostic’s average, standard deviation, min,
and max value over each epoch and across MPI workers.
You Should Know
All SLC algorithm implementations use an EpochLogger.
These loggers allow you to write these diagnostics to the stdout
, a csv/txt
file, TensorBoard and/or
Weights & Biases.
Examples
First, let’s look at a simple example of how an EpochLogger
keeps track of a diagnostic value:
>>> from stable_learning_control.utils.logx import EpochLogger
>>> epoch_logger = EpochLogger()
>>> for i in range(10):
epoch_logger.store(Test=i)
>>> epoch_logger.log_tabular('Test', with_min_and_max=True)
>>> epoch_logger.dump_tabular()
-------------------------------------
| AverageTest | 4.5 |
| StdTest | 2.87 |
| MaxTest | 9 |
| MinTest | 0 |
-------------------------------------
The store()
method is used to save all
values of Test
to the EpochLogger
’s internal
state. Then, when log_tabular()
is called,
it computes the average, standard deviation, min, and max of Test
over all of the values in the internal state.
The internal state is wiped clean after the call to log_tabular()
(to prevent leakage into the diagnostics at the next epoch). Finally, dump_tabular()
is called to write the diagnostics to file, TensorBoard, Weights & Biases and/or stdout.
Next, let’s use the Pytorch Classifier tutorial to look at an entire training procedure with the logger embedded, to highlight configuration and model saving as well as diagnostic logging:
1 import time
2 import math
3
4 import torch
5 import torch.nn as nn
6 import torch.nn.functional as F
7 import torchvision
8 import torch.optim as optim
9 import torchvision.transforms as transforms
10 import matplotlib.pyplot as plt
11 import numpy as np
12
13 from stable_learning_control.utils.log_utils.logx import EpochLogger
14
15
16 class Net(nn.Module):
17 def __init__(self):
18 super(Net, self).__init__()
19 self.conv1 = nn.Conv2d(3, 6, 5)
20 self.pool = nn.MaxPool2d(2, 2)
21 self.conv2 = nn.Conv2d(6, 16, 5)
22 self.fc1 = nn.Linear(16 * 5 * 5, 120)
23 self.fc2 = nn.Linear(120, 84)
24 self.fc3 = nn.Linear(84, 10)
25
26 def forward(self, x):
27 x = self.pool(F.relu(self.conv1(x)))
28 x = self.pool(F.relu(self.conv2(x)))
29 x = x.view(-1, 16 * 5 * 5)
30 x = F.relu(self.fc1(x))
31 x = F.relu(self.fc2(x))
32 x = self.fc3(x)
33 return x
34
35
36 def imshow(img):
37 img = img / 2 + 0.5 # unnormalize.
38 npimg = img.numpy()
39 plt.imshow(np.transpose(npimg, (1, 2, 0)))
40 plt.show()
41
42 # Simple script for training an CNN on CIFAR10.
43 def train_cifar10(
44 epochs=2,
45 batch_size=4,
46 lr=1e-3,
47 logger_kwargs=dict(),
48 save_freq=1,
49 ):
50 # Setup logger and save hyperparameters.
51 logger = EpochLogger(**logger_kwargs, verbose_fmt="tab")
52 logger.save_config(locals())
53
54 # Load and preprocess CIFAR10 data
55 transform = transforms.Compose(
56 [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
57 )
58 trainset = torchvision.datasets.CIFAR10(
59 root="./data", train=True, download=True, transform=transform
60 )
61 trainloader = torch.utils.data.DataLoader(
62 trainset, batch_size=batch_size, shuffle=True, num_workers=2
63 )
64 classes = (
65 "plane",
66 "car",
67 "bird",
68 "cat",
69 "deer",
70 "dog",
71 "frog",
72 "horse",
73 "ship",
74 "truck",
75 )
76
77 # print information about the dataset.
78 total_samples = len(trainset)
79 n_iterations = math.ceil(total_samples / batch_size)
80 logger.log(
81 "We perform {} epochs on our dataset that contains {} samples ".format(
82 epochs,
83 total_samples,
84 )
85 + "and each epoch has {} iterations.".format(n_iterations),
86 type="info",
87 )
88
89 # get some random training images.
90 dataiter = iter(trainloader)
91 images, labels = dataiter.next()
92
93 # show images.
94 imshow(torchvision.utils.make_grid(images))
95 logger.log(
96 "labels:" + " ".join("%5s" % classes[labels[j]] for j in range(4)), type="info"
97 ) # print labels.
98
99 # Define a Convolutional Neural Network.
100 net = Net()
101
102 # Define a Loss function and optimizer.
103 criterion = nn.CrossEntropyLoss()
104 optimizer = optim.SGD(net.parameters(), lr=lr, momentum=0.9)
105
106 # Setup model saving.
107 logger.setup_pytorch_saver(net)
108
109 # Run main training loop.
110 start_time = time.time()
111 for epoch in range(epochs): # loop over the dataset multiple times.
112 running_loss = 0.0
113 correct = 0
114 for i, data in enumerate(trainloader, 0):
115 # get the inputs; data is a list of [inputs, labels]
116 inputs, labels = data
117
118 # zero the parameter gradients.
119 optimizer.zero_grad()
120
121 # forward + backward + optimize
122 outputs = net(inputs)
123 loss = criterion(outputs, labels)
124 logger.store(Loss=loss)
125 loss.backward()
126 optimizer.step()
127
128 # calculate accuracy and increment running loss.
129 _, predicted = torch.max(outputs.data, 1)
130 correct += (predicted == labels).float().sum()
131 accuracy = 100 * correct / len(trainset)
132 running_loss += loss.item()
133
134 # print diagnostics.
135 running_loss += loss.item()
136 if i % 2000 == 1999: # print every 2000 mini-batches
137 logger.log(
138 "[%d, %5d] loss: %.3f, acc: %.3f"
139 % (epoch + 1, i + 1, running_loss / 2000, accuracy)
140 )
141 logger.store(Loss=loss, Acc=accuracy)
142 running_loss = 0.0
143 correct = 0
144
145 # Save model.
146 if (epoch % save_freq == 0) or (epoch == epochs - 1):
147 logger.save_state(state_dict=dict(), itr=None)
148
149 # Log info about epoch.
150 logger.log_tabular("Epoch", epoch, tb_write=True)
151 logger.log_tabular("Acc", with_min_and_max=True, tb_write=True)
152 logger.log_tabular("Loss", average_only=True, tb_write=True)
153 logger.log_tabular(
154 "TotalGradientSteps", (epoch + 1) * total_samples, tb_write=True
155 )
156 logger.log_tabular("Time", time.time() - start_time)
157 logger.dump_tabular()
158
159 logger.log("Finished Training")
160
161 if __name__ == "__main__":
162 train_cifar10()
In this example, observe that
on line 13, the
EpochLogger
class is imported.On line 51, the
EpochLogger
object is initiated. We set thestdout
format to the “tabular” format.On line 52,
save_config()
is used to save the hyperparameter configuration to a JSON file.On lines 80-87, 95-97, 137-140 and 159 we log information to the
stdout
. Additionally we can specify theuse_wandb
flag to log information to Weights & Biases.On lines 107
setup_pytorch_saver()
is used to prepare the logger to save the key elements of the CNN model.On line 141, diagnostics are saved to the logger’s internal state via
store()
.On line 147, the CNN model saved once per epoch via
save_state()
.On lines 150-157,
log_tabular()
anddump_tabular()
are used to write the epoch diagnostics to file. Note that the keys passed intolog_tabular()
are the same as the keys passed intostore()
. We use thetb_write=True
option to enable TensorBoard logging.
After this script has finished, the following files should be added to a /tmp/experiments/<TIMESTAMP>
folder:
events.out.tfevents.*
: The TensorBoard event file containing the logged diagnostics (if TensorBoard logging is enabled).progress.csv
: Contains the logged diagnostics.config.json
: Contains the hyperparameter configuration.vars.pkl
: Contains some additional saved variables used for easy loading.wandb
: Contains the Weights & Biases logging files (if Weights & Biases logging is enabled).pyt_save
: Contains the saved PyTorch model (if PyTorch backend is used).tf_save
: Contains the saved TensorFlow model (if TensorFlow backend is used).
If you requested TensorBoard logging, you can use the tensorboard --logdir=/tmp/experiments
command to see the diagnostics in TensorBoard. For Weights & Biases logging, you can use
the wandb login
command to log in to your Weights & Biases account. You can view your logged diagnostics on the Weights & Biases website.
Logging and TensorFlow
The preceding example was given in Pytorch. For TensorFlow, everything is the same except for L42-43:
instead of setup_pytorch_saver()
, you would
use setup_tf_saver()
and you would pass it
a TensorFlow Module (the algorithm you train) as an argument.
The behavior of save_state()
is the same as in the
PyTorch case: each time it is called,
it’ll save the latest version of the TensorFlow module.
Logging and MPI
You Should Know
Several RL algorithms are easily parallelized using MPI to average gradients and/or
other key quantities. The SLC loggers are designed to be well-behaved when using MPI:
things will only get written to stdout, file or TensorBoard from the process with rank
1. But information from other processes is preserved if you use the EpochLogger.
2. Everything passed into EpochLogger via store
, regardless of which process
3. it’s stored in, gets used to compute average/std/min/max values for a diagnostic.
Logger Classes
- class stable_learning_control.utils.log_utils.logx.Logger(output_dir=None, output_fname='progress.csv', exp_name=None, quiet=False, verbose_fmt='line', verbose_vars=[], save_checkpoints=False, backend='torch', output_dir_exists_warning=True, use_wandb=False, wandb_job_type=None, wandb_project=None, wandb_group=None, wandb_run_name=None)[source]
A general-purpose logger.
Makes it easy to save diagnostics, hyperparameter configurations, the state of a training run, and the trained model.
- __init__(output_dir=None, output_fname='progress.csv', exp_name=None, quiet=False, verbose_fmt='line', verbose_vars=[], save_checkpoints=False, backend='torch', output_dir_exists_warning=True, use_wandb=False, wandb_job_type=None, wandb_project=None, wandb_group=None, wandb_run_name=None)[source]
Initialise a Logger.
- Parameters:
output_dir (str, optional) – A directory for saving results to. If
None
, defaults to a temp directory of the form/tmp/experiments/somerandomnumber
.output_fname (str, optional) – Name for the (comma/tab) separated-value file containing metrics logged throughout a training run. Defaults to to
progress.csv
which uses commas as separators.exp_name (str, optional) – Experiment name. If you run multiple training runs and give them all the same
exp_name
, the plotter will know to group them. (Use case: if you run the same hyperparameter configuration with multiple random seeds, you should give them all the sameexp_name
.)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
tab
which supplies them as a table andline
which prints them in one line. Default is set in theuser_config
file.verbose_vars (list, optional) – A list of variables you want to log to the stdout. By default all variables are logged.
save_checkpoints (bool, optional) – Save checkpoints during training. Defaults to
False
.backend (str, optional) – The backend you want to use for writing to TensorBoard. Options are:
tf2
ortorch
. Defaults totorch
.output_dir_exists_warning (bool, optional) – Whether to print a warning when the output directory already exists. Defaults to
True
.use_wandb (bool, optional) – Whether to use Weights & Biases for logging. 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. Defaults to
None
which means that the project name is automatically generated.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.
- tb_writer[source]
A TensorBoard writer. This is only created when you log a variable to TensorBoard or set the
Logger.use_tensorboard
variable toTrue
.
- log(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.
- log_to_tb(key, val, tb_prefix=None, tb_alias=None, global_step=None)[source]
Log a value to TensorBoard.
- Parameters:
key (str) – The name of the diagnostic.
val (object) – A value for 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, tb_write=False, tb_prefix=None, tb_alias=None)[source]
Log a value of some diagnostic.
Call this only once for each diagnostic quantity, each iteration. After using
log_tabular()
to store values for each diagnostic, make sure to calldump_tabular()
to write them out to file, TensorBoard andstdout
(otherwise they will not get saved anywhere).- Parameters:
key (str) – The name of the diagnostic. If you are logging a diagnostic whose state has previously been saved with
EpochLogger.store()
, the key here has to match the key you used there.val (object) – A value for the diagnostic. If you have previously saved values for this key via
EpochLogger.store()
, do not provide aval
here.tb_write (bool, optional) – Boolean specifying whether you also want to write the value to the TensorBoard logfile. Defaults to
False
.tb_metrics (Union[list[str], str], optional) – List containing the metrics you want to write to TensorBoard. Options are [
avg
,std
,min
,max
].`` Defaults toavg
.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(global_step=None)[source]
Write all of the diagnostics from the current iteration.
Writes both to
stdout
, TensorBoard and to the output file.- Parameters:
global_step (int, optional) – Global step value to record. Uses internal step counter if global step is not supplied.
- get_logdir(*args, **kwargs)[source]
Get Logger and TensorBoard SummaryWriter logdirs.
- Parameters:
*args – All args to pass to the Summary/SummaryWriter object.
**kwargs – All kwargs to pass to the Summary/SummaryWriter object.
- Returns:
dict containing:
- Return type:
(dict)
- save_config(config)[source]
Log an experiment configuration.
Call this once at the top of your experiment, passing in all important config vars as a dict. This will serialize the config to JSON, while handling anything which can’t be serialized in a graceful way (writing as informative a string as possible). The resulting JSON will be saved to the output directory, made available to TensorBoard and Weights & Biases and printed to stdout.
Example
logger = EpochLogger(**logger_kwargs) logger.save_config(locals())
- Parameters:
config (object) – Configuration Python object you want to save.
- classmethod load_env(env_path)[source]
Loads a pickled environment.
- Parameters:
config_path (str) – Folder that contains the pickled environment.
- Returns:
The gymnasium environment.
- Return type:
(
gym.env
)
- save_to_json(input_object, output_filename, output_path=None)[source]
Save python object to Json file. This method will serialize the object to JSON, while handling anything which can’t be serialized in a graceful way (writing as informative a string as possible).
- Parameters:
input_object (object) – The input object you want to save.
output_filename (str) – The output filename.
output_path (str) – The output path. By default the
Logger.output_dir
is used.
- save_state(state_dict, itr=None)[source]
Saves the state of an experiment.
Important
To be clear: this is about saving state, not logging diagnostics. All diagnostic logging is separate from this function. This function will save whatever is in
state_dict
—usually just a copy of the environment—and the most recent parameters for the model you previously set up saving for withsetup_tf_saver()
orsetup_pytorch_saver()
.Call with any frequency you prefer. If you only want to maintain a single state and overwrite it at each call with the most recent version, leave
itr=None
. If you want to keep all of the states you save, provide unique (increasing) values for ‘itr’.
- setup_tf_saver(what_to_save)[source]
Set up easy model saving for a single Tensorlow model.
- Parameters:
what_to_save (object) – Any Tensorflow model or serializable object containing TensorFlow models.
- setup_pytorch_saver(what_to_save)[source]
Set up easy model saving for a single PyTorch model.
- Parameters:
what_to_save (object) – Any PyTorch model or serializable object containing PyTorch models.
- property use_tensorboard[source]
Variable specifying whether the logger uses TensorBoard. A TensorBoard writer is created on the
Logger.tb_writer
attribute whenuse_tensorboard
is set toTrue
- watch_model_in_wandb(model)[source]
Watch model parameters in Weights & Biases.
- Parameters:
model (torch.nn.Module) – Model to watch on Weights & Biases.
- log_model_to_tb(model, input_to_model=None, *args, **kwargs)[source]
Add model to tb summary.
Wrapper that calls the
torch.utils.tensorboard.SummaryWriter.add_graph
ortf.summary.graph
(depending on the backend) method while first making sure a SummaryWriter object exits.- Parameters:
model (union[torch.nn.Module, tf.keras.Model]) – Model to add to the summary.
input_to_model (union[torch.Tensor, tf.Tensor]) – Input tensor to the model.
*args – All args to pass to the Summary/SummaryWriter object.
**kwargs – All kwargs to pass to the Summary/SummaryWriter object.
- add_tb_scalar(*args, **kwargs)[source]
Add scalar to tb summary.
Wrapper that calls the
torch.utils.tensorboard.SummaryWriter.add_scalar
ortf.summary.scalar
(depending on the backend) method while first making sure a SummaryWriter object exits. These methods can be used to add a scalar to the summary.- Parameters:
*args – All args to pass to the Summary/SummaryWriter object.
**kwargs – All kwargs to pass to the Summary/SummaryWriter object.
- tb_scalar(*args, **kwargs)[source]
Wrapper for making the
add_tb_scalar()
method available directly under thescalar
alias.- Parameters:
*args – All args to pass to the
add_tb_scalar()
method.**kwargs – All kwargs to pass to the
add_tb_scalar()
method.
- add_tb_histogram(*args, **kwargs)[source]
Add histogram to tb summary.
Wrapper that calls the
torch.utils.tensorboard.SummaryWriter.add_histogram
ortf.summary.histogram
(depending on the backend) method while first making sure a SummaryWriter object exits. These methods can be used to add a histogram to the summary.- Parameters:
*args – All args to pass to the Summary/SummaryWriter object.
**kwargs – All kwargs to pass to the Summary/SummaryWriter object.
- tb_histogram(*args, **kwargs)[source]
Wrapper for making the
add_tb_histogram()
method available directly under thetb_histogram
alias.- Parameters:
*args – All args to pass to the
add_tb_histogram()
method.**kwargs – All kwargs to pass to the
add_tb_histogram()
method.
- add_tb_image(*args, **kwargs)[source]
Add image to tb summary.
Wrapper that calls the
torch.utils.tensorboard.SummaryWriter.add_image
ortf.summary.image
(depending on the backend) method while first making sure a SummaryWriter object exits. These methods can be used to add a image to the summary.- Parameters:
*args – All args to pass to the Summary/SummaryWriter object.
**kwargs – All kwargs to pass to the Summary/SummaryWriter object.
- tb_image(*args, **kwargs)[source]
Wrapper for making the
add_tb_image()
method available directly under thetb_image
alias.- Parameters:
*args – All args to pass to the
add_tb_image()
method.**kwargs – All kwargs to pass to the
add_tb_image()
method.
- add_tb_text(*args, **kwargs)[source]
Add text to tb summary.
Wrapper that calls the
torch.utils.tensorboard.SummaryWriter.add_text
ortf.summary.add_text
(depending on the backend) method while first making sure a SummaryWriter object exits. These methods can be used to add text to the summary.- Parameters:
*args – All args to pass to the Summary/SummaryWriter object.
**kwargs – All kwargs to pass to the Summary/SummaryWriter object.
- tb_text(*args, **kwargs)[source]
Wrapper for making the
add_tb_text()
method available directly under thetext
alias.- Parameters:
*args – All args to pass to the
add_tb_text()
method.**kwargs – All kwargs to pass to the
add_tb_text()
method.
- add_tb_graph(*args, **kwargs)[source]
Add graph to tb summary.
Wrapper that calls the
torch.utils.tensorboard.SummaryWriter.add_graph
ortf.summary.add_graph
(depending on the backend) method while first making sure a SummaryWriter object exits. These methods can be used to add a graph to the summary.- Parameters:
*args – All args to pass to the Summary/SummaryWriter object.
**kwargs – All kwargs to pass to the Summary/SummaryWriter object.
- tb_graph(*args, **kwargs)[source]
Wrapper for making the
add_tb_graph()
method available directly under thetb_graph
alias.- Parameters:
*args – All args to pass to the
add_tb_graph()
method.**kwargs – All kwargs to pass to the
add_tb_graph()
method.
- add_tb_hparams(*args, **kwargs)[source]
Adds hyper parameters to tb summary.
Wrapper that calls the
torch.utils.tensorboard.SummaryWriter.add_hparams
method while first making sure a SummaryWriter object exits. Theadd_hparams
method adds a set of hyperparameters to be compared in TensorBoard.- Parameters:
*args – All args to pass to the Summary/SummaryWriter object.
**kwargs – All kwargs to pass to the Summary/SummaryWriter object.
- Raises:
NotImplementedError – Raised if you try to call this method when using the TensorFlow backend.
- flush_tb_writer(*args, **kwargs)[source]
Flush tb event file to disk.
Wrapper that calls the
torch.utils.tensorboard.SummaryWriter.flush
ortf.summary.flush
(depending on the backend) method while first making sure a SummaryWriter object exits. These methods can be used to flush the event file to disk.- Parameters:
*args – All args to pass to the Summary/SummaryWriter object.
**kwargs – All kwargs to pass to the Summary/SummaryWriter object.
- class stable_learning_control.utils.log_utils.logx.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[source]
Dictionary used to store variables you want to log into the
EpochLogger
current state.- Type:
- store(tb_write=False, tb_aliases={}, 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.
Logger helper functions
- 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='train', wandb_project='stable-learning-control', wandb_group=None, wandb_run_name=None, quiet=False, verbose_fmt='line', 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: