MAPTensor

class torchhd.MAPTensor[source]

Multiply Add Permute

Proposed in Multiplicative Binding, Representation Operators & Analogy, this model works with dense bipolar hypervectors with elements from \(\{-1,1\}\).

bind(other: MAPTensor) MAPTensor[source]

Bind the hypervector with other using element-wise multiplication.

This produces a hypervector dissimilar to both.

Binding is used to associate information, for instance, to assign values to variables.

Parameters:

other (MAPTensor) – other input hypervector

Shapes:
  • Self: \((*)\)

  • Other: \((*)\)

  • Output: \((*)\)

Examples:

>>> a, b = torchhd.MAPTensor.random(2, 10)
>>> a
tensor([ 1., -1.,  1.,  1., -1.,  1.,  1., -1.,  1.,  1.])
>>> b
tensor([-1.,  1., -1.,  1.,  1.,  1.,  1.,  1., -1., -1.])
>>> a.bind(b)
tensor([-1., -1., -1.,  1., -1.,  1.,  1., -1., -1., -1.])
bundle(other: MAPTensor) MAPTensor[source]

Bundle the hypervector with other using element-wise sum.

This produces a hypervector maximally similar to both.

The bundling operation is used to aggregate information into a single hypervector.

Parameters:

other (MAPTensor) – other input hypervector

Shapes:
  • Self: \((*)\)

  • Other: \((*)\)

  • Output: \((*)\)

Examples:

>>> a, b = torchhd.MAPTensor.random(2, 10)
>>> a
tensor([-1., -1., -1., -1., -1.,  1., -1.,  1.,  1.,  1.])
>>> b
tensor([ 1., -1.,  1., -1., -1.,  1., -1., -1.,  1.,  1.])
>>> a.bundle(b)
tensor([ 0., -2.,  0., -2., -2.,  2., -2.,  0.,  2.,  2.])
clipping(kappa) MAPTensor[source]

Performs the clipping function that clips the lower and upper values.

Parameters:

kappa (int) – specifies the range of the clipping function.

Shapes:
  • Self: \((*)\)

  • Output: \((*)\)

Examples:

>>> a = torchhd.MAPTensor.random(30, 10).multibundle()
>>> a
MAP([-8.,  0.,  6.,  8.,  4., -6.,  0., -2.,  0., -4.])
>>> a.clipping(4)
MAP([-4.,  0.,  4.,  4.,  4., -4.,  0., -2.,  0., -4.])
cosine_similarity(others: MAPTensor, *, dtype=None, eps=1e-08) Tensor[source]

Cosine similarity with other hypervectors

dot_similarity(others: MAPTensor, *, dtype=None) Tensor[source]

Inner product with other hypervectors

classmethod empty(num_vectors: int, dimensions: int, *, dtype=None, device=None, requires_grad=False) MAPTensor[source]

Creates a set of hypervectors representing empty sets.

When bundled with a hypervector \(x\), the result is \(x\).

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

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

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

>>> torchhd.MAPTensor.empty(3, 6)
tensor([[0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.]])
classmethod identity(num_vectors: int, dimensions: int, *, dtype=None, device=None, requires_grad=False) MAPTensor[source]

Creates a set of identity hypervectors.

When bound with a random-hypervector \(x\), the result is \(x\).

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

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

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

>>> torchhd.MAPTensor.identity(3, 6)
tensor([[1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1.]])
inverse() MAPTensor[source]

Invert the hypervector for binding.

Each hypervector in MAP is its own inverse, so this returns a copy of self.

Shapes:
  • Self: \((*)\)

  • Output: \((*)\)

Examples:

>>> a = torchhd.MAPTensor.random(1, 10)
>>> a
tensor([[-1., -1., -1.,  1.,  1.,  1., -1.,  1., -1.,  1.]])
>>> a.inverse()
tensor([[-1., -1., -1.,  1.,  1.,  1., -1.,  1., -1.,  1.]])
multibind() MAPTensor[source]

Bind multiple hypervectors

multibundle() MAPTensor[source]

Bundle multiple hypervectors

negative() MAPTensor[source]

Negate the hypervector for the bundling inverse

Shapes:
  • Self: \((*)\)

  • Output: \((*)\)

Examples:

>>> a = torchhd.MAPTensor.random(1, 10)
>>> a
tensor([[-1., -1.,  1.,  1.,  1., -1.,  1., -1., -1., -1.]])
>>> a.negative()
tensor([[ 1.,  1., -1., -1., -1.,  1., -1.,  1.,  1.,  1.]])
permute(shifts: int = 1) MAPTensor[source]

Permute the hypervector.

The permutation operator is commonly used to assign an order to hypervectors.

Parameters:

shifts (int, optional) – The number of places by which the elements of the tensor are shifted.

Shapes:
  • Self: \((*)\)

  • Output: \((*)\)

Examples:

>>> a = torchhd.MAPTensor.random(1, 10)
>>> a
tensor([[ 1.,  1.,  1., -1., -1., -1.,  1., -1., -1.,  1.]])
>>> a.permute()
tensor([[ 1.,  1.,  1.,  1., -1., -1., -1.,  1., -1., -1.]])
classmethod random(num_vectors: int, dimensions: int, *, generator=None, dtype=None, device=None, requires_grad=False) MAPTensor[source]

Creates a set of random independent hypervectors.

The resulting hypervectors are sampled uniformly at random from the dimensions-dimensional hyperspace.

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

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

  • generator (torch.Generator, optional) – a pseudorandom number generator for sampling.

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

>>> torchhd.MAPTensor.random(3, 6)
tensor([[-1.,  1., -1.,  1.,  1., -1.],
        [ 1., -1.,  1.,  1.,  1.,  1.],
        [-1.,  1.,  1.,  1., -1., -1.]])
>>> torchhd.MAPTensor.random(3, 6, dtype=torch.long)
tensor([[-1,  1, -1, -1,  1,  1],
        [ 1,  1, -1, -1, -1, -1],
        [-1, -1, -1,  1, -1, -1]])