tf.split
Stay organized with collections
Save and categorize content based on your preferences.
Splits a tensor value
into a list of sub tensors.
tf.split(
value, num_or_size_splits, axis=0, num=None, name='split'
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
See also tf.unstack
.
If num_or_size_splits
is an int
, then it splits value
along the
dimension axis
into num_or_size_splits
smaller tensors. This requires that
value.shape[axis]
is divisible by num_or_size_splits
.
If num_or_size_splits
is a 1-D Tensor (or list), then value
is split into
len(num_or_size_splits)
elements. The shape of the i
-th
element has the same size as the value
except along dimension axis
where
the size is num_or_size_splits[i]
.
For example:
x = tf.Variable(tf.random.uniform([5, 30], -1, 1))
# Split `x` into 3 tensors along dimension 1
s0, s1, s2 = tf.split(x, num_or_size_splits=3, axis=1)
tf.shape(s0).numpy()
array([ 5, 10], dtype=int32)
# Split `x` into 3 tensors with sizes [4, 15, 11] along dimension 1
split0, split1, split2 = tf.split(x, [4, 15, 11], 1)
tf.shape(split0).numpy()
array([5, 4], dtype=int32)
tf.shape(split1).numpy()
array([ 5, 15], dtype=int32)
tf.shape(split2).numpy()
array([ 5, 11], dtype=int32)
Args |
value
|
The Tensor to split.
|
num_or_size_splits
|
Either an int indicating the number of splits
along axis or a 1-D integer Tensor or Python list containing the sizes
of each output tensor along axis . If an int , then it must evenly
divide value.shape[axis] ; otherwise the sum of sizes along the split
axis must match that of the value .
|
axis
|
An int or scalar int32 Tensor . The dimension along which
to split. Must be in the range [-rank(value), rank(value)) . Defaults to
0.
|
num
|
Optional, an int , used to specify the number of outputs when it
cannot be inferred from the shape of size_splits .
|
name
|
A name for the operation (optional).
|
Returns |
if num_or_size_splits is an int returns a list of
num_or_size_splits Tensor objects; if num_or_size_splits is a 1-D
list or 1-D Tensor returns num_or_size_splits.get_shape[0]
Tensor objects resulting from splitting value .
|
Raises |
ValueError
|
If num is unspecified and cannot be inferred.
|
ValueError
|
If num_or_size_splits is a scalar Tensor .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.split\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/array_ops.py#L1710-L1789) |\n\nSplits a tensor `value` into a list of sub tensors.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.split`](https://www.tensorflow.org/api_docs/python/tf/split)\n\n\u003cbr /\u003e\n\n tf.split(\n value, num_or_size_splits, axis=0, num=None, name='split'\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Distributed training with Core APIs and DTensor](https://www.tensorflow.org/guide/core/distribution) - [Understanding masking \\& padding](https://www.tensorflow.org/guide/keras/understanding_masking_and_padding) | - [Using DTensors with Keras](https://www.tensorflow.org/tutorials/distribute/dtensor_keras_tutorial) - [Distributed training with DTensors](https://www.tensorflow.org/tutorials/distribute/dtensor_ml_tutorial) - [Convolutional Variational Autoencoder](https://www.tensorflow.org/tutorials/generative/cvae) - [MoViNet for streaming action recognition](https://www.tensorflow.org/hub/tutorials/movinet) - [Bayesian Modeling with Joint Distribution](https://www.tensorflow.org/probability/examples/Modeling_with_JointDistribution) |\n\nSee also [`tf.unstack`](../tf/unstack).\n\nIf `num_or_size_splits` is an `int`, then it splits `value` along the\ndimension `axis` into `num_or_size_splits` smaller tensors. This requires that\n`value.shape[axis]` is divisible by `num_or_size_splits`.\n\nIf `num_or_size_splits` is a 1-D Tensor (or list), then `value` is split into\n`len(num_or_size_splits)` elements. The shape of the `i`-th\nelement has the same size as the `value` except along dimension `axis` where\nthe size is `num_or_size_splits[i]`.\n\n#### For example:\n\n x = tf.Variable(tf.random.uniform([5, 30], -1, 1))\n\n # Split `x` into 3 tensors along dimension 1\n s0, s1, s2 = tf.split(x, num_or_size_splits=3, axis=1)\n tf.shape(s0).numpy()\n array([ 5, 10], dtype=int32)\n\n # Split `x` into 3 tensors with sizes [4, 15, 11] along dimension 1\n split0, split1, split2 = tf.split(x, [4, 15, 11], 1)\n tf.shape(split0).numpy()\n array([5, 4], dtype=int32)\n tf.shape(split1).numpy()\n array([ 5, 15], dtype=int32)\n tf.shape(split2).numpy()\n array([ 5, 11], dtype=int32)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `value` | The `Tensor` to split. |\n| `num_or_size_splits` | Either an `int` indicating the number of splits along `axis` or a 1-D integer `Tensor` or Python list containing the sizes of each output tensor along `axis`. If an `int`, then it must evenly divide `value.shape[axis]`; otherwise the sum of sizes along the split axis must match that of the `value`. |\n| `axis` | An `int` or scalar `int32` `Tensor`. The dimension along which to split. Must be in the range `[-rank(value), rank(value))`. Defaults to 0. |\n| `num` | Optional, an `int`, used to specify the number of outputs when it cannot be inferred from the shape of `size_splits`. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| if `num_or_size_splits` is an `int` returns a list of `num_or_size_splits` `Tensor` objects; if `num_or_size_splits` is a 1-D list or 1-D `Tensor` returns `num_or_size_splits.get_shape[0]` `Tensor` objects resulting from splitting `value`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-------------------------------------------------|\n| `ValueError` | If `num` is unspecified and cannot be inferred. |\n| `ValueError` | If `num_or_size_splits` is a scalar `Tensor`. |\n\n\u003cbr /\u003e"]]