Main Content

Training Deep Learning Models in MATLAB

You can train and customize a deep learning model in various ways—for example, you can retrain a pretrained model with new data (transfer learning), train a network from scratch, or define a deep learning model as a function and use a custom training loop. Use this flow chart to choose the training method that is best suited for your task.

Tip

For information on computer vision workflows including for object detection, see Computer Vision Using Deep Learning. To learn about importing networks and network architectures from TensorFlow™-Keras, Caffe, and the ONNX™ (Open Neural Network Exchange) model format, see Deep Learning Import and Export.

Training Methods

This table provides more information about the different training methods.

MethodMore Information
Use network directly

If a pretrained network already performs the task you require, then you do not need to retrain the network. Instead, you can make predictions with the network directly using the classify and predict functions.

For an example, see Classify Image Using GoogLeNet.

Train network using trainingOptions and trainNetwork

If you have a network specified as a layer array or layer graph, and the trainingOptions function provides all the options you need, then you can train the network using the trainNetwork function.

For an example showing how to retrain a network (transfer learning), see Train Deep Learning Network to Classify New Images. For an example showing how to train a network from scratch, see Create Simple Deep Learning Network for Classification.

Train network using dlnetwork object and custom training loop

For most tasks, you can control the training algorithm details using the trainingOptions and trainNetwork functions. If the trainingOptions function does not provide the options you need for your task (for example, a custom learn rate schedule), then you can define your own custom training loop using a dlnetwork object. A dlnetwork object allows you to train a network specified as a layer graph using automatic differentiation.

For loss functions that cannot be specified using an output layer, you can specify the loss in a custom training loop. For an example showing how to train a network with a custom learn rate schedule, see Train Network Using Custom Training Loop.

To learn, more see Define Custom Training Loops, Loss Functions, and Networks.

Train network using model function and custom training loop

For networks that cannot be created using layer graphs, you can define custom networks as a function. For an example showing how to train a deep learning model defined as a function, see Train Network Using Model Function.

If parts of the network can be created using a layer graph, then you can define those parts as layer graphs and the unsupported parts using model functions.

Decisions

This table provides more information on each decision in the flow chart.

DecisionMore Information
Does Deep Learning Toolbox™ provide a suitable pretrained network?

For most tasks, you can use or retrain a pretrained network such as googlenet.

For a list of pretrained deep learning networks in MATLAB®, see Pretrained Deep Neural Networks. You can use pretrained networks directly with new data, or you can retrain them with new data for different tasks using transfer learning.

Can you use the network without retraining?

If there is a pretrained network that already performs the task you need, then you can use the network directly without retraining. For example, you can use the googlenet network to classify images over 1000 classes. To make predictions with the network directly, use the classify and predict functions. For an example, see Classify Image Using GoogLeNet.

If you need to retrain the network, for example to retrain the model to classify a different set of classes, then you can retrain the network using transfer learning.

Can you define the model as a layer array or graph?

Most deep learning models can be specified as a layer array or layer graph. In other words, the model can be defined as a collection of layers with layer outputs connected to other layer inputs.

Some network architectures cannot be defined as a layer graph. For example, Siamese networks require weight sharing and cannot be defined as a layer graph and you must define the model as a function. For an example, see Train Network Using Model Function.

Does the network have a single output only?

For networks with multiple outputs, you must train the network using a custom training loop. For an example, see Train Network with Multiple Outputs.

Does Deep Learning Toolbox provide the intermediate layers you need?

Deep Learning Toolbox provides many different layers for deep learning tasks. For a list of layers, see List of Deep Learning Layers.

If Deep Learning Toolbox provides the intermediate layer (layers in the middle of the network) that you need, then you can define the network as a layer array or layer graph using these layers. Otherwise, try defining any unsupported layers as custom layers. For more information, see Define Custom Deep Learning Layers.

Can you define the unsupported intermediate layers as custom layers?

If Deep Learning Toolbox does not provide the layer you need, then you can try defining a custom deep learning layer. For more information, see Define Custom Deep Learning Layers.

If you can define a custom layer for any unsupported layers, then you can include these custom layers in a layer array or layer graph. Otherwise, specify the deep learning model using a function and train using a custom training loop. For an example, see Train Network Using Model Function.

Does Deep Learning Toolbox provide the output layers you need?

Output layers specify the loss function used for training. Deep Learning Toolbox provides different output layers for deep learning tasks. For example, classificationLayer and regressionLayer. For a list of output layers, see the Output Layers section in the page List of Deep Learning Layers.

If Deep Learning Toolbox provides the output layers that you need, then you can define a layer graph using these layers. Otherwise, try defining any unsupported output layers as a custom layer. For more information, see Define Custom Deep Learning Layers.

Can you define the unsupported output layers as custom layers?

If Deep Learning Toolbox does not provide the output layer you need, then you can try defining a custom output layer. For more information, see Define Custom Deep Learning Layers.

If you can define a custom output layer for any unsupported output layers, then you can include these custom layers in a layer array or layer graph. Otherwise, train the model using a dlnetwork object and a custom training loop, and specify a custom loss function. For an example, see Train Network Using Custom Training Loop.

Does the trainingOptions function provide the options you need?

The trainingOptions function provides many options for customizing a the training process. If the trainingOptions function provides all the options you need for training, then you can train the deep learning network using the trainNetwork function. For an example, see Create Simple Deep Learning Network for Classification.

If the trainingOptions function does not provide the training option you need, for example, a custom learning rate schedule, then you can define a custom training loop using a dlnetwork object. For an example, see Train Network Using Custom Training Loop.

See Also

|

Related Topics