← back

Create Composite Hypervector for a Dataset Row

#74 · Linear Algebra · Medium

⊣ Solve on deep-ml.com

Problem

Create a composite hypervector for a dataset row using hyperdimensional computing. Given a row of feature values and a set of base hypervectors for each feature, encode the row as a single composite hypervector by binding (element-wise XOR or multiplication) each feature value with its position vector and then bundling (summing) them.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import numpy as np

def create_composite_hypervector(features, base_vectors, value_vectors):
    dim = len(base_vectors[0])
    composite = np.zeros(dim, dtype=float)

    for i, value in enumerate(features):
        # Bind: element-wise multiplication of feature base vector with value vector
        bound = np.array(base_vectors[i]) * np.array(value_vectors[value])
        # Bundle: accumulate
        composite += bound

    # Binarize: threshold at 0
    composite = np.where(composite >= 0, 1, -1)
    return composite.tolist()

Explanation

  1. Each feature has a base hypervector representing its position/role.
  2. Each possible value has a value hypervector encoding that value.
  3. Binding (element-wise multiplication) associates a value with its feature position.
  4. Bundling (element-wise addition) combines all feature-value pairs into one vector.
  5. The composite is binarized by thresholding to produce a final bipolar hypervector.

Complexity

  • Time: O(f * d) where f is the number of features and d is hypervector dimension
  • Space: O(d) for the composite hypervector