FlattenLayer - Flatten layer - MATLAB (original) (raw)

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

Syntax

Description

`layer` = flattenLayer creates a flatten layer.

`layer` = flattenLayer('Name',`Name`) sets the optional Name property using a name-value pair. For example,flattenLayer('Name','flatten1') creates a flatten layer with name 'flatten1'.

example

Properties

expand all

Data Types: char | string

This property is read-only.

Number of inputs to the layer, stored as 1. This layer accepts a single input only.

Data Types: double

This property is read-only.

Input names, stored as {'in'}. This layer accepts a single input only.

Data Types: cell

This property is read-only.

Number of outputs from the layer, stored as 1. This layer has a single output only.

Data Types: double

This property is read-only.

Output names, stored as {'out'}. This layer has a single output only.

Data Types: cell

Object Functions

Examples

collapse all

Create a flatten layer with the name 'flatten1'.

layer = flattenLayer('Name','flatten1')

layer = FlattenLayer with properties:

Name: 'flatten1'

Algorithms

expand all

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 format consists of one or more of these characters:

For example, you can describe 2-D image data that is 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, as having the format "SSCB" (spatial, spatial, channel, batch).

You can interact with these dlarray objects in automatic differentiation workflows, such as those for developing a custom layer, using a functionLayer object, or using the forward and predict functions withdlnetwork 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 aFunctionLayer object with the Formattable property set to 0 (false), then the layer receives an unformatted dlarray object with dimensions ordered according 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
"CB" (channel, batch) "CB" (channel, batch)
"SCB" (spatial, channel, batch)
"SSCB" (spatial, spatial, channel, batch)
"SSSCB" (spatial, spatial, channel, batch)
"CBT" (channel, batch, time) "CBT" (channel, batch, time)
"SCBT" (spatial, channel, batch, time)
"SSCBT" (spatial, spatial, channel, batch, time)
"SSSCBT" (spatial, spatial, spatial, channel, batch, time)
"CU" (channel, unspecified) "CU" (channel, unspecified)
"SC" (spatial, channel)
"SSC" (spatial, spatial, channel)
"SSSC" (spatial, spatial, spatial, channel)

In dlnetwork objects, FlattenLayer objects also support these input and output format combinations.

Input Format Output Format
"CT" (channel, time) "CT" (channel, time)
"SCT" (spatial, channel, time)
"SSCT" (spatial, spatial, channel, time)
"SSSCT" (spatial, spatial, spatial, channel, time)

FlattenLayer objects support complex-valued input and outputs. (since R2024a) The layer applies the same underlying operation to complex-valued input as it does to real-valued input and outputs complex-valued data where applicable.

Extended Capabilities

Version History

Introduced in R2019a

expand all

FlattenLayer objects support complex-valued input and outputs. The layer applies the same underlying operation to complex-valued input as it does to real-valued input and outputs complex-valued data where applicable.