mygrad.reshape#

mygrad.reshape(a: ArrayLike, newshape: int | Shape, *, constant: bool | None = None) Tensor[source]#

Returns a tensor with a new shape, without changing its data.

This docstring was adapted from numpy.reshape

Parameters:
aArrayLike

The tensor to be reshaped

newshapeUnion[int, Tuple[int, …]]

The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D tensor of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the tensor and remaining dimensions.

constantbool, optional(default=False)

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

Returns:
mygrad.Tensor

a with its shape changed permuted. A new tensor is returned.

Notes

reshape utilizes C-ordering, meaning that it reads & writes elements using C-like index ordering; the last axis index changing fastest, and, proceeding in reverse order, the first axis index changing slowest.

Examples

>>> import mygrad as mg
>>> a = mg.Tensor([[1,2,3], [4,5,6]])
>>> mg.reshape(a, 6)
Tensor([1, 2, 3, 4, 5, 6])
>>> mg.reshape(a, (3,-1))   # the unspecified value is inferred to be 2
Tensor([[1, 2],
        [3, 4],
        [5, 6]])