Generate MATLAB Code from Deep Network Designer
The Deep Network Designer app enables you to generate MATLAB® code that recreates the building, editing, and training of a network in the app.
In the Designer tab, you can generate a live script to:
Recreate the layers in your network. Select Export > Generate Code.
Recreate the layers in your network, including any initial parameters. Select Export > Generate Code with Initial Parameters.
In the Training tab, you can generate a live script to:
Recreate the building and training of a network you construct in Deep Network Designer. Select Export > Generate Code for Training.
Generate MATLAB Code to Recreate Network Layers
Generate MATLAB code for recreating the network constructed in Deep Network Designer. In the Designer tab, choose one of these options:
To recreate the layers in your network, select Export > Generate Code. This network does not contain initial parameters, such as pretrained weights.
To recreate the layers in your network, including any initial parameters, select Export > Generate Code with Initial Parameters. The app creates a live script and a MAT-file containing the initial parameters (weights and biases) from your network. Run the script to recreate the network layers, including the learnable parameters from the MAT-file. Use this option to preserve the weights if you want to perform transfer learning.
Running the generated script returns the network architecture as a variable in the
workspace. Depending on the network architecture, the variable is a layer graph named
lgraph or a layer array named
layers. For an example
of training a network exported from Deep Network Designer, see Create Simple Sequence Classification Network Using Deep Network Designer.
Generate MATLAB Code to Train Network
To recreate the construction and training of a network in Deep Network Designer, generate MATLAB code after training. For an example of using Deep Network Designer to train an image classification network, see Transfer Learning with Deep Network Designer.
Once training is complete, on the Training tab, select Export > Generate Code for Training. The app creates a live script and a MAT-file containing the initial parameters (weights and biases) from your network. If you import data from the workspace into Deep Network Designer then this is also contained in the generated MAT-file.
Running the generated script builds the network (including the learnable parameters from the MAT-file), imports the data, sets the training options, and trains the network. Examine the generated script to learn how to construct and train a network at the command line.
If you change the network, training and validation data, or training options, click Train before generating the live script.
You can also use the generated script as a starting point to create deep learning experiments which sweep through a range of hyperparameter values or use Bayesian optimization to find optimal training options. For an example showing how to use Experiment Manager to tune the hyperparameters of a network trained in Deep Network Designer, see Adapt Code Generated in Deep Network Designer for Use in Experiment Manager.
Use Network for Prediction
Suppose that the trained network is contained in the variable
To use the trained network for prediction, use the
For example, suppose you have a trained image classification network. Use the exported
network to predict the class of
img = imread("peppers.png"); img = imresize(img, net.Layers(1).InputSize(1:2)); label = predict(net, img); imshow(img); title(label);
 Kudo, Mineichi, Jun Toyama, and Masaru Shimbo. “Multidimensional Curve Classification Using Passing-through Regions.” Pattern Recognition Letters 20, no. 11–13 (November 1999): 1103–11. https://doi.org/10.1016/S0167-8655(99)00077-X.
 Kudo, Mineichi, Jun Toyama, and Masaru Shimbo. Japanese Vowels Data Set. Distributed by UCI Machine Learning Repository. https://archive.ics.uci.edu/ml/datasets/Japanese+Vowels.