创建一个简单的有向无环图(DAG)网络用于深度学习,训练网络对数字图像进行分类。
layers = [
imageInputLayer([28 28 1],'Name','input')
convolution2dLayer(5,16,'Padding','same','Name','conv_1')
batchNormalizationLayer('Name','BN_1')
reluLayer('Name','relu_1')
convolution2dLayer(3,32,'Padding','same','Stride',2,'Name','conv_2')
batchNormalizationLayer('Name','BN_2')
reluLayer('Name','relu_2')
convolution2dLayer(3,32,'Padding','same','Name','conv_3')
batchNormalizationLayer('Name','BN_3')
reluLayer('Name','relu_3')
additionLayer(2,'Name','add')
averagePooling2dLayer(2,'Stride',2,'Name','avpool')
fullyConnectedLayer(10,'Name','fc')
softmaxLayer('Name','softmax')
classificationLayer('Name','classOutput')];
lgraph = layerGraph(layers);
figure
plot(lgraph)
创建从'relu_1'到'add'层的快捷连接。因为您在创建层时指定了添加层的输入数量为两个,所以该层有两个输入名称为“in1”和“in2”。'relu_3'层已经连接到'in1'输入。将'relu_1'层连到'skipConv'层,将'skipConv'层连接到'add'层的'in2'输入。加法层现在对'relu_3'和'skipConv'层的输出求和。
skipConv = convolution2dLayer(1,32,'Stride',2,'Name','skipConv');%
lgraph = addLayers(lgraph,skipConv);
lgraph = connectLayers(lgraph,'relu_1','skipConv');
lgraph = connectLayers(lgraph,'skipConv','add/in2');
figure
plot(lgraph);
[XTrain,YTrain] = digitTrain4DArrayData;
[XValidation,YValidation] = digitTest4DArrayData;
options = trainingOptions('sgdm',...
'MaxEpochs',6,...
'Shuffle','every-epoch',...
'ValidationData',{XValidation,YValidation},...
'ValidationFrequency',20,...
'Verbose',false,...
'Plots','training-progress');
net = trainNetwork(XTrain,YTrain,lgraph,options);