-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhat_is_the_bread_for_lava.clj
706 lines (628 loc) · 22.7 KB
/
what_is_the_bread_for_lava.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
(ns what-is-the-bread-for-lava
(:require
[tech.v3.datatype.functional :as f]
[tech.v3.datatype :as dtype]
[tech.v3.tensor :as dtt]
[tech.v3.datatype.bitmap :as bitmap]
[fastmath.random :as fm.rand]
[fastmath.core :as fm]
[bennischwerdtner.sdm.sdm :as sdm]
[bennischwerdtner.hd.binary-sparse-segmented :as
hd]
[bennischwerdtner.pyutils :as pyutils]
[tech.v3.datatype.unary-pred :as unary-pred]
[tech.v3.datatype.argops :as dtype-argops]
[bennischwerdtner.hd.data :as hdd]))
;; -----------------
;;
;; Pretend for a moment you have a cognitive system that already established certain 'samenesses'
;;
(def liquid (hdd/clj->vsa :liquid))
;; forgot about the honey
;; (def honey (hd/thin (hd/superposition liquid (hdd/clj->vsa :honey))))
(def butter (hd/thin (hd/superposition liquid (hdd/clj->vsa :butter))))
(def lava (hd/thin (hd/superposition liquid (hdd/clj->vsa :lava))))
;; -------------------------------------------------------
;; Model stuff with finite state automatons
;;
;; Say that the outcome of spread is a surface filled with *something*
;;
;;
(def bread-domain
(hdd/finite-state-automaton-1
(hdd/clj->vsa* [[:bread :spread {:surface butter}]
[:bread :spread {:surface :vegan-spread}]
[:bread :crumble {:crumps :bread}]
[:bread :forget {:bread :molded}]])))
(def lava-domain
(hdd/finite-state-automaton-1
(hdd/clj->vsa* [[:rocks :spread {:surface lava}]
[:lava :freeze :rocks]
[:vulcano :erupt {:spew lava}]
[:rocks :forget {:rocks :ancient}]])))
;; what is the bread for lava?
;; first ask what is the action I would do with lava and bread,
;; the similarity of lava and liquid doing work now, the shared :spread comes out:
(hdd/cleanup*
(hd/unbind
bread-domain
(hd/bind
(hdd/clj->vsa* :bread)
(hd/permute (hdd/clj->vsa* {:surface lava})))))
'(:spread)
;; the essential mechanism for this is at fun_with_trees.clj
(let
[the-action-that-would-lead-to-lava-surface-given-a-bread
(hd/unbind bread-domain
(hd/bind (hdd/clj->vsa* :bread)
(hd/permute (hdd/clj->vsa*
{:surface lava}))))]
[:spread-lava-in-bread-domain
(hdd/cleanup*
(hdd/automaton-source
bread-domain
the-action-that-would-lead-to-lava-surface-given-a-bread
(hdd/clj->vsa* {:surface lava})))
:spread-lava-in-bread+lava-domain
(hdd/cleanup*
(hdd/automaton-source
(hdd/union bread-domain lava-domain)
;; for this to work, I need need to cleanup with
;; an item memory
;; (literature mentions this as challange.
;; Resonator networks can do this efficiently)
;; --------------------------------------------
;; cleanup to prestine :spread
(hdd/clj->vsa
(hdd/cleanup
the-action-that-would-lead-to-lava-surface-given-a-bread))
(hdd/clj->vsa* {:surface lava})))])
[:spread-lava-in-bread-domain '(:bread)
:spread-lava-in-bread+lava-domain '(:rocks :bread)]
;; -----------------------------------------------------------
;;
;; in some ways you expect something that is now 'superimposed with bread'?
;; Perhaps it is exactly this superposition that is the early making of a personal 'inside joke'
;; The concept that bread and lava now coexist?
;; I don't know.
;;
;; -----------
;; Admittedly, :spread is doing a lot of work here.
;;
;; -----------------------------------------------------------
(=
(hdd/finite-state-automaton-1
(hdd/clj->vsa* [[:bread :spread {:surface butter}]]))
;; -------------------------------
;; expands to
(hd/bind
(hd/bind (hdd/clj->vsa* :bread) (hdd/clj->vsa* :spread))
(hd/permute (hd/bind (hdd/clj->vsa* :surface) butter))))
true
(=
(hdd/automaton-source
(hd/bind (hd/bind (hdd/clj->vsa* :bread)
(hdd/clj->vsa* :spread))
(hd/permute (hd/bind (hdd/clj->vsa* :surface)
butter)))
(hdd/clj->vsa* :spread)
(hd/bind (hdd/clj->vsa* :surface) butter))
(hd/unbind
(hd/bind (hd/bind (hdd/clj->vsa* :bread)
(hdd/clj->vsa* :spread))
(hd/permute (hd/bind (hdd/clj->vsa* :surface)
butter)))
(hd/bind (hdd/clj->vsa* :spread)
(hd/permute (hd/bind (hdd/clj->vsa* :surface)
butter))))
;; ---------------------------
;; The user can work this out for themselves, it becomes:
(hdd/clj->vsa* :bread))
true
;; ...
;; if you put a superposition instead of bread:
(= (hdd/automaton-source
;; Flipping the args here for the subtle reason
;; that dense hdvs don't actually have a
;; commutative bind. (The
;; bit count of the second arg is preserved)
;; This affects the outcome of '='
(hd/bind (hd/permute (hd/bind (hdd/clj->vsa* :surface)
butter))
(hd/bind (hdd/clj->vsa* :spread)
(hdd/clj->vsa* #{:bread :rocks})))
(hdd/clj->vsa* :spread)
(hd/bind (hdd/clj->vsa* :surface) butter))
;; --------------------------------------------------
;; expanded:
(hd/unbind
(hd/bind (hd/permute (hd/bind (hdd/clj->vsa* :surface)
butter))
(hd/bind (hdd/clj->vsa* :spread)
(hdd/clj->vsa* #{:bread :rocks})))
(hd/bind (hdd/clj->vsa* :spread)
(hd/permute (hd/bind (hdd/clj->vsa* :surface)
butter))))
;; --------------------------------------------------
;; ... then a superposition comes out here:
;;
(hdd/clj->vsa* #{:bread :rocks}))
true
;; ----------------------------------------
;;
;;
;; (⊕ bread-domain lava-domain )
;;
;; I use #{} interchangibly with ⊕
;;
;;
;; comes down essentially to
;;
;; #{:bread :rocks} ⊙ :spread ⊙ ~ p(:surface ⊙ :liquid)
;;
;;
;; Then querying with as destination {:surface lava} and :spread as input token
;;
;;
;; a = #{:bread :rocks} ⊙ :spread ⊙ ~ p(:surface ⊙ :liquid)
;;
;; a ⊘ ( :spread ⊙ p( ~ {:suface :liquid} ) )
;;
;; -> #{:bread :rocks}
;;
;; ------------------------------------------------
;; more mechanism:
(hdd/cleanup-verbose
(hd/unbind
(hdd/automaton-destination
;; you see that such an automaton supports 2
;; paths towards ~ {:surface liquid}
(hdd/finite-state-automaton-1
(hdd/clj->vsa*
[[:rocks :spread {:surface #{:butter :liquid}}]
[:bread :spread {:surface #{:lava :liquid}}]]))
(hdd/clj->vsa* :spread)
(hdd/clj->vsa* #{:rocks :bread}))
(hdd/clj->vsa* :surface)))
;; ({:k :liquid
;; :similarity 0.55
;; :v #tech.v3.tensor<int8> [10000]
;; [0 0 0 ... 0 0 0]}
;; {:k :lava
;; :similarity 0.3
;; :v #tech.v3.tensor<int8> [10000]
;; [0 0 0 ... 0 0 0]}
;; {:k :butter
;; :similarity 0.15
;; :v #tech.v3.tensor<int8> [10000]
;; [0 0 0 ... 0 0 0]})
(hdd/cleanup-verbose
(hdd/automaton-source
;; you see that such an automaton supports 2
;; paths towards ~ {:surface liquid}
(hdd/finite-state-automaton-1
(hdd/clj->vsa*
[[:rocks :spread {:surface butter}]
[:bread :spread {:surface lava}]]))
(hdd/clj->vsa* :spread)
(hdd/clj->vsa* {:surface liquid})))
;; ({:k :rocks
;; :similarity 0.6
;; :v #tech.v3.tensor<int8> [10000]
;; [0 0 0 ... 0 0 0]}
;; {:k :bread
;; :similarity 0.45
;; :v #tech.v3.tensor<int8> [10000]
;; [0 0 0 ... 0 0 0]})
(hdd/cleanup-verbose
(hdd/automaton-source
;; you see that such an automaton supports 2
;; paths towards ~ {:surface liquid}
(hdd/finite-state-automaton-1
(hdd/clj->vsa*
[[:rocks :spread {:surface butter}]
[:bread :spread {:surface lava}]]))
(hdd/clj->vsa* :spread)
(hdd/clj->vsa* {:surface lava})))
;; ({:k :bread
;; :similarity 1.0
;; :v #tech.v3.tensor<int8> [10000]
;; [0 0 0 ... 0 0 0]}
;; {:k :rocks
;; :similarity 0.3
;; :v #tech.v3.tensor<int8> [10000]
;; [0 0 0 ... 0 0 0]})
(hdd/cleanup-verbose
(hdd/automaton-source
;; you see that such an automaton supports 2
;; paths towards ~ {:surface liquid}
(hd/superposition
(hdd/finite-state-automaton-1
(hdd/clj->vsa*
[[:rocks :spread {:surface butter}]]))
(hdd/finite-state-automaton-1
(hdd/clj->vsa*
[[:bread :spread {:surface lava}]])))
(hdd/clj->vsa* :spread)
(hdd/clj->vsa* {:surface lava})))
;; ({:k :bread
;; :similarity 1.0
;; :v #tech.v3.tensor<int8> [10000]
;; [0 0 0 ... 0 0 0]}
;; {:k :rocks
;; :similarity 0.3
;; :v #tech.v3.tensor<int8> [10000]
;; [0 0 0 ... 0 0 0]})
;; -------------------------------------------------------
;; Model as finite state automaton
;; -------------------------------------------------------
;; - this is exploratory, premilinary
;;
;; -----
;; We need to be able to cleanup composits for this to work, use an sdm as item memory
;; (serves as an example usage of sdm)
;;
(defprotocol ItemMemory
(remember [this addr])
(recover [this addr-prime]))
(def item-memory
(let [sdm (sdm/->sdm {:address-count (long 1e5)
:address-density 0.00003
:word-length (long 1e4)})]
(reify
ItemMemory
(remember [this addr] (sdm/write sdm addr addr 1))
(recover [this addr-prime]
;; didn't figure out the interface yet
;; so I specify it in user space here.
(some->
(sdm/converged-lookup-impl
sdm
addr-prime
{:decoder-threshold 1
:stop? (fn [acc next-outcome]
(cond
(< 0.9 (:confidence next-outcome))
{:stop-reason :high-confidence
:success? true}
;;
;; two times low
;; confidence, means
;; it's diverging I
;; think
;;
(when-let [last-confidence
(:confidence
(peek (:result-xs
acc)))]
(< (+ (:confidence next-outcome)
last-confidence)
0.2))
{:stop-reason :diverging
:success? false}))
;; higher top-k so I get overlap with
;; compositis
;; of 'order 2' (hereby definend as sort
;; roughly the multiple of contributing
;; seed vectors)
:top-k 2})
:result-address
sdm/torch->jvm
(dtt/->tensor :datatype :int8))))))
;; -----------
(defn hdv-nodes
[xs]
(->> (tree-seq (fn [x] (and (not (hd/hv? x)) (seq? x)))
seq
xs)
(filter hd/hv?)))
(defn remember-leaves!
[xs]
(doseq [item (hdv-nodes xs)]
(remember item-memory item))
xs)
;; -----------
;; Model the bread domain as a finite state automaton.
(def bread-domain
(hdd/finite-state-automaton-1
(remember-leaves!
(hdd/clj->vsa*
[[{:bread {:surface :empty}}
{:spread butter}
{:bread {:surface butter}}]
[{:bread {:surface butter}} {:spread butter}
{:bread {:surface butter :thick? true}}]
[{:bread {:surface butter}} {:scrub-off butter}
{:bread {:surface butter :thin? true}}]]))))
;; -----------------------------------------------------------
;; Update: Using clj->vsa* dsl
(let
[the-action-that-would-lead-to-lava-surface-given-a-bread
(hdd/clj->vsa*
[:*.< bread-domain :bread :_ {:surface lava}])]
[:spread-lava-in-bread-domain
(hdd/cleanup*
(hdd/clj->vsa*
[:*.< bread-domain :_ the-action-that-would-lead-to-lava-surface-given-a-bread
{:surface lava}]))
:spread-lava-in-bread+lava-domain
(hdd/cleanup*
(hdd/clj->vsa*
[:*.<
[:+ bread-domain lava-domain]
:_
(hdd/clj->vsa (hdd/cleanup the-action-that-would-lead-to-lava-surface-given-a-bread))
{:surface lava}]))])
'[:spread-lava-in-bread-domain (:bread)
:spread-lava-in-bread+lava-domain (:rocks :bread)]
;; ... Arguably not better, lol
;;
;; -----------------------------------------
;; Appendix: Checking some properties of the sdm in usage
;;
(comment
(let [item-memory
(let [sdm (sdm/->sdm {:address-count (long 1e5)
:address-density 0.00003
:word-length (long 1e4)})]
(reify
ItemMemory
(remember [this addr]
(sdm/write sdm addr addr 1))
(recover [this addr-prime]
;; didn't figure out the interface yet
;; so I specify it in user space here.
(some->
(sdm/converged-lookup-impl
sdm
addr-prime
{:decoder-threshold 1
:stop?
(fn [acc next-outcome]
(cond
(< 0.9
(:confidence next-outcome))
{:stop-reason :high-confidence
:success? true}
;;
;; two times low
;; confidence, means
;; it's diverging I
;; think
;;
(when-let [last-confidence
(:confidence (peek (:result-xs acc)))]
(< (+ (:confidence next-outcome)
last-confidence)
0.2))
{:stop-reason :diverging
:success? false}))
:top-k 1})
:result-address
sdm/torch->jvm
(dtt/->tensor :datatype :int8)))))]
(remember item-memory (hdd/clj->vsa :a))
(doall
(for
[n (range 1000)]
(remember item-memory (hd/->seed))))
(hdd/cleanup* (recover item-memory (hdd/clj->vsa :a))))
'(:a)
(let [item-memory
(let [sdm (sdm/->sdm {:address-count (long 1e5)
:address-density 0.00003
:word-length (long 1e4)})]
(reify
ItemMemory
(remember [this addr]
(sdm/write sdm addr addr 1))
(recover [this addr-prime]
;; didn't figure out the interface yet
;; so I specify it in user space here.
(some->
(sdm/converged-lookup-impl
sdm
addr-prime
{:decoder-threshold 1
:stop?
(fn [acc next-outcome]
(cond
(< 0.9
(:confidence next-outcome))
{:stop-reason :high-confidence
:success? true}
;;
;; two times low
;; confidence, means
;; it's diverging I
;; think
;;
(when-let [last-confidence
(:confidence
(peek (:result-xs
acc)))]
(< (+ (:confidence
next-outcome)
last-confidence)
0.2))
{:stop-reason :diverging
:success? false}))
:top-k 1})
:result-address
sdm/torch->jvm
(dtt/->tensor :datatype :int8)))))]
(remember item-memory (hdd/clj->vsa :a))
(map (fn [x]
(some->> (recover item-memory x)
(hdd/cleanup*)))
[(hdd/clj->vsa :a) (hd/weaken (hdd/clj->vsa :a) 0.5)
(hd/weaken (hdd/clj->vsa :a) 0.75)
(hd/weaken (hdd/clj->vsa :a) 1)]))
'((:a) (:a) (:a) nil)
;; ------------------
;; T = 1000
;; (test data set count)
;;
(def outcome
(doall
(for [n (range 5)]
(let [item-memory
(let [sdm (sdm/->sdm {:address-count (long 1e5)
:address-density 0.00003
:word-length (long 1e4)})]
(reify
ItemMemory
(remember [this addr]
(sdm/write sdm addr addr 1))
(recover [this addr-prime]
;; didn't figure out the interface yet
;; so I specify it in user space here.
(some->
(sdm/converged-lookup-impl
sdm
addr-prime
{:decoder-threshold 1
:stop?
(fn [acc next-outcome]
(def acc acc)
(def next-outcome next-outcome)
(cond
(< 0.9
(:confidence next-outcome))
{:stop-reason :high-confidence
:success? true}
;;
;; two times low
;; confidence, means
;; it's diverging I
;; think
;;
(when-let [last-confidence
(:confidence
(peek (:result-xs
acc)))]
(< (+ (:confidence
next-outcome)
last-confidence)
0.2))
{:stop-reason :diverging
:success? false}))
:top-k 1})
:result-address
sdm/torch->jvm
(dtt/->tensor :datatype :int8)))))]
(remember item-memory (hdd/clj->vsa :a))
(doall (for [n (range 1000)]
(remember item-memory (hd/->seed))))
(doall
(for [n [5 7 10 15]]
(let [x (hd/thin (apply hd/superposition
(concat [(hdd/clj->vsa :a)]
(repeatedly n #(hd/->hv)))))]
{:noise-factor n
:recovered? (= '(:a)
(some->> (recover item-memory x)
(hdd/cleanup*)))})))))))
;; superposition with 5 random vectors and thinning
;; recovered only 3/5 times
'(({:noise-factor 5 :recovered? true}
{:noise-factor 7 :recovered? false}
{:noise-factor 10 :recovered? false}
{:noise-factor 15 :recovered? false})
({:noise-factor 5 :recovered? true}
{:noise-factor 7 :recovered? false}
{:noise-factor 10 :recovered? true}
{:noise-factor 15 :recovered? false})
({:noise-factor 5 :recovered? false}
{:noise-factor 7 :recovered? false}
{:noise-factor 10 :recovered? true}
{:noise-factor 15 :recovered? false})
({:noise-factor 5 :recovered? false}
{:noise-factor 7 :recovered? false}
{:noise-factor 10 :recovered? true}
{:noise-factor 15 :recovered? false})
({:noise-factor 5 :recovered? false}
{:noise-factor 7 :recovered? true}
{:noise-factor 10 :recovered? false}
{:noise-factor 15 :recovered? false}))
;; -------------------------------------------
;; without thinning
(def outcome
(doall
(for [n (range 5)]
(let [item-memory
(let [sdm (sdm/->sdm {:address-count (long 1e5)
:address-density 0.00003
:word-length (long 1e4)})]
(reify
ItemMemory
(remember [this addr]
(sdm/write sdm addr addr 1))
(recover [this addr-prime]
;; didn't figure out the interface yet
;; so I specify it in user space here.
(some->
(sdm/converged-lookup-impl
sdm
addr-prime
{:decoder-threshold 1
:stop?
(fn [acc next-outcome]
(cond
(< 0.9
(:confidence next-outcome))
{:stop-reason :high-confidence
:success? true}
;;
;; two times low
;; confidence, means
;; it's diverging I
;; think
;;
(when-let [last-confidence
(:confidence
(peek (:result-xs
acc)))]
(< (+ (:confidence
next-outcome)
last-confidence)
0.2))
{:stop-reason :diverging
:success? false}))
:top-k 1})
:result-address
sdm/torch->jvm
(dtt/->tensor :datatype :int8)))))]
(remember item-memory (hdd/clj->vsa :a))
(doall (for [n (range 1000)]
(remember item-memory (hd/->seed))))
(doall
(for [n [5 7 10 15]]
(let [x (apply hd/superposition
(concat [(hdd/clj->vsa :a)]
(repeatedly n #(hd/->hv))))]
{:noise-factor n
:recovered? (= '(:a)
(some->> (recover item-memory x)
(hdd/cleanup*)))})))))))
;; if we you don't thin, you can query and recover in this config
(({:noise-factor 5 :recovered? true}
{:noise-factor 7 :recovered? true}
{:noise-factor 10 :recovered? true}
{:noise-factor 15 :recovered? true})
({:noise-factor 5 :recovered? true}
{:noise-factor 7 :recovered? true}
{:noise-factor 10 :recovered? true}
{:noise-factor 15 :recovered? true})
({:noise-factor 5 :recovered? true}
{:noise-factor 7 :recovered? true}
{:noise-factor 10 :recovered? true}
{:noise-factor 15 :recovered? true})
({:noise-factor 5 :recovered? true}
{:noise-factor 7 :recovered? true}
{:noise-factor 10 :recovered? true}
{:noise-factor 15 :recovered? true})
({:noise-factor 5 :recovered? true}
{:noise-factor 7 :recovered? true}
{:noise-factor 10 :recovered? true}
{:noise-factor 15 :recovered? true})))