深度学习的例子

Explore deep learning examples, and learn how you can get started in MATLAB.

从头开始训练模型

In this example, we want to train aconvolutional neural network(CNN)识别手写数字。我们将使用来自MNIST数据集,其中包含60000的图像handwritten numbers 0-9. Here is a random sample of 25 handwritten numbers in the MNIST dataset:

By using a simple dataset, we'll be able to cover all the key steps in the deep learning workflow without dealing with challenges such as processing power or datasets that are too large to fit into memory. You can apply the workflow described here to more complex deep learning problems and larger data sets.

If you are just getting started with applying deep learning, another advantage to using this data set is that you can train it without investing in an expensive GPU.

即使数据集很简单,具有正确的深度学习模型和培训选项,也可以实现超过99%的精度。那么,我们如何创建一个将我们到这一点的模型呢?

This will be an iterative process in which we build on previous training results to figure out how to approach the training problem. The steps are as follows:

1. Accessing the Data

We begin by downloading themnistimages into MATLAB. Datasets are stored in many different file types. This data is stored as binary files, which MATLAB can quickly use and reshape into images.
这些代码行将读取原始二进制文件,并创建所有训练图像的数组。

rawimgdatatrain = uint8(fread(fid,numimg * numrows * numcols,“uint8”);%将数据部分重塑为4D数组rawimgdatatrain = reshape(rawimgdatatrain,[numrows,numcols,numimgs]);imgdatatrain(:,:,:,1,ii)= uint8(rawimgdatatrain(::,::,ii));

我们可以通过在命令窗口中键入WHOS来检查数据的大小和类。

>>谁imgDataTrainName Size Bytes Class imgDataTrain 28x28x1x60000 47040000 uint8

这些图像很小 - 只有28 x 28像素 - 并且总培训图像有60000个。

The next task would be image labeling, but since the MNIST images come with labels, we can skip that tedious step and quickly move on to building our neural network.

2.创建和配置网络层

我们将首先建立CNN,这是最常见的深度学习网络。

About CNNS

CNN拍摄图像,将其通过网络层,然后输出最终类。该网络可以具有数十个或数百个层,每个层都学会检测图像的不同特征。将过滤器应用于不同分辨率的每个训练图像,每个卷积图像的输出被用作下一层的输入。万博 尤文图斯过滤器可以以非常简单的功能(例如亮度和边缘)启动,并增加复杂性,以随着图层的进展而独特地定义对象的功能。

To learn more about the structure of a CNN, watch:

由于我们正在从头开始训练CNN,因此我们必须首先指定其将包含哪些层以及以什么顺序。

layers = [imageInputlayer([28 28 1])卷积2Dlayer(3,16,'填充',1) batchNormalizationLayer reluLayer maxPooling2dLayer(2,'Stride',2) convolution2dLayer(3,32,'填充',1) batchNormalizationLayer reluLayer maxPooling2dLayer(2,'Stride',2) convolution2dLayer(3,64,'填充',1)batchnormalizationlayer relulayer plullconnectedlayer(10)softmaxlayer classificationlayer];

您可以在文档中了解有关所有这些层的更多信息。

3.培训网络

首先,我们选择培训选项。有很多选择。该表显示了最常用的选项。

Commonly Used Training Options

培训选项 定义 Hint
训练进度的情节

The plot shows the mini-batch loss and accuracy. It includes a stop button that lets you halt network training at any point.

采用(“绘图”,“训练过程”)绘制网络训练时的进度。
Max epochs

An epoch is the full pass of the training algorithm over the entire training set.

(‘MaxEpoch’,20)

指定的时期越多,网络训练的时间越长,但是每个时期的精度可能会提高。

迷你批量尺寸

迷你批次是同时处理的培训数据集的子集。

(‘MiniBatchSize’,64)

The larger the mini-batch, the faster the training, but the maximum size will be determined by the GPU memory. If you get a memory error when training, reduce the mini-batch size.

Learning rate 这是控制训练速度的主要参数。 较低的学习率可以带来更准确的结果,但是网络可能需要更长的时间才能训练。

我们首先指定两个选项:绘图进度和Minibatch大小。

miniBatchSize = 8192; options = trainingOptions('sgdm',...'MiniBatchSize', miniBatchSize,...“阴谋”,'training-progress'); net = trainNetwork(imgDataTrain, labelsTrain, layers, options);

然后,我们运行网络并监视其进度。

4. Checking Network Accuracy

Our goal is to have the accuracy of the model increase over time. As the network trains, the progress plot appears.

We'll try altering the training options and the network configuration.

Changing Training Options

First, we'll adjust the learning rate. We set the initial learning rate to be much lower than the default rate of 0.01.

'InitialLearnRate', 0.0001

As a result of changing that one parameter, we get a much better result—nearly 99%!

对于某些应用程序,此结果将令人满意,但您可能还记得我们的目标是99%。

Changing the Network Configuration

Getting to 99% from 90% requires a deeper network and many rounds of trial and error. We add more layers, including batch normalizationlayers, which will help speed up the network收敛(它正确响应新输入的点)。

The network is now “deeper”. This time, we'll change the network but leave the training options the same as they were before.

After the network has trained, we test it on 10,000 images.

predlabelstest = net.classify(imgdatatest);精度= sum(predlabelstest == labelstest) / numel(labelstest)testaccuracy = 0.9913

现在,我们可以使用它在线图像中甚至在实时视频流中识别手写字母。

When creating a network from scratch, you are responsible for determining the network configuration. This approach gives you the most control over the network, and can produce impressive results, but it requires an understanding of the structure of a neural network and the many options for layer types and configuration.


Learn More

跟随一个例子

了解如何培训卷积神经网络(CNN)以识别手写数字。