ReshapeLayer

ReshapeLayer[dims]

represents a net layer that reinterprets the input to be an array of dimensions dims.

Details and Options

  • ReshapeLayer[][input] explicitly computes the output from applying the layer to input.
  • ReshapeLayer[][{input1,input2,}] explicitly computes outputs for each of the inputi.
  • When given a NumericArray as input, the output will be a NumericArray.
  • ReshapeLayer exposes the following ports for use in NetGraph etc.:
  • "Input"an array of arbitrary rank
    "Output"an array of dimension dims
  • In ReshapeLayer[dims], the following elements are allowed in the list dims:
  • nexplicitly specify the size
    Inheritedcopy the size from the input
    Automaticinfer the size from the size of the other dimensions
  • The total number of elements in the input array is equal to the total number of elements in the output array.
  • Only one Automatic is allowed in the list of dimensions.
  • ReshapeLayer is typically used inside NetChain, NetGraph, etc.
  • ReshapeLayer normally infers the dimensions of its input from its context in NetChain etc. To specify the dimensions explicitly as {n1,n2,}, use ReshapeLayer["Input"->{n1,n2,}].
  • Options[ReshapeLayer] gives the list of default options to construct the layer. Options[ReshapeLayer[]] gives the list of default options to evaluate the layer on some data.
  • Information[ReshapeLayer[]] gives a report about the layer.
  • Information[ReshapeLayer[],prop] gives the value of the property prop of ReshapeLayer[]. Possible properties are the same as for NetGraph.

Examples

open allclose all

Basic Examples  (3)

Create a ReshapeLayer that reshapes any input into a 2×3 matrix:

Create a ReshapeLayer that reshapes any input into a 2×3 matrix:

Apply the layer to a vector:

Apply the layer to a 4-dimensional array:

Create a ReshapeLayer that reshapes a length-1 vector into a single number:

Apply the layer to an input:

Scope  (4)

Create a ReshapeLayer that reshapes an input array of a specific size:

Apply the layer to a vector:

Create a ReshapeLayer that copies the size of the first dimension of its input:

Apply the layer to an array of dimensions 6×1×4:

Create a ReshapeLayer that copies the size of the first dimension of its input and infers the size of the fourth dimension from the others:

Apply the layer to an array of dimensions 6×4×4:

Create a ReshapeLayer the takes a 32×32 RGB image and returns a 96×32 grayscale image:

Properties & Relations  (3)

FlattenLayer can be implemented using ReshapeLayer. Flatten the first two dimensions of a 2×3×3 array by reshaping it to the correct output dimensions:

The specifications Automatic and Inherited allow ReshapeLayer to recompute its output size when its input size is changed by NetReplacePart. Create a ReshapeLayer:

Change its input size:

If the same ReshapeLayer was created with fixed specifications, resizing would lead to a failure:

For the case of integer dimension specifications, ReshapeLayer computes:

Evaluate on some data:

Possible Issues  (2)

The total number of elements in the input must equal the total number in the output:

Only one Automatic is allowed in the dimension specification, as more than one would lead to ambiguities in the output size:

Interactive Examples  (1)

Reshape a list into a matrix by manipulating the number of rows and columns:

Neat Examples  (2)

Reshape a list into an array of depth 3 and use Image3D to visualize the result:

Reshape a list in various ways:

Introduced in 2016
 (11.0)
 |
Updated in 2019
 (12.0)