Main Content

Debug Code Before and After Running Experiments

Since R2023a

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:

  1. Open the experiment.

  2. In the Experiment Manager toolstrip, select Run > Debug .

  3. In dialog box, specify the hyperparameter values for your experiment.

  4. Click Start.

To debug your code after you run the experiment:

  1. Open the results for the experiment.

  2. 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.

  3. 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.

Experiment Manager sets a breakpoint on the first line of code in your setup or training 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:

  • functionName_params — A structure with fields from the Experiment Manager hyperparameter table

  • functionName_output — A cell array that contains the output values returned by the setup or training function

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:

  1. In the MATLAB Command Window, call trainNetwork using the output of your setup function. For example, if your setup function is called RegressionExperiment_setup, enter:

    [tNet,tInfo] = trainNetwork(RegressionExperiment_setup_output{:});
  2. Create a structure called trialInfo that contains the fields trainedNetwork, trainingInfo, and parameters. For values, use the outputs of the trainNetwork function and the hyperparameters used for training. For example, if your setup function is called RegressionExperiment_setup, enter:

    trialInfo = struct(trainedNetwork=tNet, ...
        trainingInfo=tInfo, ...
        parameters=RegressionExperiment_setup_params);
  3. 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.

  4. In the metric function, set breakpoints as described in Set Breakpoints.

    Set a breakpoint on a line of code in the metric function.

  5. 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 called Accuracy, 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.

See Also

Apps

Functions

Objects

Related Topics