graph

torchhd.graph(input: VSATensor, *, directed=False) VSATensor[source]

Graph from node hypervector pairs.

If directed=False this computes:

\[\bigoplus_{i = 0}^{n - 1} V_{0,i} \otimes V_{1,i}\]

If directed=True this computes:

\[\bigoplus_{i = 0}^{n - 1} V_{0,i} \otimes \Pi(V_{1,i})\]
Parameters:
  • input (VSATensor) – tensor containing pairs of node hypervectors that share an edge.

  • directed (bool, optional) – specify if the graph is directed or not. Default: False.

Shapes:
  • Input: \((*, 2, n, d)\)

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

Examples:

>>> x = torchhd.random(4, 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.]])
>>> edges = torch.tensor([[0, 0, 1, 2], [1, 2, 2, 3]])
>>> edges_hv = torch.index_select(x, 0, edges.ravel()).view(2, 4, 6)
>>> edges_hv
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.],
        [-1., -1.,  1., -1.,  1., -1.],
        [-1., -1.,  1., -1.,  1., -1.],
        [ 1., -1., -1., -1.,  1., -1.]]])
>>> torchhd.graph(edges_hv)
tensor([ 2.,  4., -2.,  0.,  4.,  0.])