mindspore.ops.BroadcastTo

View Source On Gitee
class mindspore.ops.BroadcastTo(shape)[source]

Broadcasts input tensor to a given shape.

Refer to mindspore.ops.broadcast_to() for more details.

Parameters

shape (tuple) – The target shape to broadcast. Can be fully specified, or have -1 in one position where it will be substituted by the input tensor’s shape in that position, see example.

Inputs:
  • input_x (Tensor) - The input tensor of any dimension.

Outputs:

Tensor, with the given shape and the same data type as input_x.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> shape = (2, 3)
>>> x = Tensor(np.array([1, 2, 3]).astype(np.float32))
>>> output = ops.BroadcastTo(shape=shape)(x)
>>> print(output)
[[1. 2. 3.]
 [1. 2. 3.]]
>>>
>>> shape = (-1, 2)
>>> x = Tensor(np.array([[1], [2]]).astype(np.float32))
>>> output = ops.BroadcastTo(shape=shape)(x)
>>> print(output)
[[1. 1.]
 [2. 2.]]