-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm_and_tape.clj
645 lines (578 loc) · 18.4 KB
/
fsm_and_tape.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
(ns fsm-and-tape
(:require
[bennischwerdtner.sdm.sdm :as sdm]
[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]
[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.prot :as prot]
[bennischwerdtner.hd.codebook-item-memory :as codebook]
[bennischwerdtner.hd.ui.audio :as audio]
[bennischwerdtner.hd.data :as hdd]))
(alter-var-root
#'hdd/*item-memory*
(constantly (codebook/codebook-item-memory 1000)))
(defn read-1
[sdm addr top-k]
(let [lookup-outcome (sdm/lookup sdm addr top-k 1)]
(when (< 0.2 (:confidence lookup-outcome))
(some-> lookup-outcome
:result
sdm/torch->jvm
(dtt/->tensor :datatype :int8)))))
;; Turing machine:
;; Finite State Machine and a Tape
;;
;; FSM, is a list of transition tuples:
;;
;; [ old-state, input-symbol, new-state, output-symbol, action ]
;; tape:
;;
;; read-tape
;; write, grows left and right
;;
(defprotocol Tape
(write [this s])
;; move and read have a top-k argument, specifying the degree (count)
;; of superposition (roughly in units of seed vectors worth of bits)
;;
;;
(read-tape [this]
[this top-k])
(move
[this direction]
[this direction top-k]))
(defprotocol FSM
(transition [this current-state input-symbol]))
;; This will require some philosophical explanation,
;; but I think it makes sense.
;; implemented via vsa bind/unbind.
(defprotocol Entanglement
(flip [this input]))
(defn entangle [symbols]
(hd/bind* (map hdd/clj->vsa* symbols)))
(extend-protocol Entanglement
Object
(flip [this other]
(hdd/clj->vsa* [:. this other])))
;; ----
(defn sdm-fsm-target
[target-state output action]
{:action action
:output output
:target-state target-state})
(defn sdm-outcome->action [outcome] (hdd/clj->vsa* [:. outcome :action]))
(defn sdm-outcome->output [outcome] (hdd/clj->vsa* [:. outcome :output]))
(defn sdm-outcome->target-state [outcome] (hdd/clj->vsa* [:. outcome :target-state]))
(comment
(sdm/lookup sdm (hdd/clj->vsa* [:* :s0 0]) 3 1)
(f/sum (read-1 sdm (hdd/clj->vsa* [:* :s0 0]) 3))
60.0
(for [branch [:action :output :target-state]]
(hdd/clj->vsa*
[:?? [:. (read-1 sdm (hdd/clj->vsa* [:* :s0 0]) 3) branch]]))
(doseq [branch [:action :output :target-state]]
(sdm/write
sdm
(hdd/clj->vsa* [:* :s0 0 branch])
(hdd/clj->vsa*
({:action :right
:output 0
:target-state :s0}
branch))
1))
(for [branch [:action :output :target-state]]
(hdd/clj->vsa*
[:?? (read-1 sdm (hdd/clj->vsa* [:* :s0 0 branch]) 1)])))
(defn ->sdm-fsm
[quintuples]
(let [sdm (sdm/->sdm {:address-count (long 1e6)
:address-density 0.000003
:word-length (long 1e4)})]
(doseq [[state input-symbol target-state output-symbol
action]
quintuples]
(sdm/write sdm
(hdd/clj->vsa* [:* state input-symbol])
(hdd/clj->vsa* (sdm-fsm-target
target-state
output-symbol
action))
1))
(reify
FSM
(transition [this current-state input-symbol]
(read-1 sdm
(hdd/clj->vsa* [:* current-state
input-symbol])
3)))))
(comment
;; -----------------------------
;; Example data:
;;
;; This is a parity finder turing machine:
;;
(def
quintuples
[[:s0 0 :s0 0 :right]
[:s0 1 :s1 0 :right]
[:s0 :b :halt true :-]
;; -------------------------------
[:s1 0 :s1 0 :right]
[:s1 1 :s0 0 :right]
[:s1 :b :halt false :-]])
(def fsm (->sdm-fsm quintuples))
(hdd/cleanup*
(hdd/clj->vsa* [:. (transition fsm :s0 0) :output]))
(hdd/cleanup*
(hdd/clj->vsa* [:. (transition fsm :s0 1) :action])))
;; ----------------------
(defn iteratively-override
[sdm address s top-k]
;;
;; I am under the impression that an implementation
;; in neuronal circuits is straightforward.
;;
;; Caveats
;;
;; I: 'current-content' register: My idea is that it
;; would be implemented with a stable neuronal
;; ensemble. Here, this is 'infinitely faithful' in
;; computer memory.
;;
;; II:
;; The notion of halting this subroutine surely must
;; be a best effort thing in a biological system.
;; (Perhaps with restarts etc.)
;;
;;
(loop [current-content (read-1 sdm address top-k)]
(if (and current-content
(<= 0.95 (hd/similarity current-content s)))
current-content
(recur (do (sdm/write sdm address s 1)
(read-1 sdm address top-k))))))
(comment
(iteratively-override sdm (hdd/clj->vsa* :foo) (hdd/clj->vsa* :bar) 1)
(hd/similarity
(hdd/clj->vsa* :bar)
(read-1 sdm (hdd/clj->vsa* :foo) 1)))
(defn sdm-get-create
([sdm addr] (sdm-get-create sdm addr 1))
([sdm addr top-k]
(let [content
(read-1 sdm addr top-k)]
;;
;; This could be implemented by pre-allocated
;; random seeds.
;; (for instance neuronal ensembles created during
;; brain development) (G. Buzsáki)
;;
;; I.e. for everything that you ask for, you get
;; an answer (which can be a fresh random seed).
;;
;; Here, we allocate a random symbol the moment we
;; look instead. But I still feel entitled to call
;; it biologically principled.
;;
(when-not content
(sdm/write sdm addr (hd/->seed) 1))
{:existed? (boolean content)
:target (read-1 sdm addr top-k)})))
;; --------------------------
(comment
(sdm/write sdm (hdd/clj->vsa* [:* head :right]) (hd/->seed) 1)
(get-edge-create sdm head :right)
(get-edge-create sdm head :left))
(comment
(hd/similarity (hdd/clj->vsa* :a)
(flip (entangle [:a :b])
(hdd/clj->vsa* [:-- :b 0.5])))
0.4)
(defn ->tape
([]
(let [sdm (sdm/->sdm {:address-count (long 1e6)
:address-density 0.000003
:word-length (long 1e4)})
head (atom (hd/->seed))
directions-entanglement (entangle [:right :left])]
(reify
Tape
;; 2. tape element
;; address -> content
;;
(write [this s]
(iteratively-override sdm
@head
(hdd/clj->vsa* s)
1))
(read-tape [this] (read-tape this 1))
(read-tape [this top-k] (read-1 sdm @head top-k))
(move [this direction] (move this direction 1))
(move [this direction top-k]
;;
;; 1. edge:
;; current address * :right -> next-address
;;
;; To move, ask the system for the content
;; at addr address * :right, which is a
;; fresh random seed, if it doesn't exist
;;
;;
;; move:
;; 1. get/create current-addr * right ->
;; target (next-address)
;; 2. update head to target address
;; 3. handle backwards edge
;; The result is the tape head points to the
;; (possibly fresh) tile to the right
;;
(let [current-head @head
direction (hdd/clj->vsa* direction)
{:keys [target]} (sdm-get-create
sdm
(hdd/clj->vsa*
[:* current-head
direction])
;; allow target to
;; be in
;; superposition
top-k)
;; I also need to update the other
;; direction
reverse-dir
;;
;; symbolically:
;; ({:left :right :right :left}
;; direction)
;;
;; hdc:
;;
;; Of course, the interesting thing
;; is that direction is allowed to
;; be a superposition of directions.
(flip directions-entanglement direction)
;;
;; create the backward edge
;;
;;
_ (sdm/write sdm
(hdd/clj->vsa* [:* target
reverse-dir])
current-head
1)]
(reset! head target))))))
([initial-tape]
(let [res (->tape)]
(doseq [symb initial-tape]
(do (write res symb) (move res :right)))
(doseq [_ initial-tape] (move res :left))
res)))
;; ---------------------------------
;;
(comment
(def tape (->tape [:a :b :c]))
(do
(def tape (->tape))
(write tape :a)
(move tape :right)
(read-tape tape))
nil
(do
(def tape (->tape))
(write tape :a)
(move tape :right)
(move tape :left)
(hdd/cleanup* (read-tape tape)))
'(:a)
(do
(def tape (->tape [:a :b :c]))
(write tape :a)
(move tape :right)
(move tape :left)
(hdd/cleanup* (read-tape tape)))
'(:a)
(do
(def tape (->tape [:a :b :c]))
(write tape :a)
(move tape :right)
(move tape :right)
(hdd/cleanup* (read-tape tape)))
'(:c)
(do
(def tape (->tape [:a :b :c]))
(write tape :a)
(move tape :right)
(move tape :right)
(move tape :right)
(read-tape tape))
nil
(do
(def tape (->tape [:a :b :c]))
(write tape :a)
(move tape :right)
(move tape :right)
(move tape :right)
(move tape :left)
(move tape :left)
(hdd/cleanup* (read-tape tape)))
'(:b)
(do
(def tape (->tape [:a :b :c]))
(write tape :a)
(move tape :right)
(move tape :right)
(move tape :right)
(move tape :left)
(move tape :left)
(move tape
(hdd/clj->vsa* [:+ [:-- :right 0.5]
[:-- :left 0.5]])
2)
(hdd/cleanup* (read-tape tape 2)))
'(:a :c)
s
)
(defn turing-machine
[{:as turing-machine
:keys [fsm-quintuples initial-tape initial-state
max-steps]}]
(reductions
(fn [{:as machine
:keys [tape fsm current-state input-symbol]} n]
(let [outcome
(transition fsm current-state input-symbol)
action (sdm-outcome->action outcome)
output (sdm-outcome->output outcome)
target-state (sdm-outcome->target-state
outcome)]
;;
;; you can ask what is the :halt vote
;;
(if (< 0.9
(hd/similarity action (hdd/clj->vsa* :halt)))
;; Interesting problem comes up: When fsm is
;; in superposition, and the outcome of the
;; fsm contains 'halt', then what to halt?
(ensure-reduced {:current-state target-state
:halted? true
:n n
:output output
:tape tape})
(do (write tape output)
(move tape action)
(assoc machine
:action action
:input-symbol (read-tape tape)
:current-state target-state)))))
(let [tape (->tape initial-tape)]
(assoc turing-machine
:fsm (->sdm-fsm fsm-quintuples)
:input-symbol (read-tape tape)
:current-state (hdd/clj->vsa* initial-state)
:tape tape))
(if max-steps (range max-steps) (range))))
;; ----
;; Parity finder
;;
;; (from Minsky Finite and Infite Machines):
;;
(def outcomes
(into []
(turing-machine
{:fsm-quintuples [[:s0 0 :s0 0 :right]
[:s0 1 :s1 0 :right]
[:s0 :b :halt true :halt]
;; -------------------------------
[:s1 0 :s1 0 :right]
[:s1 1 :s0 0 :right]
[:s1 :b :halt false :halt]]
:initial-state :s0
:initial-tape [1 1 :b]})))
(hdd/cleanup* (:output (peek outcomes)))
'(true)
(def outcomes
(into []
(turing-machine
{:fsm-quintuples [[:s0 0 :s0 0 :right]
[:s0 1 :s1 0 :right]
[:s0 :b :halt true :halt]
;; -------------------------------
[:s1 0 :s1 0 :right]
[:s1 1 :s0 0 :right]
[:s1 :b :halt false :halt]]
:initial-state :s0
:initial-tape [1 1 0 1 :b]})))
(hdd/cleanup* (:output (peek outcomes)))
'(false)
(do
(def outcomes
(into []
(turing-machine
{:fsm-quintuples [[:s0 0 :s0 0 :right]
[:s0 1 :s1 0 :right]
[:s0 :b :halt true :halt]
;; -------------------------------
[:s1 0 :s1 0 :right]
[:s1 1 :s0 0 :right]
[:s1 :b :halt false :halt]]
:initial-state :s0
:initial-tape [:b]})))
(hdd/cleanup* (:output (peek outcomes))))
'(true)
(do (def outcomes
(into []
(turing-machine
{:fsm-quintuples
[[:s0 0 :s0 0 :right] [:s0 1 :s1 0 :right]
[:s0 :b :halt true :halt]
;; -------------------------------
[:s1 0 :s1 0 :right] [:s1 1 :s0 0 :right]
[:s1 :b :halt false :halt]]
:initial-state :s0
:initial-tape [1 :b]})))
(hdd/cleanup* (:output (peek outcomes))))
'(false)
(do (def outcomes
(into []
(turing-machine
{:fsm-quintuples
[[:s0 0 :s0 0 :right] [:s0 1 :s1 0 :right]
[:s0 :b :halt true :halt]
;; -------------------------------
[:s1 0 :s1 0 :right] [:s1 1 :s0 0 :right]
[:s1 :b :halt false :halt]]
:initial-state :s0
:initial-tape [0 0 0 0 0 1 0 0 0 :b]})))
(hdd/cleanup* (:output (peek outcomes))))
'(false)
(time
(do (def outcomes
(into []
(turing-machine
{:fsm-quintuples
[[:s0 0 :s0 0 :right] [:s0 1 :s1 0 :right]
[:s0 :b :halt true :halt]
;; -------------------------------
[:s1 0 :s1 0 :right] [:s1 1 :s0 0 :right]
[:s1 :b :halt false :halt]]
:initial-state :s0
:initial-tape [0 0 0 0 0 1 0 0 0 1 :b]})))
(hdd/cleanup* (:output (peek outcomes)))))
'(true)
;; kinda slow impl
;; "Elapsed time: 743.824952 msecs"
(do (def outcomes
(into []
(turing-machine
{:fsm-quintuples
[[:s0 0 :s0 0 :right] [:s0 1 :s1 0 :right]
[:s0 :b :halt true :halt]
;; -------------------------------
[:s1 0 :s1 0 :right] [:s1 1 :s0 0 :right]
[:s1 :b :halt false :halt]]
:initial-state :s0
:initial-tape [0 :b]})))
(hdd/cleanup* (:output (peek outcomes))))
'(true)
(do (def outcomes
(into []
(turing-machine
{:fsm-quintuples
[[:s0 0 :s0 0 :right] [:s0 1 :s1 0 :right]
[:s0 :b :halt true :halt]
;; -------------------------------
[:s1 0 :s1 0 :right] [:s1 1 :s0 0 :right]
[:s1 :b :halt false :halt]]
:initial-state :s0
:initial-tape [0 1 :b]})))
(hdd/cleanup* (:output (peek outcomes))))
'(false)
(do (def outcomes
(into []
(turing-machine
{:fsm-quintuples
[[:s0 0 :s0 0 :right] [:s0 1 :s1 0 :right]
[:s0 :b :halt true :halt]
;; -------------------------------
[:s1 0 :s1 0 :right] [:s1 1 :s0 0 :right]
[:s1 :b :halt false :halt]]
:initial-state :s0
:initial-tape [0 1 1 :b]})))
(hdd/cleanup* (:output (peek outcomes))))
'(true)
;; --------------------------------------
;; 'halt' ideas:
;;
;; Idea 1:
;; - try 'remove' the 'halting' fsm from the
;; superimposed fms, and keep around
;; everything that halts as statistical
;; outcome.
;; - but how to know what part of the fsm
;; resulted in the halting?
;; - one could re-run with less top-k and a
;; little random seeds, thereby finding the
;; cores of the fsm, then only continue
;; with the cores that don't halt.
;; - this suggests a thought pump kind of
;; search algorithm in the first place, run
;; turing machine until 'halt' is part of
;; the output, then narrow down from where
;; the halt came?
;;
;; Idea 2:
;; - conventionally, make the halt output
;; simply keep the head of the tape
;; - :halt outcome also writes the output to
;; the tape
;; - ? :halt action as unit vector so it
;; stays automatically? (not sure if I can do
;; that with my block sparse codes).
;; - then run until the movement of the
;; superimposed turing machines has settled
;; to some specified degree.
;; - reading should then return the
;; superpositions of outcomes
;;
;; - the obvious cognitive connotation of
;; this is that you can either wait for a
;; majority to halt, then you get the
;; majority statistical vote of outcomes
;; ('system 1' kinda thing).
;;
;; - running longer gives you the long tail
;; of 'thought trains'. *Selecting* in some
;; way exactly *not* the large
;; statistically overlapping outcomes, but
;; *collapse* to some unlikely outcome
;; (deep thought / system 2 ).
;; - Perhaps a mechanic for software
;; synthesis
;; - if you now *select* and make
;; this tail end the new statistically
;; likely. (Memetics)
;;
;; You see 2 memetic drivers, if we reward
;; all circuits that contribute to the
;; outcomes we *select*, then 1) finish very
;; fast, then you are part of the outcome in
;; mode 1 every time 2) be useful and deep,
;; dare I say perhaps creative and be
;; selected in mode 2
;;
;;
(comment
(def sdm (sdm/->sdm {:address-count (long 1e6)
:address-density 0.000003
:word-length (long 1e4)}))
(time (sdm/write sdm (hd/->seed) (hd/->seed) 1)))
;; questions:
;; - how to branch
;; - how to jump
;;