mindspore.Tensor.repeat
- mindspore.Tensor.repeat(*repeats)
对张量的每一维度按照指定的重复次数复制其中的元素。
此方法会拷贝此张量的数据。
输出张量的shape可以描述如下,其中
为 repeats 中的元素个数:警告
这是一个实验性API,后续可能修改或删除。
说明
如需对单个轴的每个元素单独指定重复次数,请参考
mindspore.Tensor.repeat_interleave()
。- 参数:
*repeats (int) - self 在每个维度上的重复次数,应当为非负数,且
1
表示在该维度上保持不变。 repeats 的元素个数应当不小于 self 的维度数。当 self 的维度数小于 repeats 的元素个数时, self 将被广播到与 repeats 元素个数相同的维度(如样例所示)。
- 返回:
Tensor,按照指定的重复次数拷贝元素后的新张量。
- 异常:
RuntimeError - repeats 的元素个数少于 self 的维度数,或 repeats 中有元素为负数。
RuntimeError - repeats 的元素个数或 self 的维度数超过8。
TypeError - repeats 参数类型错误。
- 支持平台:
Ascend
样例:
>>> 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]]]
- mindspore.Tensor.repeat(repeats)
对张量的每一维度按照指定的重复次数复制其中的元素。
此方法会拷贝此张量的数据。
除了由接受变长int型参数,变为接受1个list或tuple型参数外,其他操作与使用 *repeats 参数的重载保持一致。
输出张量的shape可以描述如下,其中
为 repeats 中的元素个数:警告
这是一个实验性API,后续可能修改或删除。
说明
如需对单个轴的每个元素单独指定重复次数,请参考
mindspore.Tensor.repeat_interleave()
。- 参数:
repeats (Union[tuple[int], list[int]]) - self 在每个维度上的重复次数,应当为非负数,且
1
表示在该维度上保持不变。 repeats 的元素个数应当不小于 self 的维度数。当 self 的维度数小于 repeats 的元素个数时, self 将被广播到与 repeats 元素个数相同的维度(如样例所示)。
- 返回:
Tensor,按照指定的重复次数拷贝元素后的新张量。
- 异常:
RuntimeError - repeats 的元素个数少于 self 的维度数,或 repeats 中有元素为负数。
RuntimeError - repeats 的元素个数或 self 的维度数超过8。
TypeError - repeats 参数类型错误。
参见
mindspore.Tensor.reshape()
:为Tensor指定新的shape,不更改Tensor的数据。mindspore.Tensor.resize()
:就地改变Tensor的shape和size。mindspore.Tensor.repeat_interleave()
:将一个Tensor指定轴上的每个元素按指定次数进行重复。mindspore.Tensor.tile()
:对一个Tensor在每个轴上重复指定次数,但对参数 repeats 的个数没有要求。
- 支持平台:
Ascend
样例:
>>> 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]]]