atleast_2d

paddle. atleast_2d ( *inputs, name=None ) [source]

Convert inputs to tensors and return the view with at least 2-dimension. Two or high-dimensional inputs are preserved.

The following diagram illustrates the behavior of atleast_2d on different dimensional inputs for the following cases:

  1. A 0-dim tensor input.

  2. A 0-dim tensor and a 1-dim tensor input.

  3. A 0-dim tensor and a 3-dim tensor input.

legend of atleast_2d API

In each case, the function returns the tensors (or a list of tensors) in views with at least 2 dimensions.

Parameters
  • inputs (Tensor|list(Tensor)) – One or more tensors. The data type is float16, float32, float64, int16, int32, int64, int8, uint8, complex64, complex128, bfloat16 or bool.

  • name (str|None, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

One Tensor, if there is only one input. List of Tensors, if there are more than one inputs.

Examples

>>> import paddle

>>> # one input
>>> x = paddle.to_tensor(123, dtype='int32')
>>> out = paddle.atleast_2d(x)
>>> print(out)
Tensor(shape=[1, 1], dtype=int32, place=Place(cpu), stop_gradient=True,
[[123]])

>>> # more than one inputs
>>> x = paddle.to_tensor(123, dtype='int32')
>>> y = paddle.to_tensor([1.23], dtype='float32')
>>> out = paddle.atleast_2d(x, y)
>>> print(out)
[Tensor(shape=[1, 1], dtype=int32, place=Place(cpu), stop_gradient=True,
[[123]]), Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1.23000002]])]

>>> # more than 2-D input
>>> x = paddle.to_tensor(123, dtype='int32')
>>> y = paddle.to_tensor([[[1.23]]], dtype='float32')
>>> out = paddle.atleast_2d(x, y)
>>> print(out)
[Tensor(shape=[1, 1], dtype=int32, place=Place(cpu), stop_gradient=True,
[[123]]), Tensor(shape=[1, 1, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[1.23000002]]])]