mygrad.broadcast_to#

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

Broadcast a tensor to a new shape.

This docstring was adapted from numpy.broadcast_to.

Parameters:
aArrayLike

The tensor to be broadcasted

shape: Tuple[int, …]

The shape of the broadcasted tensor. This shape should be broadcast-compatible with the original shape.

constantbool, optional(default=False)

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

Returns:
mygrad.Tensor
Raises:
ValueError

If the array is not compatible with the new shape according to Numpy’s broadcasting rules.

Examples

>>> import mygrad as mg
>>> x = mg.Tensor([1, 2, 3])
>>> mg.broadcast_to(x, (3,3))
Tensor([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]])
>>> mg.broadcast_to(x, (4,4))
Traceback (most recent call last) -> Tensor:
...
ValueError: operands could not be broadcast together with remapped
shapes [original->remapped]: (3,) and requested shape (4,4)