unflatten#

ivy.unflatten(x, /, dim, shape, *, out=None)[source]#

Expand a dimension of the input tensor over multiple dimensions.

Parameters:
  • x (Union[Array, NativeArray]) – input tensor.

  • dim (int) – dimension to be unflattened, specified as an index into input.shape.

  • shape (Tuple[int]) – new shape of the unflattened dimension. One of its elements can be -1 in which case the corresponding output dimension is inferred. Otherwise, the product of sizes must equal input.shape[dim].

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – view of input with the specified dimension unflattened.

This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments.

Examples

>>> ivy.unflatten(torch.randn(3, 4, 1), dim=1, shape=(2, 2)).shape
torch.Size([3, 2, 2, 1])
>>> ivy.unflatten(torch.randn(3, 4, 1), dim=1, shape=(-1, 2)).shape
torch.Size([3, 2, 2, 1])
>>> ivy.unflatten(torch.randn(5, 12, 3), dim=-2, shape=(2, 2, 3, 1, 1)).shape
torch.Size([5, 2, 2, 3, 1, 1, 3])
Array.unflatten(self, /, shape, dim=0, *, out=None)[source]#

ivy.Array instance method variant of ivy.unflatten. This method simply wraps the function, and so the docstring for ivy.unflatten also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array

  • shape (Union[Tuple[int], Array, NativeArray]) – array indices. Must have an integer data type.

  • dim (Optional[int], default: 0) – axis over which to unflatten. If axis is negative, the function must determine the axis along which to select values by counting from the last dimension. By default, the flattened input array is used.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – an array having the same data type as x. The output array must have the same rank (i.e., number of dimensions) as x and must have the same shape as x, except for the axis specified by dim which is replaced with a tuple specified in shape.

Examples

With ‘ivy.Array’ input:

>>> x = ivy.array([[1.2, 2.3, 3.4, 4.5],
...               [5.6, 6.7, 7.8, 8.9]])
>>> dim = 1
>>> shape = (2, 2)
>>> y = ivy.zeros([2, 2, 2])
>>> x.unflatten(shape=shape, dim=dim, out=y)
>>> print(y)
ivy.array([[[1.2, 2.3], [3.4, 4.5]], [[5.6, 6.7], [7.8, 8.9]]])
Container.unflatten(self, /, shape, dim=0, *, out=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

ivy.Container instance method variant of ivy.unflatten. This method simply wraps the function, and so the docstring for ivy.unflatten also applies to this method with minimal changes.

Parameters:
  • self (Container) – input array

  • shape (Union[Tuple[int], Array, NativeArray, Container]) – array indices. Must have an integer data type.

  • dim (Optional[Union[int, Container]], default: 0) – axis over which to unflatten. If axis is negative, the function must determine the axis along which to select values by counting from the last dimension. By default, the flattened input array is used.

  • out (Optional[Union[Array, Container]], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Return type:

Container

Returns:

ret – an array having the same data type as x. The output array must have the same rank (i.e., number of dimensions) as x and must have the same shape as x, except for the axis specified by dim which is replaced with a tuple specified in shape.

Examples

With ‘ivy.Container’ input:

>>> x = ivy.Container(a = ivy.array([[True, False, False, True],
...                                 [False, True, False, True]]),
...                     b = ivy.array([[1.2, 2.3, 3.4, 4.5],
...                                   [5.6, 6.7, 7.8, 8.9]]),
...                     c = ivy.array([[1, 2, 3, 4],
...                                   [5, 6, 7, 8]]))
>>> dim = 1
>>> shape = (2, 2)
>>> y = x.unflatten(shape=shape, dim=dim)
>>> print(y)
{
    a: ivy.array([[[True, False], [False, True]],
                  [[False, True], [False, True]]]),
    b: ivy.array([[[1.2, 2.3], [3.4, 4.5]], [[5.6, 6.7], [7.8, 8.9]]]),
    c: ivy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
}