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
assembles the layer array or layer graph assembledNet
= assembleNetwork(layers
)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(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
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
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 aLayerGraph
object, thenassembledNet
is aDAGNetwork
object.
Version History
Introduced in R2018bassembleNetwork
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 supportsdlnetwork
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 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. |
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)