Main Content

MonitorLogger

Log reinforcement learning training data to monitor window

    Description

    Use a MonitorLogger object to log data to a monitor window, within the train function or inside a custom training loop. To log data when using the train function, specify appropriate callback functions in MonitorLogger, as shown in the examples. These callbacks are executed at different stages of training, for example, EpisodeFinishedFcn is executed after the completion of an episode. The output of a callback function is a structure containing the data to log at that stage of training.

    Note

    MonitorLogger is an handle object. If you assign an existing MonitorLogger object to a new MonitorLogger object, both the new object and the original one refer to the same underlying object in memory. To preserve the original object parameters for later use, save the object to a MAT-file. For more information about handle objects, see Handle Object Behavior.

    Creation

    Create a MonitorLogger object using rlDataLogger specifying a trainingProgressMonitor object as input argument.

    Properties

    expand all

    Object containing a set of logging options, returned as a MonitorLoggingOptions object. A MonitorLoggingOptions object has the following properties, which you can access using dot notation after creating the MonitorLogger object.

    Frequency for writing data to the monitor window, specified as a positive integer. It is the number of episodes after which data is transmitted to the trainingProgressMonitor object. The default is 1.

    Maximum number of episodes, specified as a positive integer. When using train, the value is automatically initialized. Set this value when using the logger object in a custom training loop. The default is 500.

    Callback to log data after episode completion, specified as a function handle object. The specified function must return a structure containing the data to log, such as experiences, simulation information, or initial observations.

    Example: @myEpisodeLoggingFcn

    Callback to log data after training step completion within an episode, specified as a function handle object. The specified function must return a structure containing the data to log, such as for example the state of the agent's exploration policy.

    For multi agent training, AgentStepFinishedFcn can be a cell array of function handles with as many elements as number of agent groups.

    Note

    Logging data using the AgentStepFinishedFcn callback is not supported when training agents in parallel with the train function.

    Example: @myAgentStepLoggingFcn

    Callback to log data after completion of the learn subroutine, specified as a function handle object. The specified function must return a structure containing the data to log, such as the training losses of the actor and critic networks, or, for a model-based agent, the environment model training losses.

    For multi agent training, AgentLearnFinishedFcn can be a cell array of function handles with as many elements as number of agent groups.

    Example: @myLearnLoggingFcn

    Object Functions

    setupSet up reinforcement learning environment or initialize data logger object
    cleanupClean up reinforcement learning environment or data logger object

    Examples

    collapse all

    This example shows how to log and visualize data to the window of a trainingProgressMonitor object when using train.

    Create a trainingProgressMonitor object. Creating the object also opens a window associated with the object.

    monitor = trainingProgressMonitor();

    Create a MonitorLogger object using rlDataLogger.

    logger = rlDataLogger(monitor);

    Create callback functions to log the data (for this example, see the helper function section), and specify the appropriate callback functions in the logger object.

    logger.AgentLearnFinishedFcn = @myAgentLearnFinishedFcn;

    To train the agent, you can now call train, passing logger as an argument such as in the following command.

    trainResult = train(agent, env, trainOpts, Logger=logger);
    

    While the training progresses, data will be logged to the training monitor object, and visualized in the associated window.

    Note that only scalar data can be logged with a monitor logger object.

    Example Logging Functions

    Define a logging function that logs data periodically at the completion of the learning subroutine.

    function dataToLog = myAgentLearnFinishedFcn(data)
    
        if mod(data.AgentLearnCount, 2) == 0
            dataToLog.ActorLoss  = data.ActorLoss;
            dataToLog.CriticLoss = data.CriticLoss;
        else
            dataToLog = [];
        end
        
    end

    Limitations

    • Only scalar data is supported when logging data with a MonitorLogger object. The structure returned by the callback functions must contain fields with scalar data.

    • Resuming of training from a previous training result is not supported when logging data with a MonitorLogger object.

    Version History

    Introduced in R2022b