Main Content

SeriesNetwork

(Not recommended) Series network for deep learning

SeriesNetwork objects are not recommended. Use dlnetwork objects instead. For more information, see Version History.

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.

Creation

There are several ways to create a SeriesNetwork object:

Note

To learn about other pretrained networks, such as googlenet and resnet50, see Pretrained Deep Neural Networks.

Properties

expand all

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
resetStateReset state parameters of neural network
plotPlot neural network architecture

Examples

collapse all

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

Figure contains 20 axes objects. Axes object 1 contains an object of type image. Axes object 2 contains an object of type image. Axes object 3 contains an object of type image. Axes object 4 contains an object of type image. Axes object 5 contains an object of type image. Axes object 6 contains an object of type image. Axes object 7 contains an object of type image. Axes object 8 contains an object of type image. Axes object 9 contains an object of type image. Axes object 10 contains an object of type image. Axes object 11 contains an object of type image. Axes object 12 contains an object of type image. Axes object 13 contains an object of type image. Axes object 14 contains an object of type image. Axes object 15 contains an object of type image. Axes object 16 contains an object of type image. Axes object 17 contains an object of type image. Axes object 18 contains an object of type image. Axes object 19 contains an object of type image. Axes object 20 contains an object of type image.

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);

Figure Training Progress (15-Aug-2023 20:45:20) contains 2 axes objects and another object of type uigridlayout. Axes object 1 with xlabel Iteration, ylabel Loss contains 6 objects of type patch, text, line. Axes object 2 with xlabel Iteration, ylabel Accuracy (%) contains 6 objects of type patch, text, line.

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)
accuracy = 0.9416

Extended Capabilities

Version History

Introduced in R2016a

collapse all

R2024a: Not recommended

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 than LayerGraph and trainNetwork 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 RecommendedRecommended
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;