Equal partitioning
Divide a feature or sequence dimension into equal sections for parallel processing.
TensorFlow · Function
Splits a tensor along a chosen dimension and returns the pieces as a list. Use an integer for equal-sized outputs or provide explicit sizes for uneven outputs.
tf.split divides a tensor along one selected dimension and returns the resulting tensors as a list. You can request an equal number of pieces or provide the exact size of each piece; the latter changes only the selected dimension while preserving the others.
tf.split(value, num_or_size_splits, axis=0, num=None, name='split')| Name | Type | Description |
|---|---|---|
value | Tensor | The Tensor to split. |
num_or_size_splits | int, 1-D integer Tensor, or Python list | An integer number of equal splits, or a one-dimensional integer Tensor or Python list containing each output size. |
axis | int or scalar int32 Tensor | Dimension along which to split; defaults to 0 and must be in the valid rank range. |
num | int or None | Optional output count when it cannot be inferred from the size specification. |
name | str or None | Optional operation name. |
A list of tensors split from value along axis.
import tensorflow as tf
x = tf.reshape(tf.range(12), [2, 6])
parts = tf.split(x, num_or_size_splits=3, axis=1)
for part in parts:
print(part.numpy())This creates a 2×6 tensor and returns three tensors by dividing its second dimension into equal sections.
Divide a feature or sequence dimension into equal sections for parallel processing.
Extract variable-width slices when output sections have different sizes.
Separate a batch, channel, or spatial dimension before applying independent tensor operations.
tf.split returns a list of tensors created by partitioning value along axis. With an integer num_or_size_splits, it makes that many equal-sized outputs, so the selected dimension must divide evenly.
Follow-up: What changes when num_or_size_splits is a list? Each list entry specifies one output's size along axis, and the entries must sum to the input size there.
The axis argument selects the dimension to partition and defaults to 0. It may be an integer or scalar int32 tensor, and it must fall within the valid rank-based range.
Follow-up: Why can a negative axis be useful? It addresses dimensions from the end, making code less dependent on the tensor's leading-dimension layout.
num_or_size_splits can be a one-dimensional integer tensor or Python list of output sizes. The i-th output keeps value's shape except that its axis dimension becomes the i-th requested size.
Follow-up: When might num be needed? It can specify the output count when TensorFlow cannot infer it from the shape of size_splits.
tf.concattf.raw_ops.Splittf.sparse.splittf.unstack