/
assembly_calculus.cljs
831 lines (721 loc) · 27.3 KB
/
assembly_calculus.cljs
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
(ns ftlm.vehicles.assembly-calculus
(:require
[tech.v3.datatype.argops :as argops]
[tech.v3.datatype.functional :as dtype-fn]
[tech.v3.datatype :as dtype]
["mathjs" :as mathjs]))
;; -----------------------------------------------
;; paper: https://arxiv.org/abs/2110.03171
;;
;; Assemblies!
;; Sanotsh Vempala's cell asssembly inspired model
;; "Let's assume the brain is implementing an assembly calculus"
;;
;; https://github.com/mdabagia/learning-with-assemblies
;; Braibenberg is stretching them up to memes and memeplexes!
;; ---- Cell assemblies ----
;; Essentials:
;; 1. Hebbian plasticity - fire together, wire together
;; Update: - actually it's not hebbian after all
;; Tzitzitlini Alejandre-GarcíaSamuel KimJesús Pérez-OrtegaRafael Yuste (2022)
;; And perhaps fair to call them Ensembles because that name was around before Hebb
;; 2. Directed graph of excitatory activations with dynamic edge weights
;; 3. Discrete timesteps
;; 4. Idealized inhibition model. That is we don't model the inhibitory neurons but a 'sheet inhibition', for instance with a cap-k algorithm
;; (Braitenberg calls this a thought pump)
;;
;;
;; Implementation
;; thank you mathjs
;; https://dev.to/mrinasugosh/ml-fundamentals-in-javascript-5fm5
;; +----------------+
;; +----------------+ higher probably within a row
;; +----------------+
;; +----------------+
;; The other one I want to try
;; /\
;; /- \
;; /- \
;; /-- \-
;; /- \
;; /- \
;; -- \
;; ------------------------------
;; coll -1, coll x , coll + 1, ...
;; then everbody inside a row (or coll, whatever) is connected more and then spreads out to the neighbours
;;
;; Allowing self-connections
;;
;; geometry model takes 3 args, the neuron that will recieve the connection (i)
;; the neurons that will provide the connection (j)
;; it returns a connection probability
(defn ->directed-graph-with-geometry
[n-neurons geometry-model]
(.map (mathjs/matrix (mathjs/zeros #js [n-neurons
n-neurons])
"sparse")
(fn [_ idx _]
(if (< (mathjs/random 0 1)
(geometry-model (aget idx 0) (aget idx 1)))
1
0))))
(defn ->random-directed-graph
[n-neurons density]
(->directed-graph-with-geometry
n-neurons
(fn [_ _]
(< (mathjs/random 0 1) density))))
;; Synaptic input is the sum of the weights of the active activations
;; Everybody that is active is contributing to my chance of being active
(defn synaptic-input
[weights activations]
(let [n (.get (mathjs/squeeze (mathjs/size weights)) #js[1])]
(if (zero? (mathjs/count activations))
(mathjs/zeros n)
(-> (mathjs/subset weights
(mathjs/index activations
(mathjs/range 0 n)))
(mathjs/sum 0)
(mathjs/squeeze)))))
(defn ->indices
[m index]
(.subset (mathjs/range 0 (mathjs/count m)) index))
(defn indices->binary-array
[indices max-index]
(.subset (mathjs/zeros max-index)
(mathjs/index indices)
1))
;; this is the perf bottleneck atm for the hebbian neuronal area
(defn normalize
[weights]
(let [sums (mathjs/squeeze (mathjs/sum weights 0))]
(.map weights
(fn [v idx _m]
(mathjs/divide v (.get sums #js[(aget idx 1)])))
true)))
;; the active activations are a set of indices
(defn ->activations [n-neurons]
(mathjs/range 0 n-neurons))
(defn ->neurons [n-neurons]
(mathjs/zeros #js [n-neurons]))
;; ---------------------
;; Hebbian plasticity dictates that w-ij be increased by a factor of 1 + β at time t + 1
;; if j fires at time t and i fires at time t + 1,
;; --------------
;;
;; You look at all inputs from the last time step,
;; then you select the ones that are active next
;; In effect, everytime I am active, I can look who activated me. For those connections we increase the weight
(defn hebbian-plasticity
[{:keys [plasticity weights current-activations
next-activations]}]
(.subset
weights
(mathjs/index current-activations next-activations)
(mathjs/multiply (mathjs/subset weights
(mathjs/index
current-activations
next-activations))
(+ 1 plasticity))))
;; ==============================
;; binary hebbian-plasticity
;; ==============================
;; ---
;; For each neuron that is active right now, look at the neurons that were active at the last step,
;; for each of those edges (i->j) there is a chance, proportional to β, that there is a new synapse formed between the 2 neurons.
;; Doesn't matter if there is already a synapse or not.
;;
;; ---
;;
;; In order for the network to not be overrun with synapses everywhere, in a second process prune synapses.
;; It looks to me like the simplest way to do this is to simply prune 10% or something of the existing
;; synapses.
;;
;; Intuitively, this probably preserves cell assemblies and their connections.
;;
;;
;; The concept of synapse turnover is very biologically harmonious. [insert links to Seth Grant research]
;; https://youtu.be/vWuSpIAZW9s?si=lDeQLXHed9fIBGAP
;;
;; You could also model a lifetime for each synapse formed.
;; The plasticity rule could then reset the lifetime.
;;
;; If you do pruning, you have another problem: How to not accidentally starve the network of connections?
;; A simple counter meassure would be to not let the synapse count drop below a certain number.
;; We could also say that we throw away a starved network and start fresh. (new random connections).
;;
;; It is biological intuitive, that some processes form (semi random) fresh connections. You might wonder if
;; such a thing would happen during sleep. This would re-normalize the network, make new interpretions possible again.
;;
;; We could simply do this by shooting a bit of random activation into the network. The plasticity rules
;; would then already make new synapses.
;; As a variation of this you can construct geometry in the network, by making not completely random activation, but
;; random activation with geometry.
;; For instance, if you make a wave across the network, you automatically create synapses to neighbouring neurons.
;;
;;
;; params: β `plasticity` is now the chance that a new synapse is formed, per time step.
;; (per pair of active neurons)
;;
(defn scalar? [m]
(zero? (mathjs/count (mathjs/size m))))
(defn binary-hebbian-plasticity
[{:keys [plasticity weights current-activations
next-activations]}]
(let [subset (.subset weights
(mathjs/index current-activations
next-activations))
new-subset (if (scalar? subset)
(mathjs/bitOr subset
(< (mathjs/random) plasticity))
(mathjs/map subset
(fn [v _idx _m]
(mathjs/bitOr v
(< (mathjs/random) plasticity)))))]
(.subset weights
(mathjs/index current-activations
next-activations)
new-subset)))
;;
;; This is the simple version, return new weights with prune-factor synapses removed.
;; This potentially starves the network of connections.
;;
;; The synapse turnover rate is given by
;; --------------------------------------
;; 1. The plasticity (chance for new synapse forming when 2 neurons are active across 2 time steps)
;; 2. The prune-event-rate (how often compared to neuron steps)
;; 3. The prune-factor (how many synapses are removed at a prune-event)
;;
;;
;; ---
;; Biologically, it would be easier for me to think in terms of synapse lifespan.
;; However, intuitively, it should all average out with large numbers.
;;
;;
;; ---
;; I notice when using this, you need to start needing to balance how much synapses you have
;;
(defn binary-prune-synapses
[{:as state :keys [prune-factor]}]
(let [survival-chance (- 1 prune-factor)]
(update state
:weights
(fn [weights]
(.map weights
(fn [v _idx _m]
(< (mathjs/random) survival-chance))
true)))))
;; this keeps a fixed density of synapses
(defn prune-synapses-fixed-density
[{:as state :keys [density n-neurons]}]
(update
state
:weights
(fn [weights]
(let [prune-rate (/ (- (.density weights) density)
(.density weights))]
(if-not (< 0 prune-rate)
weights
;; this a perf bottleneck
(.map weights
(fn [v _idx _m]
(boolean (< prune-rate (mathjs/random))))
:skip-zeros))))))
(comment
(binary-prune-synapses
(mathjs/matrix (clj->js [[1 1] [1 1]]) "sparse")
1.0)
(binary-hebbian-plasticity
{:current-activations #js [0 1]
:next-activations #js [0 1]
:plasticity 1.0
:weights (mathjs/matrix (clj->js [[0 0] [0 0]])
"sparse")}))
;; ---------------------------
;; Inhbition model
;; --------------------------
;; Who is active in the next time step?
;; cap-k answers this question by saying the top k neurons with the highest synaptic input
(defn cap-k
[k synaptic-input]
(into-array :int (take k (argops/argsort > (.valueOf (mathjs/filter synaptic-input #(mathjs/larger % 0)))))))
(defn indices-above-input-cutoff [synaptic-input threshold]
(.subset (mathjs/range 0 (mathjs/count synaptic-input))
(mathjs/index (mathjs/larger synaptic-input threshold))))
(def threshold-inhibiton indices-above-input-cutoff)
(defn update-neuronal-area
[{:as state
:keys [activations weights inhibition-model
plasticity-model]}]
(if (zero? (mathjs/count activations))
state
(let [synaptic-input (synaptic-input weights activations)
next-active (inhibition-model state synaptic-input)
next-weights (if plasticity-model
(plasticity-model
(assoc state
:current-activations
activations
:next-activations next-active))
weights)]
(assoc state
:activations next-active
:weights next-weights))))
;; same but different calling conventions
;; inhibition model takes a state, returns a state
(defn update-neuronal-area-2
[{:as state
:keys [activations weights inhibition-model
n-neurons
plasticity-model]}]
(let [synaptic-input
(if
(zero? (mathjs/count activations))
(mathjs/zeros #js [n-neurons])
(synaptic-input weights activations))
state (inhibition-model
(assoc state
:synaptic-input
synaptic-input))
next-weights
(if plasticity-model
(plasticity-model
(assoc state
:current-activations activations
:next-activations (:activations state)))
weights)]
(assoc state :weights next-weights)))
;; inputs are just a set of neuron indices
;; you might decide to call the inhibition model here, but whatever.
;; there will be another time step soon
(defn set-input
[state input]
(assoc state :activations input))
(defn append-input
[state input]
(update state
:activations
(fn [activations]
(mathjs/setUnion activations input))))
(defn append-input-2
[state inputs]
(update state
:activations
(fn [activations]
(reduce (fn [activations input] (mathjs/setUnion activations input)) activations inputs))))
(defn read-activations
[{:keys [activations]}]
(when activations (.valueOf activations)))
(defn activation-intersection [set1 set2]
(mathjs/setIntersect set1 set2))
(defn count-intersection
[a b]
(mathjs/count (activation-intersection a b)))
(defn ->projection
"`projection-model` is a function that takes an index and returns
wheter there neuron `i` is a projection neuron.
If it returns a number, that is taken a probability between 0 and 1"
[n-neurons projection-model]
(mathjs/subset
(mathjs/range 0 n-neurons)
(mathjs/index
(.map (mathjs/matrix (mathjs/zeros #js [n-neurons]))
(fn [v idx _]
(let [p (projection-model idx)]
(boolean (cond (number? p)
(< (mathjs/random 0 1) p)
:else p))))))))
(def count-projection mathjs/count)
(def count-activation mathjs/count)
(defn ->rand-projection
[n-neurons p-probability]
(->projection n-neurons (constantly p-probability)))
(defn read-projection [proj]
(.valueOf proj))
;; ==================
;; 1 bit version
;; - use geometry
;; - synapses are 1 bit,
;; - no plasticity
;;
(defn gaussian [amplitude mean std-deviation x]
(* amplitude (Math/exp
(-
(/ (Math/pow (- x mean) 2)
(* 2 (Math/pow std-deviation 2)))))))
;;
;; You have the highest probability of connecting to yourself
;; then you count the distance, just the linear distance
;;
;; +--------------------------------+
;; | |
;; +------------+----+--------------+
;; | .. i - 1 | i | i + 1, .. |
;; +------------+----+--------------+
;; ^
;; |
;; |
;; highest connection probability to iself
;; basically each neuron has a strip connectivity probably following a gaussian
;; (consider the colls I draw in the ui purely visual with this model.
;; the connectivity goes across the width boundary)
;;
;;
;; This creates something like 'columns' of std-deviation length neurons,
;; they overlap
;;
(defn lin-gaussian-geometry
[{:keys [amplitude std-deviation density-factor]}]
(fn [i j]
(gaussian (or amplitude (+ 0.5 density-factor))
0
std-deviation
(- j i))))
;;
;; Making it wrap like a torus would
;;
(defn lin-gaussian-geometry-wrap
[{:keys [amplitude std-deviation density-factor
n-neurons]}]
(fn [i j]
(gaussian (or amplitude (+ 0.5 density-factor))
0
std-deviation
(min (abs (- j i))
(abs (+ (- n-neurons j) i))))))
(def identity-plasticity :weights)
;; less relevant stuff:
;; ------------------------
;; A sensory apparatus
;; from sensory units -> neuron indices
;; With 2 versions, one off and another on
;; This models inhibitory interneurons, and represents the absence of a signal
;; This is similar to the canonical receptive fields of neuro-science
(defn sensory-apparatus-projection [n-neurons k-sensory-units projection-density]
(into
[]
(for [_ (range k-sensory-units)]
;; off / on projections (i.e the absence of a sensory unit being active is projected to some neurons)
{false
;; using only 20% of the connetions for off, this way the inputs are less similar
(into #{} (repeatedly (* n-neurons (* 0.2 projection-density)) #(rand-int n-neurons)))
true
(into #{} (repeatedly (* n-neurons projection-density) #(rand-int n-neurons)))})))
(defn sensory-apparatus
[{:keys [n-neurons k-sensory-units projection-density] :as state}]
(assoc
state
:sensory-projection
(sensory-apparatus-projection n-neurons k-sensory-units projection-density)))
(defn ->sensory-inputs [input-states sensory-projection]
(mathjs/matrix
(into-array :int
(into #{}
(mapcat
(fn [[idx input-active?]]
(get-in sensory-projection [idx input-active?]))
(map-indexed vector input-states))))))
(defn ->uni-directional-fiber
[n-neurons-1 n-neurons-2 density]
(.map (mathjs/matrix (mathjs/zeros #js [n-neurons-1
n-neurons-2])
"sparse")
(fn [_ _ _] (if (< (mathjs/random) density) 1 0))))
;; 2 weights, off and on
;; simulating inhibitory interneurons, signaling the absence of something
(defn ->input-fiber
[input-space n-area density]
{:input-count (:n-neurons input-space)
:off-fibers (->uni-directional-fiber (:n-neurons
input-space)
(:n-neurons n-area)
(/ density 10))
:on-fibers (->uni-directional-fiber (:n-neurons
input-space)
(:n-neurons n-area)
density)
:output-count (:n-neurons n-area)})
(defn ->mask
[max-index activations]
(if (zero? (mathjs/count activations))
(mathjs/and (mathjs/zeros max-index) 1.0)
(mathjs/and (.subset (mathjs/zeros max-index)
(mathjs/index activations)
1.0)
1.0)))
(defn input-fiber-activations
[input-space
{:keys [input-count output-count off-fibers on-fibers]}]
(let [activations (read-activations input-space)
input->indices (fn [input]
(->indices
input
(mathjs/index
(mathjs/larger input 0))))]
(mathjs/setUnion
(input->indices
(synaptic-input
off-fibers
(mathjs/not (->mask input-count activations))))
(input->indices (synaptic-input on-fibers
activations)))))
;; ----------------------------------------------------------------------------------------------------------
;; ================
;; attenuation
;; ================
;;
;;
;; See https://faster-than-light-memes.xyz/biological-notes-on-the-vehicles-cell-assemblies.html
;; (attenuation)
;;
;;
;; Here, I do 1 simpler that is just carry over an attenuation malus
;;
;;
;; 'Rolling malus' implementation
;;
;; 1. Everytime you are active, your malus goes up.
;; 2. Every time step the mulus decays.
;; 3. The malus is applied to the synaptic input divisively
;;
;; kinda simplest thing, 1-3 could also be more complicated functions.
;;
(defn attenuation
[{:as state
:keys [attenuation-malus attenuation-decay
attenuation-malus-factor synaptic-input n-neurons
activations]}]
(let [attenuation-malus (or attenuation-malus
(mathjs/matrix
(mathjs/zeros
#js [n-neurons])))
;; decay the malus from previous step
attenuation-malus (mathjs/multiply
attenuation-malus
(- 1 attenuation-decay))
attenuation-malus
;; accumulate the malus on everybody active
(.subset attenuation-malus
(mathjs/index activations)
(mathjs/add (mathjs/subset
attenuation-malus
(mathjs/index activations))
attenuation-malus-factor))]
(assoc state
:synaptic-input (mathjs/dotDivide
synaptic-input
(mathjs/add 1 attenuation-malus))
:attenuation-malus attenuation-malus)))
;; ================
;; intrinsic excitability plasticity
;; ================
;;
;; https://faster-than-light-memes.xyz/biological-notes-on-the-vehicles-cell-assemblies.html
;; *Iceberg Cell Assemblies*
;;
;; Paper: https://www.biorxiv.org/content/10.1101/2020.07.29.223966v1
;;
;;
;; quote:
;; we find increases in neuronal excitability, accompanied by increases in membrane resistance and a reduction in spike threshold. We conclude that the formation of neuronal ensemble by photostimulation is mediated by cell-intrinsic changes in excitability, rather than by Hebbian synaptic plasticity or changes in local synaptic connectivity. We propose an “iceberg” model, by which increased neuronal excitability makes subthreshold connections become suprathreshold, increasing the functional effect of already existing synapses and generating a new neuronal ensemble.
;;___
;;
;;
;; So instead of hebbian plasticity, we can try to model a cell-intrinsic `excitability`, `intrinsic-excitability`.
;;
;; The easiest model coming to mind is the same as the attenuation above. (but inverted).
;; A cummulative excitability, with a decay.
;;
;;
;; `excitability-growth`: Could have been called learning rate, to make it sound like machine learning
;;
;; `excitability-decay`: Relative decay each time step
;;
;; `excitabilities`: You could imagine pre-allocating this, probably this is somewhat random in biology.
;;
;;
;; 0. Grow excitability for all active neurons, (add excitability-growth)
;; 1. Decay excitabilities multiplicatively by excitability-decay
;; 2. excitabilities multiplicatively on the sum of the inputs for each neuron.
;;
;;
;; 0-2 could also be more complecated functions
;;
;; An excitability of 0.0 means your inputs are at baseline.
;; An excitability of 1.0 means your inputs count double and so forth.
;; In principle, negative numbers are allowed. (but not utilized by this step model here).
;; In this case this would flip into an attenuation or depression model.
;;
;; -1.0 is the minimal meaningful number, saying that synaptic input is 0, beyond that you would
;; get negative synaptic-inputs, which are not defined behaviour.
;;
;;
(defn intrinsic-excitability
[{:as state
:keys [excitabilities excitability-growth
excitability-decay synaptic-input n-neurons
activations]}]
(let [excitabilities
(or excitabilities
(mathjs/matrix
(mathjs/zeros
#js
[n-neurons]
)))
;; decay excitabilities
excitabilities (mathjs/multiply
excitabilities
(- 1 excitability-decay))
excitabilities
;; accumulate the excitabilities on everybody
;; active
(.subset excitabilities
(mathjs/index activations)
(mathjs/add (mathjs/subset
excitabilities
(mathjs/index activations))
excitability-growth))]
(assoc state
;; if you have excitability, your inputs count
;; more
:synaptic-input
(mathjs/dotMultiply
synaptic-input
(mathjs/add 1 excitabilities))
:excitabilities excitabilities)))
;; ================
;; Skipping Rate
;; ================
;;
;; Neuron 'failure rate' to fire.
;;
;; This is biologically plausible. At first glance this looks like an imperfection,
;; but I am reasononing that this would modify the dynamism of the substrate.
;;
;; Higher neuron failure rate is similar to attenuation, it makes the ensembles spread more
;; and be less stuck. But high attenuation makes the move around, where the skip rate makes them spread more
;; if applied before threshold model, and simply smaller if applied after the threshold model.
;;
;; Intuitively, this means you might find other attractor states, if you have some neurons that usually strongly
;; contribute to one interpretation, now you have the chance to find a new interpretation.
;;
;; This has been described under the topic of 'assembly shift' [insert paper]
;;
;; This could be applied before or after the threshold model, with distinct results.
;;
;; Skip rate is counterintiuitive, if one thinks of single neurons, and might (or not) be implemented 'explicitly' biochemically in brain.
(defn neuron-skip
"Give every neuron at each timestep a chance to =skip=.
`skip-rate`: The chance for each neuron to recieve 0 inputs, effectively skipping it.
"
[{:as state
:keys [synaptic-input n-neurons activations skip-rate]}]
(update
state
:synaptic-input
(fn [inputs]
(mathjs/dotMultiply
inputs
(.map (mathjs/zeros n-neurons)
(fn [v idx _]
(if (< (mathjs/random) skip-rate) 0 1)))))))
(defn neuron-skip-inhibition
"Give every neuron at each timestep a chance to =skip=.
This is an inhibition model modifier, applied `after` the threshold model.
"
[{:as state :keys [skip-rate n-neurons]}]
(update
state
:activations
(fn [activations]
(let
[skip? (mathjs/filter
(mathjs/range 0 n-neurons)
(fn [_] (< (mathjs/random) skip-rate)))]
(if-not (< 0 (mathjs/count skip?))
activations
(mathjs/setDifference
activations
skip?))))))
;; ==================================
;; random, intrinsic firing rate
;; ==================================
(defn intrinsic-firing-rate
"Applied after the threshold model.
Says that neurons are allowed to fire at each time step with chance `intrinsic-firing-rate`.
"
[{:as state :keys [n-neurons intrinsic-firing-rate]}]
(update state
:activations
(fn [activations]
(mathjs/setUnion
(or activations #js [])
(mathjs/filter (mathjs/range 0 n-neurons)
(fn [_]
(< (mathjs/random)
intrinsic-firing-rate)))))))
(defn wires->activations
[wires activations]
(synaptic-input wires activations))
(defn fixed-window-accumulate-and-fire
[{:as state
:keys [tick-window threshold n-neurons inputs tick]}
synaptic-inputs]
(let [state (->
state
(update :inputs mathjs/add synaptic-inputs)
(update :tick (fnil inc 0)))
state (if-not (zero? (mod tick tick-window))
state
(-> state
(assoc :inputs (->neurons n-neurons))
(assoc :activations
(indices-above-input-cutoff
(:inputs state)
threshold))))]
state))
(defn filter-nils
[op]
(fn [& inputs]
(let [xs (filter identity inputs)]
(if (< 1 (count xs)) (apply op xs) (first xs)))))
(def accumulate-inputs (filter-nils mathjs/add))
(def subtract (filter-nils mathjs/subtract))
(defn ->ones [size]
(mathjs/ones (clj->js size)))
(defn ->wire-1
[n-inputs n-neurons target-model]
(let [w (mathjs/matrix (mathjs/zeros #js[n-inputs n-neurons]) "sparse")]
(.map w
(fn [v idx _]
(if (target-model (.valueOf idx)) 1 0)))))
(defn ->one-neuron-per-wire
[n-neurons n-wires]
(let [ijs (into #{}
(map-indexed
(fn [j i] #js [i j])
(take n-wires
(shuffle (range n-neurons)))))]
(->wire-1 n-neurons n-wires ijs)))
(defn ->targets [n-wries target-model]
(.map
(mathjs/matrix
(mathjs/range 0 n-wries))
(fn [v _ _]
(target-model v))))
(defn target-inputs
[targets activations]
(mathjs/subset targets (mathjs/index activations)))
(defn wire->targets
[wire]
(let [sum (mathjs/sum wire 0)]
(.subset (mathjs/range 0 (mathjs/count sum))
(mathjs/index
(mathjs/larger (mathjs/squeeze sum) 0)))))
(defn wire->sources [wire] (wire->targets (mathjs/transpose wire)))
(comment
(->wire-1 10 3 (fn [[i j]] (zero? i)))
(wire->targets (->wire-1 3 5 (fn [[i j]] (zero? i))))
(wire->sources (->wire-1 5 3 (fn [[i j]] (zero? i))))
(wire->sources (->wire-1 3 5 (fn [[i j]] (zero? i))))
(wire->sources (->wire-1 3 5 (fn [[i j]] true))))