Debug Code Before and After Running Experiments
In Experiment Manager, you
use functions to configure the training data, network architecture, and training options for
the experiment, specify the training procedure used by the experiment, and evaluate the
results of the experiment. You can diagnose problems in your code before or after you run the
experiment. After you debug the setup function for a built-in training experiment, you can
also call the built-in training function trainNetwork
and debug your metric functions.
Debug Setup and Training Functions
To debug your code before you run the experiment:
Open the experiment.
In the Experiment Manager toolstrip, select Run > Debug
.
In dialog box, specify the hyperparameter values for your experiment.
Click Start.
To debug your code after you run the experiment:
Open the results for the experiment.
In the results table, select a trial to debug. To ensure reproducibility, Experiment Manager reuses the hyperparameter values and the random seed saved for this trial.
Right-click the trial and select Debug
.
Experiment Manager opens the setup or training function in MATLAB® Editor, places a breakpoint in first line of code, and runs the function.
MATLAB pauses at each line of code indicated by a breakpoint. When the function execution stops at a breakpoint, you can add other breakpoints to your function, view the values of your variables, step through the code line by line, or continue to the next breakpoint. For more information, see Debug MATLAB Code Files.
For custom training experiments, a Training Progress window plots the metric values for your experiment, as described in Monitor Custom Training Loop Progress.
Tip
To examine the values stored in the MetricData
and
InfoData
properties of the trainingProgressMonitor
object associated with the Training Progress window,
pause the execution before you reach the end of your training function. When the function
runs to completion, Experiment Manager closes the Training Progress window and deletes the
trainingProgressMonitor
object.
After the function runs to completion, you can verify your results by examining the hyperparameters and output values stored in these workspace variables:
— A structure with fields from the Experiment Manager hyperparameter tablefunctionName
_params
— A cell array that contains the output values returned by the setup or training functionfunctionName
_output
Debug Metric Functions
After you debug the setup function for a built-in training experiment, you can inspect
the training data and training options in the MATLAB Workspace browser or visualize the network layers in the Deep Network Designer
app. You can also call the built-in training function trainNetwork
and step through your metric functions:
In the MATLAB Command Window, call
trainNetwork
using the output of your setup function. For example, if your setup function is calledRegressionExperiment_setup
, enter:[tNet,tInfo] = trainNetwork(RegressionExperiment_setup_output{:});
Create a structure called
trialInfo
that contains the fieldstrainedNetwork
,trainingInfo
, andparameters
. For values, use the outputs of thetrainNetwork
function and the hyperparameters used for training. For example, if your setup function is calledRegressionExperiment_setup
, enter:trialInfo = struct(trainedNetwork=tNet, ... trainingInfo=tInfo, ... parameters=RegressionExperiment_setup_params);
In Experiment Manager, in the experiment pane, under Metrics, select the name of a metric function and click Edit. The metric function opens in MATLAB Editor.
In the metric function, set breakpoints as described in Set Breakpoints.
In the MATLAB Command Window, call the metric function using the
trialInfo
structure as the input to the function. For example, if your metric function is calledAccuracy
, enter:metricOutput = Accuracy(trialInfo)
MATLAB pauses at each line of code indicated by a breakpoint. When the function execution stops at the breakpoint, you can view the values of your variables, step through the code line by line, or continue to the next breakpoint. After the function runs to completion, examine the output value. The output must be a scalar number, a logical value, or a string.