Store data in the internal memory of a (file or monitor) logger object
Log Data to Disk in a Custom Training Loop
This example shows how to log data to disk when training an agent using a custom training loop.
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.
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
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
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.
context — Name of the saved variable
char vector | ...
Name of the saved variable, specified as either a string or character vector. All
data associated with iteration
iter and the name
context is vertically concatenated in a single MATLAB® variable named
context. This variable is then written
to the logger target (either a MAT-file or a
trainingProgressMonitor object) when
write is invoked
data — Data to be saved
any fundamental MATLAB datatype | ...
Data to be saved, specified as any fundamental MATLAB datatype. Data associated with the same iteration and the same context name is grouped in a single variable.
iter — Iteration number
positive integer | ...
Iteration number, specified as a positive integer. When
is executed multiple times with the same iteration number, data is appended to the
memory entry for that iteration. This memory entry, which can contain several context
variables, is then written to the logger target as a single unit (for example as a
single MAT-file) when
write is invoked for
Introduced in R2022b