训练卷积神经网络的一些合成图像的手写数字,然后在测试集上运行训练好的网络,计算其准确性。
[XTrain, YTrain] = digitTrain4DArrayData;%导入训练数据
layers = [ ...
imageInputLayer([28 28 1])
convolution2dLayer(5,20)
reluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];%定义CNN神经网络:输入为图像并给定图像大小;卷积核为5x5,20个;激励函数采用relu;采用2x2步长为2的最大池化
options = trainingOptions('sgdm', 'Plots', 'training-progress');%采用SGDM求解器并画出网络训练过程
net = trainNetwork(XTrain, YTrain, layers, options);
[XTest, YTest] = digitTest4DArrayData;%导入测试数据
YPred = classify(net, XTest);
accuracy = sum(YTest == YPred)/numel(YTest)
accuracy =
0.9770