|
| 1 | +import brainpy as bp |
| 2 | + |
| 3 | +neu_pars = dict(V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5., |
| 4 | + V_initializer=bp.init.Normal(-55., 2.)) |
| 5 | + |
| 6 | + |
| 7 | +class EICOBA_PreAlign(bp.DynamicalSystemNS): |
| 8 | + def __init__(self, num_exc, num_inh, inp=20.): |
| 9 | + super().__init__() |
| 10 | + |
| 11 | + self.inp = inp |
| 12 | + self.E = bp.dyn.LifRefLTC(num_exc, **neu_pars) |
| 13 | + self.I = bp.dyn.LifRefLTC(num_inh, **neu_pars) |
| 14 | + |
| 15 | + self.E2I = bp.dyn.ProjAlignPre( |
| 16 | + pre=self.E, |
| 17 | + syn=bp.dyn.Expon.desc(self.E.varshape, tau=5.), |
| 18 | + delay=None, |
| 19 | + comm=bp.dnn.CSRLinear(bp.conn.FixedProb(0.02, pre=self.E.num, post=self.I.num), 0.6), |
| 20 | + out=bp.dyn.COBA(E=0.), |
| 21 | + post=self.I, |
| 22 | + ) |
| 23 | + self.E2E = bp.dyn.ProjAlignPre( |
| 24 | + pre=self.E, |
| 25 | + syn=bp.dyn.Expon.desc(self.E.varshape, tau=5.), |
| 26 | + delay=None, |
| 27 | + comm=bp.dnn.CSRLinear(bp.conn.FixedProb(0.02, pre=self.E.num, post=self.E.num), 0.6), |
| 28 | + out=bp.dyn.COBA(E=0.), |
| 29 | + post=self.E, |
| 30 | + ) |
| 31 | + self.I2E = bp.dyn.ProjAlignPre( |
| 32 | + pre=self.I, |
| 33 | + syn=bp.dyn.Expon.desc(self.I.varshape, tau=10.), |
| 34 | + delay=None, |
| 35 | + comm=bp.dnn.CSRLinear(bp.conn.FixedProb(0.02, pre=self.I.num, post=self.E.num), 6.7), |
| 36 | + out=bp.dyn.COBA(E=-80.), |
| 37 | + post=self.E, |
| 38 | + ) |
| 39 | + self.I2I = bp.dyn.ProjAlignPre( |
| 40 | + pre=self.I, |
| 41 | + syn=bp.dyn.Expon.desc(self.I.varshape, tau=10.), |
| 42 | + delay=0., |
| 43 | + comm=bp.dnn.CSRLinear(bp.conn.FixedProb(0.02, pre=self.I.num, post=self.I.num), 6.7), |
| 44 | + out=bp.dyn.COBA(E=-80.), |
| 45 | + post=self.I, |
| 46 | + ) |
| 47 | + |
| 48 | + def update(self): |
| 49 | + self.E2I() |
| 50 | + self.I2I() |
| 51 | + self.I2E() |
| 52 | + self.E2E() |
| 53 | + self.E(self.inp) |
| 54 | + self.I(self.inp) |
| 55 | + |
| 56 | + |
| 57 | +class EICOBA_PostAlign(bp.DynamicalSystemNS): |
| 58 | + def __init__(self, num_exc, num_inh, inp=20.): |
| 59 | + super().__init__() |
| 60 | + self.inp = inp |
| 61 | + |
| 62 | + self.E = bp.dyn.LifRefLTC(num_exc, **neu_pars) |
| 63 | + self.I = bp.dyn.LifRefLTC(num_inh, **neu_pars) |
| 64 | + |
| 65 | + self.E2E = bp.dyn.ProjAlignPost( |
| 66 | + pre=self.E, |
| 67 | + delay=None, |
| 68 | + comm=bp.dnn.EventCSRLinear(bp.conn.FixedProb(0.02, pre=self.E.num, post=self.E.num), 0.6), |
| 69 | + syn=bp.dyn.Expon.desc(self.E.varshape, tau=5.), |
| 70 | + out=bp.dyn.COBA.desc(E=0.), |
| 71 | + post=self.E, |
| 72 | + ) |
| 73 | + self.E2I = bp.dyn.ProjAlignPost( |
| 74 | + pre=self.E, |
| 75 | + delay=None, |
| 76 | + comm=bp.dnn.EventCSRLinear(bp.conn.FixedProb(0.02, pre=self.E.num, post=self.I.num), 0.6), |
| 77 | + syn=bp.dyn.Expon.desc(self.I.varshape, tau=5.), |
| 78 | + out=bp.dyn.COBA.desc(E=0.), |
| 79 | + post=self.I, |
| 80 | + ) |
| 81 | + self.I2E = bp.dyn.ProjAlignPost( |
| 82 | + pre=self.I, |
| 83 | + delay=None, |
| 84 | + comm=bp.dnn.EventCSRLinear(bp.conn.FixedProb(0.02, pre=self.I.num, post=self.E.num), 6.7), |
| 85 | + syn=bp.dyn.Expon.desc(self.E.varshape, tau=10.), |
| 86 | + out=bp.dyn.COBA.desc(E=-80.), |
| 87 | + post=self.E, |
| 88 | + ) |
| 89 | + self.I2I = bp.dyn.ProjAlignPost( |
| 90 | + pre=self.I, |
| 91 | + delay=None, |
| 92 | + comm=bp.dnn.EventCSRLinear(bp.conn.FixedProb(0.02, pre=self.I.num, post=self.I.num), 6.7), |
| 93 | + syn=bp.dyn.Expon.desc(self.I.varshape, tau=10.), |
| 94 | + out=bp.dyn.COBA.desc(E=-80.), |
| 95 | + post=self.I, |
| 96 | + ) |
| 97 | + |
| 98 | + def update(self): |
| 99 | + self.E2I() |
| 100 | + self.I2I() |
| 101 | + self.I2E() |
| 102 | + self.E2E() |
| 103 | + self.E(self.inp) |
| 104 | + self.I(self.inp) |
| 105 | + |
| 106 | + |
| 107 | +class EINet(bp.Network): |
| 108 | + def __init__(self, scale=1.0, method='exp_auto'): |
| 109 | + # network size |
| 110 | + num_exc = int(3200 * scale) |
| 111 | + num_inh = int(800 * scale) |
| 112 | + |
| 113 | + # neurons |
| 114 | + pars = dict(V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5., |
| 115 | + V_initializer=bp.init.Normal(-55., 2.)) |
| 116 | + E = bp.neurons.LIF(num_exc, **pars, method=method) |
| 117 | + I = bp.neurons.LIF(num_inh, **pars, method=method) |
| 118 | + |
| 119 | + # synapses |
| 120 | + we = 0.6 / scale # excitatory synaptic weight (voltage) |
| 121 | + wi = 6.7 / scale # inhibitory synaptic weight |
| 122 | + E2E = bp.synapses.Exponential(E, E, bp.conn.FixedProb(prob=0.02), |
| 123 | + g_max=we, tau=5., method=method, |
| 124 | + output=bp.synouts.COBA(E=0.)) |
| 125 | + E2I = bp.synapses.Exponential(E, I, bp.conn.FixedProb(prob=0.02), |
| 126 | + g_max=we, tau=5., method=method, |
| 127 | + output=bp.synouts.COBA(E=0.)) |
| 128 | + I2E = bp.synapses.Exponential(I, E, bp.conn.FixedProb(prob=0.02), |
| 129 | + g_max=wi, tau=10., method=method, |
| 130 | + output=bp.synouts.COBA(E=-80.)) |
| 131 | + I2I = bp.synapses.Exponential(I, I, bp.conn.FixedProb(prob=0.02), |
| 132 | + g_max=wi, tau=10., method=method, |
| 133 | + output=bp.synouts.COBA(E=-80.)) |
| 134 | + |
| 135 | + super(EINet, self).__init__(E2E, E2I, I2E, I2I, E=E, I=I) |
| 136 | + |
| 137 | + |
| 138 | +# num_device = 8 |
| 139 | +# bm.set_host_device_count(num_device) |
| 140 | +# bm.sharding.set(mesh_axes=(bp.dyn.PNEU_AXIS,), mesh_shape=(num_device, )) |
| 141 | + |
| 142 | +def run3(): |
| 143 | + net = EICOBA_PreAlign(3200, 800) |
| 144 | + runner = bp.DSRunner(net, monitors={'E.spike': net.E.spike}) |
| 145 | + print(runner.run(100., eval_time=True)) |
| 146 | + bp.visualize.raster_plot(runner.mon.ts, runner.mon['E.spike'], show=True) |
| 147 | + |
| 148 | + |
| 149 | +def run1(): |
| 150 | + net = EICOBA_PostAlign(3200, 800) |
| 151 | + runner = bp.DSRunner(net, monitors={'E.spike': net.E.spike}) |
| 152 | + print(runner.run(100., eval_time=True)) |
| 153 | + bp.visualize.raster_plot(runner.mon.ts, runner.mon['E.spike'], show=True) |
| 154 | + |
| 155 | + |
| 156 | +def run2(): |
| 157 | + net = EINet() |
| 158 | + runner = bp.DSRunner(net, |
| 159 | + monitors=['E.spike'], |
| 160 | + inputs=[('E.input', 20.), ('I.input', 20.)]) |
| 161 | + r = runner.run(100., eval_time=True) |
| 162 | + print(r) |
| 163 | + bp.visualize.raster_plot(runner.mon.ts, runner.mon['E.spike'], show=True) |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == '__main__': |
| 167 | + # run1() |
| 168 | + # run2() |
| 169 | + run3() |
0 commit comments