@@ -1204,6 +1204,96 @@ def expand_as(x, y, name=None):
...
@@ -1204,6 +1204,96 @@ def expand_as(x, y, name=None):
returnout
returnout
defbroadcast_to(x,shape,name=None):
"""
Broadcast the input tensor to a given shape.
Both the number of dimensions of ``x`` and the number of elements in ``shape`` should be less than or equal to 6. The dimension to broadcast to must have a value 1.
Args:
x (Tensor): The input tensor, its data type is bool, float32, float64, int32 or int64.
shape (list|tuple|Tensor): The result shape after broadcasting. The data type is int32. If shape is a list or tuple, all its elements
should be integers or 1-D Tensors with the data type int32. If shape is a Tensor, it should be an 1-D Tensor with the data type int32.
The value -1 in shape means keeping the corresponding dimension unchanged.
name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name` .
Returns:
N-D Tensor: A Tensor with the given shape. The data type is the same as ``x``.
Examples:
.. code-block:: python
import paddle
data = paddle.to_tensor([1, 2, 3], dtype='int32')
out = paddle.broadcast_to(data, shape=[2, 3])
print(out)
# [[1, 2, 3], [1, 2, 3]]
"""
ifin_dygraph_mode():
returncore.ops.expand_v2(x,'shape',shape)
ifisinstance(shape,Variable):
assertlen(shape.shape)==1,('shape must be an 1-D Tensor.')
else:
foreleminshape:
ifisinstance(elem,Variable):
assertlen(elem.shape)==1,(
'Elements in shape must be 1-D Tensors or integers.')
else:
ifsix.PY3:
type_tuple=(int,np.int32,np.int64)
elifsix.PY2:
type_tuple=(int,long,np.int32,np.int64)
assertisinstance(elem,type_tuple),(
'Elements in shape must be 1-D Tensors or integers.')