tensor_split¶
将输入 Tensor 沿着轴 axis 分割成多个子 Tensor,允许进行不等长地分割。
参数¶
x (Tensor) - 输入变量,数据类型为 bool、bfloat16、float16、float32、float64、uint8、int8、int32、int64 的多维 Tensor,其维度必须大于 0。
num_or_indices (int|list|tuple) - 如果
num_or_indices是一个整数n,则x沿axis拆分为n部分。如果x可被n整除,则每个部分都是x.shape[axis]/n。如果x不能被n整除,则第一个int(x.shape[axis]%n)分割大小将为int(x.shape[axis]/n)+1,其余部分的大小将是int(x.shape[axis]/n)。如果num_or_indices是整数索引的列表或元组,则在每个索引处沿axis分割x。例如,num_or_indices=[2, 4]在axis=0时将沿轴 0 将x拆分为x[:2]、x[2:4]和x[4:]。axis (int|Tensor,可选) - 整数或者形状为[]的 0-D Tensor,数据类型为 int32 或 int64。表示需要分割的维度。如果
axis < 0,则划分的维度为rank(x) + axis。默认值为 0。name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。
返回¶
list[Tensor],分割后的 Tensor 列表。
代码示例¶
>>> import paddle
>>> # x is a Tensor of shape [8]
>>> # evenly split
>>> x = paddle.rand([8])
>>> out0, out1 = paddle.tensor_split(x, num_or_indices=2)
>>> print(out0.shape)
[4]
>>> print(out1.shape)
[4]
>>> # not evenly split
>>> out0, out1, out2 = paddle.tensor_split(x, num_or_indices=3)
>>> print(out0.shape)
[3]
>>> print(out1.shape)
[3]
>>> print(out2.shape)
[2]
>>> # split with indices
>>> out0, out1, out2 = paddle.tensor_split(x, num_or_indices=[2, 3])
>>> print(out0.shape)
[2]
>>> print(out1.shape)
[1]
>>> print(out2.shape)
[5]
>>> # split along axis
>>> # x is a Tensor of shape [7, 8]
>>> x = paddle.rand([7, 8])
>>> out0, out1 = paddle.tensor_split(x, num_or_indices=2, axis=1)
>>> print(out0.shape)
[7, 4]
>>> print(out1.shape)
[7, 4]
>>> out0, out1, out2 = paddle.tensor_split(x, num_or_indices=[2, 3], axis=1)
>>> print(out0.shape)
[7, 2]
>>> print(out1.shape)
[7, 1]
>>> print(out2.shape)
[7, 5]