flatten

paddle.fluid.layers.flatten(x, axis=1, name=None)[source]

Flatten op

Flatten the input tensor into a 2D matrix.

For Example:

Case 1:

  Given
    X.shape = (3, 100, 100, 4)

  and
    axis = 2

  We get:
    Out.shape = (3 * 100, 4 * 100)

Case 2:

  Given
    X.shape = (3, 100, 100, 4)

  and
    axis = 0

  We get:
    Out.shape = (1, 3 * 100 * 100 * 4)
Parameters
  • x (Variable) – A tensor of rank >= axis. A tensor with type float32, float64, int8, int32, int64.

  • axis (int) – Indicate up to which input dimensions (exclusive) should be flattened to the outer dimension of the output. The value for axis must be in the range [0, R], where R is the rank of the input tensor. Default: 1.

  • name (str, Optional) – For details, please refer to Name. Generally, no setting is required. Default: None.

Returns

A 2D tensor with the contents of the input tensor, with input dimensions up to axis flattened to the outer dimension of the output and remaining input dimensions flattened into the inner dimension of the output. A Tensor with type same as input x.

Return type

Variable

Raises
  • ValueError – If x is not a variable.

  • ValueError – If axis is not in range [0, rank(x)].

Examples

import paddle.fluid as fluid
x = fluid.data(name="x", shape=[4, 4, 3], dtype="float32")
# x shape is [4, 4, 3]
out = fluid.layers.flatten(x=x, axis=2)
# out shape is [16, 3]