mygrad.concatenate#

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

Join a sequence of tensors along an existing axis.

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

Parameters:
tensorsSequence[ArrayLike]

The tensors must have the same shape, except in the dimension corresponding to axis (the first, by default).

axisOptional[int]

The axis along which the tensors will be joined. If axis is None, tensors are flattened before use. Default is 0.

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.

dtypeOptional[DTypeLikeReals]

If provided, the destination array will have this dtype. Cannot be provided together with out.

Requires numpy 1.20 or higher.

Returns:
resTensor

The concatenated tensor.

See also

stack

Stack a sequence of tensors along a new 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

Examples

>>> import mygrad as mg
>>> a = mg.tensor([[1, 2], [3, 4]])
>>> b = mg.tensor([[5, 6]])
>>> mg.concatenate((a, b), axis=0)
Tensor([[1, 2],
       [3, 4],
       [5, 6]])
>>> mg.concatenate((a, b.T), axis=1)
Tensor([[1, 2, 5],
       [3, 4, 6]])
>>> mg.concatenate((a, b), axis=None)
Tensor([1, 2, 3, 4, 5, 6])