main content

create either a file logger object or a monitor logger object to log training data -凯发k8网页登录

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

since r2022b

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

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.

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"

example logging functions

episode completion logging function. this function is automatically called by the training loop at the end of each episode, and must return a structure containing the episode-related data to log, such as experiences, simulation information, or initial observations.

function datatolog = myepisodefinishedfcn(data)
    datatolog.experience = data.experience;
end

agent step completion logging function. this function is automatically called by the training loop at the end of each training step, and must return a structure containing the step-related data to log, such as for example the state of the agent exploration policy.

function datatolog = myagentstepfinishedfcn(data)
    datatolog.state = getstate(getexplorationpolicy(data.agent));
end

learn subroutine completion logging function. this function is automatically called by the training loop at the end of each learning subroutine, and must return a structure containing the learning-related 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.

function datatolog = myagentlearnfinishedfcn(data)
    datatolog.actorloss  = data.actorloss;
    datatolog.criticloss = data.criticloss;
end

for the specific function signatures and more information on the function input structure, see the corresponding properties of filelogger. for a related example, see log training data to disk.

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. for the specific function signatures and more information on the function input structure, see the corresponding properties of monitorlogger.

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. this function is automatically called by the training loop at the end of each learning subroutine, and must return a structure containing the learning-related 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.

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

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 .

output arguments

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

网站地图