mygrad.atleast_1d#

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

Convert inputs to tensors with at least one dimension.

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

This docstring was adapted from numpy.atleast_1d.

Parameters:
tens1, tens2, …ArrayLike

One or more input tensors.

Returns:
retTensor | List[Tensor]

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

Examples

>>> import mygrad as mg
>>> mg.atleast_1d(1.0)
array([1.])
>>> x = mg.arange(9.0).reshape(3,3)
>>> np.atleast_1d(x)
Tensor([[0., 1., 2.],
        [3., 4., 5.],
        [6., 7., 8.]])
>>> mg.atleast_1d(x) is x
True
>>> mg.atleast_1d(1, [3, 4])
[Tensor([1]), Tensor([3, 4])]

numpy.atleast_1d will dispatch appropriately on tensors.

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

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

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