Density

class torchhd.embeddings.Density(in_features: int, out_features: int, vsa: Literal['BSC', 'MAP', 'HRR', 'FHRR', 'BSBC', 'VTB', 'MCR', 'CGR'] = 'MAP', low: float = 0.0, high: float = 1.0, device=None, dtype=None, requires_grad: bool = False, **kwargs)[source]

Performs the transformation of input data into hypervectors according to the intRVFL model.

See details in Density Encoding Enables Resource-Efficient Randomly Connected Neural Networks.

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

  • out_features (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 of the thermometer encoding represent. Default: 0.0

  • high (float, optional) – The upper bound of the real number range that the levels of the thermometer encoding represent. Default: 1.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.

Examples:

>>> embed = embeddings.Density(6, 5)
>>> x = torch.randn(3, 6)
>>> x
tensor([[ 0.5430,  1.0740,  0.7250, -0.3410, -0.1318,  1.3188],
        [ 0.4373,  1.2400, -0.2264,  1.2448, -0.2040, -0.7831],
        [ 1.7460, -0.7359, -1.3271,  0.4338, -0.2401,  1.6553]])
>>> embed(x)
MAPTensor([[ 2.,  2., -2., -2.,  0.],
           [ 4.,  0.,  6.,  4.,  0.],
           [ 4., -4., -2., -4., -4.]])
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.