Callbacks for logging data

Log

The Log Callback serves the purpose of logging data generated during a training run. The log holds the following objects of interest:

  • batch_log - a dictionary of batch-wise logged data. Each key is a string denoting the name of a logged attribute. The values are lists of lists, where each sub-list is the value of the given attribute for each item in a batch. For example log.batch_log['rewards'][5] would yield an array of rewards for each item in batch 5

  • timelog - a dictionary of lists. The keys denote different training steps (build_buffer, sample_batch, etc) with how long the step took for each batch. To view the times for all training stages, use the Log.plot_timelog function.

  • metrics - a dictionary of lists. Each key is the name of a tracked metric. Each value is a list containing the value of that metric for each batch. Metrics are single scalar values, one value per batch. Metrics can be plotted with the Log.plot_metrics function

  • unique_samples - a set containing every unique sample seen during training. This can be used to quickly reference if a sample has been seen before

  • df - a dataframe containing every unique sample and every batch_log value associated with that sample

class Log[source]

Log() :: Callback

log_to_df[source]

log_to_df(log, keys=None)

Custom Metrics Logging

Adding new items to metric tracking and batch logging is easy.

Use the add_log to add a new term to the batch log. At some point during the batch, add the values to be logged to the current BatchState with an attribute name that matches the name added to the log. The batch log will automatically add the values to the batch log.

Use add_metric to add a new term to the metric log. At some point during the batch, compute the metric you wish to log. Then use Log.update_metric to add the value to the metric log.

Here's an outline implementation:

class MyCallback(Callback):
    def __init__(self):
        super().__init__(name='my_callback')

    def setup(self):
        log = self.environment.log
        log.add_log(self.name) # adding new term to batch log
        log.add_metric(self.name) # adding new term to metrics

    def compute_reward(self):
        # make tensor of dummy rewards
        batch_state = self.environment.batch_state
        bs = len(batch_state.samples)
        rewards = to_device(torch.ones(bs).float())

        batch_state.rewards += rewards
        batch_state[self.name] = rewards # this is the value the batch log will pick up

    def after_compute_reward(self):
        log = self.environment.log
        batch_state = self.environment.batch_state

        my_callback_rewards = batch_state[self.name]
        my_metric = my_callback_rewards.mean()

        log.update_metric(self.name, my_metric.detach().cpu().numpy()) # update metric value

Log Callbacks

Several logging related callbacks

class StatsCallback[source]

StatsCallback(batch_attribute, grabname=None, include_buffer=True, name='stats', order=20) :: Callback

StatsCallback - base class for callbacks related to calculating stats from batches

Inputs:

  • batch_attribute str: attribute to grab from the log

  • grabname Optional[str]: if passed, the batch_attribute values will be subset for those where source==grabname

  • include_buffer bool: if True, values sourced from the buffer that match grabname will be included

  • name str: callback name

  • order int: callback order

class MaxCallback[source]

MaxCallback(batch_attribute, grabname, include_buffer=True) :: StatsCallback

MaxCallback - adds a metric tracking the maximum of batch_attribute, subset by grabname, printed every batch report

Inputs:

  • batch_attribute str: attribute to grab from the log

  • grabname Optional[str]: if passed, the batch_attribute values will be subset for those where source==grabname

  • include_buffer bool: if True, values sourced from the buffer that match grabname will be included

class MinCallback[source]

MinCallback(batch_attribute, grabname, include_buffer=True) :: StatsCallback

MinCallback - adds a metric tracking the minimum of batch_attribute, subset by grabname, printed every batch report

Inputs:

  • batch_attribute str: attribute to grab from the log

  • grabname Optional[str]: if passed, the batch_attribute values will be subset for those where source==grabname

  • include_buffer bool: if True, values sourced from the buffer that match grabname will be included

class MeanCallback[source]

MeanCallback(batch_attribute, grabname, include_buffer=True) :: StatsCallback

MeanCallback - adds a metric tracking the mean of batch_attribute, subset by grabname, printed every batch report

Inputs:

  • batch_attribute str: attribute to grab from the log

  • grabname Optional[str]: if passed, the batch_attribute values will be subset for those where source==grabname

  • include_buffer bool: if True, values sourced from the buffer that match grabname will be included

class PercentileCallback[source]

PercentileCallback(batch_attribute, grabname, percentile, include_buffer=True) :: StatsCallback

PercentileCallback - adds a metric tracking the percentile percentile value of batch_attribute, subset by grabname, printed every batch report

Inputs:

  • batch_attribute str: attribute to grab from the log

  • grabname Optional[str]: if passed, the batch_attribute values will be subset for those where source==grabname

  • percentile str: what percentile value to use

  • include_buffer bool: if True, values sourced from the buffer that match grabname will be included

class SaveLogDF[source]

SaveLogDF(frequency, save_path) :: Callback

SaveLogDF - periodically saves the Log dataframe during training

Inputs:

  • frequency int: how often to save

  • save_path str: directory to save files to