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

基于超向量的树数据结构。

创建一个空树。

Parameters:
  • dimensions (int) – 树的维度。

  • vsa – (VSAOptions, 可选): 指定使用的超向量类型和操作 (默认: "MAP").

  • dtype (torch.dtype, 可选) – 返回张量的期望数据类型。默认值:如果 None,则使用全局默认值(参见 torch.set_default_tensor_type())。

  • device (torch.device, 可选) – 返回张量的期望设备。默认值:如果 None,则使用当前设备作为默认张量类型(参见 torch.set_default_tensor_type())。device 对于 CPU 张量类型将是 CPU,对于 CUDA 张量类型将是当前的 CUDA 设备。

示例:

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

向树中添加一个叶子。

Parameters:
  • value (VSATensor) – 表示边第一个节点的超向量。

  • path (List[str]) – 叶子的路径,使用 'l' 表示左,'r' 表示右。

示例:

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

清空树。

示例:

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

返回由路径指定的值,可以是子树或节点。

Parameters:

path (List[str]) – 想要获取的树或节点的路径。

示例:

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

返回树在相应级别的左分支。

示例:

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

返回树在相应级别的右分支。

示例:

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