ReshapeLayer
ReshapeLayer[spec]
represents a net layer that reshapes the input array according to the specification spec.
Details and Options
- ReshapeLayer exposes the following ports for use in NetGraph etc.:
-
"Input" an array of arbitrary rank "ReferenceShape" an array to copy the shape from (optional) "Output" the reshaped array - In ReshapeLayer[spec], the following values for spec are allowed:
-
{dim1,…} explicitly specify the dimensions "ReferenceShape" the dimensions of the array provided to the "Reference" port - In ReshapeLayer[{dim1,…}], the following elements are allowed for dimi:
-
n explicitly specify the dimension Inherited copy the dimension from the input Automatic infer the dimension from the others All copy the remaining dimensions from the input - Only one Automatic or one All is allowed in the list of dimensions.
- The total number of elements in the input array is equal to the total number of elements in the output array. The total number of elements in the input array is equal to the total number of elements in the output array.
- 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,…}].
- ReshapeLayer[…][input] explicitly computes the output from applying the layer to input.
- ReshapeLayer[…][{input1,input2,…}] explicitly computes outputs for each of the inputi.
- With a "ReferenceShape" reshape specification, the input can be passed as ReshapeLayer[…][<"Input"input,"ReferenceShape"ref >] or ReshapeLayer[…][<"Input"{input1,…},"ReferenceShape"{ref1,…} >].
- When given a NumericArray as input, the output will be a NumericArray.
- 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 allBasic 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 4-dimensional array:
Create a ReshapeLayer that reshapes a length-1 vector into a single number:
Scope (5)
Create a ReshapeLayer that reshapes an input array of a specific size:
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 that adds a dimension of size 1 on the deepest level of an array:
Create a ReshapeLayer that reshapes the input to match the shape of a reference array:
Applications (1)
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:
If the same ReshapeLayer was created with fixed specifications, resizing would lead to a failure:
For the case of integer dimension specifications, ReshapeLayer computes:
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)
Neat Examples (2)
Compute all possible 3D shapes:
Reshape a list into an array of depth 3 and use Image3D to visualize the result:
Text
Wolfram Research (2016), ReshapeLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/ReshapeLayer.html (updated 2025).
CMS
Wolfram Language. 2016. "ReshapeLayer." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/ReshapeLayer.html.
APA
Wolfram Language. (2016). ReshapeLayer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ReshapeLayer.html