mindspore.ops.split

查看源文件
mindspore.ops.split(tensor, split_size_or_sections, axis=0)[源代码]

沿指定轴将输入tensor切分成多个子tensor。

参数:
  • tensor (Tensor) - 输入tensor。

  • split_size_or_sections (Union[int, tuple(int), list(int)]) - 切分后子tensor的大小。

  • axis (int,可选) - 指定分割轴,默认 0

说明

  • 如果 split_size_or_sections 是int类型, tensor 将被均匀切分成块,每块大小为 split_size_or_sections ,若 tensor.shape[axis] 不能被 split_size_or_sections 整除,则最后一块大小为余数;

  • 如果 split_size_or_sections 是tuple或list类型,tensor 将沿 axis 轴被切分成 len(split_size_or_sections) 块,大小为 split_size_or_sections

返回:

一个由tensor组成的tuple。

支持平台:

Ascend GPU CPU

样例:

>>> import mindspore
>>> # case1: `split_size_or_sections` is an int type
>>> input_x = mindspore.ops.arange(10).astype("float32")
>>> output = mindspore.ops.split(tensor=input_x, split_size_or_sections=3)
>>> print(output)
(Tensor(shape=[3], dtype=Float32, value=[0.00000000e+00, 1.00000000e+00, 2.00000000e+00]),
 Tensor(shape=[3], dtype=Float32, value=[3.00000000e+00, 4.00000000e+00, 5.00000000e+00]),
 Tensor(shape=[3], dtype=Float32, value=[6.00000000e+00, 7.00000000e+00, 8.00000000e+00]),
 Tensor(shape=[1], dtype=Float32, value=[9.00000000e+00]))
>>> # case2: `split_size_or_sections` is a list type
>>> output = mindspore.ops.split(tensor=input_x, split_size_or_sections=[3, 3, 4])
>>> print(output)
(Tensor(shape=[3], dtype=Float32, value=[0.00000000e+00, 1.00000000e+00, 2.00000000e+00]),
 Tensor(shape=[3], dtype=Float32, value=[3.00000000e+00, 4.00000000e+00, 5.00000000e+00]),
 Tensor(shape=[4], dtype=Float32, value=[6.00000000e+00, 7.00000000e+00, 8.00000000e+00, 9.00000000e+00]))