ML Community Day is November 9! Join us for updates from TensorFlow, JAX, and more Learn more

tf.lookup.experimental.MutableHashTable

A generic mutable hash table implementation.

Inherits From: TrackableResource

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.

MutableHashTable requires additional memory during checkpointing and restore operations to create temporary key and value tensors.

Example usage:

table = tf.lookup.experimental.MutableHashTable(key_dtype=tf.string,
                                                value_dtype=tf.int64,
                                                default_value=-1)
keys_tensor = tf.constant(['a', 'b', 'c'])
vals_tensor = tf.constant([7, 8, 9], dtype=tf.int64)
input_tensor = tf.constant(['a', 'f'])
table.insert(keys_tensor, vals_tensor)
table.lookup(input_tensor).numpy()
array([ 7, -1])
table.remove(tf.constant(['c']))
table.lookup(keys_tensor).numpy()
array([ 7, 8, -1])
sorted(table.export()[0].numpy())
[b'a', b'b']
sorted(table.export()[1].numpy())
[7, 8]

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

ValueError If checkpoint is True and no name was specified.

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.

Methods

export

View source

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.

insert