reshape( x, shape, actual_shape=None, act=None, inplace=False, name=None )
paddle.reshape :alias: paddle.reshape,paddle.tensor.reshape,paddle.tensor.manipulation.reshape
This operator changes the shape of
xwithout changing its data.
The target shape can be given by
actual_shapeare set at the same time,
actual_shapehas a higher priority than
shapebut at this time
shapecan only be an integer list or tuple, and
shapestill 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.
actual_shapewill be deprecated in the future and only use
shapeinstead to represent the target shape.
x (Tensor) – An N-D Tensor. The data type is
shape (list|tuple|Tensor) – Define the target shape. At most one dimension of the target shape can be -1. The data type is
shapeis a list or tuple, the elements of it should be integers or Tensors with shape . If
shapeis an Tensor, it should be an 1-D Tensor .
actual_shape (variable, optional) – An 1-D
LoDTensor. The data type is
int32. If provided, reshape according to this given shape rather than
shapespecifying shape. That is to say
actual_shapehas a higher priority than
shape(Tensor). This argument
actual_shapewill be removed in a future version. Instructions for updating:
actual_shapewill be removed in future versions and replaced by
act (str, optional) – The non-linear activation to be applied to the reshaped input. Default None.
inplace (bool, optional) – If
inplaceis True, the input and output of
layers.reshapeare the same variable. Otherwise, the input and output of
layers.reshapeare different variable. Default False. Note that if
xis more than one OPs’ input,
inplacemust 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 .
A reshaped Tensor with the same data type as
x. It is a new tensor variable if
False, otherwise it is
actis None, return the reshaped tensor variable, otherwise return the activated tensor variable.
- Return type
import paddle import paddle.fluid as fluid paddle.enable_static() # example 1: # attr shape is a list which doesn't contain Tensors. 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]) # the shape of reshaped_1 is [2,4,3,2]. # example 2: # attr shape is a list which contains Tensors. data_2 = fluid.layers.fill_constant([2,25], "int32", 3) dim = fluid.layers.fill_constant(, "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].