tf.lookup.StaticHashTable
Stay organized with collections
Save and categorize content based on your preferences.
A generic hash table that is immutable once initialized.
Inherits From: TrackableResource
tf.lookup.StaticHashTable(
initializer, default_value, name=None, experimental_is_anonymous=False
)
Used in the notebooks
Example usage:
keys_tensor = tf.constant(['a', 'b', 'c'])
vals_tensor = tf.constant([7, 8, 9])
input_tensor = tf.constant(['a', 'f'])
table = tf.lookup.StaticHashTable(
tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor),
default_value=-1)
table.lookup(input_tensor).numpy()
array([ 7, -1], dtype=int32)
Or for more pythonic code:
table[input_tensor].numpy()
array([ 7, -1], dtype=int32)
The result of a lookup operation has the same shape as the argument:
input_tensor = tf.constant([['a', 'b'], ['c', 'd']])
table[input_tensor].numpy()
array([[ 7, 8],
[ 9, -1]], dtype=int32)
Args |
initializer
|
The table initializer to use. See HashTable kernel for
supported key and value types.
|
default_value
|
The value to use if a key is missing in the table.
|
name
|
A name for the operation (optional).
|
experimental_is_anonymous
|
Whether to use anonymous mode for the
table (default is False). In anonymous mode, the table
resource can only be accessed via a resource handle. It can't
be looked up by a name. When all resource handles pointing to
that resource are gone, the resource will be deleted
automatically.
|
Attributes |
default_value
|
The default value of the table.
|
key_dtype
|
The table key dtype.
|
name
|
The name of the table.
|
resource_handle
|
Returns the resource handle associated with this Resource.
|
value_dtype
|
The table value dtype.
|
Methods
export
View source
export(
name=None
)
Returns tensors of all keys and values in the table.
Args |
name
|
A name for the operation (optional).
|
Returns |
A pair of tensors with the first tensor containing all keys and the
second tensors containing all values in the table.
|
lookup
View source
lookup(
keys, name=None
)
Looks up keys
in a table, outputs the corresponding values.
The default_value
is used for keys not present in the table.
Args |
keys
|
Keys to look up. May be either a SparseTensor or dense Tensor .
|
name
|
A name for the operation (optional).
|
Returns |
A SparseTensor if keys are sparse, a RaggedTensor if keys are ragged,
otherwise a dense Tensor .
|
Raises |
TypeError
|
when keys or default_value doesn't match the table data
types.
|
size
View source
size(
name=None
)
Compute the number of elements in this table.
Args |
name
|
A name for the operation (optional).
|
Returns |
A scalar tensor containing the number of elements in this table.
|
__getitem__
View source
__getitem__(
keys
)
Looks up keys
in a table, outputs the corresponding values.
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.lookup.StaticHashTable\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/lookup_ops.py#L281-L413) |\n\nA generic hash table that is immutable once initialized.\n\nInherits From: [`TrackableResource`](../../tf/saved_model/experimental/TrackableResource) \n\n tf.lookup.StaticHashTable(\n initializer, default_value, name=None, experimental_is_anonymous=False\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Federated Learning for Text Generation](https://www.tensorflow.org/federated/tutorials/federated_learning_for_text_generation) - [Client-efficient large-model federated learning via \\`federated_select\\` and sparse aggregation](https://www.tensorflow.org/federated/tutorials/sparse_federated_learning) - [Feature Engineering using TFX Pipeline and TensorFlow Transform](https://www.tensorflow.org/tfx/tutorials/tfx/penguin_tft) - [Preprocessing data with TensorFlow Transform](https://www.tensorflow.org/tfx/tutorials/transform/census) - [](https://www.tensorflow.org/tfx/transform/get_started) |\n\n#### Example usage:\n\n keys_tensor = tf.constant(['a', 'b', 'c'])\n vals_tensor = tf.constant([7, 8, 9])\n input_tensor = tf.constant(['a', 'f'])\n table = tf.lookup.StaticHashTable(\n tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor),\n default_value=-1)\n table.lookup(input_tensor).numpy()\n array([ 7, -1], dtype=int32)\n\nOr for more pythonic code: \n\n table[input_tensor].numpy()\n array([ 7, -1], dtype=int32)\n\nThe result of a lookup operation has the same shape as the argument: \n\n input_tensor = tf.constant([['a', 'b'], ['c', 'd']])\n table[input_tensor].numpy()\n array([[ 7, 8],\n [ 9, -1]], dtype=int32)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `initializer` | The table initializer to use. See `HashTable` kernel for supported key and value types. |\n| `default_value` | The value to use if a key is missing in the table. |\n| `name` | A name for the operation (optional). |\n| `experimental_is_anonymous` | Whether to use anonymous mode for the table (default is False). In anonymous mode, the table resource can only be accessed via a resource handle. It can't be looked up by a name. When all resource handles pointing to that resource are gone, the resource will be deleted automatically. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ---------- ||\n|-------------------|------------------------------------------------------------|\n| `default_value` | The default value of the table. |\n| `key_dtype` | The table key dtype. |\n| `name` | The name of the table. |\n| `resource_handle` | Returns the resource handle associated with this Resource. |\n| `value_dtype` | The table value dtype. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `export`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/lookup_ops.py#L378-L394) \n\n export(\n name=None\n )\n\nReturns tensors of all keys and values in the table.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|--------|--------------------------------------|\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 pair of tensors with the first tensor containing all keys and the second tensors containing all values in the table. ||\n\n\u003cbr /\u003e\n\n### `lookup`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/lookup_ops.py#L229-L271) \n\n lookup(\n keys, name=None\n )\n\nLooks up `keys` in a table, outputs the corresponding values.\n\nThe `default_value` is used for keys not present in the table.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|--------|--------------------------------------------------------------------|\n| `keys` | Keys to look up. May be either a `SparseTensor` or dense `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 `SparseTensor` if keys are sparse, a `RaggedTensor` if keys are ragged, otherwise a dense `Tensor`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|-------------|--------------------------------------------------------------------|\n| `TypeError` | when `keys` or `default_value` doesn't match the table data types. |\n\n\u003cbr /\u003e\n\n### `size`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/lookup_ops.py#L217-L227) \n\n size(\n name=None\n )\n\nCompute the number of elements in this table.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|--------|--------------------------------------|\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 scalar tensor containing the number of elements in this table. ||\n\n\u003cbr /\u003e\n\n### `__getitem__`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/lookup_ops.py#L171-L173) \n\n __getitem__(\n keys\n )\n\nLooks up `keys` in a table, outputs the corresponding values."]]