Level

class torchhd.embeddings.Level(num_embeddings: int, embedding_dim: int, vsa: Literal['BSC', 'MAP', 'HRR', 'FHRR', 'BSBC', 'VTB', 'MCR', 'CGR'] = 'MAP', low: float = 0.0, high: float = 1.0, randomness: float = 0.0, requires_grad: bool = False, max_norm: float | None = None, norm_type: float = 2.0, scale_grad_by_freq: bool = False, sparse: bool = False, device=None, dtype=None, **kwargs)[source]

Embedding wrapper around level().

Class inherits from Embedding and supports the same keyword arguments.

Parameters:
  • num_embeddings (int) – the number of hypervectors to generate.

  • embedding_dim (int) – the dimensionality of the hypervectors.

  • vsa – (VSAOptions, optional): specifies the hypervector type to be instantiated. Default: "MAP".

  • low (float, optional) – The lower bound of the real number range that the levels represent. Default: 0.0

  • high (float, optional) – The upper bound of the real number range that the levels represent. Default: 1.0

  • randomness (float, optional) – r-value to interpolate between level-hypervectors at 0.0 and random-hypervectors at 1.0. Default: 0.0.

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

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

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Values outside the interval between low and high are clipped to the closed bound.

Examples:

>>> emb = embeddings.Level(4, 6)
>>> x = torch.rand(4)
>>> x
tensor([0.6444, 0.9286, 0.9225, 0.3675])
>>> emb(x)
MAPTensor([[ 1.,  1.,  1., -1.,  1.,  1.],
           [ 1.,  1., -1.,  1.,  1.,  1.],
           [ 1.,  1., -1.,  1.,  1.,  1.],
           [ 1.,  1.,  1., -1., -1.,  1.]])

>>> emb = embeddings.Level(4, 6, "BSC")
>>> x = torch.rand(4)
>>> x
tensor([0.1825, 0.1541, 0.4435, 0.1512])
>>> emb(x)
BSCTensor([[False,  True, False, False, False, False],
           [False,  True, False, False, False, False],
           [False,  True, False, False, False, False],
           [False,  True, False, False, False, False]])
forward(input: Tensor) Tensor[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.