Main Content

rlDataLogger

Creates either a file logger object or a monitor logger object to log training data

    Description

    example

    fileLgr = rlDataLogger() creates the FileLogger object fileLgr for logging training data to disk.

    monLgr = rlDataLogger(tpm) creates the MonitorLogger object monLgr for logging training data to the TrainingProgressMonitor object tpm, and its associated window.

    Examples

    collapse all

    This example shows how to log data to disk when using train.

    Create a FileLogger object using rlDataLogger.

    logger = rlDataLogger();

    Specify a directory to save logged data.

    logger.LoggingOptions.LoggingDirectory = "myDataLog";

    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. For a related example, see Log Training Data To Disk.

    logger.EpisodeFinishedFcn    = @myEpisodeFinishedFcn;
    logger.AgentStepFinishedFcn  = @myAgentStepFinishedFcn;
    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 specified directory, according to the rule specified in the FileNameRule property of logger.LoggingOptions.

    logger.LoggingOptions.FileNameRule
    ans = 
    "loggedData<id>"
    

    Example Logging Functions

    Episode completion logging function.

    function dataToLog = myEpisodeFinishedFcn(data)
        dataToLog.Experience = data.Experience;
    end

    Agent step completion logging function.

    function dataToLog = myAgentStepFinishedFcn(data)
        dataToLog.State = getState(getExplorationPolicy(data.Agent));
    end

    Learn subroutine completion logging function.

    function dataToLog = myAgentLearnFinishedFcn(data)
        dataToLog.ActorLoss  = data.ActorLoss;
        dataToLog.CriticLoss = data.CriticLoss;
    end

    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

    This example shows how to log data to disk when training an agent using a custom training loop.

    Create a FileLogger object using rlDataLogger.

    flgr = rlDataLogger();

    Set up the logger object. This operation initializes the object performing setup tasks such as, for example, creating the directory to save the data files.

    setup(flgr);

    Within a custom training loop, you can now store data to the logger object memory and write data to file.

    For this example, store random numbers to the file logger object, grouping them in the variables Context1 and Context2. When you issue a write command, a MAT file corresponding to an iteration and containing both variables is saved with the name specified in flgr.LoggingOptions.FileNameRule, in the folder specified by flgr.LoggingOptions.LoggingDirectory.

    for iter = 1:10
    
        % Store three random numbers in memory 
        % as elements of the variable "Context1"
        for ct = 1:3
            store(flgr, "Context1", rand, iter);
        end
    
        % Store a random number in memory 
        % as the variable "Context2"
        store(flgr, "Context2", rand, iter);
    
        % Write data to file every 4 iterations
        if mod(iter,4)==0
            write(flgr);
        end
    
    end

    Clean up the logger object. This operation performs clean up tasks like for example writing to file any data still in memory.

    cleanup(flgr);

    Input Arguments

    collapse all

    Training progress monitor object, specified as a trainingProgressMonitor object. This object is used to track the progress of training, update information fields, record metric values, and produce training plots for custom deep learning training loops. For more information, see Monitor Custom Training Loop Progress.

    Output Arguments

    collapse all

    File logger object, returned as a FileLogger object.

    Monitor logger object, returned as a MonitorLogger object.

    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.

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

    Version History

    Introduced in R2022b