View source on GitHub |
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
| Used in the tutorials |
|---|
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)
Methods
export
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
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
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__
__getitem__( keys ) Looks up keys in a table, outputs the corresponding values.
View source on GitHub