mygrad.atleast_2d#

mygrad.atleast_2d(tensors: ArrayLike, *, constant: bool | None = None) Tensor[source]#
mygrad.atleast_2d(*tensors: ArrayLike, constant: bool | None = None) List[Tensor]

Convert inputs to tensors with at least one dimension.

Scalar inputs are converted to 2-dimensional tensors, whilst higher-dimensional inputs are preserved.

This docstring was adapted from numpy.atleast_2d.

Parameters:
tens1, tens2, …ArrayLike

One or more input tensors.

Returns:
retTensor | List[Tensor]

A tensor, or list of tensors, each with a.ndim >= 2. Copies are made only if necessary.

Examples

>>> import mygrad as mg
>>> mg.atleast_2d(3.0)
Tensor([[3.]])
>>> x = mg.arange(3.0)
>>> mg.atleast_2d(x)
array([[0., 1., 2.]])
>>> mg.atleast_2d(x).base is x
True
>>> mg.atleast_2d(1, [1, 2], [[1, 2]])
[Tensor([[1]]), Tensor([[1, 2]]), Tensor([[1, 2]])]

numpy.atleast_2d will dispatch appropriately on tensors.

>>> x = mg.tensor(2.)
>>> np.atleast_2d(x)
Tensor([[2.]])
>>> np.atleast_2d(x).backward()
>>> x.grad
array(1.)

If any argument to numpy.atleast_2d is a Tensor, mygrad.atleast_2d will be dispatched on all of the arguments.

>>> np.atleast_2d(x, 1.)
[Tensor([[2.]]), Tensor([[1.]])]