Main Content

FileLogger

Log reinforcement learning training data to MAT files

    Description

    Use a FileLogger object to log data to MAT files, within the train function or inside a custom training loop. To log data when using the train function, specify appropriate callback functions in FileLogger, 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

    FileLogger is a handle object. If you assign an existing FileLogger object to a new FileLogger 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 FileLogger object using rlDataLogger without any input arguments.

    Properties

    expand all

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

    Name or fully qualified path of the logging directory, specified as a string or a character array. This is the name of the directory where the MAT files containing the logged data are saved. As a default, a subdirectory called logs is created in the current folder during setup and files are saved there during training.

    Rule to name the MAT files, specified as a string or a character array. For example, the naming rule "episode<id>" results in the file names episode001.mat, episode002.mat and so on.

    MAT file versions, specified as a string or character array. The default is "-v7". For more information, see MAT-File Versions.

    Option to use compression when saving data to a MAT file, specified as a logical variable. The default is true. For more information, see MAT-File Versions.

    Frequency for writing data to disk, specified as a positive integer. It is the number of episodes after which data is saved to disk. 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 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 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);

    Limitations

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

    Version History

    Introduced in R2022b