W3cubDocs

/PyTorch 2.9

torch.atleast_2d

torch.atleast_2d(*tensors) [source]

Returns a 2-dimensional view of each input tensor with zero dimensions. Input tensors with two or more dimensions are returned as-is.

Parameters

input (Tensor or sequence of Tensors) – tensor(s) to be converted to at least 2-dimensional.

Returns

output (Tensor or tuple of Tensors)

Example:

>>> x = torch.tensor(1.)
>>> x
tensor(1.)
>>> torch.atleast_2d(x)
tensor([[1.]])
>>> x = torch.arange(4).view(2, 2)
>>> x
tensor([[0, 1],
        [2, 3]])
>>> torch.atleast_2d(x)
tensor([[0, 1],
        [2, 3]])
>>> x = torch.tensor(0.5)
>>> y = torch.tensor(1.)
>>> torch.atleast_2d((x, y))
(tensor([[0.5000]]), tensor([[1.]]))
>>> torch.atleast_2d()
()

© 2025, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://docs.pytorch.org/docs/2.9/generated/torch.atleast_2d.html