# tf.compat.v1.lookup.StaticHashTable

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/lookup_ops.py#L410-L465) |

A generic hash table that is immutable once initialized.

Inherits From: [`StaticHashTable`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/lookup/StaticHashTable), [`TrackableResource`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/saved_model/experimental/TrackableResource)

    tf.compat.v1.lookup.StaticHashTable(
        initializer, default_value, name=None, experimental_is_anonymous=False
    )

When running in graph mode, you must evaluate the tensor returned by
`tf.tables_initializer()` before evaluating the tensor returned by
this class's `lookup()` method. Example usage in graph mode:

    keys_tensor = tf.constant([1, 2])
    vals_tensor = tf.constant([3, 4])
    input_tensor = tf.constant([1, 5])
    table = tf.lookup.StaticHashTable(
        tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor), -1)
    out = table.lookup(input_tensor)
    with tf.Session() as sess:
        sess.run(tf.tables_initializer())
        print(sess.run(out))

Note that in graph mode if you set `experimental_is_anonymous` to
`True`, you should only call `Session.run` once, otherwise each
`Session.run` will create (and destroy) a new table unrelated to
each other, leading to errors such as "Table not initialized".
You can do so like this:

    keys_tensor = tf.constant([1, 2])
    vals_tensor = tf.constant([3, 4])
    input_tensor = tf.constant([1, 5])
    table = tf.lookup.StaticHashTable(
        tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor), -1,
        experimental_is_anonymous=True)
    with tf.control_dependencies([tf.tables_initializer()]):
      out = table.lookup(input_tensor)
    with tf.Session() as sess:
      print(sess.run(out))

In eager mode, no special code is needed to initialize the table.
Example usage in eager mode:

    tf.enable_eager_execution()
    keys_tensor = tf.constant([1, 2])
    vals_tensor = tf.constant([3, 4])
    input_tensor = tf.constant([1, 5])
    table = tf.lookup.StaticHashTable(
        tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor), -1)
    print(table.lookup(input_tensor))

<br />

<br />

<br />

| ## 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. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `default_value` | The default value of the table. |
| `initializer` | <br /> <br /> |
| `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. |

<br />

## Methods

### `export`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/lookup_ops.py#L372-L388)

    export(
        name=None
    )

Returns tensors of all keys and values in the table.

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | A name for the operation (optional). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A pair of tensors with the first tensor containing all keys and the second tensors containing all values in the table. ||

<br />

### `lookup`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/lookup_ops.py#L226-L265)

    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.

<br />

<br />

<br />

| Args ||
|---|---|
| `keys` | Keys to look up. May be either a `SparseTensor` or dense `Tensor`. |
| `name` | A name for the operation (optional). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A `SparseTensor` if keys are sparse, a `RaggedTensor` if keys are ragged, otherwise a dense `Tensor`. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `TypeError` | when `keys` or `default_value` doesn't match the table data types. |

<br />

### `size`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/lookup_ops.py#L214-L224)

    size(
        name=None
    )

Compute the number of elements in this table.

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | A name for the operation (optional). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A scalar tensor containing the number of elements in this table. ||

<br />

### `__getitem__`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/lookup_ops.py#L168-L170)

    __getitem__(
        keys
    )

Looks up `keys` in a table, outputs the corresponding values.