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

R2024a: Not recommended

assembleNetwork is not recommended. Use dlnetwork objects instead.

The assembleNetwork function returns SeriesNetwork and DAGNetwork objects, which are not recommended. There are no plans to remove support for the assembleNetwork function. However, dlnetwork objects are recommended instead and have these advantages:

  • 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 update your code to use dlnetwork objects, use one of these options:

Not RecommendedRecommended
Assemble SeriesNetwork or DAGNetwork object using a layer array or layer graph.Build dlnetwork object directly and do not include any output layers. Most functions that support LayerGraph objects also support dlnetwork objects. To initialize the network, use the initialize function.
Assemble imported network returned by the importKerasLayers, importTensorFlowLayers, or importONNXLayers functions.Import the network using the importNetworkFromTensorFlow or importNetworkFromONNX functions.