Debian Codegraph @

examples/frompapers/Nicola_Clopath_2017.py

raw
1 #!/usr/bin/env python3
2 """
3 FORCE training of a Leaky IF model to mimic a sinusoid (5 Hz) oscillator
4
5 Nicola, W., Clopath, C.
6 Supervised learning in spiking neural networks with FORCE training
7 Nat Commun 8, 2208 (2017)
8
9 https://doi.org/10.1038/s41467-017-01827-3
10
11 Based on https://github.com/ModelDBRepository/190565/blob/master/CODE%20FOR%20FIGURE%202/LIFFORCESINE.m
12
13 Sebastian Schmitt, 2022
14 """
15
16 from brian2 import NeuronGroup, Synapses, StateMonitor, SpikeMonitor
17 from brian2 import run, defaultclock, network_operation
18 from brian2 import ms, second, Hz
19 import matplotlib.pyplot as plt
20 from matplotlib.ticker import MaxNLocator
21 import numpy as np
22
23 # set seed for reproducible figures
24 np.random.seed(1)
25
26 # decay time of synaptic kernal
27 td = 20*ms
28
29 # rise time of synaptic kernal
30 tr = 2*ms
31
32 # membrane time constant
33 tm = 10*ms
34
35 # refractory period
36 tref = 2*ms
37
38 # reset potential
39 vreset = -65
40
41 # peak/threshold potential
42 vpeak = -40
43
44 # bias
45 BIAS = vpeak
46
47 # integration time step
48 defaultclock.dt = 0.05*ms
49
50 # total duration of simulation
51 T = 15*second
52
53 # start of training
54 imin = 5*second
55
56 # end of training
57 icrit = 10*second
58
59 # interval of training
60 step = 2.5*ms
61
62 # feedback scale factor
63 Q = 10
64
65 # neuron-to-neuron connection scale factor
66 G = 0.04
67
68 # connection probability
69 p = 0.1
70
71 # number of neurons
72 N = 2000
73
74 # correlation weight matrix for RLMS
75 alpha = defaultclock.dt/second*0.1
76 Pinv = np.eye(N)*alpha
77
78 # Sinusoid oscillator
79 def zx(t):
80 freq = 5*Hz
81 return np.sin(2*np.pi*freq*t)
82
83 neurons = NeuronGroup(N,
84 """
85 dv/dt = (-v + BIAS + IPSC + E*z)/tm: 1 (unless refractory)
86 dIPSC/dt = -IPSC/tr + h : 1
87 dh/dt = -h/td : 1/second
88 dr/dt = -r/tr + hr : 1
89 dhr/dt = -hr/td : 1/second
90 BPhi : 1
91 z : 1 (shared)
92 E : 1
93 """,
94 method="euler",
95 threshold="v>=vpeak",
96 reset="v=vreset; hr += 1/(tr*td)*second",
97 refractory=tref)
98
99 # fixed feedback weights
100 neurons.E = (2*np.random.uniform(size=N)-1)*Q
101
102 # initial membrane voltage
103 neurons.v = vreset + np.random.uniform(size=N)*(30-vreset)
104
105 synapses = Synapses(neurons, neurons, "w : second", on_pre="h += w/(tr*td)")
106 synapses.connect()
107 omega = G*(np.random.normal(size=(N,N))*(np.random.uniform(size=(N,N))<p))/(np.sqrt(N)*p)
108 synapses.w = omega.flatten()*second
109
110 spikemon = SpikeMonitor(neurons[:20])
111 statemon_BPhi = StateMonitor(neurons, "BPhi", record=range(10))
112 statemon_z = StateMonitor(neurons, "z", record=[0])
113
114 # linear readout
115 @network_operation(dt=defaultclock.dt)
116 def readout(t):
117 neurons.z = np.dot(neurons.BPhi, neurons.r)
118
119 # FORCE training
120 @network_operation(dt=step)
121 def train(t):
122 global Pinv
123 if t > imin and t < icrit:
124 cd = [email protected]
125 err = neurons.z - zx(t)
126 neurons.BPhi -= cd*err
127 Pinv -= np.outer(cd,cd)/( 1 + np.dot(neurons.r, cd))
128
129 run(T, report="text")
130
131 fig, axes = plt.subplots(2,2, figsize=(10,10))
132 axes = axes.flatten()
133
134 axes[0].set_title("Spike raster")
135 axes[0].scatter(spikemon.t/second,spikemon.i, marker='|', linestyle="None", color="black", s=100)
136 axes[0].set_xlim((imin-2*second)/second, imin/second+2)
137 axes[0].set_ylim(0, len(spikemon.source))
138 axes[0].set_xlabel("t [s]")
139 axes[0].set_ylabel("Neuron")
140 axes[0].yaxis.set_major_locator(MaxNLocator(integer=True))
141
142 axes[1].plot(statemon_z.t/second, zx(statemon_z.t), linestyle='--', color='k')
143 axes[1].plot(statemon_z.t/second,statemon_z.z[0])
144
145 axes[1].set_title("Target and readout")
146 axes[1].annotate('RLS ON', xy=(imin/second, -1.05), xytext=(imin/second, -1.35),
147 arrowprops=dict(facecolor='black', shrink=1), ha="center")
148 axes[1].annotate('RLS OFF', xy=(icrit/second, -1.05), xytext=(icrit/second, -1.35),
149 arrowprops=dict(facecolor='black', shrink=1), ha="center")
150 axes[1].set_xlabel("t [s]")
151 axes[1].set_xlim((imin-1*second)/second, T/second)
152 axes[1].set_ylim(-1.4,1.1)
153
154 axes[2].set_title("Error")
155 axes[2].plot(statemon_z.t/second, statemon_z.z[0] - zx(statemon_z.t))
156 axes[2].annotate('RLS ON', xy=(imin/second, -0.15), xytext=(imin/second, -0.4),
157 arrowprops=dict(facecolor='black', shrink=1), ha="center")
158 axes[2].annotate('RLS OFF', xy=(icrit/second, -0.15), xytext=(icrit/second, -0.4),
159 arrowprops=dict(facecolor='black', shrink=1), ha="center")
160 axes[2].set_xlabel("t [s]")
161 axes[2].set_xlim((imin-1*second)/second, T/second)
162 axes[2].set_ylim(-1,1)
163
164 axes[3].set_title("Decoders")
165 for j in range(len(statemon_BPhi.record)):
166 axes[3].plot(statemon_BPhi.t/second,statemon_BPhi.BPhi[j])
167 axes[3].set_xlim((imin-1*second)/second, T/second)
168 axes[3].set_xlabel("t [s]")
169 axes[3].set_ylim(-0.00020, 0.00015)
170 axes[3].set_yticklabels([])
171 axes[3].annotate('RLS ON', xy=(imin/second, -0.0001455), xytext=(imin/second, -0.00019),
172 arrowprops=dict(facecolor='black', shrink=1), ha="center")
173 axes[3].annotate('RLS OFF', xy=(icrit/second, -0.0001455), xytext=(icrit/second, -0.00019),
174 arrowprops=dict(facecolor='black', shrink=1), ha="center")
175
176 fig.tight_layout()