repeat_interleave
- paddle. repeat_interleave ( x: Tensor, repeats: int | Tensor, axis: int | None = None, name: str | None = None, *, output_size: int | None = None ) Tensor [source]
-
Returns a new tensor which repeats the
xtensor along dimensionaxisusing the entries inrepeatswhich is a int or a Tensor.The image illustrates a typical case of the repeat_interleave operation. Given a tensor
[[1, 2, 3], [4, 5, 6]], with the repeat countsrepeats = [3, 2, 1]and parameteraxis = 1, it means that the elements in the 1st column are repeated 3 times, the 2nd column is repeated 2 times, and the 3rd column is repeated 1 time.The final output is a 2D tensor:
[[1, 1, 1, 2, 2, 3], [4, 4, 4, 5, 5, 6]].
Note
Alias Support: The parameter name
inputcan be used as an alias forx, anddimcan be used as an alias foraxis. For example,repeat_interleave(input=tensor_x, dim=1, ...)is equivalent torepeat_interleave(x=tensor_x, axis=1, ...).- Parameters
-
x (Tensor) – The input Tensor to be operated. The data of
xcan be one of float32, float64, int32, int64. alias:input.repeats (Tensor|int) – The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.
axis (int|None, optional) – The dimension in which we manipulate. Default: None, the output tensor is flatten. alias:
dim.name (str|None, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to api_guide_Name.
output_size (int, optional) – Total output size for the given axis (e.g. sum of repeats). If given, it will avoid stream synchronization needed to calculate output shape of the tensor.
- Returns
-
Tensor, A Tensor with same data type as
x.
Examples
>>> import paddle >>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]]) >>> repeats = paddle.to_tensor([3, 2, 1], dtype='int32') >>> out = paddle.repeat_interleave(x, repeats, 1) >>> print(out) Tensor(shape=[2, 6], dtype=int64, place=Place(cpu), stop_gradient=True, [[1, 1, 1, 2, 2, 3], [4, 4, 4, 5, 5, 6]]) >>> out = paddle.repeat_interleave(x, 2, 0) >>> print(out) Tensor(shape=[4, 3], dtype=int64, place=Place(cpu), stop_gradient=True, [[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6]]) >>> out = paddle.repeat_interleave(x, 2, None) >>> print(out) Tensor(shape=[12], dtype=int64, place=Place(cpu), stop_gradient=True, [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6])
