def test_lif(num, device): print('Scale:{}, Model:LIF, Device:{}, '.format(num, device), end='') st_build = time.time() bp.profile.set(jit=True, device=device, dt=0.1, numerical_method='exponential') V_rest = -52.0 V_reset = -60.0 V_th = -50.0 neu = bpmodels.neurons.get_LIF(V_rest=V_rest, V_reset=V_reset, V_th=V_th, noise=0., mode='scalar') syn = bpmodels.synapses.get_exponential(tau_decay=2.0, mode='scalar') num_exc = int(num / 2) num_inh = int(num / 2) prob = 0.01 JE = 1 / np.sqrt(prob * num_exc) JI = 1 / np.sqrt(prob * num_inh) group = bp.NeuGroup(neu, geometry=num_exc + num_inh, monitors=['spike']) group.ST['V'] = np.random.random(num_exc + num_inh) * (V_th - V_rest) + V_rest exc_conn = bp.SynConn(syn, pre_group=group[:num_exc], post_group=group, conn=bp.connect.FixedProb(prob=prob)) exc_conn.ST['g'] = JE inh_conn = bp.SynConn(syn, pre_group=group[num_exc:], post_group=group, conn=bp.connect.FixedProb(prob=prob)) exc_conn.ST['g'] = -JI net = bp.Network(group, exc_conn, inh_conn) ed_build = time.time() st_run = time.time() net.run(duration=1000., inputs=(group, 'ST.input', 3.)) ed_run = time.time() build_time = float(ed_build - st_build) run_time = float(ed_run - st_run) print('BuildT:{:.2f}s, RunT:{:.2f}s'.format(build_time, run_time)) return run_time, build_time
if pre['sp'][pre_id] > 0.: post_ids = pre2post[pre_id] post['gi'][post_ids] += wi exc_syn = bp.SynType('exc_syn', steps=exc_update, ST=bp.types.SynState([])) inh_syn = bp.SynType('inh_syn', steps=inh_update, ST=bp.types.SynState([])) group = bp.NeuGroup(neuron, geometry=num_exc + num_inh, monitors=['sp']) group.ST['V'] = El + (np.random.randn(num_exc + num_inh) * 5 - 5) group.ST['ge'] = (np.random.randn(num_exc + num_inh) * 1.5 + 4) * 10. / unit group.ST['gi'] = (np.random.randn(num_exc + num_inh) * 12 + 20) * 10. / unit exc_conn = bp.SynConn(exc_syn, pre_group=group[:num_exc], post_group=group, conn=bp.connect.FixedProb(prob=0.02)) inh_conn = bp.SynConn(inh_syn, pre_group=group[num_exc:], post_group=group, conn=bp.connect.FixedProb(prob=0.02)) net = bp.Network(group, exc_conn, inh_conn) t0 = time.time() net.run(10 * 1000., report=True) print('Used time {} s.'.format(time.time() - t0)) bp.visualize.raster_plot(net.ts, group.mon.sp, show=True)
def run_brianpy(num_neu, duration, device='cpu'): num_inh = int(num_neu / 5) num_exc = num_neu - num_inh bp.profile.set(jit=True, device=device, dt=dt) # Parameters taum = 20 taue = 5 taui = 10 Vt = -50 Vr = -60 El = -60 Erev_exc = 0. Erev_inh = -80. I = 20. we = 0.6 # excitatory synaptic weight (voltage) wi = 6.7 # inhibitory synaptic weight ref = 5.0 neu_ST = bp.types.NeuState({ 'sp_t': -1e7, 'V': Vr, 'spike': 0., 'ge': 0., 'gi': 0. }) @bp.integrate def int_ge(ge, t): return -ge / taue @bp.integrate def int_gi(gi, t): return -gi / taui @bp.integrate def int_V(V, t, ge, gi): return (ge * (Erev_exc - V) + gi * (Erev_inh - V) + (El - V) + I) / taum def neu_update(ST, _t): ST['ge'] = int_ge(ST['ge'], _t) ST['gi'] = int_gi(ST['gi'], _t) ST['spike'] = 0. if (_t - ST['sp_t']) > ref: V = int_V(ST['V'], _t, ST['ge'], ST['gi']) ST['spike'] = 0. if V >= Vt: ST['V'] = Vr ST['spike'] = 1. ST['sp_t'] = _t else: ST['V'] = V neuron = bp.NeuType(name='COBA', ST=neu_ST, steps=neu_update, mode='scalar') def syn_update1(pre, post, pre2post): for pre_id in range(len(pre2post)): if pre['spike'][pre_id] > 0.: post_ids = pre2post[pre_id] for i in post_ids: post['ge'][i] += we exc_syn = bp.SynType('exc_syn', steps=syn_update1, ST=bp.types.SynState([]), mode='vector') def syn_update2(pre, post, pre2post): for pre_id in range(len(pre2post)): if pre['spike'][pre_id] > 0.: post_ids = pre2post[pre_id] for i in post_ids: post['gi'][i] += wi inh_syn = bp.SynType('inh_syn', steps=syn_update2, ST=bp.types.SynState([]), mode='vector') group = bp.NeuGroup(neuron, geometry=num_exc + num_inh) group.ST['V'] = np.random.randn(num_exc + num_inh) * 5. - 55. exc_conn = bp.SynConn(exc_syn, pre_group=group[:num_exc], post_group=group, conn=bp.connect.FixedProb(prob=0.02)) inh_conn = bp.SynConn(inh_syn, pre_group=group[num_exc:], post_group=group, conn=bp.connect.FixedProb(prob=0.02)) net = bp.Network(group, exc_conn, inh_conn) t0 = time.time() net.run(duration) t = time.time() - t0 print(f'BrainPy ({device}) used time {t} s.') return t
# Set pre & post NeuGroup pre = bp.NeuGroup(LIF_neuron, geometry=(10, ), monitors=['V', 'input', 'spike']) pre.runner.set_schedule(['input', 'update', 'monitor', 'reset']) pre.ST['V'] = -65. post = bp.NeuGroup(LIF_neuron, geometry=(10, ), monitors=['V', 'input', 'spike']) post.runner.set_schedule(['input', 'update', 'monitor', 'reset']) pre.ST['V'] = -65. # Set synapse connection & network two_exponentials = bp.SynConn(model=two_exponentials_syn, pre_group=pre, post_group=post, conn=bp.connect.All2All(), monitors=['g'], delay=10.) two_exponentials.runner.set_schedule( ['input', 'update', 'output', 'monitor']) net = bp.Network(pre, two_exponentials, post) current = bp.inputs.spike_current([5., 10., 15., 20.], bp.profile._dt, 1., duration=duration) net.run(duration=duration, inputs=[two_exponentials, 'pre.spike', current, "="], report=True) # Figure
def test_hh(num, device): print('Scale:{}, Model:HH, Device:{}, '.format(num, device), end='') st_build = time.time() bp.profile.set(jit=True, device=device, dt=0.1, numerical_method='exponential') num_exc = int(num * 0.8) num_inh = int(num * 0.2) num = num_exc + num_inh Cm = 200 # Membrane Capacitance [pF] gl = 10. # Leak Conductance [nS] El = -60. # Resting Potential [mV] g_Na = 20. * 1000 ENa = 50. # reversal potential (Sodium) [mV] g_Kd = 6. * 1000 # K Conductance [nS] EK = -90. # reversal potential (Potassium) [mV] VT = -63. Vt = -20. # Time constants taue = 5. # Excitatory synaptic time constant [ms] taui = 10. # Inhibitory synaptic time constant [ms] # Reversal potentials Ee = 0. # Excitatory reversal potential (mV) Ei = -80. # Inhibitory reversal potential (Potassium) [mV] # excitatory synaptic weight we = 6.0 * np.sqrt(3200) / np.sqrt( num_exc) # excitatory synaptic conductance [nS] # inhibitory synaptic weight wi = 67.0 * np.sqrt(800) / np.sqrt( num_inh) # inhibitory synaptic conductance [nS] inf = 0.05 neu_ST = bp.types.NeuState('V', 'm', 'n', 'h', 'sp', 'ge', 'gi') @bp.integrate def int_ge(ge, t): return -ge / taue @bp.integrate def int_gi(gi, t): return -gi / taui @bp.integrate def int_m(m, t, V): a = 13.0 - V + VT b = V - VT - 40.0 m_alpha = 0.32 * a / (exp(a / 4.) - 1.) m_beta = 0.28 * b / (exp(b / 5.) - 1.) dmdt = (m_alpha * (1. - m) - m_beta * m) return dmdt @bp.integrate def int_m_zeroa(m, t, V): b = V - VT - 40.0 m_alpha = 0.32 m_beta = 0.28 * b / (exp(b / 5.) - 1.) dmdt = (m_alpha * (1. - m) - m_beta * m) return dmdt @bp.integrate def int_m_zerob(m, t, V): a = 13.0 - V + VT m_alpha = 0.32 * a / (exp(a / 4.) - 1.) m_beta = 0.28 dmdt = (m_alpha * (1. - m) - m_beta * m) return dmdt @bp.integrate def int_h(h, t, V): h_alpha = 0.128 * exp((17. - V + VT) / 18.) h_beta = 4. / (1. + exp(-(V - VT - 40.) / 5.)) dhdt = (h_alpha * (1. - h) - h_beta * h) return dhdt @bp.integrate def int_n(n, t, V): c = 15. - V + VT n_alpha = 0.032 * c / (exp(c / 5.) - 1.) n_beta = .5 * exp((10. - V + VT) / 40.) dndt = (n_alpha * (1. - n) - n_beta * n) return dndt @bp.integrate def int_n_zero(n, t, V): n_alpha = 0.032 n_beta = .5 * exp((10. - V + VT) / 40.) dndt = (n_alpha * (1. - n) - n_beta * n) return dndt @bp.integrate def int_V(V, t, m, h, n, ge, gi): g_na_ = g_Na * (m * m * m) * h g_kd_ = g_Kd * (n * n * n * n) dvdt = (gl * (El - V) + ge * (Ee - V) + gi * (Ei - V) - g_na_ * (V - ENa) - g_kd_ * (V - EK)) / Cm return dvdt def neu_update(ST, _t): ST['ge'] = int_ge(ST['ge'], _t) ST['gi'] = int_gi(ST['gi'], _t) if abs(ST['V'] - (40.0 + VT)) < inf: ST['m'] = int_m_zerob(ST['m'], _t, ST['V']) elif abs(ST['V'] - (13.0 + VT)) < inf: ST['m'] = int_m_zeroa(ST['m'], _t, ST['V']) else: ST['m'] = int_m(ST['m'], _t, ST['V']) ST['h'] = int_h(ST['h'], _t, ST['V']) if abs(ST['V'] - (15.0 + VT)) > inf: ST['n'] = int_n(ST['n'], _t, ST['V']) else: ST['n'] = int_n_zero(ST['n'], _t, ST['V']) V = int_V(ST['V'], _t, ST['m'], ST['h'], ST['n'], ST['ge'], ST['gi']) sp = ST['V'] < Vt and V >= Vt ST['sp'] = sp ST['V'] = V neuron = bp.NeuType(name='CUBA-HH', ST=neu_ST, steps=neu_update, mode='scalar') requires_exc = { 'pre': bp.types.NeuState( ['sp'], help='Pre-synaptic neuron state must have "spike" item.'), 'post': bp.types.NeuState( ['ge'], help='Post-synaptic neuron state must have "V" and "input" item.') } def update_syn_exc(ST, pre, post): if pre['sp']: post['ge'] += we exc_syn = bp.SynType(name='exc_syn', ST=bp.types.SynState(), requires=requires_exc, steps=update_syn_exc, mode='scalar') requires_inh = { 'pre': bp.types.NeuState( ['sp'], help='Pre-synaptic neuron state must have "spike" item.'), 'post': bp.types.NeuState( ['gi'], help='Post-synaptic neuron state must have "V" and "input" item.') } def update_syn_inh(ST, pre, post): if pre['sp']: post['gi'] -= wi inh_syn = bp.SynType(name='inh_syn', ST=bp.types.SynState(), requires=requires_inh, steps=update_syn_inh, mode='scalar') group = bp.NeuGroup(neuron, geometry=num) group.ST['V'] = El + (np.random.randn(num_exc + num_inh) * 5. - 5.) group.ST['ge'] = (np.random.randn(num_exc + num_inh) * 1.5 + 4.) * 10. group.ST['gi'] = (np.random.randn(num_exc + num_inh) * 12. + 20.) * 10. exc_conn = bp.SynConn(exc_syn, pre_group=group[:num_exc], post_group=group, conn=bp.connect.FixedProb(prob=0.02)) inh_conn = bp.SynConn(inh_syn, pre_group=group[num_exc:], post_group=group, conn=bp.connect.FixedProb(prob=0.02)) net = bp.Network(group, exc_conn, inh_conn) ed_build = time.time() st_run = time.time() net.run(duration=1000.0) ed_run = time.time() build_time = float(ed_build - st_build) run_time = float(ed_run - st_run) print('BuildT:{:.2f}s, RunT:{:.2f}s'.format(build_time, run_time)) return run_time, build_time
pre = bp.NeuGroup(LIF_neuron, geometry=(10, ), monitors=['V', 'input', 'spike']) pre.runner.set_schedule(['input', 'update', 'monitor', 'reset']) pre.pars['V_rest'] = -65. pre.ST['V'] = -65. post = bp.NeuGroup(LIF_neuron, geometry=(10, ), monitors=['V', 'input', 'spike']) post.runner.set_schedule(['input', 'update', 'monitor', 'reset']) post.pars['V_rest'] = -65. post.ST['V'] = -65. gabaa = bp.SynConn(model=GABAa_syn, pre_group=pre, post_group=post, conn=bp.connect.All2All(), monitors=['s'], delay=10.) gabaa.runner.set_schedule(['input', 'update', 'output', 'monitor']) net = bp.Network(pre, gabaa, post) current = bp.inputs.spike_current([10, 110, 210, 300, 305, 310, 315, 320], bp.profile._dt, 1., duration=duration) net.run(duration=duration, inputs=[gabaa, 'pre.spike', current, "="], report=True) # paint gabaa
bp.profile.set(dt=.1) n_post = 1 neuron = rate_neuron() post = bp.NeuGroup(neuron, n_post, monitors=['r']) pre = bp.NeuGroup(neuron, 20, monitors=['r']) #mode = 'matrix' mode = 'vector' print(mode) bcm1 = get_BCM(learning_rate=0.005, w_max=w_max, mode=mode) bcm = bp.SynConn(model=bcm1, pre_group=pre, post_group=post, conn=bp.connect.All2All(), monitors=['w', 'dwdt'], delay=0) bcm.r_th = np.zeros(n_post) bcm.post_r = np.zeros(n_post) bcm.sum_post_r = np.zeros(n_post) net = bp.Network(pre, bcm, post) # group selection group1, duration = bp.inputs.constant_current(([1.5, 1], [0, 1]) * 20) group2, duration = bp.inputs.constant_current(([0, 1], [1., 1]) * 20) input_r = np.vstack(((group1, ) * 10, (group2, ) * 10))
# set params pre_neu_num = 2 post_neu_num = 1 bp.profile.set(jit=True, dt=0.02, merge_steps=True, show_code=False) # build network fr_neu = get_fr_neu() oja_synapse = bpmodels.learning_rules.get_Oja() pre_neu = bp.NeuGroup(fr_neu, geometry=(pre_neu_num, ), monitors=['r']) post_neu = bp.NeuGroup(fr_neu, geometry=(post_neu_num, ), monitors=['r']) pre_neu.ST['r'] = 1. post_neu.ST['r'] = 1. syn = bp.SynConn(oja_synapse, pre_group=pre_neu, post_group=post_neu, conn=bp.connect.All2All(), monitors=['w']) net = bp.Network(pre_neu, syn, post_neu) # create input current_mat_in = [] current_mat_out = [] current1, _ = bp.inputs.constant_current([(2., 20.), (0., 20.)] * 3 + [(0., 20.), (0., 20.)] * 2) current2, _ = bp.inputs.constant_current([(0., 20.), (2., 20.)] * 5) current3, _ = bp.inputs.constant_current([(2., 20.), (0., 20.)] * 5) current_mat_in = np.vstack((current1, current2)) #current_mat_out = np.vstack((current3, current3)) current_mat_out = current3