-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
1800 lines (1475 loc) · 73.5 KB
/
model.py
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""Some reservoir tweaks are inspired by Nicola and Clopath, arxiv, 2016 and Miconi 2016."""
import numpy as np
import matplotlib.pyplot as plt
import sys, shelve
try:
import torch
except ImportError:
print('Torch not available')
class PFCMD():
def __init__(self, PFC_G, PFC_G_off, learning_rate,
noiseSD, tauError, plotFigs=True, saveData=False):
self.RNGSEED = 5 # random seed: 5
np.random.seed([self.RNGSEED])
self.Nsub = 200 # number of neurons per cue
self.Ntasks = 2 # number of contexts = number of MD cells.
self.xorTask = False # use xor Task or simple 1:1 map task
# self.xorTask = True # use xor Task or simple 1:1 map task
if self.xorTask:
self.inpsPerTask = 4 # number of cue combinations per task
else:
self.inpsPerTask = 2
self.Ncues = self.Ntasks * self.inpsPerTask # number of input cues
self.Nneur = self.Nsub * (self.Ncues + 1) # number of neurons
self.Nout = 2 # number of outputs
self.tau = 0.02
self.dt = 0.001
self.tsteps = 200 # number of timesteps in a trial
self.cuesteps = 100 # number of time steps for which cue is on
self.noiseSD = noiseSD
self.saveData = saveData
self.tau_times = 4 # 4
self.Hebb_learning_rate = 1e-4 # 1e-4
self.Num_MD = 6
self.learning_rate = learning_rate # if the learning rate is too large,
# output weights can change too much within a trial / training cycle,
# then the output interference depends on the order of cues within a cycle
# typical value is 1e-5, and can vary from 1e-4 to 1e-6
self.tauError = tauError # smooth the error a bit, so that weights don't fluctuate
self.MDeffect = True # True # whether to have MD present or not
self.MDEffectType = 'submult' # MD subtracts from across tasks and multiplies within task
# self.MDEffectType = 'subadd' # MD subtracts from across tasks and adds within task
# self.MDEffectType = 'divadd' # MD divides from across tasks and adds within task
# self.MDEffectType = 'divmult' # MD divides from across tasks and multiplies within task
self.dirConn = False # direct connections from cue to output, also learnable
self.outExternal = True # True: output neurons are external to the PFC
# (i.e. weights to and from (outFB) are not MD modulated)
# False: last self.Nout neurons of PFC are output neurons
self.outFB = False # if outExternal, then whether feedback from output to reservoir
self.noisePresent = False # False # add noise to all reservoir units
self.positiveRates = True # whether to clip rates to be only positive, G must also change
self.MDlearn = True # False # whether MD should learn
# TODO: possibly to make task representations disjoint (not just orthogonal)
# self.MDstrength = None # if None, use wPFC2MD, if not None as below, just use context directly
# self.MDstrength = 0. # a parameter that controls how much the MD disjoints task representations.
self.MDstrength = 1. # a parameter that controls how much the MD disjoints task representations.
# zero would be a pure reservoir, 1 would be full MDeffect
# -1 for zero recurrent weights
self.wInSpread = False # Spread wIn also into other cue neurons to see if MD disjoints representations
self.blockTrain = True # first half of training is context1, second half is context2
self.depress = False # a depressive term if there is pre-post firing
self.multiAttractorReservoir = False # increase the reservoir weights within each cue
# all uniformly (could also try Hopfield style for the cue pattern)
# Perhaps I shouldn't have self connections / autapses?!
# Perhaps I should have sparse connectivity?
if self.MDstrength < 0.: self.Jrec *= 0.
# I don't want to have an if inside activation
# as it is called at each time step of the simulation
# But just defining within __init__
# doesn't make it a member method of the class,
# hence the special self.__class__. assignment
# wIn = np.random.uniform(-1,1,size=(self.Nneur,self.Ncues))
# wDir and wOut are set in the main training loop
if self.outExternal and self.outFB:
self.wFB = np.random.uniform(-1, 1, size=(self.Nneur, self.Nout)) \
* self.G / np.sqrt(self.Nsub * 2) * PFC_G
self.cue_eigvecs = np.zeros((self.Ncues, self.Nneur))
self.plotFigs = plotFigs
self.cuePlot = (0, 0)
if self.saveData:
self.fileDict = shelve.open('dataPFCMD/data_reservoir_PFC_MD' + \
str(self.MDstrength) + \
'_R' + str(self.RNGSEED) + \
(
'_xor' if self.xorTask else '') + '.shelve')
self.meanAct = np.zeros(shape=(self.Ntasks * self.inpsPerTask, \
self.tsteps, self.Nneur))
class PFC():
def __init__(self, n_neuron, n_neuron_per_cue, positiveRates=True, MDeffect=True):
self.Nneur = n_neuron
self.Nsub = n_neuron_per_cue
self.useMult = True
self.noisePresent = False
self.noiseSD = 1e-3#1e-3
self.tau = 0.02
self.dt = 0.001
self.positiveRates = positiveRates
if self.positiveRates:
# only +ve rates
self.activation = lambda inp: np.clip(np.tanh(inp), 0, None)
else:
# both +ve/-ve rates as in Miconi
self.activation = lambda inp: np.tanh(inp)
self.G = 0.75 # determines also the cross-task recurrence
# With MDeffect = True and MDstrength = 0, i.e. MD inactivated
# PFC recurrence is (1+PFC_G_off)*Gbase = (1+1.5)*0.75 = 1.875
# So with MDeffect = False, ensure the same PFC recurrence for the pure reservoir
if not MDeffect: self.G = 1.875
self.init_activity()
self.init_weights()
def init_activity(self):
self.xinp = np.random.uniform(0, 0.1, size=(self.Nneur))
self.activity = self.activation(self.xinp)
def init_weights(self):
self.Jrec = np.random.normal(size=(self.Nneur, self.Nneur)) \
* self.G / np.sqrt(self.Nsub * 2)
# make mean input to each row zero,
# helps to avoid saturation (both sides) for positive-only rates.
# see Nicola & Clopath 2016
self.Jrec -= np.mean(self.Jrec, axis=1)[:, np.newaxis]
# mean of rows i.e. across columns (axis 1),
# then expand with np.newaxis
# so that numpy's broadcast works on rows not columns
def __call__(self, input, input_x=None, *args, **kwargs):
"""Run the network one step
For now, consider this network receiving input from PFC,
input stands for activity of PFC neurons
output stands for output current to PFC neurons
Args:
input: array (n_neuron,)
input_x: array (n_neuron,), modulatory input that multiplicatively
interact with the neurons
Returns:
output: array (n_output,)
"""
if input_x is None:
input_x = np.zeros_like(input)
xadd = np.dot(self.Jrec, self.activity)
xadd += input_x + input # MD inputs
self.xinp += self.dt / self.tau * (-self.xinp + xadd)
if self.noisePresent:
self.xinp += np.random.normal(size=(self.Nneur)) * self.noiseSD \
* np.sqrt(self.dt) / self.tau
rout = self.activation(self.xinp)
self.activity = rout
return rout
def update_weights(self, input, activity, output):
self.trace = self.trace + activity
w_input = self.w_input + input * self.trace
w_output = self.w_output + input * self.trace
class MD():
def __init__(self, Nneur, Num_MD, num_active=1, positiveRates=True,
dt=0.001):
self.Nneur = Nneur
self.Num_MD = Num_MD
self.positiveRates = positiveRates
self.num_active = num_active # num_active: num MD active per context
self.tau = 0.02
self.tau_times = 4
self.dt = dt
self.tsteps = 200
self.Hebb_learning_rate = 1e-4
# working!
Gbase = 0.75 # determines also the cross-task recurrence
# self.MDstrength = 1
# if self.MDstrength is None:
# MDval = 1.
# elif self.MDstrength < 0.:
# MDval = 0.
# else:
# MDval = self.MDstrength
# # subtract across tasks (task with higher MD suppresses cross-tasks)
# self.wMD2PFC = np.ones(shape=(self.Nneur, self.Num_MD)) * (
# -10.) * MDval
# self.useMult = True
# # multiply recurrence within task, no addition across tasks
# ## choose below option for cross-recurrence
# ## if you want "MD inactivated" (low recurrence) state
# ## as the state before MD learning
# # self.wMD2PFCMult = np.zeros(shape=(self.Nneur,self.Ntasks))
# # choose below option for cross-recurrence
# # if you want "reservoir" (high recurrence) state
# # as the state before MD learning (makes learning more difficult)
# self.wMD2PFCMult = np.ones(shape=(self.Nneur, self.Num_MD)) \
# * PFC_G_off / Gbase * (1 - MDval)
# # threshold for sharp sigmoid (0.1 width) transition of MDinp
# self.MDthreshold = 0.4
#
# # With MDeffect = True and MDstrength = 0, i.e. MD inactivated
# # PFC recurrence is (1+PFC_G_off)*Gbase = (1+1.5)*0.75 = 1.875
# # So with MDeffect = False, ensure the same PFC recurrence for the pure reservoir
# # if not self.MDeffect: Gbase = 1.875
self.wPFC2MD = np.random.normal(0, 1 / np.sqrt(
self.Num_MD * self.Nneur), size=(self.Num_MD, self.Nneur))
self.wMD2PFC = np.random.normal(0, 1 / np.sqrt(
self.Num_MD * self.Nneur), size=(self.Nneur, self.Num_MD))
self.wMD2PFCMult = np.random.normal(0, 1 / np.sqrt(
self.Num_MD * self.Nneur), size=(self.Nneur, self.Num_MD))
self.MDpreTrace = np.zeros(shape=(self.Nneur))
self.MDpostTrace = np.zeros(shape=(self.Num_MD))
self.MDpreTrace_threshold = 0
# Choose G based on the type of activation function
# unclipped activation requires lower G than clipped activation,
# which in turn requires lower G than shifted tanh activation.
if self.positiveRates:
self.G = Gbase
self.tauMD = self.tau * self.tau_times ##self.tau
else:
self.G = Gbase
self.MDthreshold = 0.4
self.tauMD = self.tau * 10 * self.tau_times
self.init_activity()
def init_activity(self):
self.MDinp = np.zeros(shape=self.Num_MD)
def __call__(self, input, *args, **kwargs):
"""Run the network one step
For now, consider this network receiving input from PFC,
input stands for activity of PFC neurons
output stands for output current to MD neurons
Args:
input: array (n_input,)
Returns:
output: array (n_output,)
"""
# MD decays 10x slower than PFC neurons,
# so as to somewhat integrate PFC input
if self.positiveRates:
self.MDinp += self.dt / self.tauMD * \
(-self.MDinp + np.dot(self.wPFC2MD, input))
else: # shift PFC rates, so that mean is non-zero to turn MD on
self.MDinp += self.dt / self.tauMD * \
(-self.MDinp + np.dot(self.wPFC2MD, (input + 1. / 2)))
#num_active = np.round(self.Num_MD / self.Ntasks)
MDout = self.winner_take_all(self.MDinp)
self.update_weights(input, MDout)
return MDout
def update_trace(self, rout, MDout):
# MD presynaptic traces filtered over 10 trials
# Ideally one should weight them with MD syn weights,
# but syn plasticity just uses pre!
self.MDpreTrace += 1. / self.tsteps / 5. * \
(-self.MDpreTrace + rout)
self.MDpostTrace += 1. / self.tsteps / 5. * \
(-self.MDpostTrace + MDout)
# MDoutTrace = self.MDpostTrace
MDoutTrace = self.winner_take_all(self.MDpostTrace)
# MDoutTrace = np.zeros(self.Num_MD)
# MDpostTrace_sorted = np.sort(self.MDpostTrace)
# num_active = np.round(self.Num_MD / self.Ntasks)
# # MDthreshold = np.mean(MDpostTrace_sorted[-4:])
# MDthreshold = np.mean(
# MDpostTrace_sorted[-int(num_active) * 2:])
# # MDthreshold = np.mean(self.MDpostTrace)
# index_pos = np.where(self.MDpostTrace >= MDthreshold)
# index_neg = np.where(self.MDpostTrace < MDthreshold)
# MDoutTrace[index_pos] = 1
# MDoutTrace[index_neg] = 0
return MDoutTrace
def update_weights(self, rout, MDout):
"""Update weights with plasticity rules.
Args:
rout: input to MD
MDout: activity of MD
"""
MDoutTrace = self.update_trace(rout, MDout)
# if self.MDpostTrace[0] > self.MDpostTrace[1]: MDoutTrace = np.array([1,0])
# else: MDoutTrace = np.array([0,1])
self.MDpreTrace_threshold = np.mean(self.MDpreTrace)
# self.MDpreTrace_threshold = np.mean(self.MDpreTrace)+0.4*np.std(self.MDpreTrace)
#self.MDpreTrace_threshold = np.mean(self.MDpreTrace[:self.Nsub * self.Ncues]) # first 800 cells are cue selective
# MDoutTrace_threshold = np.mean(MDoutTrace) #median
MDoutTrace_threshold = 0.5
# update and clip the weights
# original
wPFC2MDdelta = 0.5 * self.Hebb_learning_rate * np.outer(MDoutTrace - MDoutTrace_threshold,self.MDpreTrace - self.MDpreTrace_threshold)
self.wPFC2MD = np.clip(self.wPFC2MD + wPFC2MDdelta, 0., 1.)
self.wMD2PFC = np.clip(self.wMD2PFC + (wPFC2MDdelta.T), -10., 0.)
self.wMD2PFCMult = np.clip(self.wMD2PFCMult + 0.1*(wPFC2MDdelta.T), 0.,7. / self.G)
# slow-decaying PFC-MD weights
# wPFC2MDdelta = 30000 * self.Hebb_learning_rate * np.outer(MDoutTrace - MDoutTrace_threshold,self.MDpreTrace - self.MDpreTrace_threshold)
# self.wPFC2MD += 1. / self.tsteps / 5. * (-1.0 * self.wPFC2MD + 1.0 * wPFC2MDdelta)
# self.wPFC2MD = np.clip(self.wPFC2MD, 0., 1.)
#
# self.wMD2PFC += 1. / self.tsteps / 5. * (-1.0 * self.wMD2PFC + 1.0 * (wPFC2MDdelta.T))
# self.wMD2PFC = np.clip(self.wMD2PFC, -10., 0.)
#
# self.wMD2PFCMult += 1. / self.tsteps / 5. * (-1.0 * self.wMD2PFCMult + 1.0 * (wPFC2MDdelta.T))
# self.wMD2PFCMult = np.clip(self.wMD2PFCMult, 0.,7. / self.G)
# decaying PFC-MD weights
# alpha = 0 # 0.5 when shift on, 0 when shift off
# self.wPFC2MD = np.clip((1-alpha)* self.wPFC2MD + wPFC2MDdelta, 0., 1.)
# self.wMD2PFC = np.clip((1-alpha) * self.wMD2PFC + (wPFC2MDdelta.T), -10., 0.)
# self.wMD2PFCMult = np.clip((1-alpha) * self.wMD2PFCMult + (wPFC2MDdelta.T), 0.,7. / self.G)
def winner_take_all(self, MDinp):
'''Winner take all on the MD
'''
# Thresholding
MDout = np.zeros(self.Num_MD)
MDinp_sorted = np.sort(MDinp)
# num_active = np.round(self.Num_MD / self.Ntasks)
MDthreshold = np.mean(MDinp_sorted[-int(self.num_active) * 2:])
#MDthreshold = MDinp_sorted[-int(self.num_active)]
# MDthreshold = np.mean(MDinp)
index_pos = np.where(MDinp >= MDthreshold)
index_neg = np.where(MDinp < MDthreshold)
MDout[index_pos] = 1
MDout[index_neg] = 0
return MDout
class OutputLayer():
def __init__(self, n_input, n_out, dt):
self.dt = dt
self.tau = 0.02
self.tauError = 0.001
self.Nout = n_out
self.Nneur = n_input
self.learning_rate = 5e-6
self.wOut = np.random.uniform(-1, 1,
size=(
self.Nout, self.Nneur)) / self.Nneur
self.state = np.zeros(shape=self.Nout)
self.error_smooth = np.zeros(shape=self.Nout)
self.activation = lambda inp: np.clip(np.tanh(inp), 0, None)
def __call__(self, input, target, *args, **kwargs):
outAdd = np.dot(self.wOut, input)
self.state += self.dt / self.tau * (-self.state + outAdd)
output = self.activation(self.state)
self.update_weights(input, output, target)
return output
def update_weights(self, input, output, target):
"""error-driven i.e. error*pre (perceptron like) learning"""
error = output - target
self.error_smooth += self.dt / self.tauError * (-self.error_smooth +
error)
self.wOut += -self.learning_rate \
* np.outer(self.error_smooth, input)
class SensoryInputLayer():
def __init__(self, n_sub, n_cues, n_output):
# TODO: Hard-coded for now
self.Ncues = n_cues
self.Nsub = n_sub
self.Nneur = n_output
self.positiveRates = True
self.weightNoise = False
self.weightOverlap = False
self.wIn = np.zeros((self.Nneur, self.Ncues))
self.cueFactor = 1.5
if self.positiveRates:
lowcue, highcue = 0.5, 1.
else:
lowcue, highcue = -1., 1
for cuei in np.arange(self.Ncues):
self.wIn[self.Nsub * cuei:self.Nsub * (cuei + 1), cuei] = \
np.random.uniform(lowcue, highcue, size=self.Nsub) \
* self.cueFactor
# add random noise to input weights
if self.weightNoise==True:
noiseSD = 1e-1
self.wIn += np.random.normal(size=(np.shape(self.wIn))) * noiseSD
# Input weights have overlops (mix neurons)
if self.weightOverlap == True:
''' overlap across rules'''
# for cuei in np.arange(self.Ncues):
# N_overlap = int(self.Nsub/2)
# N_overlap = 200
# self.wIn[self.Nsub * cuei:self.Nsub * (cuei + 1)+N_overlap, cuei] = \
# np.random.uniform(lowcue, highcue, size=self.Nsub+N_overlap) \
# * self.cueFactor
##
# ''' overlap across context'''
N_overlap = 50# 15
self.wIn[400:400+N_overlap,0] = np.random.uniform(lowcue, highcue, size=N_overlap) * self.cueFactor
# self.wIn[400:400+N_overlap,1] = np.random.uniform(lowcue, highcue, size=N_overlap) * self.cueFactor
# self.wIn[400:400+N_overlap,3] = np.random.uniform(lowcue, highcue, size=N_overlap) * self.cueFactor
# self.wIn[600:600+N_overlap,0] = np.random.uniform(lowcue, highcue, size=N_overlap) * self.cueFactor
self.wIn[600:600+N_overlap,1] = np.random.uniform(lowcue, highcue, size=N_overlap) * self.cueFactor
# self.wIn[600:600+N_overlap,2] = np.random.uniform(lowcue, highcue, size=N_overlap) * self.cueFactor
# self.wIn[0:0+N_overlap,1] = np.random.uniform(lowcue, highcue, size=N_overlap) * self.cueFactor
self.wIn[0:0+N_overlap,2] = np.random.uniform(lowcue, highcue, size=N_overlap) * self.cueFactor
# self.wIn[0:0+N_overlap,3] = np.random.uniform(lowcue, highcue, size=N_overlap) * self.cueFactor
# self.wIn[200:200+N_overlap,0] = np.random.uniform(lowcue, highcue, size=N_overlap) * self.cueFactor
# self.wIn[200:200+N_overlap,2] = np.random.uniform(lowcue, highcue, size=N_overlap) * self.cueFactor
self.wIn[200:200+N_overlap,3] = np.random.uniform(lowcue, highcue, size=N_overlap) * self.cueFactor
## plot Win
# import seaborn as sns
# ax = sns.heatmap(self.wIn,cmap='Reds')
# ax.set_yticks(np.arange(0,1001,200))
# ax.set_yticklabels(np.arange(0,1001,200), rotation=0)
# ax.set_xticklabels(np.arange(1,5,1), rotation=0)
# ax.set_xlabel('Cue Inputs')
# ax.set_ylabel('PFC Neuron Index')
# ax.set_title('Input Weights')
# plt.tight_layout()
# import pdb;pdb.set_trace()
# ramdom init input weights
# self.wIn = np.random.uniform(0, 1, size=(self.Nneur, self.Ncues))
# init input weights with Gaussian Distribution
# self.wIn = np.zeros((self.Nneur, self.Ncues))
# self.wIn = np.random.normal(0, 1, size=(self.Nneur, self.Ncues))
# self.wIn[self.wIn<0] = 0
self._use_torch = False
def __call__(self, input):
if self._use_torch:
input = input.numpy()
output = np.dot(self.wIn, input)
if self._use_torch:
#output = torch.from_numpy(output, dtype=torch.float).astype(torch.float)
output = torch.from_numpy(output).type(torch.float)
return output
def torch(self, use_torch=True):
self._use_torch = use_torch
def shift(self, shift=0):
'''
shift Win to test shift problem in PFC_MD model
'''
self.wIn = np.roll(self.wIn, shift=shift, axis=0)
class SensoryInputLayer_NoiseNeuro():
def __init__(self, n_sub, n_cues, n_output):
self.Ncues = n_cues
self.Nsub = n_sub
self.Nneur = n_output
self.positiveRates = True
self.wIn = np.zeros((self.Nneur, self.Ncues+1)) ## additional noise neuron
self.cueFactor = 1.5
if self.positiveRates:
lowcue, highcue = 0.5, 1.
else:
lowcue, highcue = -1., 1
for cuei in np.arange(self.Ncues):
self.wIn[self.Nsub * cuei:self.Nsub * (cuei + 1), cuei] = \
np.random.uniform(lowcue, highcue, size=self.Nsub) \
* self.cueFactor
self.wIn[:,self.Ncues] = np.random.uniform(lowcue, highcue, size=self.Nneur) \
* self.cueFactor
## plot Win
# import seaborn as sns
# ax = sns.heatmap(self.wIn,cmap='Reds')
# ax.set_yticks(np.arange(0,1001,200))
# ax.set_yticklabels(np.arange(0,1001,200), rotation=0)
# ax.set_xticklabels(np.arange(1,6,1), rotation=0)
# ax.set_xlabel('Cue Inputs')
# ax.set_ylabel('PFC Neuron Index')
# ax.set_title('Input Weights')
# plt.tight_layout()
# import pdb;pdb.set_trace()
self._use_torch = False
def __call__(self, input):
if self._use_torch:
input = input.numpy()
output = np.dot(self.wIn, input)
if self._use_torch:
#output = torch.from_numpy(output, dtype=torch.float).astype(torch.float)
output = torch.from_numpy(output).type(torch.float)
return output
def torch(self, use_torch=True):
self._use_torch = use_torch
class FullNetwork():
def __init__(self, Num_PFC, n_neuron_per_cue, Num_MD, num_active,
MDeffect=True):
dt = 0.001
self.pfc = PFC(Num_PFC, n_neuron_per_cue, MDeffect=MDeffect)
self.sensory2pfc = SensoryInputLayer(
n_sub=n_neuron_per_cue,
n_cues=4,
n_output=Num_PFC)
self.pfc2out = OutputLayer(n_input=Num_PFC, n_out=2, dt=dt)
self.pfc_output_t = np.array([])
self.MDeffect = MDeffect
if self.MDeffect:
self.md = MD(Nneur=Num_PFC, Num_MD=Num_MD, num_active=num_active,
dt=dt)
self.md_output = np.zeros(Num_MD)
index = np.random.permutation(Num_MD)
self.md_output[index[:num_active]] = 1 # randomly set part of md_output to 1
self.md_output_t = np.array([])
#import pdb;pdb.set_trace()
def __call__(self, input, target, *args, **kwargs):
"""
Args:
input: (n_time, n_input)
target: (n_time, n_output)
"""
self._check_shape(input, target)
n_time = input.shape[0]
tsteps = 200
self.pfc.init_activity() # Reinit PFC activity
pfc_output = self.pfc.activity
if self.MDeffect:
self.md.init_activity() # Reinit MD activity
output = np.zeros((n_time, target.shape[-1]))
self.pfc_output_t *= 0
if self.MDeffect:
self.md_output_t *= 0
for i in range(n_time):
input_t = input[i]
target_t = target[i]
if i % tsteps == 0: # Reinit activity for each trial
self.pfc.init_activity() # Reinit PFC activity
pfc_output = self.pfc.activity
if self.MDeffect:
self.md.init_activity() # Reinit MD activity
input2pfc = self.sensory2pfc(input_t)
#import pdb;pdb.set_trace()
if self.MDeffect:
self.md_output = self.md(pfc_output)
self.md.MD2PFCMult = np.dot(self.md.wMD2PFCMult, self.md_output)
rec_inp = np.dot(self.pfc.Jrec, self.pfc.activity)
md2pfc_weights = (self.md.MD2PFCMult / np.round(self.md.Num_MD / 2))
md2pfc = md2pfc_weights * rec_inp
md2pfc += np.dot(self.md.wMD2PFC / np.round(self.md.Num_MD /2), self.md_output)
pfc_output = self.pfc(input2pfc, md2pfc)
# pfc_output = pfc_output.reshape((1,pfc_output.shape[0]))
# md_output = self.md_output
# md_output = md_output.reshape((1,md_output.shape[0]))
if i==0:
self.pfc_output_t = pfc_output.reshape((1,pfc_output.shape[0]))
self.md_output_t = self.md_output.reshape((1,self.md_output.shape[0]))
else:
self.pfc_output_t = np.concatenate((self.pfc_output_t, pfc_output.reshape((1,pfc_output.shape[0]))),axis=0)
self.md_output_t = np.concatenate((self.md_output_t, self.md_output.reshape((1,self.md_output.shape[0]))),axis=0)
else:
pfc_output = self.pfc(input2pfc)
if i==0:
self.pfc_output_t = pfc_output.reshape((1,pfc_output.shape[0]))
else:
self.pfc_output_t = np.concatenate((self.pfc_output_t, pfc_output.reshape((1,pfc_output.shape[0]))),axis=0)
output[i] = self.pfc2out(pfc_output, target_t)
# for i in range(n_time):
# input_t = input[i]
# target_t = target[i]
#
# input2pfc = self.sensory2pfc(input_t)
# if self.MDeffect:
# self.md.MD2PFCMult = np.dot(self.md.wMD2PFCMult, self.md_output)
# rec_inp = np.dot(self.pfc.Jrec, self.pfc.activity)
# md2pfc = (self.md.MD2PFCMult / np.round(self.md.Num_MD / 2))
# md2pfc = md2pfc * rec_inp # minmax 5
# md2pfc += np.dot(self.md.wMD2PFC / np.round(self.md.Num_MD /2), self.md_output)
# pfc_output = self.pfc(input2pfc, md2pfc)
# self.md_output = self.md(pfc_output)
# if i==50:
# self.pfc_output_t = pfc_output
# else:
# pfc_output = self.pfc(input2pfc)
# output[i] = self.pfc2out(pfc_output, target_t)
return output
def _check_shape(self, input, target):
assert len(input.shape) == 2
assert len(target.shape) == 2
assert input.shape[0] == target.shape[0]
import torch
from torch import nn
class PytorchPFC(nn.Module):
def __init__(self, n_neuron, n_neuron_per_cue, pfcNoise, positiveRates=True, MDeffect=True, noisePresent = False):
super().__init__()
self.Nneur = n_neuron
self.Nsub = n_neuron_per_cue
self.useMult = True
self.noisePresent = noisePresent
self.noiseSD = pfcNoise #1e-2 # 1e-3
self.tau = 0.02
self.dt = 0.001
self.positiveRates = positiveRates
if self.positiveRates:
# only +ve rates
self.activation = lambda inp: torch.clip(torch.tanh(inp), 0, None)
else:
# both +ve/-ve rates as in Miconi
self.activation = lambda inp: torch.tanh(inp)
self.G = 0.75 # determines also the cross-task recurrence
if not MDeffect: self.G = 1.875
self.init_activity()
self.init_weights()
def init_activity(self):
self.xinp = torch.rand(self.Nneur) * 0.1
self.activity = self.activation(self.xinp)
def init_weights(self):
self.Jrec = torch.normal(mean=0, std=self.G / np.sqrt(self.Nsub * 2),
size=(self.Nneur, self.Nneur))
# make mean input to each row zero,
# helps to avoid saturation (both sides) for positive-only rates.
# see Nicola & Clopath 2016
# mean of rows i.e. across columns (axis 1),
# then expand with np.newaxis
# so that numpy's broadcast works on rows not columns
self.Jrec -= torch.mean(self.Jrec, dim=1).unsqueeze_(dim=1)
self.Jrec.requires_grad = True # Block when using PytorchMD and PytorchPFCMD.
def forward(self, input, input_x=None):
"""Run the network one step
For now, consider this network receiving input from PFC,
input stand for activity of PFC neurons
output stand for output current to PFC neurons
Args:
input: array (n_neuron,)
input_x: array (n_neuron,), modulatory input that multiplicatively
interact with the neurons
Returns:
output: array (n_output,)
"""
if input_x is None:
input_x = torch.zeros(input.shape)
xadd = torch.matmul(self.Jrec, self.activity)
xadd += input_x + input # MD inputs
self.xinp += self.dt / self.tau * (-self.xinp + xadd)
if self.noisePresent:
self.xinp += torch.normal(mean=0, std=self.noiseSD * np.sqrt(self.dt) / self.tau, size=(self.Nneur,))
rout = self.activation(self.xinp)
self.activity = rout
return rout
#model = PytorchPFC(n_neuron=10, n_neuron_per_cue=1)
#input = torch.randn(10)
#output = model(input)
#print(output.shape)
class PytorchMD(nn.Module):
def __init__(self, Nneur, Num_MD, num_active=1, positiveRates=True, dt=0.001):
super().__init__()
self.Nneur = Nneur
self.Num_MD = Num_MD
self.positiveRates = positiveRates
self.num_active = num_active # num_active: num MD active per context
self.tau = 0.02
self.tau_times = 4
self.dt = dt
self.tsteps = 200
self.Hebb_learning_rate = 1e-4
# working!
Gbase = 0.75 # determines also the cross-task recurrence
self.wPFC2MD = torch.normal(mean=0, std=1 / np.sqrt(
self.Num_MD * self.Nneur), size=(self.Num_MD, self.Nneur))
self.wMD2PFC = torch.normal(mean=0, std=1 / np.sqrt(
self.Num_MD * self.Nneur), size=(self.Nneur, self.Num_MD))
self.wMD2PFCMult = torch.normal(mean=0, std=1 / np.sqrt(
self.Num_MD * self.Nneur), size=(self.Nneur, self.Num_MD))
self.MDpreTrace = torch.zeros((self.Nneur))
self.MDpostTrace = torch.zeros((self.Num_MD))
self.MDpreTrace_threshold = 0
# Choose G based on the type of activation function
# unclipped activation requires lower G than clipped activation,
# which in turn requires lower G than shifted tanh activation.
if self.positiveRates:
self.G = Gbase
self.tauMD = self.tau * self.tau_times ##self.tau
else:
self.G = Gbase
self.MDthreshold = 0.4
self.tauMD = self.tau * 10 * self.tau_times
self.init_activity()
def init_activity(self):
self.MDinp = torch.zeros(self.Num_MD)
def forward(self, input, *args, **kwargs):
"""Run the network one step
For now, consider this network receiving input from PFC,
input stands for activity of PFC neurons
output stands for output current to MD neurons
Args:
input: array (n_input,)
Returns:
output: array (n_output,)
"""
# MD decays 10x slower than PFC neurons,
# so as to somewhat integrate PFC input
if self.positiveRates:
self.MDinp += self.dt / self.tauMD * \
(-self.MDinp + torch.matmul(self.wPFC2MD, input))
else: # shift PFC rates, so that mean is non-zero to turn MD on
self.MDinp += self.dt / self.tauMD * \
(-self.MDinp + torch.matmul(self.wPFC2MD, (input + 1. / 2)))
# num_active = np.round(self.Num_MD / self.Ntasks)
MDout = self.winner_take_all(self.MDinp)
self.update_weights(input, MDout)
return MDout
def update_trace(self, rout, MDout):
# MD presynaptic traces filtered over 5/10 trials
# Ideally one should weight them with MD syn weights,
# but syn plasticity just uses pre!
self.MDpreTrace += 1. / self.tsteps / 5. * \
(-self.MDpreTrace + rout)
self.MDpostTrace += 1. / self.tsteps / 5. * \
(-self.MDpostTrace + MDout)
# MDoutTrace = self.MDpostTrace
MDoutTrace = self.winner_take_all(self.MDpostTrace)
# MDoutTrace = np.zeros(self.Num_MD)
# MDpostTrace_sorted = np.sort(self.MDpostTrace)
# num_active = np.round(self.Num_MD / self.Ntasks)
# # MDthreshold = np.mean(MDpostTrace_sorted[-4:])
# MDthreshold = np.mean(
# MDpostTrace_sorted[-int(num_active) * 2:])
# # MDthreshold = np.mean(self.MDpostTrace)
# index_pos = np.where(self.MDpostTrace >= MDthreshold)
# index_neg = np.where(self.MDpostTrace < MDthreshold)
# MDoutTrace[index_pos] = 1
# MDoutTrace[index_neg] = 0
return MDoutTrace
def update_weights(self, rout, MDout):
"""Update weights with plasticity rules.
Args:
rout: input to MD
MDout: activity of MD
"""
MDoutTrace = self.update_trace(rout, MDout)
# if self.MDpostTrace[0] > self.MDpostTrace[1]: MDoutTrace = np.array([1,0])
# else: MDoutTrace = np.array([0,1])
self.MDpreTrace_threshold = torch.mean(self.MDpreTrace)
# self.MDpreTrace_threshold = np.mean(self.MDpreTrace[:self.Nsub * self.Ncues]) # first 800 cells are cue selective
# MDoutTrace_threshold = np.mean(MDoutTrace) #median
MDoutTrace_threshold = 0.5
wPFC2MDdelta = 0.5 * self.Hebb_learning_rate * torch.outer(MDoutTrace - MDoutTrace_threshold,
self.MDpreTrace - self.MDpreTrace_threshold)
# Update and clip the weights
self.wPFC2MD = torch.clip(self.wPFC2MD + wPFC2MDdelta, 0., 1.)
self.wMD2PFC = torch.clip(self.wMD2PFC + 0.1*(wPFC2MDdelta.T), -10., 0.) # 0.1
self.wMD2PFCMult = torch.clip(self.wMD2PFCMult + 0.1*(wPFC2MDdelta.T), 0., 7. / self.G) # 0.1
def winner_take_all(self, MDinp):
'''Winner take all on the MD
'''
# Thresholding
MDout = torch.zeros(self.Num_MD)
MDinp_sorted, ind_MDinp_sorted = torch.sort(MDinp)
# num_active = np.round(self.Num_MD / self.Ntasks)
MDthreshold = torch.mean(MDinp_sorted[-int(self.num_active) * 2:])
#MDthreshold = MDinp_sorted[-int(self.num_active)]
# MDthreshold = np.mean(MDinp)
index_pos = torch.where(MDinp >= MDthreshold)
index_neg = torch.where(MDinp < MDthreshold)
MDout[index_pos] = 1
MDout[index_neg] = 0
return MDout
# class TempNetwork():
# def __init__(self, Num_PFC, n_neuron_per_cue, Num_MD, num_active,
# MDeffect=True):
# dt = 0.001
# self.pfc = PytorchPFC(Num_PFC, n_neuron_per_cue, MDeffect=MDeffect)
# self.sensory2pfc = SensoryInputLayer(
# n_sub=n_neuron_per_cue,
# n_cues=4,
# n_output=Num_PFC)
# self.sensory2pfc.torch(use_torch=True)
# self.pfc2out = OutputLayer(n_input=Num_PFC, n_out=2, dt=dt)
# self.pfc_output_t = np.array([])
# self.MDeffect = MDeffect
# if self.MDeffect:
# self.md = MD(Nneur=Num_PFC, Num_MD=Num_MD, num_active=num_active,
# dt=dt)
# self.md_output = np.zeros(Num_MD)
# index = np.random.permutation(Num_MD)
# self.md_output[index[:num_active]] = 1 # randomly set part of md_output to 1
# self.md_output_t = np.array([])
# def __call__(self, input, target, *args, **kwargs):
# """
# Args:
# input: (n_time, n_input)
# target: (n_time, n_output)
# """
# self._check_shape(input, target)
# n_time = input.shape[0]
# tsteps = 200
# self.pfc.init_activity() # Reinit PFC activity
# pfc_output = self.pfc.activity.numpy()
# if self.MDeffect:
# self.md.init_activity() # Reinit MD activity
# output = np.zeros((n_time, target.shape[-1]))
# self.pfc_output_t *= 0
# if self.MDeffect:
# self.md_output_t *= 0
# for i in range(n_time):
# input_t = input[i]
# target_t = target[i]
# if i % tsteps == 0: # Reinit activity for each trial
# self.pfc.init_activity() # Reinit PFC activity
# pfc_output = self.pfc.activity.numpy()
# if self.MDeffect:
# self.md.init_activity() # Reinit MD activity
# input2pfc = self.sensory2pfc(input_t)
# #import pdb;pdb.set_trace()
# if self.MDeffect:
# self.md_output = self.md(pfc_output)
# self.md.MD2PFCMult = np.dot(self.md.wMD2PFCMult, self.md_output)
# rec_inp = np.dot(self.pfc.Jrec, self.pfc.activity)
# md2pfc_weights = (self.md.MD2PFCMult / np.round(self.md.Num_MD / 2))
# md2pfc = md2pfc_weights * rec_inp
# md2pfc += np.dot(self.md.wMD2PFC / np.round(self.md.Num_MD /2), self.md_output)
# pfc_output = self.pfc(torch.from_numpy(input2pfc),
# torch.from_numpy(md2pfc)).numpy()
# if i==0:
# self.pfc_output_t = pfc_output.reshape((1,pfc_output.shape[0]))
# self.md_output_t = self.md_output.reshape((1,self.md_output.shape[0]))
# else:
# self.pfc_output_t = np.concatenate((self.pfc_output_t, pfc_output.reshape((1,pfc_output.shape[0]))),axis=0)
# self.md_output_t = np.concatenate((self.md_output_t, self.md_output.reshape((1,self.md_output.shape[0]))),axis=0)
# else:
# pfc_output = self.pfc(torch.from_numpy(input2pfc)).numpy()
# if i==0:
# self.pfc_output_t = pfc_output.reshape((1,pfc_output.shape[0]))
# else:
# self.pfc_output_t = np.concatenate((self.pfc_output_t, pfc_output.reshape((1,pfc_output.shape[0]))),axis=0)
# output[i] = self.pfc2out(pfc_output, target_t)
# return output
# def _check_shape(self, input, target):
# assert len(input.shape) == 2
# assert len(target.shape) == 2
# assert input.shape[0] == target.shape[0]
class PytorchPFCMD(nn.Module):
def __init__(self, Num_PFC, n_neuron_per_cue, n_cues, Num_MD, num_active, num_output, pfcNoise, MDeffect=True, noisePresent = False, noiseInput = False):
super().__init__()
"""
additional noise input neuron if noiseInput is true
"""
dt = 0.001
if noiseInput==False:
self.sensory2pfc = SensoryInputLayer(
n_sub=n_neuron_per_cue,
n_cues=n_cues,
n_output=Num_PFC)
self.sensory2pfc.torch(use_torch=True)
# try learnable input weights
# self.PytorchSensory2pfc = nn.Linear(4, Num_PFC)
else:
self.sensory2pfc = SensoryInputLayer_NoiseNeuro(
n_sub=n_neuron_per_cue,
n_cues=n_cues,
n_output=Num_PFC)
self.sensory2pfc.torch(use_torch=True)
self.pfc = PytorchPFC(Num_PFC, n_neuron_per_cue, pfcNoise, MDeffect=MDeffect, noisePresent = noisePresent)
#self.pfc2out = OutputLayer(n_input=Num_PFC, n_out=2, dt=dt)
self.pfc2out = nn.Linear(Num_PFC, num_output)
#self.pfc_output_t = np.array([])
self.MDeffect = MDeffect
if self.MDeffect:
self.md = MD(Nneur=Num_PFC, Num_MD=Num_MD, num_active=num_active,
dt=dt)
self.md_output = np.zeros(Num_MD)
index = np.random.permutation(Num_MD)
self.md_output[index[:num_active]] = 1 # randomly set part of md_output to 1
self.md_output_t = np.array([])
self.num_output = num_output