HashTable

class torchhd.structures.HashTable(dimensions: int, vsa: Literal['BSC', 'MAP', 'HRR', 'FHRR', 'BSBC', 'VTB', 'MCR', 'CGR'] = 'MAP', *, device=None, dtype=None)[source]
class torchhd.structures.HashTable(input: VSATensor, *, size=0)

Hypervector hash table data structure.

Creates an empty hash table of dim dimensions or a hash table from an input tensor.

Parameters:
  • dimensions (int) – number of dimensions of the hash table.

  • vsa – (VSAOptions, optional): specifies the hypervector type and operations used (Default: "MAP").

  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()).

  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • input (VSATensor) – tensor representing a hash table.

  • size (int, optional) – the size of the hash table provided as input. Default: 0.

Examples:

>>> H = structures.HashTable(10000)

>>> x = functional.random(3, 10000)
>>> M = structures.HashTable(x)
__getitem__(key: VSATensor) VSATensor[source]

Gets the approximate value from the key in the hash table.

Parameters:

key (VSATensor) – Hypervector used as key for looking its value.

Examples:

>>> H[letters_hv[0]]
tensor([ 1., -1.,  1.,  ..., -1.,  1., -1.])
__len__() int[source]

Returns the size of the hash table.

Examples:

>>> len(H)
0
add(key: VSATensor, value: VSATensor) None[source]

Adds one (key, value) pair to the hash table.

Parameters:
  • key (VSATensor) – Hypervector used as key for adding the key-value pair.

  • value (VSATensor) – Tensor to be added as value to the hash table

Examples:

>>> letters = list(string.ascii_lowercase)
>>> letters_hv = torchhd.random(len(letters), 10000)
>>> values = functional.random(2, 10000)
>>> H.add(letters_hv[0], values[0])
clear() None[source]

Empties the hash table.

Examples:

>>> H.clear()
classmethod from_tensors(keys: VSATensor, values: VSATensor)[source]

Creates a hash table from a set of keys and values hypervectors.

See: hash_table().

Parameters:
  • keys (VSATensor) – Set of key hypervectors to add in the hash table.

  • values (VSATensor) – Set of value hypervectors to add in the hash table.

Examples::
>>> letters_hv = torchhd.random(len(letters), 10000)
>>> values = torchhd.random(len(letters), 10000)
>>> H = structures.HashTable.from_tensors(letters_hv, values)
get(key: VSATensor) VSATensor[source]

Gets the approximate value from the key in the hash table.

Parameters:

key (VSATensor) – Hypervector used as key for looking its value.

Examples:

>>> H.get(letters_hv[0])
tensor([ 1., -1.,  1.,  ..., -1.,  1., -1.])
remove(key: VSATensor, value: VSATensor) None[source]

Removes one (key, value) pair from the hash table.

Parameters:
  • key (VSATensor) – Hypervector used as key for removing the key-value pair.

  • value (VSATensor) – Tensor to be removed linked to the key

Examples:

>>> H.remove(letters_hv[0], values[0])
replace(key: VSATensor, old: VSATensor, new: VSATensor) None[source]

Replace the value from key-value pair in the hash table.

Parameters:
  • key (VSATensor) – Hypervector used as key for looking its value.

  • old (VSATensor) – Old value hypervector.

  • new (VSATensor) – New value hypervector.

Examples:

>>> H.replace(letters_hv[0], values[0], values[1])