# tf.contrib.lookup.IdTableWithHashBuckets

<br />

|---|
| [![](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#L829-L1033) |

String to Id table wrapper that assigns out-of-vocabulary keys to buckets.

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

    tf.contrib.lookup.IdTableWithHashBuckets(
        table, num_oov_buckets, hasher_spec=tf.contrib.lookup.FastHashSpec, name=None,
        key_dtype=None
    )

For example, if an instance of `IdTableWithHashBuckets` is initialized with a
string-to-id table that maps:

- `emerson -> 0`
- `lake -> 1`
- `palmer -> 2`

The `IdTableWithHashBuckets` object will performs the following mapping:

- `emerson -> 0`
- `lake -> 1`
- `palmer -> 2`
- `<other term> -> bucket_id`, where bucket_id will be between `3` and `3 + num_oov_buckets - 1`, calculated by: `hash(<term>) % num_oov_buckets + vocab_size`

If input_tensor is `["emerson", "lake", "palmer", "king", "crimson"]`,
the lookup result is `[0, 1, 2, 4, 7]`.

If `table` is None, only out-of-vocabulary buckets are used.

#### Example usage:

    num_oov_buckets = 3
    input_tensor = tf.constant(["emerson", "lake", "palmer", "king", "crimnson"])
    table = tf.IdTableWithHashBuckets(
        tf.StaticHashTable(tf.TextFileIdTableInitializer(filename),
                           default_value),
        num_oov_buckets)
    out = table.lookup(input_tensor).
    table.init.run()
    print(out.eval())

The hash function used for generating out-of-vocabulary buckets ID is handled
by `hasher_spec`.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `table` | Table that maps [`tf.string`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf#string) or [`tf.int64`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf#int64) keys to [`tf.int64`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf#int64) ids. |
| `num_oov_buckets` | Number of buckets to use for out-of-vocabulary keys. |
| `hasher_spec` | A `HasherSpec` to specify the hash function to use for assignation of out-of-vocabulary buckets (optional). |
| `name` | A name for the operation (optional). |
| `key_dtype` | Data type of keys passed to `lookup`. Defaults to `table.key_dtype` if `table` is specified, otherwise [`tf.string`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf#string). Must be string or integer, and must be castable to `table.key_dtype`. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | when `table` in None and `num_oov_buckets` is not positive. |
| `TypeError` | when `hasher_spec` is invalid. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `init` | DEPRECATED FUNCTION <br /> > [!WARNING] > **Warning:** THIS FUNCTION IS DEPRECATED. It will be removed after 2018-12-15. Instructions for updating: Use `initializer` instead. |
| `initializer` | <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

### `lookup`

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

    lookup(
        keys, name=None
    )

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

It assigns out-of-vocabulary keys to buckets based in their hashes.

<br />

<br />

<br />

| Args ||
|---|---|
| `keys` | Keys to look up. May be either a `SparseTensor` or dense `Tensor`. |
| `name` | Optional name for the op. |

<br />

<br />

<br />

<br />

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

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `TypeError` | when `keys` doesn't match the table key data type. |

<br />

### `size`

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

    size(
        name=None
    )

Compute the number of elements in this table.