# FlattenLayer

represents a net layer that flattens any input array into a vector.

FlattenLayer[n]

represents a net layer that flattens its input to level n.

# Details and Options • is equivalent to .
• FlattenLayer[][input] explicitly computes the output from applying the layer to input.
• FlattenLayer[][{input1,input2,}] explicitly computes outputs for each of the inputi.
• When given a NumericArray as input, the output will be a NumericArray.
• In FlattenLayer[n], n can be one of the following:
•  n flatten the first n+1 dimensions together -n flatten the last n+1 dimensions together Infinity flatten all dimensions
• FlattenLayer exposes the following ports for use in NetGraph etc.:
•  "Input" an array of arbitrary rank "Output" an array
• FlattenLayer is typically used inside NetChain, NetGraph, etc. to transform higher-rank arrays into vectors.
• FlattenLayer normally infers the dimensions of its input from its context in NetChain etc. To specify the dimensions explicitly as {n1,n2,}, use FlattenLayer["Input"->{n1,n2,}].
• Options[FlattenLayer] gives the list of default options to construct the layer. Options[FlattenLayer[]] gives the list of default options to evaluate the layer on some data.
• Information[FlattenLayer[]] gives a report about the layer.
• Information[FlattenLayer[],prop] gives the value of the property prop of FlattenLayer[]. Possible properties are the same as for NetGraph.

# Examples

open allclose all

## Basic Examples(2)

Create a FlattenLayer:

Create a FlattenLayer with specified input dimensions:

Apply the layer to a matrix:

## Scope(3)

Create a FlattenLayer that flattens the first two dimensions of the input, preserving the other dimensions:

Apply the layer to an input:

Create a FlattenLayer that flattens the first three dimensions of its input together, and apply it to a 2×3×4×5 array:

Create and apply a FlattenLayer that flattens the last three dimensions together:

Create and apply a layer that flattens all the dimensions together:

Create a FlattenLayer with specified input dimensions:

Thread the layer across a batch of inputs:

## Properties & Relations(4)

behaves like Flatten:

FlattenLayer[n] computes:

Create a rank-3 array:

Flatten the first two dimensions:

Flatten the last two dimensions:

FlattenLayer[n] is equivalent to a ReshapeLayer[dims], where the dims depend on n and the dimensions of the input to FlattenLayer. Create a rank-3 array:

Apply a particular FlattenLayer:

Apply an equivalent ReshapeLayer:

In previous versions of the Wolfram Language, FlattenLayer was often used to convert a higher-rank array into a vector suitable for DotPlusLayer, the previous version of LinearLayer: Unlike DotPlusLayer, LinearLayer can take arbitrary array inputs, and so the FlattenLayer is unnecessary:

## Possible Issues(2)

To prevent inadvertent errors, FlattenLayer[n] requires the input to have rank greater than n: In contrast, Flatten[input,n] works for any n:

accepts arrays of any rank:

FlattenLayer cannot accept symbolic inputs: Introduced in 2016
(11.0)
|
Updated in 2017
(11.1)