tf.lookup.experimental.DenseHashTable

A mutable hash table with faster lookups and higher memory usage.

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.

Compared to MutableHashTable, DenseHashTable offers generally faster insert, remove and lookup operations, in exchange for a higher overall memory footprint.

It uses "open addressing" with quadratic reprobing to resolve collisions. This requires specifying two keys in the key space, empty_key and deleted_key, that can never inserted into the table.

Unlike MutableHashTable, DenseHashTable does not require additional memory for temporary tensors created during checkpointing and restore operations.

Example usage:

table = tf.lookup.experimental.DenseHashTable(
    key_dtype=tf.string,
    value_dtype=tf.int64,
    default_value=-1,
    empty_key='',
    deleted_key='$')
keys = tf.constant(['a', 'b', 'c'])
values = tf.constant([0, 1, 2], dtype=tf.int64)
table.insert(keys, values)
table.remove(tf.constant(['c']))
table.lookup(tf.constant(['a', 'b', 'c','d'])).numpy()
array([ 0,  1, -1, -1])

key_dtypethe type of the key tensors.
value_dtypethe type of the value tensors.
default_valueThe value to use if a key is missing in the table.
empty_keythe key to use to represent empty buckets internally. Must not be used in insert, remove or lookup operations.
deleted_keythe 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_bucketsthe initial number of buckets (optional, default to 2^17=131072). Note that the default value is relatively large (~1MB), so if you are going to create many tables (likely the case when experimental_is_anonymous is True), you should set initial_num_buckets to a smaller value to reduce memory usage.
nameA name for the operation (optional).
checkpointif 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.
experimental_is_anonymousWhether 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.

ValueErrorIf checkpoint is True and no name was specified.

key_dtypeThe table key dtype.
nameThe name of the table.
resource_handleReturns the resource handle associated with this Resource.
value_dtypeThe table value dtype.

Methods

erase

View source

Removes keys and its associated values from the table.

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

Args
keysKeys to remove. Can be a tensor of any shape. Must match the table's key type.
nameA name for the operation (optional).

Returns
The created Operation.

Raises
TypeErrorwhen keys do not match the table data types.

export

View source

Returns tensors of all keys and values in the table.

Args
nameA 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

View source

Associates keys with values.

Args
keysKeys to insert. Can be a tensor of any shape. Must match the table's key type.
valuesValues to be associated with keys. Must be a tensor of the same shape as keys and match the table's value type.
nameA name for the operation (optional).

Returns
The created Operation.

Raises
TypeErrorwhen keys or values doesn't match the table data types.

insert_or_assign

View source

Associates keys with values.

Args
keysKeys to insert. Can be a tensor of any shape. Must match the table's key type.
valuesValues to be associated with keys. Must be a tensor of the same shape as keys and match the table's value type.
nameA name for the operation (optional).

Returns
The created Operation.

Raises
TypeErrorwhen keys or values doesn't match the table data types.

lookup

View source

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

The default_value is used for keys not present in the table.

Args
keysKeys to look up. Can be a tensor of any shape. Must match the table's key_dtype.
nameA name for the operation (optional).

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

Raises
TypeErrorwhen keys do not match the table data types.

remove

View source

Removes keys and its associated values from the table.

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

Args
keysKeys to remove. Can be a tensor of any shape. Must match the table's key type.
nameA name for the operation (optional).

Returns
The created Operation.

Raises
TypeErrorwhen keys do not match the table data types.

size

View source

Compute the number of elements in this table.

Args
nameA name for the operation (optional).

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

__getitem__

View source

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