mygrad.stack#

mygrad.stack((t1, t2, ...), axis=0, out=None, *, constant=None)[source]#

Join a sequence of tensors along a new axis.

This docstring was adapted from that of numpy.stack [1]

Parameters
tensorsSequence[ArrayLike]

Each tensor must have the same shape.

axisOptional[int]

The axis in the result tensor along which the input tensors are stacked.

outOptional[Union[ndarray, Tensor]]

If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.

constantOptional[bool]

If True, this tensor is treated as a constant, and thus does not facilitate back propagation (i.e. constant.grad will always return None).

Defaults to False for float-type data. Defaults to True for integer-type data.

Integer-type tensors must be constant.

Returns
resTensor

The stacked tensor has one more dimension than the input arrays.

See also

concatenate

Join a sequence of tensors along an existing axis.

hstack

Stack tensors in sequence horizontally (column wise).

vstack

Stack tensors in sequence vertically (row wise).

dstack

Stack tensors in sequence depth wise (along third dimension).

References

1

Retrieved from https://numpy.org/doc/stable/reference/generated/numpy.stack.html

Examples

>>> import mygrad as mg
>>> a = mg.tensor([1, 2, 3])
>>> b = mg.tensor([-1, -2, -3])
>>> mg.stack((a, b))
Tensor([[ 1,  2,  3],
        [-1, -2, -3]])
>>> mg.stack((a, b), axis=-1)
Tensor([[1, -1],
        [2, -2],
        [3, -3]])