HoloVec Quickstart Guide

Topics: Installation, basic workflow, encoding, binding, retrieval Time: 5 minutes Prerequisites: None Related: 01_basic_operations.py, 02_models_comparison.py

This quickstart demonstrates the core HoloVec workflow in under 100 lines. You’ll encode data, compose representations, and retrieve information using hyperdimensional computing - brain-inspired computing with 10,000-dimensional vectors.

 16 from holovec import VSA
 17
 18 print("=" * 70)
 19 print("HoloVec Quickstart - Hyperdimensional Computing in 5 Minutes")
 20 print("=" * 70)
 21 print()
 22
 23 # ============================================================================
 24 # Step 1: Create a VSA Model
 25 # ============================================================================
 26 print("Step 1: Creating a VSA model")
 27 print("-" * 70)
 28
 29 # Create a FHRR model (Fourier Holographic Reduced Representations)
 30 # FHRR provides exact inverses and works well with continuous encoders
 31 model = VSA.create('FHRR', dim=10000, seed=42)
 32
 33 print(f"Model: {model.model_name}")
 34 print(f"Dimension: {model.dimension}")
 35 print(f"Backend: {model.backend.name}")
 36 print()
 37
 38 # ============================================================================
 39 # Step 2: Encode Symbolic Data
 40 # ============================================================================
 41 print("Step 2: Encoding symbolic data")
 42 print("-" * 70)
 43
 44 # Create random hypervectors for symbols
 45 # Each symbol gets a unique 10,000-dimensional vector
 46 alice = model.random(seed=1)
 47 bob = model.random(seed=2)
 48 loves = model.random(seed=3)
 49 hates = model.random(seed=4)
 50
 51 print("Encoded symbols:")
 52 print(f"  alice → 10000-dim hypervector")
 53 print(f"  bob   → 10000-dim hypervector")
 54 print(f"  loves → 10000-dim hypervector")
 55 print(f"  hates → 10000-dim hypervector")
 56 print()
 57
 58 # ============================================================================
 59 # Step 3: Compose Representations with Binding
 60 # ============================================================================
 61 print("Step 3: Composing structured representations")
 62 print("-" * 70)
 63
 64 # Bind vectors to create structured representations
 65 # Binding creates a new vector that is dissimilar to both operands
 66 alice_loves_bob = model.bind(model.bind(alice, loves), bob)
 67 alice_hates_bob = model.bind(model.bind(alice, hates), bob)
 68
 69 print("Created compositional structures:")
 70 print(f"  'Alice loves Bob' → single hypervector")
 71 print(f"  'Alice hates Bob' → single hypervector")
 72 print()
 73
 74 # These two statements are completely different
 75 similarity = model.similarity(alice_loves_bob, alice_hates_bob)
 76 print(f"Similarity between statements: {similarity:.3f}")
 77 print("  (Low similarity confirms they represent different facts)")
 78 print()
 79
 80 # ============================================================================
 81 # Step 4: Query and Retrieve Information
 82 # ============================================================================
 83 print("Step 4: Querying compositional structures")
 84 print("-" * 70)
 85
 86 # Query: Who does Alice love?
 87 # Unbind 'alice' and 'loves' from 'alice_loves_bob'
 88 query_result = model.unbind(model.unbind(alice_loves_bob, alice), loves)
 89
 90 # Check similarity to answer
 91 similarity_bob = model.similarity(query_result, bob)
 92 similarity_alice = model.similarity(query_result, alice)
 93
 94 print("Query: Who does Alice love?")
 95 print(f"  Similarity to 'bob':   {similarity_bob:.3f}  ← Answer!")
 96 print(f"  Similarity to 'alice': {similarity_alice:.3f}")
 97 print()
 98
 99 # ============================================================================
100 # Step 5: Encode Continuous Values
101 # ============================================================================
102 print("Step 5: Encoding continuous values")
103 print("-" * 70)
104
105 from holovec.encoders import FractionalPowerEncoder
106
107 # Create an encoder for temperatures (0-100°C)
108 # bandwidth controls sensitivity: higher = more distinct encodings for distant values
109 temp_encoder = FractionalPowerEncoder(model, min_val=0, max_val=100, bandwidth=2.0)
110
111 # Encode temperatures
112 temp_25 = temp_encoder.encode(25.0)
113 temp_26 = temp_encoder.encode(26.0)
114 temp_50 = temp_encoder.encode(50.0)
115
116 print("Encoded temperatures:")
117 print(f"  25°C → hypervector")
118 print(f"  26°C → hypervector")
119 print(f"  50°C → hypervector")
120 print()
121
122 # Similar values have high similarity
123 sim_25_26 = model.similarity(temp_25, temp_26)
124 sim_25_50 = model.similarity(temp_25, temp_50)
125
126 print(f"Similarity (25°C, 26°C): {sim_25_26:.3f}  ← Close values, high similarity")
127 print(f"Similarity (25°C, 50°C): {sim_25_50:.3f}  ← Distant values, low similarity")
128 print()
129
130 # ============================================================================
131 # Step 6: Build Associative Memory
132 # ============================================================================
133 print("Step 6: Building associative memory")
134 print("-" * 70)
135
136 # Create a simple associative memory
137 # Bind objects to their properties
138 hot = model.random(seed=10)
139 cold = model.random(seed=11)
140
141 fire_is_hot = model.bind(model.random(seed=20), hot)  # fire → hot
142 ice_is_cold = model.bind(model.random(seed=21), cold)  # ice → cold
143
144 # Bundle related facts into knowledge base
145 knowledge = model.bundle([fire_is_hot, ice_is_cold])
146
147 print("Created knowledge base:")
148 print(f"  'fire is hot' + 'ice is cold' → single hypervector")
149 print()
150
151 # Query: What is fire?
152 fire = model.random(seed=20)
153 fire_property = model.unbind(knowledge, fire)
154
155 sim_hot = model.similarity(fire_property, hot)
156 sim_cold = model.similarity(fire_property, cold)
157
158 print("Query: What property does fire have?")
159 print(f"  Similarity to 'hot':  {sim_hot:.3f}  ← Answer!")
160 print(f"  Similarity to 'cold': {sim_cold:.3f}")
161 print()
162
163 # ============================================================================
164 # Summary
165 # ============================================================================
166 print("=" * 70)
167 print("Summary: What You've Learned")
168 print("=" * 70)
169 print()
170 print("✓ Created a 10,000-dimensional VSA model")
171 print("✓ Encoded symbolic data as hypervectors")
172 print("✓ Composed structured representations with binding")
173 print("✓ Retrieved information through unbinding")
174 print("✓ Encoded continuous values with similarity preservation")
175 print("✓ Built a simple associative memory")
176 print()
177 print("Next steps:")
178 print("  → 01_basic_operations.py - Deep dive into VSA operations")
179 print("  → 02_models_comparison.py - Compare different VSA models")
180 print("  → 10_encoders_scalar.py - Master continuous value encoding")
181 print()
182 print("Full documentation: https://docs.holovecai.com")
183 print("=" * 70)

Gallery generated by Sphinx-Gallery