ngrams

torchhd.ngrams(input: VSATensor, n: int = 3) VSATensor[source]

Creates a hypervector with the \(n\)-gram statistics of the input.

\[\bigoplus_{i=0}^{m - n} \bigotimes_{j = 0}^{n - 1} \Pi^{n - j - 1}(V_{i + j})\]

Note

For \(n=1\) use multiset() instead and for \(n=m\) use bind_sequence() instead.

Parameters:
  • input (VSATensor) – The value hypervectors.

  • n (int, optional) – The size of each \(n\)-gram, \(1 \leq n \leq m\). Default: 3.

Shapes:
  • Input: \((*, m, d)\)

  • Output: \((*, d)\)

Examples:

>>> x = torchhd.random(5, 6)
>>> x
tensor([[-1., -1., -1.,  1.,  1.,  1.],
        [ 1., -1.,  1.,  1.,  1.,  1.],
        [-1., -1.,  1.,  1., -1., -1.],
        [-1., -1.,  1.,  1., -1.,  1.],
        [ 1., -1.,  1.,  1., -1.,  1.]])
>>> torchhd.ngrams(x)
tensor([-1., -1.,  1., -3., -1., -3.])