tf.IndexedSlices
Stay organized with collections
Save and categorize content based on your preferences.
A sparse representation of a set of tensor slices at given indices.
tf.IndexedSlices(
values, indices, dense_shape=None
)
Used in the notebooks
This class is a simple wrapper for a pair of Tensor
objects:
values
: A Tensor
of any dtype with shape [D0, D1, ..., Dn]
.
indices
: A 1-D integer Tensor
with shape [D0]
.
An IndexedSlices
is typically used to represent a subset of a larger
tensor dense
of shape [LARGE0, D1, .. , DN]
where LARGE0 >> D0
.
The values in indices
are the indices in the first dimension of
the slices that have been extracted from the larger tensor.
The dense tensor dense
represented by an IndexedSlices
slices
has
dense[slices.indices[i], :, :, :, ...] = slices.values[i, :, :, :, ...]
The IndexedSlices
class is used principally in the definition of
gradients for operations that have sparse gradients
(e.g. tf.gather
).
v = tf.Variable([[0.,1, 2], [2, 3, 4], [4, 5, 6], [6, 7, 8]])
with tf.GradientTape() as tape:
r = tf.gather(v, [1,3])
index_slices = tape.gradient(r,v)
index_slices
<...IndexedSlices object ...>
index_slices.indices.numpy()
array([1, 3], dtype=int32)
index_slices.values.numpy()
array([[1., 1., 1.],
[1., 1., 1.]], dtype=float32)
Contrast this representation with
tf.sparse.SparseTensor
,
which uses multi-dimensional indices and scalar values.
Attributes |
dense_shape
|
A 1-D Tensor containing the shape of the corresponding dense tensor.
|
device
|
The name of the device on which values will be produced, or None .
|
dtype
|
The DType of elements in this tensor.
|
graph
|
The Graph that contains the values, indices, and shape tensors.
|
indices
|
A 1-D Tensor containing the indices of the slices.
|
name
|
The name of this IndexedSlices .
|
op
|
The Operation that produces values as an output.
|
shape
|
Gets the tf.TensorShape representing the shape of the dense tensor.
|
values
|
A Tensor containing the values of the slices.
|
Methods
consumers
View source
consumers()
__neg__
View source
__neg__()
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.IndexedSlices\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/indexed_slices.py#L54-L196) |\n\nA sparse representation of a set of tensor slices at given indices.\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.IndexedSlices`](https://www.tensorflow.org/api_docs/python/tf/IndexedSlices)\n\n\u003cbr /\u003e\n\n tf.IndexedSlices(\n values, indices, dense_shape=None\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Client-efficient large-model federated learning via \\`federated_select\\` and sparse aggregation](https://www.tensorflow.org/federated/tutorials/sparse_federated_learning) |\n\nThis class is a simple wrapper for a pair of `Tensor` objects:\n\n- `values`: A `Tensor` of any dtype with shape `[D0, D1, ..., Dn]`.\n- `indices`: A 1-D integer `Tensor` with shape `[D0]`.\n\nAn `IndexedSlices` is typically used to represent a subset of a larger\ntensor `dense` of shape `[LARGE0, D1, .. , DN]` where `LARGE0 \u003e\u003e D0`.\nThe values in `indices` are the indices in the first dimension of\nthe slices that have been extracted from the larger tensor.\n\nThe dense tensor `dense` represented by an `IndexedSlices` `slices` has \n\n dense[slices.indices[i], :, :, :, ...] = slices.values[i, :, :, :, ...]\n\nThe `IndexedSlices` class is used principally in the definition of\ngradients for operations that have sparse gradients\n(e.g. [`tf.gather`](../tf/gather)). \n\n v = tf.Variable([[0.,1, 2], [2, 3, 4], [4, 5, 6], [6, 7, 8]])\n with tf.GradientTape() as tape:\n r = tf.gather(v, [1,3])\n index_slices = tape.gradient(r,v)\n index_slices\n \u003c...IndexedSlices object ...\u003e\n index_slices.indices.numpy()\n array([1, 3], dtype=int32)\n index_slices.values.numpy()\n array([[1., 1., 1.],\n [1., 1., 1.]], dtype=float32)\n\nContrast this representation with\n[`tf.sparse.SparseTensor`](../tf/sparse/SparseTensor),\nwhich uses multi-dimensional indices and scalar values.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ---------- ||\n|---------------|--------------------------------------------------------------------------------------------|\n| `dense_shape` | A 1-D `Tensor` containing the shape of the corresponding dense tensor. |\n| `device` | The name of the device on which `values` will be produced, or `None`. |\n| `dtype` | The `DType` of elements in this tensor. |\n| `graph` | The `Graph` that contains the values, indices, and shape tensors. |\n| `indices` | A 1-D `Tensor` containing the indices of the slices. |\n| `name` | The name of this `IndexedSlices`. |\n| `op` | The `Operation` that produces `values` as an output. |\n| `shape` | Gets the [`tf.TensorShape`](../tf/TensorShape) representing the shape of the dense tensor. |\n| `values` | A `Tensor` containing the values of the slices. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `consumers`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/indexed_slices.py#L195-L196) \n\n consumers()\n\n### `__neg__`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/indexed_slices.py#L162-L163) \n\n __neg__()"]]