Main Content

Specify Layers of Convolutional Neural Network

You can build and customize a deep learning model in various ways—for example, you can import and adapt a pretrained model, build a network from scratch, or define a deep learning model as a function.

In most cases, you can specify many types of deep learning models as a neural network of layers and then train it using the trainnet function. For a list of layers and how to create them, see List of Deep Learning Layers.

For simple neural networks with layers connected in series, you can specify the architecture as an array of layers. For example, to create a neural network that classifies 28-by-28 grayscale images into 10 classes, you can specify the layer array:

layers = [
    imageInputLayer([28 28 1])
    convolution2dLayer(5,20)
    batchNormalizationLayer
    reluLayer
    fullyConnectedLayer(10)
    softmaxLayer];

For neural networks with more complex structure, for example neural networks with branching, you can specify the neural network as a dlnetwork object. You can add and connect layers using the addLayers and connectLayers functions, respectively. For example, to create a multi-input network that classifies pairs of 224-by-224 RGB and 64-by-64 grayscale images into 10 classes, you can specify the neural network:

net = dlnetwork;
layers = [
    imageInputLayer([224 224 3])
    convolution2dLayer(5,128)
    batchNormalizationLayer
    reluLayer
    flattenLayer
    concatenationLayer(1,2,Name="cat")
    fullyConnectedLayer(10)
    softmaxLayer];

net = addLayers(net,layers);

layers = [
    imageInputLayer([64 64 1])
    convolution2dLayer(5,128)
    batchNormalizationLayer
    reluLayer
    flattenLayer(Name="flatten2")];

net = addLayers(net,layers);
net = connectLayers(net,"flatten2","cat/in2");
View the neural network in a plot.
figure
plot(net)

For models that cannot be specified as networks of layers, you can define the model as a function. For an example showing how to train a deep learning model defined as a function, see Train Network Using Model Function.

See Also

| |

Related Topics