composer.loggers.in_memory_logger#

Logs metrics to dictionary objects that persist in memory throughout training.

Useful for collecting and plotting data inside notebooks.

Classes

InMemoryLogger

Logs metrics to dictionary objects that persist in memory throughout training.

class composer.loggers.in_memory_logger.InMemoryLogger(log_level=LogLevel.BATCH)[source]#

Bases: composer.core.logging.base_backend.LoggerCallback

Logs metrics to dictionary objects that persist in memory throughout training. Useful for collecting and plotting data inside notebooks.

Example usage:
from composer.loggers import InMemoryLogger
from composer.trainer import Trainer
from composer.core.logging import LogLevel
logger = InMemoryLogger(
    log_level=LogLevel.BATCH
)
trainer = Trainer(
    model=model,
    train_dataloader=train_dataloader,
    eval_dataloader=eval_dataloader,
    max_duration="1ep",
    optimizers=[optimizer],
    loggers=[logger]
)
# Get data from logger. If you are using multiple loggers, be sure to confirm
# which index in trainer.logger.backends contains your desired logger.
logged_data = trainer.logger.backends[0].data
Parameters

log_level (str or LogLevel, optional) โ€“ LogLevel (i.e. unit of resolution) at which to record. Defaults to BATCH, which records everything.

data#

Mapping of a logged key to a (Timestamp, LogLevel, TLogDataValue) tuple. This dictionary contains all logged data.

Type

dict

most_recent_values#

Mapping of a key to the most recent value for that key.

Type

Dict[str, TLogData]

most_recent_timestamps#

Mapping of a key to the Timestamp of the last logging call for that key.

Type

Dict[str, Timestamp]

get_timeseries(metric)[source]#

Returns logged data as dict containing values of a desired metric over time.

Parameters

metric (str) โ€“ Metric of interest. Must be present in self.data.keys().

Returns

timeseries (Dict[str, TLogData]) โ€“ Dictionary in which one key is metric, and the associated value is a list of values of that metric. The remaining keys are each a unit of time, and the associated values are each a list of values of that time unit for the corresponding index of the metric. For example: >>> InMemoryLogger.get_timeseries(metric=โ€accuracy/valโ€) {โ€œaccuracy/valโ€: [31.2, 45.6, 59.3, 64.7, โ€œepochโ€: [1, 2, 3, 4, โ€ฆ], โ€ฆ], โ€œbatchโ€: [49, 98, 147, 196, โ€ฆ], โ€ฆ}

Example

import matplotlib.pyplot as plt

from composer.core.logging import LogLevel
from composer.core.time import Time, Timestamp
from composer.loggers import InMemoryLogger

in_mem_logger = InMemoryLogger(LogLevel.BATCH)

# Populate the logger with data
for b in range(0,3):
    datapoint = b * 3
    timestamp = Timestamp(epoch=Time(0, "ep"),
                        batch=Time(b, "ba"),
                        batch_in_epoch=Time(0, "ba"),
                        sample=Time(0, "sp"),
                        sample_in_epoch=Time(0, "sp"),
                        token=Time(0, "tok"),
                        token_in_epoch=Time(0, "tok"))
    in_mem_logger.log_metric(timestamp=timestamp,
        log_level=LogLevel.BATCH, data={"accuracy/val": datapoint})
timeseries = in_mem_logger.get_timeseries("accuracy/val")
plt.plot(timeseries["batch"], timeseries["accuracy/val"])
plt.xlabel("Batch")
plt.ylabel("Validation Accuracy")