# reshape¶

paddle.fluid.layers.reshape(x, shape, actual_shape=None, act=None, inplace=False, name=None)[source]

This operator changes the shape of x without changing its data.

The target shape can be given by shape or actual_shape. When shape and actual_shape are set at the same time, actual_shape has a higher priority than shape but at this time shape can only be an integer list or tuple, and shape still should be set correctly to guarantee shape inference in compile-time.

Some tricks exist when specifying the target shape.

1. -1 means the value of this dimension is inferred from the total element number of x and remaining dimensions. Thus one and only one dimension can be set -1.

2. 0 means the actual dimension value is going to be copied from the corresponding dimension of x. The index of 0s in shape can not exceed the dimension of x.

Here are some examples to explain it.

1. Given a 3-D tensor x with a shape [2, 4, 6], and the target shape is [6, 8], the reshape operator will transform x into a 2-D tensor with shape [6, 8] and leaving x’s data unchanged.

2. Given a 3-D tensor x with a shape [2, 4, 6], and the target shape specified is [2, 3, -1, 2], the reshape operator will transform x into a 4-D tensor with shape [2, 3, 4, 2] and leaving x’s data unchanged. In this case, one dimension of the target shape is set to -1, the value of this dimension is inferred from the total element number of x and remaining dimensions.

3. Given a 3-D tensor x with a shape [2, 4, 6], and the target shape is [-1, 0, 3, 2], the reshape operator will transform x into a 4-D tensor with shape [2, 4, 3, 2] and leaving x’s data unchanged. In this case, besides -1, 0 means the actual dimension value is going to be copied from the corresponding dimension of x.

Note:

The parameter actual_shape will be deprecated in the future and only use shape instead to represent the target shape.

Parameters
• x (Variable) – A Tensor or LoDTensor . The data type is float32, float64, int32 or int64.

• shape (list|tuple|Variable) – Define the target shape. At most one dimension of the target shape can be -1. The data type is int32 . If shape is a list or tuple, the elements of it should be integers or Tensors with shape [1]. If shape is an Variable, it should be an 1-D Tensor .

• actual_shape (variable, optional) – An 1-D Tensor or LoDTensor . The data type is int32 . If provided, reshape according to this given shape rather than shape specifying shape. That is to say actual_shape has a higher priority than shape(list|tuple) but not shape(Variable). This argument actual_shape will be removed in a future version. Instructions for updating: actual_shape will be removed in future versions and replaced by shape.

• act (str, optional) – The non-linear activation to be applied to the reshaped input. Default None.

• inplace (bool, optional) – If inplace is True, the input and output of layers.reshape are the same variable. Otherwise, the input and output of layers.reshape are different variable. Default False. Note that if x is more than one OPs’ input, inplace must be False.

• name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name .

Returns

A Tensor or LoDTensor. The data type is same as x. It is a new tensor variable if inplace is False, otherwise it is x. If act is None, return the reshaped tensor variable, otherwise return the activated tensor variable.

Return type

Variable

Raises
• TypeError – If actual_shape is neither Variable nor None.

• ValueError – If more than one elements of shape is -1.

• ValueError – If the element of shape is 0, the corresponding dimension should be less than or equal to the dimension of x.

• ValueError – If the elements in shape is negative except -1.

Examples

# example 1:
# attr shape is a list which doesn't contain tensor Variable.
data_1 = fluid.data(
name='data_1', shape=[2, 4, 6], dtype='float32')
reshaped_1 = fluid.layers.reshape(
x=data_1, shape=[-1, 0, 3, 2], inplace=True)
# the shape of reshaped_1 is [2,4,3,2].

# example 2:
# attr shape is a list which contains tensor Variable.
data_2 = fluid.layers.fill_constant([2,25], "int32", 3)
dim = fluid.layers.fill_constant([1], "int32", 5)
reshaped_2 = fluid.layers.reshape(data_2, shape=[dim, 10])
# the shape of reshaped_2 is [5,10].

# example 3:
data_3 = fluid.data(
name="data_3", shape=[2,4,6], dtype='float32')
reshaped_3 = fluid.layers.reshape(x=data_3, shape=[6,8])
# the shape of reshaped_3 is [6,8].