# tf.lookup.experimental.DenseHashTable

<br />

|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) TensorFlow 2 version](https://www.tensorflow.org/api_docs/python/tf/lookup/experimental/DenseHashTable) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/lookup_ops.py#L1831-L2123) |

A generic mutable hash table implementation using tensors as backing store.

Inherits From: [`LookupInterface`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/contrib/lookup/LookupInterface)

#### View aliases


**Main aliases**

\`tf.contrib.lookup.MutableDenseHashTable\`
**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.lookup.experimental.DenseHashTable`](https://www.tensorflow.org/api_docs/python/tf/lookup/experimental/DenseHashTable), \`tf.compat.v2.lookup.experimental.DenseHashTable\`

<br />

    tf.lookup.experimental.DenseHashTable(
        key_dtype, value_dtype, default_value, empty_key, deleted_key,
        initial_num_buckets=None, name='MutableDenseHashTable', checkpoint=True
    )

Data can be inserted by calling the insert method and removed by calling the
remove method. It does not support initialization via the init method.

It uses "open addressing" with quadratic reprobing to resolve collisions.
Compared to `MutableHashTable` the insert, remove and lookup operations in a
`DenseHashTable` are typically faster, but memory usage can be higher.
However, `DenseHashTable` does not require additional memory for
temporary tensors created during checkpointing and restore operations.

#### Example usage:

    table = tf.lookup.DenseHashTable(key_dtype=tf.int64,
                                     value_dtype=tf.int64,
                                     default_value=-1,
                                     empty_key=0,
                                     deleted_key=-1)

    sess.run(table.insert(keys, values))
    out = table.lookup(query_keys)
    print(out.eval())

<br />

<br />

<br />

| ## Args ||
|---|---|
| `key_dtype` | the type of the key tensors. |
| `value_dtype` | the type of the value tensors. |
| `default_value` | The value to use if a key is missing in the table. |
| `empty_key` | the key to use to represent empty buckets internally. Must not be used in insert, remove or lookup operations. |
| `deleted_key` | the key to use to represent deleted buckets internally. Must not be used in insert, remove or lookup operations and be different from the empty_key. |
| `initial_num_buckets` | the initial number of buckets. |
| `name` | A name for the operation (optional). |
| `checkpoint` | if True, the contents of the table are saved to and restored from checkpoints. If `shared_name` is empty for a checkpointed table, it is shared using the table node name. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | If checkpoint is True and no name was specified. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `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

### `erase`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/lookup_ops.py#L2033-L2058)

    erase(
        keys, name=None
    )

Removes `keys` and its associated values from the table.

If a key is not present in the table, it is silently ignored.

<br />

<br />

<br />

| Args ||
|---|---|
| `keys` | Keys to remove. Can be a tensor of any shape. Must match the table's key type. |
| `name` | A name for the operation (optional). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The created Operation. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `TypeError` | when `keys` do not match the table data types. |

<br />

### `export`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/lookup_ops.py#L2078-L2094)

    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 />

### `insert`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/lookup_ops.py#L2014-L2031)

    insert(
        keys, values, name=None
    )

Associates `keys` with `values`.

<br />

<br />

<br />

| Args ||
|---|---|
| `keys` | Keys to insert. Can be a tensor of any shape. Must match the table's key type. |
| `values` | Values to be associated with keys. Must be a tensor of the same shape as `keys` and match the table's value type. |
| `name` | A name for the operation (optional). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The created Operation. ||

<br />

<br />

<br />

<br />

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

<br />

### `insert_or_assign`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/lookup_ops.py#L1987-L2012)

    insert_or_assign(
        keys, values, name=None
    )

Associates `keys` with `values`.

<br />

<br />

<br />

| Args ||
|---|---|
| `keys` | Keys to insert. Can be a tensor of any shape. Must match the table's key type. |
| `values` | Values to be associated with keys. Must be a tensor of the same shape as `keys` and match the table's value type. |
| `name` | A name for the operation (optional). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The created Operation. ||

<br />

<br />

<br />

<br />

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

<br />

### `lookup`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/lookup_ops.py#L1961-L1985)

    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. Can be a tensor of any shape. Must match the table's key_dtype. |
| `name` | A name for the operation (optional). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A tensor containing the values in the same shape as `keys` using the table's value type. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `TypeError` | when `keys` do not match the table data types. |

<br />

### `remove`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/lookup_ops.py#L2060-L2076)

    remove(
        keys, name=None
    )

Removes `keys` and its associated values from the table.

If a key is not present in the table, it is silently ignored.

<br />

<br />

<br />

| Args ||
|---|---|
| `keys` | Keys to remove. Can be a tensor of any shape. Must match the table's key type. |
| `name` | A name for the operation (optional). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The created Operation. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `TypeError` | when `keys` do not match the table data types. |

<br />

### `size`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/lookup_ops.py#L1948-L1959)

    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 />