Create a simple directed acyclic graph (DAG) network for deep learning. Train the network to classify images of digits. The simple network in this example consists of:
A main branch with layers connected sequentially.
Ashortcut connectioncontaining a single 1-by-1 convolutional layer. Shortcut connections enable the parameter gradients to flow more easily from the output layer to the earlier layers of the network.
Create the main branch of the network as a layer array. The addition layer sums multiple inputs element-wise. Specify the number of inputs for the addition layer to sum. To easily add connections later, specify names for the first ReLU layer and the addition layer.
Create a layer graph from the layer array.layerGraph
connects all the layers inlayers
sequentially. Plot the layer graph.
Create the 1-by-1 convolutional layer and add it to the layer graph. Specify the number of convolutional filters and the stride so that the activation size matches the activation size of the third ReLU layer. This arrangement enables the addition layer to add the outputs of the third ReLU layer and the 1-by-1 convolutional layer. To check that the layer is in the graph, plot the layer graph.
Create the shortcut connection from the'relu_1'
layer to the'add'
layer. Because you specified two as the number of inputs to the addition layer when you created it, the layer has two inputs named'in1'
and'in2'
. The third ReLU layer is already connected to the'in1'
input. Connect the'relu_1'
layer to the'skipConv'
layer and the'skipConv'
layer to the'in2'
input of the'add'
layer. The addition layer now sums the outputs of the third ReLU layer and the'skipConv'
layer. To check that the layers are connected correctly, plot the layer graph.
Load the training and validation data, which consists of 28-by-28 grayscale images of digits.
Specify training options and train the network.trainNetwork
validates the network using the validation data everyValidationFrequency
iterations.
Display the properties of the trained network. The network is aDAGNetwork
object.
net = DAGNetwork with properties: Layers: [16x1 nnet.cnn.layer.Layer] Connections: [16x2 table] InputNames: {'imageinput'} OutputNames: {'classoutput'}
Classify the validation images and calculate the accuracy. The network is very accurate.