SeriesNetwork - (Not recommended) Series network for deep learning - MATLAB (original) (raw)
(Not recommended) Series network for deep learning
Description
A series network is a neural network for deep learning with layers arranged one after the other. It has a single input layer and a single output layer.
Properties
This property is read-only.
Network layers, specified as a Layer
array.
This property is read-only.
Names of the input layers, specified as a cell array of character vectors.
Data Types: cell
This property is read-only.
Names of the output layers, specified as a cell array of character vectors.
Data Types: cell
Object Functions
activations | (Not recommended) Compute deep learning network layer 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 | Plot neural network architecture |
Examples
Train network for image classification.
Load the data as an ImageDatastore
object.
digitDatasetPath = fullfile(matlabroot,'toolbox','nnet', ... 'nndemos','nndatasets','DigitDataset'); imds = imageDatastore(digitDatasetPath, ... 'IncludeSubfolders',true, ... 'LabelSource','foldernames');
The datastore contains 10,000 synthetic images of digits from 0 to 9. The images are generated by applying random transformations to digit images created with different fonts. Each digit image is 28-by-28 pixels. The datastore contains an equal number of images per category.
Display some of the images in the datastore.
figure numImages = 10000; perm = randperm(numImages,20); for i = 1:20 subplot(4,5,i); imshow(imds.Files{perm(i)}); drawnow; end
Divide the datastore so that each category in the training set has 750 images and the testing set has the remaining images from each label.
numTrainingFiles = 750; [imdsTrain,imdsTest] = splitEachLabel(imds,numTrainingFiles, ... 'randomize');
splitEachLabel
splits the image files in digitData
into two new datastores, imdsTrain
and imdsTest
.
Define the convolutional neural network architecture.
layers = [ ... imageInputLayer([28 28 1]) convolution2dLayer(5,20) reluLayer maxPooling2dLayer(2,'Stride',2) fullyConnectedLayer(10) softmaxLayer classificationLayer];
Set the options to the default settings for the stochastic gradient descent with momentum. Set the maximum number of epochs at 20, and start the training with an initial learning rate of 0.0001.
options = trainingOptions('sgdm', ... 'MaxEpochs',20,... 'InitialLearnRate',1e-4, ... 'Verbose',false, ... 'Plots','training-progress');
Train the network.
net = trainNetwork(imdsTrain,layers,options);
Run the trained network on the test set, which was not used to train the network, and predict the image labels (digits).
YPred = classify(net,imdsTest); YTest = imdsTest.Labels;
Calculate the accuracy. The accuracy is the ratio of the number of true labels in the test data matching the classifications from classify
to the number of images in the test data.
accuracy = sum(YPred == YTest)/numel(YTest)
Extended Capabilities
Usage notes and limitations:
- Only the
activations
,classify
,predict
,predictAndUpdateState
,classifyAndUpdateState
, andresetState
object functions are supported.
Usage notes and limitations:
- Only the
activations
,classify
,predict
,predictAndUpdateState
,classifyAndUpdateState
, andresetState
object functions are supported.
Version History
Introduced in R2016a
Starting in R2024a, SeriesNetwork
objects are not recommended, use dlnetwork objects instead.
There are no plans to remove support for SeriesNetwork
objects. However, dlnetwork
objects have these advantages and are recommended instead:
dlnetwork
objects are a unified data type that supports network building, prediction, built-in training, visualization, compression, verification, and custom training loops.dlnetwork
objects support a wider range of network architectures that you can create or import from external platforms.- The trainnet function supports
dlnetwork
objects, which enables you to easily specify loss functions. You can select from built-in loss functions or specify a custom loss function. - Training and prediction with
dlnetwork
objects is typically faster thanLayerGraph
andtrainNetwork
workflows.
To convert a trained SeriesNetwork
object to a dlnetwork
object, use the dag2dlnetwork function.
This table shows some typical usages of SeriesNetwork
objects and how to update your code to use dlnetwork
object functions instead.
Not Recommended | Recommended |
---|---|
Y = predict(net,X); | Y = minibatchpredict(net,X); |
Y = classify(net,X); | scores = minibatchpredict(net,X); Y = scores2label(scores,classNames); |
Y = activations(net,X,layerName); | Y = predict(net,X,Outputs=layerName); |
[net,Y] = predictAndUpdateState(net,X); | [Y,state] = predict(net,X); net.State = state; |
[net,Y] = classifyAndUpdateState(net,X); | [scores,state] = predict(net,X); Y = scores2label(scores,classNames); net.State = state; |