mindspore.Tensor.repeat

Tensor.repeat(*repeats)[source]

Copy the elements in each dimension of a Tensor based on the specified number of repetition times.

This function copies the tensor's data.

The shape of the output tensor can be described as follows, where \(n\) is the number of elements in repeats.

\[\begin{split}shape_{i} = \begin{cases} repeats_{i} * input.shape_{i} & \text{if } 0 \le i < input.{rank} \\ repeats_{i} & \text{if } input.{rank} \le i < n \\ \end{cases}\end{split}\]

Warning

This is an experimental API that is subject to change or deletion.

Note

If need to specify the number of repetition times for each element of a single dimension, please refer to mindspore.Tensor.repeat_interleave().

Parameters

*repeats (int) – Number of repetitions of self in each dimension. The value must be a non-negative number. 1 indicates that the dimension remains unchanged. The number of elements in repeats must be greater than or equals to the number of dimensions in self . When the number of dimensions of self is less than the number of elements of repeats , self is broadcasted to the number of dimensions with the same number of elements of repeats (as shown in the example).

Returns

Tensor, the new Tensor after the element is copied from the specified number of repetitions.

Raises
  • RuntimeError – If the number of elements of repeats is less than the number of dimensions of self . Or repeats has negative element.

  • RuntimeError – If the number of elements of repeats or the number of dimensions of self is larger than 8.

  • TypeError – If type of repeats is unsupported.

Supported Platforms:

Ascend

Examples

>>> from mindspore import Tensor
>>> a = Tensor([[0, 1, 2], [3, 4, 5]])
>>> print(a.repeat(3, 2))
[[0 1 2 0 1 2]
 [3 4 5 3 4 5]
 [0 1 2 0 1 2]
 [3 4 5 3 4 5]
 [0 1 2 0 1 2]
 [3 4 5 3 4 5]]
>>> print(a.repeat(2, 1, 3))  # a is treated as a shape [1, 2, 3]
[[[0 1 2 0 1 2 0 1 2]
  [3 4 5 3 4 5 3 4 5]]
 [[0 1 2 0 1 2 0 1 2]
  [3 4 5 3 4 5 3 4 5]]]
Tensor.repeat(repeats) Tensor[source]

Copy the elements in each dimension of a Tensor based on the specified number of repetition times.

This function copies the tensor's data.

Expect that a variable-length int parameter is changed to a parameter which type is list or tuple, other operations are the same as the overload with *repeats parameter.

The shape of the output tensor can be described as follows, where \(n\) is the number of elements in repeats.

\[\begin{split}shape_{i} = \begin{cases} repeats_{i} * input.shape_{i} & \text{if } 0 \le i < input.{rank} \\ repeats_{i} & \text{if } input.{rank} \le i < n \\ \end{cases}\end{split}\]

Warning

This is an experimental API that is subject to change or deletion.

Note

If need to specify the number of repetition times for each element of a single dimension, please refer to mindspore.Tensor.repeat_interleave().

Parameters

repeats (Union[tuple[int], list[int]]) – Number of repetitions of self in each dimension. The value must be a non-negative number. 1 indicates that the dimension remains unchanged. The number of elements in repeats must be greater than or equals to the number of dimensions in self . When the number of dimensions of self is less than the number of elements of repeats , self is broadcasted to the number of dimensions with the same number of elements of repeats (as shown in the example).

Returns

Tensor, the new Tensor after the element is copied from the specified number of repetitions.

Raises
  • RuntimeError – If the number of elements of repeats is less than the number of dimensions of self . Or repeats has negative element.

  • RuntimeError – If the number of elements of repeats or the number of dimensions of self is larger than 8.

  • TypeError – If type of repeats is unsupported.

See also

Supported Platforms:

Ascend

Examples

>>> from mindspore import Tensor
>>> a = Tensor([[0, 1, 2], [3, 4, 5]])
>>> print(a.repeat([3, 2]))
[[0 1 2 0 1 2]
 [3 4 5 3 4 5]
 [0 1 2 0 1 2]
 [3 4 5 3 4 5]
 [0 1 2 0 1 2]
 [3 4 5 3 4 5]]
>>> print(a.repeat(repeats=(2, 1, 3)))  # a is treated as a shape [1, 2, 3]
[[[0 1 2 0 1 2 0 1 2]
  [3 4 5 3 4 5 3 4 5]]
 [[0 1 2 0 1 2 0 1 2]
  [3 4 5 3 4 5 3 4 5]]]