mindspore.ops.broadcast_to
- mindspore.ops.broadcast_to(input, shape)[source]
Broadcasts input tensor to a given shape. The number of dimensions of input must be smaller than or equal to that of target. Suppose input shape is \((x_1, x_2, ..., x_m)\), target shape is \((*, y_1, y_2, ..., y_m)\), where \(*\) means any additional dimension. The broadcast rules are as follows:
Compare the value of \(x_m\) and \(y_m\), \(x_{m-1}\) and \(y_{m-1}\), …, \(x_1\) and \(y_1\) consecutively and decide whether these shapes are broadcastable and what the broadcast result is.
If the value pairs at a specific dim are equal, then that value goes right into that dim of output shape. With an input shape \((2, 3)\), target shape \((2, 3)\) , the inferred output shape is \((2, 3)\).
If the value pairs are unequal, there are three cases:
Case 1: If the value of the target shape in the dimension is -1, the value of the output shape in the dimension is the value of the corresponding input shape in the dimension. With an input shape \((3, 3)\), target shape \((-1, 3)\), the output shape is \((3, 3)\).
Case 2: If the value of target shape in the dimension is not -1, but the corresponding value in the input shape is 1, then the corresponding value of the output shape is that of the target shape. With an input shape \((1, 3)\), target shape \((8, 3)\), the output shape is \((8, 3)\).
Case 3: If the corresponding values of the two shapes do not satisfy the above cases, it means that broadcasting from the input shape to the target shape is not supported.
Having obtained the last m dims of the output shape, now focus on the first \(*\) dims. There are two cases:
If the first \(*\) dims of output shape do not have -1 in it, then fill the input shape with ones until their lengths are the same, and then refer to Case 2 mentioned above to calculate the output shape. With target shape \((3, 1, 4, 1, 5, 9)\), input shape \((1, 5, 9)\), the filled input shape will be \((1, 1, 1, 1, 5, 9)\) and thus the output shape is \((3, 1, 4, 1, 5, 9)\).
If the first \(*\) dims of output shape have -1 in it, it implies this -1 is corresponding to a non-existing dim so they are not broadcastable. With target shape \((3, -1, 4, 1, 5, 9)\), input shape \((1, 5, 9)\), instead of performing the dimension-filling process first, it raises an error directly.
- Supported Platforms:
AscendGPUCPU
Examples
>>> import mindspore >>> shape = (2, 3) >>> x = mindspore.tensor([1, 2, 3], mindspore.float32) >>> output = mindspore.ops.broadcast_to(x, shape) >>> print(output) [[1. 2. 3.] [1. 2. 3.]] >>> shape = (-1, 2) >>> x = mindspore.tensor([[1], [2]], mindspore.float32) >>> output = mindspore.ops.broadcast_to(x, shape) >>> print(output) [[1. 1.] [2. 2.]]