flattenLayer
Description
A flatten layer collapses the spatial dimensions of the input into the channel dimension.
For example, if the input to the layer is an H-by-W-by-C-by-N-by-S array (sequences of images), then the flattened output is an (H*W*C)-by-N-by-S array.
Creation
Properties
Name
— Layer name
""
(default) | character vector | string scalar
Layer name, specified as a character vector or a string scalar.
For Layer
array input, the trainnet
, trainNetwork
, assembleNetwork
, layerGraph
, and
dlnetwork
functions automatically assign
names to layers with the name ""
.
The FlattenLayer
object stores this property as a character vector.
Data Types: char
| string
NumInputs
— Number of inputs
1
(default)
This property is read-only.
Number of inputs to the layer, returned as 1
. This layer accepts a
single input only.
Data Types: double
InputNames
— Input names
{'in'}
(default)
This property is read-only.
Input names, returned as {'in'}
. This layer accepts a single input
only.
Data Types: cell
NumOutputs
— Number of outputs
1
(default)
This property is read-only.
Number of outputs from the layer, returned as 1
. This layer has a
single output only.
Data Types: double
OutputNames
— Output names
{'out'}
(default)
This property is read-only.
Output names, returned as {'out'}
. This layer has a single output
only.
Data Types: cell
Object Functions
Examples
Create Flatten Layer
Create a flatten layer with the name 'flatten1'
.
layer = flattenLayer('Name','flatten1')
layer = FlattenLayer with properties: Name: 'flatten1'
Create Network for Video Classification
Create a deep learning network for data containing sequences of images, such as video and medical image data.
To input sequences of images into a network, use a sequence input layer.
To apply convolutional operations independently to each time step, first convert the sequences of images to an array of images using a sequence folding layer.
To restore the sequence structure after performing these operations, convert this array of images back to image sequences using a sequence unfolding layer.
To convert images to feature vectors, use a flatten layer.
You can then input vector sequences into LSTM and BiLSTM layers.
Define Network Architecture
Create a classification LSTM network that classifies sequences of 28-by-28 grayscale images into 10 classes.
Define the following network architecture:
A sequence input layer with an input size of
[28 28 1]
.A convolution, batch normalization, and ReLU layer block with 20 5-by-5 filters.
An LSTM layer with 200 hidden units that outputs the last time step only.
A fully connected layer of size 10 (the number of classes) followed by a softmax layer and a classification layer.
To perform the convolutional operations on each time step independently, include a sequence folding layer before the convolutional layers. LSTM layers expect vector sequence input. To restore the sequence structure and reshape the output of the convolutional layers to sequences of feature vectors, insert a sequence unfolding layer and a flatten layer between the convolutional layers and the LSTM layer.
inputSize = [28 28 1]; filterSize = 5; numFilters = 20; numHiddenUnits = 200; numClasses = 10; layers = [ ... sequenceInputLayer(inputSize,'Name','input') sequenceFoldingLayer('Name','fold') convolution2dLayer(filterSize,numFilters,'Name','conv') batchNormalizationLayer('Name','bn') reluLayer('Name','relu') sequenceUnfoldingLayer('Name','unfold') flattenLayer('Name','flatten') lstmLayer(numHiddenUnits,'OutputMode','last','Name','lstm') fullyConnectedLayer(numClasses, 'Name','fc') softmaxLayer('Name','softmax') classificationLayer('Name','classification')];
Convert the layers to a layer graph and connect the miniBatchSize
output of the sequence folding layer to the corresponding input of the sequence unfolding layer.
lgraph = layerGraph(layers); lgraph = connectLayers(lgraph,'fold/miniBatchSize','unfold/miniBatchSize');
View the final network architecture using the plot
function.
figure plot(lgraph)
Algorithms
Layer Input and Output Formats
Layers in a layer array or layer graph pass data to subsequent layers as formatted dlarray
objects. The format of a dlarray
object is a string of characters, in which each character describes the corresponding dimension of the data. The formats consists of one or more of these characters:
"S"
— Spatial"C"
— Channel"B"
— Batch"T"
— Time"U"
— Unspecified
For example, 2-D image data represented as a 4-D array, where the first two dimensions correspond to the spatial dimensions of the images, the third dimension corresponds to the channels of the images, and the fourth dimension corresponds to the batch dimension, can be described as having the format "SSCB"
(spatial, spatial, channel, batch).
You can interact with these dlarray
objects in automatic differentiation workflows such as developing a custom layer, using a functionLayer
object, or using the forward
and predict
functions with dlnetwork
objects.
This table shows the supported input formats of FlattenLayer
objects and the
corresponding output format. If the software passes the output of the layer to a custom
layer that does not inherit from the nnet.layer.Formattable
class, or a
FunctionLayer
object with the Formattable
property
set to 0
(false), then the layer receives an unformatted
dlarray
object with dimensions ordered corresponding to the formats in
this table. The formats listed here are only a subset. The layer may support additional
formats such as formats with additional "S"
(spatial) or
"U"
(unspecified) dimensions.
Input Format | Output Format |
---|---|
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
|
In dlnetwork
objects, FlattenLayer
objects also
support these input and output format combinations.
Input Format | Output Format |
---|---|
|
|
| |
| |
|
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Version History
Introduced in R2019a
See Also
lstmLayer
| bilstmLayer
| gruLayer
| classifyAndUpdateState
| predictAndUpdateState
| resetState
| sequenceFoldingLayer
| sequenceUnfoldingLayer
| sequenceInputLayer
Topics
- Classify Videos Using Deep Learning
- Sequence Classification Using Deep Learning
- Time Series Forecasting Using Deep Learning
- Sequence-to-Sequence Classification Using Deep Learning
- Visualize Activations of LSTM Network
- Long Short-Term Memory Neural Networks
- Deep Learning in MATLAB
- List of Deep Learning Layers
Ouvrir l'exemple
Vous possédez une version modifiée de cet exemple. Souhaitez-vous ouvrir cet exemple avec vos modifications ?
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- 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)