unflatten

paddle. unflatten ( x, axis, shape, name=None ) [source]

Expand a certain dimension of the input x Tensor into a desired shape.

Parameters
  • x (Tensor) – An N-D Tensor. The data type is float16, float32, float64, int16, int32, int64, bool, uint16.

  • axis (int) – axis to be unflattened, specified as an index into x.shape.

  • shape (list|tuple|Tensor) – Unflatten shape on the specified axis. At most one dimension of the target shape can be -1. If the input shape does not contain -1 , the product of all elements in shape should be equal to x.shape[axis]. The data type is int . If shape is a list or tuple, the elements of it should be integers or Tensors with shape []. If shape is an Tensor, it should be an 1-D Tensor.

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

Returns

Tensor, return the unflatten tensor of x.

Examples

>>> import paddle

>>> x = paddle.randn(shape=[4, 6, 8])
>>> shape = [2, 3]
>>> axis = 1
>>> res = paddle.unflatten(x, axis, shape)
>>> print(res.shape)
[4, 2, 3, 8]

>>> x = paddle.randn(shape=[4, 6, 8])
>>> shape = (-1, 2)
>>> axis = -1
>>> res = paddle.unflatten(x, axis, shape)
>>> print(res.shape)
[4, 6, 4, 2]

>>> x = paddle.randn(shape=[4, 6, 8])
>>> shape = paddle.to_tensor([2, 2])
>>> axis = 0
>>> res = paddle.unflatten(x, axis, shape)
>>> print(res.shape)
[2, 2, 6, 8]