-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.py
199 lines (176 loc) · 5.33 KB
/
sim.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
#!/usr/bin/env python
import sys
import numpy
import matplotlib.pyplot as plt
if sys.argv[1] == "sleepON":
print("offline plasticity ON.")
homeoON = True
LTDON = True
elif sys.argv[1] == "sleepOFF":
print("offline plasticity OFF.")
homeoON = False
LTDON = False
elif sys.argv[1] == "homeostasisOFF":
print("homeostatic plasticity OFF.")
homeoON = False
LTDON = True
elif sys.argv[1] == "sleepLTDOFF":
print("sleepLTD OFF.")
homeoON = True
LTDON = False
else:
print("please specify sleepON, sleepOFF, homeostasisOFF, sleepLTDOFF.")
exit()
#parameters
N_CA1 = 400
N_CA3 = 400
N_inh = 100
p_CA3 = 0.1
p_EtoI = 0.1
p_ItoE = 0.05
wbase_CA3 = 5.0 / (N_CA3*p_CA3)
wbase_EtoI = 16.0 /(N_CA1*p_EtoI)
wbase_ItoE = 8.0 /(N_inh*p_ItoE)
eta = 2.0 / (N_CA3*p_CA3)
engram_threshold = 0.5
beta_som = 5.0
thr_som = 1.0
noise_amp = 0.5
Npattern = 3
Nreplay = 1000
replay_bias = 0.8
tau = 2.0 #ms
sim_pitch = 0.1 #ms
sim_len_ms = 20.0 #ms
sim_len_sample = int(sim_len_ms/sim_pitch)
timearr_ms = sim_len_ms/sim_len_sample*numpy.arange(sim_len_sample)
#weights
w_CA3 = wbase_CA3 * numpy.random.rand(N_CA1, N_CA3)
w_EtoI = wbase_EtoI * (numpy.random.rand(N_inh, N_CA1)<p_EtoI)
w_ItoE = wbase_ItoE * (numpy.random.rand(N_CA1, N_inh)<p_ItoE)
#input patterns
pattern_CA3 = (numpy.random.rand(Npattern, N_CA3)<p_CA3)
#functions
def step(x):
return x>0
def sigmoid(x, beta, thr):
return 1.0/(1.0+numpy.exp(-beta*(x-thr)))
def relu(x):
return numpy.maximum(x, 0.0)
def somrate(x):
return sigmoid(x, beta_som, thr_som)
def fluctuate(N):
return 0.5+1.0*numpy.random.rand(N)
def simulation_batch(r_CA3):
#initialize activity
r_exc = numpy.zeros(N_CA1)
r_inh = numpy.zeros(N_inh)
#setting inputs
CA3_input = w_CA3 @ r_CA3 + noise_amp*numpy.random.randn(N_CA1)
#simulation
for t in range(sim_len_sample):
input_E = CA3_input - w_ItoE@r_inh
input_I = w_EtoI@r_exc
r_exc = r_exc + sim_pitch * (-r_exc + somrate(input_E)) / tau
r_inh = r_inh + sim_pitch * (-r_inh + somrate(input_I)) / tau
#results:
return r_exc
######### simulation ###########
print("simulation start")
#before learning
preplay_log = numpy.zeros([Nreplay, N_CA1])
print("pre-learning")
for i in range(Nreplay):
CA3_activity = (numpy.random.rand(N_CA3)<p_CA3) * fluctuate(N_CA3)
s = simulation_batch(CA3_activity)
preplay_log[i,:] = s
r_som_pre = numpy.zeros([Npattern, N_CA1])
for i in range(Npattern):
r_som_pre[i,:] = simulation_batch(pattern_CA3[i,:])
#awake 1
print("awake 1")
LTPtag = numpy.zeros_like(w_CA3)
#learning
CA3_activity = pattern_CA3[0,:]
s = simulation_batch(CA3_activity)
engram = (s>engram_threshold)
w_CA3 = w_CA3 + eta * numpy.outer(engram, CA3_activity)
non_engram = numpy.logical_not(engram)
print("ratio of engram: ", numpy.mean(engram))
#response
r_som_awake1 = numpy.zeros([Npattern, N_CA1])
for i in range(Npattern):
r_som_awake1[i,:] = simulation_batch(pattern_CA3[i,:])
#sleep
print("sleep")
CA3_engram = pattern_CA3[0,:]
CA3_nonengram = numpy.logical_not(CA3_engram)
#offline plasticity
#SWR-LTD
if LTDON:
w_CA3 = relu(w_CA3 - eta * numpy.outer(non_engram, CA3_engram))
#scaling
if homeoON:
w_CA3 = relu(w_CA3 - eta * numpy.outer(engram, CA3_nonengram))
if homeoON and LTDON:
w_CA3 = relu(w_CA3 + eta * numpy.outer(non_engram, CA3_nonengram))
#no LTD in non-engram -> no LTP
#simulation of activity
replay_log = numpy.zeros([Nreplay, N_CA1])
for i in range(Nreplay):
if numpy.random.rand()<replay_bias:
CA3_activity = pattern_CA3[0,:] * fluctuate(N_CA3)
else:
CA3_activity = (numpy.random.rand(N_CA3)<p_CA3) * fluctuate(N_CA3)
s = simulation_batch(CA3_activity)
replay_log[i,:] = s
r_som_sleep1 = numpy.zeros([Npattern, N_CA1])
for i in range(Npattern):
r_som_sleep1[i,:] = simulation_batch(pattern_CA3[i,:])
#awake 2
print("awake 2")
#learning
CA3_activity = pattern_CA3[1,:]
s = simulation_batch(CA3_activity)
engram2 = (s>engram_threshold)
w_CA3 = w_CA3 + eta * numpy.outer(engram2, CA3_activity)
non_engram2 = numpy.logical_not(engram2)
#simulation
r_som_awake2 = numpy.zeros([Npattern, N_CA1])
for i in range(Npattern):
r_som_awake2[i,:] = simulation_batch(pattern_CA3[i,:])
#sleep 2
print("sleep 2")
CA3_engram2 = pattern_CA3[1,:]
CA3_nonengram2 = numpy.logical_not(CA3_engram2)
#SWR-LTD
w_CA3 = relu(w_CA3 - eta * numpy.outer(non_engram2, CA3_engram2))
#scaling
w_CA3 = relu(w_CA3 - eta * numpy.outer(engram2, CA3_nonengram2))
w_CA3 = relu(w_CA3 + eta * numpy.outer(non_engram2, CA3_nonengram2))
#simulation of activity
replay_log_afterB = numpy.zeros([Nreplay, N_CA1])
for i in range(Nreplay):
if numpy.random.rand()<replay_bias:
CA3_activity = pattern_CA3[1,:] * fluctuate(N_CA3)
else:
CA3_activity = (numpy.random.rand(N_CA3)<p_CA3) * fluctuate(N_CA3)
s = simulation_batch(CA3_activity)
replay_log_afterB[i,:] = s
r_som_sleep2 = numpy.zeros([Npattern, N_CA1])
for i in range(Npattern):
r_som_sleep2[i,:] = simulation_batch(pattern_CA3[i,:])
print("simulation end.")
#save results
results = []
numpy.savez("results.npz",
engram=engram,
r_som_pre=r_som_pre,
r_som_awake1=r_som_awake1,
r_som_sleep1=r_som_sleep1,
r_som_awake2=r_som_awake2,
r_som_sleep2=r_som_sleep2,
preplay_log=preplay_log,
replay_log=replay_log,
replay_log_afterB=replay_log_afterB,
)