Demo: Basic VSA Operations

This example demonstrates the core operations of VSA models: - Binding (association) - Unbinding (recovery) - Bundling (superposition) - Permutation (sequence encoding)

 12 from holovec import VSA, backend_info
 13
 14 def main():
 15     print("=" * 60)
 16     print("HoloVec Demo: Basic VSA Operations")
 17     print("=" * 60)
 18     print()
 19
 20     # Show available backends
 21     info = backend_info()
 22     print(f"Available backends: {info['available_backends']}")
 23     print(f"Recommended backend: {info['recommended_backend']}")
 24     print()
 25
 26     # Create a FHRR model (best capacity)
 27     print("Creating FHRR model (dim=512)...")
 28     model = VSA.create('FHRR', dim=512, seed=42)
 29     print(f"Model: {model}")
 30     print(f"  - Self-inverse: {model.is_self_inverse}")
 31     print(f"  - Commutative: {model.is_commutative}")
 32     print(f"  - Exact inverse: {model.is_exact_inverse}")
 33     print()
 34
 35     # Demonstration 1: Binding and Unbinding
 36     print("-" * 60)
 37     print("Demo 1: Binding and Unbinding (Association)")
 38     print("-" * 60)
 39
 40     # Create role and filler vectors
 41     role = model.random(seed=1)
 42     filler = model.random(seed=2)
 43
 44     print("Created vectors:")
 45     print(f"  role: random vector (seed=1)")
 46     print(f"  filler: random vector (seed=2)")
 47     print()
 48
 49     # Bind them
 50     bound = model.bind(role, filler)
 51     print("Binding: bound = role ⊗ filler")
 52     print(f"  Similarity(bound, role): {model.similarity(bound, role):.4f}")
 53     print(f"  Similarity(bound, filler): {model.similarity(bound, filler):.4f}")
 54     print("  → Bound vector is dissimilar to both inputs ✓")
 55     print()
 56
 57     # Unbind to recover
 58     recovered = model.unbind(bound, filler)
 59     print("Unbinding: recovered = bound ⊘ filler")
 60     print(f"  Similarity(recovered, role): {model.similarity(recovered, role):.4f}")
 61     print("  → Successfully recovered original vector ✓")
 62     print()
 63
 64     # Demonstration 2: Bundling
 65     print("-" * 60)
 66     print("Demo 2: Bundling (Superposition)")
 67     print("-" * 60)
 68
 69     # Create multiple vectors
 70     vec1 = model.random(seed=10)
 71     vec2 = model.random(seed=11)
 72     vec3 = model.random(seed=12)
 73
 74     print("Created 3 random vectors: vec1, vec2, vec3")
 75     print()
 76
 77     # Bundle them
 78     bundled = model.bundle([vec1, vec2, vec3])
 79     print("Bundling: bundled = vec1 + vec2 + vec3")
 80     print(f"  Similarity(bundled, vec1): {model.similarity(bundled, vec1):.4f}")
 81     print(f"  Similarity(bundled, vec2): {model.similarity(bundled, vec2):.4f}")
 82     print(f"  Similarity(bundled, vec3): {model.similarity(bundled, vec3):.4f}")
 83     print("  → Bundled vector is similar to all inputs ✓")
 84     print()
 85
 86     # Demonstration 3: Permutation for Sequences
 87     print("-" * 60)
 88     print("Demo 3: Permutation (Sequence Encoding)")
 89     print("-" * 60)
 90
 91     # Encode sequence [A, B, C]
 92     a = model.random(seed=20)
 93     b = model.random(seed=21)
 94     c = model.random(seed=22)
 95
 96     print("Elements: A, B, C")
 97     print("Encoding sequence: seq = A + ρ(B) + ρ²(C)")
 98     print()
 99
