Main Content

Train Deep Learning Model inMATLAB

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, seeComputer Vision Using Deep Learning. For information on importing networks and network architectures from TensorFlow™-Keras, Caffe, and the ONNX™ (Open Neural Network Exchange) model format, seeDeep Learning Import and Export.

Training Methods

This table provides information about the different training methods.

Method More 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 by using theclassifyandpredictfunctions.

For an example, seeClassify Image Using GoogLeNet.

Train network usingtrainingOptionsandtrainNetwork

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

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

Train network usingdlnetworkobject and custom training loop

For most tasks, you can control the training algorithm details using thetrainingOptionsandtrainNetworkfunctions. If thetrainingOptionsfunction does not provide the options you need for your task (for example, a custom learning rate schedule), then you can define your own custom training loop using adlnetworkobject. Adlnetworkobject 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 learning rate schedule, seeTrain Network Using Custom Training Loop.

To learn, more seeDefine 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 a custom network as a function. For an example showing how to train a deep learning model defined as a function, seeTrain Network Using Model Function.

If you can create parts of the network 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.

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

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

For a list of pretrained deep learning networks in MATLAB®, seePretrained 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 a pretrained network already performs the task you need, then you can use the network directly without retraining. For example, you can use thegooglenetnetwork to classify images in 1000 classes. To make predictions with the network directly, use theclassifyandpredictfunctions. For an example, seeClassify Image Using GoogLeNet.

If you need to retrain the network—for example, 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?

您可以指定作为躺大多数深度学习模型er array or layer graph. In other words, you can define the model 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. For these networks, you must define the model as a function. For an example, seeTrain Network Using Model Function.

网络只有一个输出吗?

For networks with multiple outputs, you must train the network using a custom training loop. For an example, seeTrain 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, seeList of Deep Learning Layers.

If Deep Learning Toolbox provides the intermediate layers (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, seeDefine 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, seeDefine Custom Deep Learning Layers.

If you can define custom layers 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 the model using a custom training loop. For an example, seeTrain 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,classificationLayerandregressionLayer. For a list of output layers, see theOutput Layerssection in the pageList 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, seeDefine 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, seeDefine 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 adlnetworkobject and a custom training loop, and specify a custom loss function. For an example, seeTrain Network Using Custom Training Loop.

Does thetrainingOptionsfunction provide the options you need?

ThetrainingOptionsfunction provides many options for customizing the training process. If thetrainingOptionsfunction provides all the options you need for training, then you can train the deep learning network using thetrainNetworkfunction. For an example, seeCreate Simple Deep Learning Network for Classification.

If thetrainingOptionsfunction does not provide the training option you need, for example, a custom learning rate schedule, then you can define a custom training loop using adlnetworkobject. For an example, seeTrain Network Using Custom Training Loop.

See Also

|

Related Topics