reshape¶
- paddle.fluid.layers.nn. reshape ( x, shape, actual_shape=None, act=None, inplace=False, name=None ) [source]
-
- Alias_main
-
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
shapeoractual_shape. Whenshapeandactual_shapeare set at the same time,actual_shapehas a higher priority thanshapebut at this timeshapecan only be an integer list or tuple, andshapestill 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_shapewill be deprecated in the future and only useshapeinstead to represent the target shape.
- Parameters
-
x (Tensor) – An N-D Tensor. The data type is
float32,float64,int32orint64.shape (list|tuple|Tensor) – Define the target shape. At most one dimension of the target shape can be -1. The data type is
int32. Ifshapeis a list or tuple, the elements of it should be integers or Tensors with shape [1]. Ifshapeis an Tensor, it should be an 1-D Tensor .actual_shape (variable, optional) – An 1-D
TensororLoDTensor. The data type isint32. If provided, reshape according to this given shape rather thanshapespecifying shape. That is to sayactual_shapehas a higher priority thanshape(list|tuple)but notshape(Tensor). This argumentactual_shapewill be removed in a future version. Instructions for updating:actual_shapewill be removed in future versions and replaced byshape.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 oflayers.reshapeare the same variable. Otherwise, the input and output oflayers.reshapeare different variable. Default False. Note that ifxis 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 .
- Returns
-
A reshaped Tensor with the same data type as
x. It is a new tensor variable ifinplaceisFalse, otherwise it isx. Ifactis None, return the reshaped tensor variable, otherwise return the activated tensor variable. - Return type
-
Tensor
Examples
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([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].
