Main Content

assembleNetwork

(Not recommended) Assemble deep learning network from pretrained layers

The assembleNetwork function is not recommended. Use dlnetwork objects instead. For more information, see Version History.

To train a network from scratch, use the trainnet function.

Description

assembledNet = assembleNetwork(layers) assembles the layer array or layer graph layers into a deep learning network ready to use for prediction.

example

Examples

collapse all

Assemble a neural network from pretrained layers without training.

Define a simple LSTM network.

numChannels = 3;
numHiddenUnits = 100;
numClasses = 10;

layers = [
    sequenceInputLayer(numChannels)
    lstmLayer(numHiddenUnits,OutputMode="last")
    fullyConnectedLayer(numClasses)
    softmaxLayer
    classificationLayer];

For the learnable layers in the network, manually set the properties that correspond to the learnable parameters. You can import or load the parameters from external sources such as MAT files. For illustrative purposes, this example sets the learnable parameters to random values.

layers(2).InputWeights = rand(4*numHiddenUnits,numChannels);
layers(2).RecurrentWeights = rand(4*numHiddenUnits,numHiddenUnits);
layers(2).Bias = rand(4*numHiddenUnits,1);

layers(3).Weights = rand(numClasses,numHiddenUnits);
layers(3).Bias = rand(numClasses,1);

Set the Classes property of the classification output layer.

classNames = string(1:10);
layers(5).Classes = classNames;

Assemble the network using the assembleNetwork function. The output network is a SeriesNetwork object that is ready to use for prediction.

net = assembleNetwork(layers)
net = 

  SeriesNetwork with properties:

         Layers: [5×1 nnet.cnn.layer.Layer]
     InputNames: {'sequenceinput'}
    OutputNames: {'classoutput'}

Input Arguments

collapse all

Neural network layers, specified as a Layer array or a LayerGraph object.

To create a neural network with all layers connected sequentially, you can use a Layer array as the input argument. In this case, the returned neural network is a SeriesNetwork object.

A directed acyclic graph (DAG) neural network has a complex structure in which layers can have multiple inputs and outputs. To create a DAG neural network, specify the neural network architecture as a LayerGraph object and then use that layer graph as the input argument to assembleNetwork.

The assembleNetwork function supports neural networks with at most one sequence input layer.

For a list of built-in layers, see List of Deep Learning Layers.

Output Arguments

collapse all

Assembled network ready for prediction, returned as a SeriesNetwork object or a DAGNetwork object. The returned network depends on the layers input argument:

  • If layers is a Layer array, then assembledNet is a SeriesNetwork object.

  • If layers is a LayerGraph object, then assembledNet is a DAGNetwork object.

Version History

Introduced in R2018b

collapse all