手写体识别——MATLAB代码
MATLAB代码,里面包括代码运行的相关变量以及提供了下载数据的链接,可以通过代码直接下载数据,方便阅读。
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 505 2018-05-02 10:43 Demo1_MNISTletterW.png
文件 211842 2018-05-02 10:43 Demo1_MNISTMNISTModel.mat
文件 3727 2019-04-24 11:25 Demo1_MNISTMNIST_Classification_Demo.m
文件 828512 2018-05-02 10:43 Demo1_MNISTMNIST_Classification_Demo_Live.html
文件 503833 2018-05-02 10:43 Demo1_MNISTMNIST_Classification_Demo_Live.mlx
文件 3508 2019-04-24 10:20 Demo1_MNISTprepareData.m
目录 0 2019-04-25 11:14 Demo1_MNIST
----------- --------- ---------- ----- ----
1551927 7
%% Deep learning for classification on the MNIST dataset
% Copyright 2018 The MathWorks Inc.
%% Prepare the dataset
%准备数据集
% The MNIST dataset is a set of handwritten digits categorized 0-9 and is
% available at http://yann.lecun.com/exdb/mnist/.
%MNIST数据集是一组手写数字,分类为0-9,可以在http://yann.lecun.com/exdb/mnist/上找到。
% The following line will download (if necessary) and prepare the dataset
% to use in MATLAB.
%如有必要,下面的代码行将下载并准备好在MATLAB中使用的数据集。
[imgDataTrain labelsTrain imgDataTest labelsTest] = prepareData;
%% Let‘s look at a few of the images
%让我们看一些图片
% Randomize the images for display
%随机显示图像
warning off images:imshow:magnificationMustBeFitForDockedFigure
perm = randperm(numel(labelsTrain) 25);
subset = imgDataTrain(::1perm);
montage(subset)
%% How do we classify a digit?
%我们如何对数字进行分类?
% First we need a model - let‘s load one
load MNISTModel
% Predict the class of an image
%预测图像的类别
randIndx = randi(numel(labelsTest));
img = imgDataTest(::1randIndx);
actualLabel = labelsTest(randIndx);
predictedLabel = net.classify(img);
imshow(img);
title([‘Predicted: ‘ char(predictedLabel) ‘ Actual: ‘ char(actualLabel)])
%% Need a starting point? Check the documentation!
%需要一个起点?检查文档!
% search “deep learning“
%搜索“深度学习”
web(fullfile(docroot ‘nnet/deep-learning-training-from-scratch.html‘))
%% Prepare the CNN
% One of the simplest possible convnets it contains one convolutional
% layer one ReLU one pooling layer and one fully connected layer
%一个最简单的卷积网络,它包含一个卷积层,一个ReLU,一个池化层,和一个完全连接的层
layers = [ imageInputlayer([28 28 1])
convolution2dlayer(520)
relulayer
maxPooling2dlayer(2 ‘Stride‘ 2)
fullyConnectedlayer(10)
softmaxlayer
classificationlayer() ]
%% Attempt 1: Set training options and train the network
%%尝试1:设置训练选项并训练网络
miniBatchSize = 8192;
options = trainingOptions( ‘sgdm‘...
‘MiniBatchSize‘ miniBatchSize...
‘Plots‘ ‘training-progress‘);
net = trainNetwork(imgDataTrain labelsTrain layers options);
%% Attempt 2: Change the learning rate
%尝试2:改变学习速度
options = trainingOptions( ‘sgdm‘...
‘MiniBatchSize‘ miniBatchSize...
‘Plots‘ ‘training-progress‘...
‘InitialLearnRate‘ 0.0001);
net = trainNetwork(imgDataTrain labelsTrain layers options);
%% Attempt 3: Change the network architecture
%尝试3:更改网络架构
layers = [
imageInputlayer([28 28 1])
convolution2dlayer(316‘Padding‘1)
batchNormalizationlayer
relulayer
maxPooling2dlayer(2‘Stride‘2)
convolution2dlayer(332‘Padding‘1)
batchNormalizationlayer
relulayer
maxPooling2dlayer(2‘Stride‘2)
convolution2dlayer(364‘Padding‘1)
batchNormalizationlayer
relulayer
fullyConnectedlayer(10)
softmaxlayer
classificationlayer];
options = trainingOptions( ‘sgdm‘
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 505 2018-05-02 10:43 Demo1_MNISTletterW.png
文件 211842 2018-05-02 10:43 Demo1_MNISTMNISTModel.mat
文件 3727 2019-04-24 11:25 Demo1_MNISTMNIST_Classification_Demo.m
文件 828512 2018-05-02 10:43 Demo1_MNISTMNIST_Classification_Demo_Live.html
文件 503833 2018-05-02 10:43 Demo1_MNISTMNIST_Classification_Demo_Live.mlx
文件 3508 2019-04-24 10:20 Demo1_MNISTprepareData.m
目录 0 2019-04-25 11:14 Demo1_MNIST
----------- --------- ---------- ----- ----
1551927 7
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)