Tree

class torchhd.structures.Tree(dimensions, vsa: Literal['BSC', 'MAP', 'HRR', 'FHRR', 'BSBC', 'VTB', 'MCR', 'CGR'] = 'MAP', device=None, dtype=None)[source]

Hypervector-based tree data structure.

Creates an empty tree.

Parameters:
  • dimensions (int) – dimensions of the tree.

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

Examples:

>>> T = structures.Tree(10000)
add_leaf(value: VSATensor, path: List[str]) None[source]

Adds a leaf to the tree.

Parameters:
  • value (VSATensor) – Hypervector representing the first node of the edge.

  • path (List[str]) – Path of the leaf, using ‘l’ to refer as left and right ‘r’.

Examples:

>>> letters = list(string.ascii_lowercase)
>>> letters_hv = torchhd.random(len(letters), 10000)
>>> T.add_leaf(letters_hv[0], ['l','l'])
clear() None[source]

Empties the tree.

Examples:

>>> T.clear()
get_leaf(path: List[str]) VSATensor[source]

Returns the value, either subtree or node given by the path.

Parameters:

path (List[str]) – Path of the tree or node wanted to get.

Examples:

>>> T.get_leaf(['l','l'])
tensor([ 1., -1.,  1.,  ...,  1.,  1., -1.])
property left: VSATensor

Returns the left branch of the tree at the corresponding level.

Examples:

>>> T.left
tensor([ 1., -1.,  1.,  ...,  1.,  1., -1.])
property right: VSATensor

Returns the right branch of the tree at the corresponding level.

Examples:

>>> T.right
tensor([-1., -1.,  1.,  ...,  1., -1., -1.])