<br />

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

Table initializers given `keys` and `values` tensors.

#### View aliases


**Compat aliases for migration**

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

[`tf.compat.v1.lookup.KeyValueTensorInitializer`](https://www.tensorflow.org/api_docs/python/tf/lookup/KeyValueTensorInitializer)

<br />

    tf.lookup.KeyValueTensorInitializer(
        keys, values, key_dtype=None, value_dtype=None, name=None
    )

### Used in the notebooks

| Used in the guide | Used in the tutorials |
|---|---|
| - [BERT Preprocessing with TF Text](https://www.tensorflow.org/text/guide/bert_preprocessing_guide) - [Subword tokenizers](https://www.tensorflow.org/text/guide/subwords_tokenizer) | - [Load text](https://www.tensorflow.org/tutorials/load_data/text) - [Federated Learning for Text Generation](https://www.tensorflow.org/federated/tutorials/federated_learning_for_text_generation) - [Apache ORC Reader](https://www.tensorflow.org/io/tutorials/orc) - [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) |

    keys_tensor = tf.constant(['a', 'b', 'c'])
    vals_tensor = tf.constant([7, 8, 9])
    input_tensor = tf.constant(['a', 'f'])
    init = tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor)
    table = tf.lookup.StaticHashTable(
        init,
        default_value=-1)
    table.lookup(input_tensor).numpy()
    array([ 7, -1], dtype=int32)

<br />

<br />

<br />

| ## Args ||
|---|---|
| `keys` | The tensor for the keys. |
| `values` | The tensor for the values. |
| `key_dtype` | The `keys` data type. Used when `keys` is a python array. |
| `value_dtype` | The `values` data type. Used when `values` is a python array. |
| `name` | A name for the operation (optional). |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `key_dtype` | The expected table key dtype. |
| `value_dtype` | The expected table value dtype. |

<br />

## Methods

### `initialize`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/ops/lookup_ops.py#L567-L586)

    initialize(
        table
    )

Initializes the given `table` with `keys` and `values` tensors.

<br />

<br />

<br />

| Args ||
|---|---|
| `table` | The table to initialize. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The operation that initializes the table. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `TypeError` | when the keys and values data types do not match the table key and value data types. |

<br />