FractionalPower

class torchhd.embeddings.FractionalPower(in_features: int, out_features: int, distribution: Distribution | Literal['sinc', 'gaussian'] = 'sinc', bandwidth: float = 1.0, vsa: Literal['HRR', 'FHRR'] = 'FHRR', device=None, dtype=None, requires_grad: bool = False)[source]

Class for fractional power encoding (FPE) method that forms hypervectors for given values, kernel shape, bandwidth, and dimensionality. Implements similarity-preserving hypervectors approximating desired kernel shape as described in Computing on Functions Using Randomized Vector Representations.

Parameters:
  • in_features (int) – the dimensionality of the input feature vector.

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

  • distribution (str, optional) – hyperparameter defining the shape of the kernel by specifying a particular probability distribution that is used to sample the base hypervector(s). Default: "sinc".

  • bandwidth (float, optional) – positive hyperparameter defining the width of the similarity kernel. Lower values lead to broader kernels while larger values lead to more narrow kernels. Default: 1.0.

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

  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None depends on 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.

Examples:

>>> embed = embeddings.FractionalPower(1, 6, "sinc", 1.0, "FHRR")
>>> embed(torch.arange(1, 4, 1.).view(-1, 1))
FHRRTensor([[-0.7181-0.6959j, -0.5269+0.8499j, -0.0848+0.9964j,  0.9720-0.2348j,
      0.6358+0.7718j,  0.4352+0.9003j],
    [ 0.0314+0.9995j, -0.4447-0.8957j, -0.9856-0.1689j,  0.8897-0.4565j,
     -0.1915+0.9815j, -0.6212+0.7836j],
    [ 0.6730-0.7396j,  0.9956+0.0940j,  0.2519-0.9678j,  0.7576-0.6527j,
     -0.8793+0.4762j, -0.9759-0.2183j]])
basis()[source]

Return the values of the base hypervector(s)

forward(input: Tensor) Tensor[source]

Creates a fractional power encoding (FPE) for given values.

Parameters:

input (Tensor) – values for which FPE hypervectors should be generated. Either a vector or a batch of vectors.

Shapes:
  • Input: \((*, f)\) where f is the in_features and * is an optional batch dimension.

  • Output: \((*, d)\) where d is the out_features and * is an optional batch dimension.

reset_parameters() None[source]

Generate the angles for basis hypervector(s) to be used for encoding the data.