assembleNetwork - (Not recommended) Assemble deep learning network from pretrained layers - MATLAB (original) (raw)
(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.
Syntax
Description
[assembledNet](#mw%5F73f415d9-06f8-4ecc-a42f-93a11f47d641) = assembleNetwork([layers](#function%5FassembleNetwork%5Fsep%5Fmw%5Fa4be7938-74e9-410c-8d2d-98ae715a53af))
assembles the layer array or layer graph layers
into a deep learning network ready to use for prediction.
Examples
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(4numHiddenUnits,numChannels); layers(2).RecurrentWeights = rand(4numHiddenUnits,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
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 aLayer
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 toassembleNetwork
.
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
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 aLayer
array, thenassembledNet
is aSeriesNetwork
object. - If
layers
is a LayerGraph object, thenassembledNet
is aDAGNetwork
object.
Version History
Introduced in R2018b
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 thanLayerGraph
andtrainNetwork
workflows.
To update your code to use dlnetwork
objects, use one of these options:
Not Recommended | Recommended |
---|---|
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 supportLayerGraph objects also support dlnetwork objects. To initialize the network, use the initialize function. |
Assemble imported network returned by theimportKerasLayers,importTensorFlowLayers, orimportONNXLayers functions. | Import the network using the importNetworkFromTensorFlow or importNetworkFromONNX functions. |