mygrad.Tensor.flatten#

Tensor.flatten(*, constant: bool | None = None) Tensor[source]#

Return a copy of the tensor collapsed into one dimension.

This docstring was adapted from numpy.ndarray.flatten.

Parameters:
constantbool, optional(default=False)

If True, the returned tensor is a constant (it does not back-propagate a gradient)

Returns:
mygrad.Tensor

A copy of the input tensor, flattened to one dimension.

Notes

To return a flattened view of the tensor, use x.reshape(-1).

Examples

>>> import mygrad as mg
>>> x = mg.Tensor([[1, 2],
...                [3, 4]])
>>> x.flatten()
Tensor([1, 2, 3, 4])