tf.slice
Stay organized with collections
Save and categorize content based on your preferences.
Extracts a slice from a tensor.
tf.slice(
input_, begin, size, name=None
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
See also tf.strided_slice
.
This operation extracts a slice of size size
from a tensor input_
starting
at the location specified by begin
. The slice size
is represented as a
tensor shape, where size[i]
is the number of elements of the 'i'th dimension
of input_
that you want to slice. The starting location (begin
) for the
slice is represented as an offset in each dimension of input_
. In other
words, begin[i]
is the offset into the i'th dimension of input_
that you
want to slice from.
Note that tf.Tensor.getitem
is typically a more pythonic way to
perform slices, as it allows you to write foo[3:7, :-2]
instead of
tf.slice(foo, [3, 0], [4, foo.get_shape()[1]-2])
.
begin
is zero-based; size
is one-based. If size[i]
is -1,
all remaining elements in dimension i are included in the
slice. In other words, this is equivalent to setting:
size[i] = input_.dim_size(i) - begin[i]
This operation requires that:
0 <= begin[i] <= begin[i] + size[i] <= Di for i in [0, n]
For example:
t = tf.constant([[[1, 1, 1], [2, 2, 2]],
[[3, 3, 3], [4, 4, 4]],
[[5, 5, 5], [6, 6, 6]]])
tf.slice(t, [1, 0, 0], [1, 1, 3]) # [[[3, 3, 3]]]
tf.slice(t, [1, 0, 0], [1, 2, 3]) # [[[3, 3, 3],
# [4, 4, 4]]]
tf.slice(t, [1, 0, 0], [2, 1, 3]) # [[[3, 3, 3]],
# [[5, 5, 5]]]
Args |
input_
|
A Tensor .
|
begin
|
An int32 or int64 Tensor .
|
size
|
An int32 or int64 Tensor .
|
name
|
A name for the operation (optional).
|
Returns |
A Tensor the same type as input_ .
|
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.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.slice\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#L938-L990) |\n\nExtracts a slice from a tensor.\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.slice`](https://www.tensorflow.org/api_docs/python/tf/slice)\n\n\u003cbr /\u003e\n\n tf.slice(\n input_, begin, size, name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Introduction to tensor slicing](https://www.tensorflow.org/guide/tensor_slicing) - [TFLite Authoring Tool](https://www.tensorflow.org/lite/guide/authoring) - [TensorFlow Lite Model Analyzer](https://www.tensorflow.org/lite/guide/model_analyzer) | - [Client-efficient large-model federated learning via \\`federated_select\\` and sparse aggregation](https://www.tensorflow.org/federated/tutorials/sparse_federated_learning) - [Graph regularization for sentiment classification using synthesized graphs](https://www.tensorflow.org/neural_structured_learning/tutorials/graph_keras_lstm_imdb) |\n\nSee also [`tf.strided_slice`](../tf/strided_slice).\n\nThis operation extracts a slice of size `size` from a tensor `input_` starting\nat the location specified by `begin`. The slice `size` is represented as a\ntensor shape, where `size[i]` is the number of elements of the 'i'th dimension\nof `input_` that you want to slice. The starting location (`begin`) for the\nslice is represented as an offset in each dimension of `input_`. In other\nwords, `begin[i]` is the offset into the i'th dimension of `input_` that you\nwant to slice from.\n\nNote that [`tf.Tensor.`**getitem**](../tf/Tensor#__getitem__) is typically a more pythonic way to\nperform slices, as it allows you to write `foo[3:7, :-2]` instead of\n`tf.slice(foo, [3, 0], [4, foo.get_shape()[1]-2])`.\n\n`begin` is zero-based; `size` is one-based. If `size[i]` is -1,\nall remaining elements in dimension i are included in the\nslice. In other words, this is equivalent to setting:\n\n`size[i] = input_.dim_size(i) - begin[i]`\n\nThis operation requires that:\n\n`0 \u003c= begin[i] \u003c= begin[i] + size[i] \u003c= Di for i in [0, n]`\n\n#### For example:\n\n t = tf.constant([[[1, 1, 1], [2, 2, 2]],\n [[3, 3, 3], [4, 4, 4]],\n [[5, 5, 5], [6, 6, 6]]])\n tf.slice(t, [1, 0, 0], [1, 1, 3]) # [[[3, 3, 3]]]\n tf.slice(t, [1, 0, 0], [1, 2, 3]) # [[[3, 3, 3],\n # [4, 4, 4]]]\n tf.slice(t, [1, 0, 0], [2, 1, 3]) # [[[3, 3, 3]],\n # [[5, 5, 5]]]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|--------------------------------------|\n| `input_` | A `Tensor`. |\n| `begin` | An `int32` or `int64` `Tensor`. |\n| `size` | An `int32` or `int64` `Tensor`. |\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| A `Tensor` the same type as `input_`. ||\n\n\u003cbr /\u003e"]]