100     sequence = model.bundle([
101         a,
102         model.permute(b, k=1),
103         model.permute(c, k=2)
104     ])
105
106     # Query position 0 (should find A)
107     sim_a = model.similarity(sequence, a)
108     print(f"Query position 0 → Similarity(seq, A): {sim_a:.4f}")
109
110     # Query position 1 (should find B)
111     query_pos1 = model.unpermute(sequence, k=1)
112     sim_b = model.similarity(query_pos1, b)
113     print(f"Query position 1 → Similarity(ρ⁻¹(seq), B): {sim_b:.4f}")
114
115     # Query position 2 (should find C)
116     query_pos2 = model.unpermute(sequence, k=2)
117     sim_c = model.similarity(query_pos2, c)
118     print(f"Query position 2 → Similarity(ρ⁻²(seq), C): {sim_c:.4f}")
119     print("  → Successfully encoded and queried sequence ✓")
120     print()
121
122     # Demonstration 4: Structured Representation
123     print("-" * 60)
124     print("Demo 4: Structured Representation")
125     print("-" * 60)
126
127     # Represent: "The ball is red and large"
128     object_role = model.random(seed=30)
129     color_role = model.random(seed=31)
130     size_role = model.random(seed=32)
131
132     ball = model.random(seed=40)
133     red = model.random(seed=41)
134     large = model.random(seed=42)
135
136     print("Creating representation:")
137     print("  'The ball is red and large'")
138     print()
139     print("Structure:")
140     print("  object=ball ⊗ color=red ⊗ size=large")
141     print()
142
143     representation = model.bundle([
144         model.bind(object_role, ball),
145         model.bind(color_role, red),
146         model.bind(size_role, large)
147     ])
148
149     # Query: What is the color?
150     print("Query: What is the color?")
151     color_query = model.unbind(representation, color_role)
152     sim_red = model.similarity(color_query, red)
153     sim_ball = model.similarity(color_query, ball)
154     sim_large = model.similarity(color_query, large)
155
156     print(f"  Similarity(query, red): {sim_red:.4f} ✓")
157     print(f"  Similarity(query, ball): {sim_ball:.4f}")
158     print(f"  Similarity(query, large): {sim_large:.4f}")
159     print("  → Correctly identified 'red' as the color ✓")
160     print()
161
162     # Demonstration 5: Fractional Power Encoding (FHRR-specific)
163     print("-" * 60)
164     print("Demo 5: Fractional Power Encoding (FHRR)")
165     print("-" * 60)
166
167     # Encode continuous value 2.5
168     base = model.random(seed=50)
169     value = 2.5
170
171     print(f"Encoding value: {value}")
172     print(f"  Using base vector and fractional power")
173     print()
174
175     encoded = model.fractional_power(base, value)
176     print(f"Encoded: base^{value}")
177
178     # Decode by dividing
179     decoded = model.fractional_power(encoded, 1.0 / value)
180     sim_base = model.similarity(decoded, base)
181
182     print(f"Decoded: encoded^(1/{value})")
183     print(f"  Similarity(decoded, base): {sim_base:.4f}")
184     print("  → Successfully encoded and decoded continuous value ✓")
185     print()
186
187     # Comparison with MAP
188     print("-" * 60)
189     print("Bonus: Comparing FHRR vs MAP")
190     print("-" * 60)
191
192     map_model = VSA.create('MAP', dim=10000, seed=42)
193     print(f"MAP model: {map_model}")
194     print(f"  - Self-inverse: {map_model.is_self_inverse}")
195     print(f"  - Exact inverse: {map_model.is_exact_inverse}")
196     print()
197
198     # Test unbinding quality
199     a_map = map_model.random(seed=1)
200     b_map = map_model.random(seed=2)
201     c_map = map_model.bind(a_map, b_map)
202     a_recovered_map = map_model.unbind(c_map, b_map)
203
204     sim_map = map_model.similarity(a_map, a_recovered_map)
205     print(f"MAP unbinding quality: {sim_map:.4f}")
206
207     a_fhrr = model.random(seed=1)
208     b_fhrr = model.random(seed=2)
209     c_fhrr = model.bind(a_fhrr, b_fhrr)
210     a_recovered_fhrr = model.unbind(c_fhrr, b_fhrr)
211
212     sim_fhrr = model.similarity(a_fhrr, a_recovered_fhrr)
213     print(f"FHRR unbinding quality: {sim_fhrr:.4f}")
214     print()
215     print("  → FHRR has better unbinding quality (exact inverse) ✓")
216     print()
217
218     print("=" * 60)
219     print("Demo Complete!")
220     print("=" * 60)
221
222
223 if __name__ == '__main__':
224     main()

Gallery generated by Sphinx-Gallery