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.gradwill always returnNone).Defaults to
Falsefor float-type data. Defaults toTruefor integer-type data.Integer-type tensors must be constant.
- Returns:
- resTensor
The stacked tensor has one more dimension than the input arrays.
See also
concatenateJoin a sequence of tensors along an existing axis.
hstackStack tensors in sequence horizontally (column wise).
vstackStack tensors in sequence vertically (row wise).
dstackStack 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]])