SeriesNetwork
用于深度学习的串行网络
说明
串行网络是一种用于深度学习的神经网络,具有依次排列的各个层。它有一个输入层和一个输出层。
创建对象
创建 SeriesNetwork
对象有多种方法:
使用
alexnet
、darknet19
、vgg16
或vgg19
加载一个预训练网络。有关示例,请参阅加载预训练的 AlexNet 卷积神经网络。使用
trainNetwork
训练或微调一个网络。有关示例,请参阅训练用于图像分类的网络。使用
assembleNetwork
函数从预训练的层组合为一个深度学习网络。
注意
要了解其他预训练网络,如 googlenet
和 resnet50
,请参阅预训练的深度神经网络。
属性
Layers
— 网络层
Layer
数组
此 属性 为只读。
网络层,指定为 Layer
数组。
InputNames
— 输入层的名称
字符向量元胞数组
此 属性 为只读。
输入层的名称,指定为字符向量元胞数组。
数据类型: cell
OutputNames
— 输出层的名称
元胞数组
此 属性 为只读。
输出层的名称,指定为字符向量元胞数组。
数据类型: cell
对象函数
activations | 计算深度学习网络层激活 |
classify | (Not recommended) Classify data using trained deep learning neural network |
predict | (Not recommended) Predict responses using trained deep learning neural network |
predictAndUpdateState | (Not recommended) Predict responses using a trained recurrent neural network and update the network state |
classifyAndUpdateState | (Not recommended) Classify data using a trained recurrent neural network and update the network state |
resetState | Reset state parameters of neural network |
plot | 绘制神经网络架构 |
示例
加载预训练的 AlexNet 卷积神经网络
加载预训练的 AlexNet 卷积神经网络,并检查层和类。
使用 alexnet
加载预训练的 AlexNet 网络。输出 net
是一个 SeriesNetwork
对象。
net = alexnet
net = SeriesNetwork with properties: Layers: [25×1 nnet.cnn.layer.Layer]
使用 Layers
属性查看网络架构。该网络由 25 个层组成。8 个层具有可学习权重:5 个卷积层和 3 个全连接层。
net.Layers
ans = 25x1 Layer array with layers: 1 'data' Image Input 227x227x3 images with 'zerocenter' normalization 2 'conv1' Convolution 96 11x11x3 convolutions with stride [4 4] and padding [0 0 0 0] 3 'relu1' ReLU ReLU 4 'norm1' Cross Channel Normalization cross channel normalization with 5 channels per element 5 'pool1' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0] 6 'conv2' Grouped Convolution 2 groups of 128 5x5x48 convolutions with stride [1 1] and padding [2 2 2 2] 7 'relu2' ReLU ReLU 8 'norm2' Cross Channel Normalization cross channel normalization with 5 channels per element 9 'pool2' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0] 10 'conv3' Convolution 384 3x3x256 convolutions with stride [1 1] and padding [1 1 1 1] 11 'relu3' ReLU ReLU 12 'conv4' Grouped Convolution 2 groups of 192 3x3x192 convolutions with stride [1 1] and padding [1 1 1 1] 13 'relu4' ReLU ReLU 14 'conv5' Grouped Convolution 2 groups of 128 3x3x192 convolutions with stride [1 1] and padding [1 1 1 1] 15 'relu5' ReLU ReLU 16 'pool5' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0] 17 'fc6' Fully Connected 4096 fully connected layer 18 'relu6' ReLU ReLU 19 'drop6' Dropout 50% dropout 20 'fc7' Fully Connected 4096 fully connected layer 21 'relu7' ReLU ReLU 22 'drop7' Dropout 50% dropout 23 'fc8' Fully Connected 1000 fully connected layer 24 'prob' Softmax softmax 25 'output' Classification Output crossentropyex with 'tench' and 999 other classes
您可以通过查看分类输出层(最终层)的 Classes
属性,来查看网络学习的类的名称。通过选择前 10 个元素来查看前 10 个类。
net.Layers(end).Classes(1:10)
ans = 10×1 categorical array
tench
goldfish
great white shark
tiger shark
hammerhead
electric ray
stingray
cock
hen
ostrich
从 Caffe 网络导入层
指定要导入的示例文件 'digitsnet.prototxt'
。
protofile = 'digitsnet.prototxt';
导入网络层。
layers = importCaffeLayers(protofile)
layers = 1x7 Layer array with layers: 1 'testdata' Image Input 28x28x1 images 2 'conv1' Convolution 20 5x5x1 convolutions with stride [1 1] and padding [0 0] 3 'relu1' ReLU ReLU 4 'pool1' Max Pooling 2x2 max pooling with stride [2 2] and padding [0 0] 5 'ip1' Fully Connected 10 fully connected layer 6 'loss' Softmax softmax 7 'output' Classification Output crossentropyex with 'class1', 'class2', and 8 other classes
训练用于图像分类的网络
将数据作为 ImageDatastore
对象加载。
digitDatasetPath = fullfile(matlabroot,'toolbox','nnet', ... 'nndemos','nndatasets','DigitDataset'); imds = imageDatastore(digitDatasetPath, ... 'IncludeSubfolders',true, ... 'LabelSource','foldernames');
该数据存储包含 10,000 个数字 0 至 9 的合成图像。这些图像是通过对使用不同字体创建的数字图像应用随机变换生成的。每个数字图像为 28×28 像素。该数据存储包含的每个类别都有相同数量的图像。
显示数据存储中的部分图像。
figure numImages = 10000; perm = randperm(numImages,20); for i = 1:20 subplot(4,5,i); imshow(imds.Files{perm(i)}); drawnow; end
划分数据存储,使训练集中的每个类别包含 750 个图像,测试集包含对应每个标签的其余图像。
numTrainingFiles = 750;
[imdsTrain,imdsTest] = splitEachLabel(imds,numTrainingFiles,'randomize');
splitEachLabel
将 digitData
中的图像文件拆分为两个新的数据存储,imdsTrain
和 imdsTest
。
定义卷积神经网络架构。
layers = [ ... imageInputLayer([28 28 1]) convolution2dLayer(5,20) reluLayer maxPooling2dLayer(2,'Stride',2) fullyConnectedLayer(10) softmaxLayer classificationLayer];
将选项设置为带动量的随机梯度下降的默认设置。将最大训练轮数设置为 20,以 0.0001 的初始学习率开始训练。
options = trainingOptions('sgdm', ... 'MaxEpochs',20,... 'InitialLearnRate',1e-4, ... 'Verbose',false, ... 'Plots','training-progress');
训练网络。
net = trainNetwork(imdsTrain,layers,options);
基于未用于训练网络的测试集运行经过训练的网络,并预测图像标签(数字)。
YPred = classify(net,imdsTest); YTest = imdsTest.Labels;
计算准确度。准确度是测试数据中与来自 classify
的分类匹配的真实标签数量与测试数据中图像数量的比率。
accuracy = sum(YPred == YTest)/numel(YTest)
accuracy = 0.9400
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
用法说明和限制:
仅支持
activations
、classify
、predict
、predictAndUpdateState
、classifyAndUpdateState
和resetState
对象函数。要创建用于代码生成的
SeriesNetwork
对象,请参阅加载预训练网络以用于代码生成 (MATLAB Coder)。
GPU 代码生成
使用 GPU Coder™ 为 NVIDIA® GPU 生成 CUDA® 代码。
用法说明和限制:
仅支持
activations
、classify
、predict
、predictAndUpdateState
、classifyAndUpdateState
和resetState
对象函数。要创建用于代码生成的
SeriesNetwork
对象,请参阅Load Pretrained Networks for Code Generation (GPU Coder)。
版本历史记录
在 R2016a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)