def _get_brian_connection(self, source_group, target_group, synapse_obj, weight_units, homogeneous=False): """ Return the Brian Connection object that connects two NeuronGroups with a given synapse model. source_group -- presynaptic Brian NeuronGroup. target_group -- postsynaptic Brian NeuronGroup synapse_obj -- name of the variable that will be modified by synaptic input. weight_units -- Brian Units object: nA for current-based synapses, uS for conductance-based synapses. """ key = (source_group, target_group, synapse_obj) if not self.brian_connections.has_key(key): assert isinstance(source_group, brian.NeuronGroup) assert isinstance(target_group, brian.NeuronGroup), type(target_group) assert isinstance(synapse_obj, basestring), "%s (%s)" % (synapse_obj, type(synapse_obj)) try: max_delay = state.max_delay*ms except Exception: raise Exception("Simulation timestep not yet set. Need to call setup()") if not homogeneous: self.brian_connections[key] = brian.DelayConnection(source_group, target_group, synapse_obj, max_delay=max_delay) else: self.brian_connections[key] = brian.Connection(source_group, target_group, synapse_obj, max_delay=state.max_delay*ms) self.brian_connections[key].weight_units = weight_units state.add(self.brian_connections[key]) self.n[key] = 0 return self.brian_connections[key]
def _connect(self): '''Connect populations.''' self._connection = brian.Connection(self._source_pop, self._target_pop, 'ge', sparseness=self._p, weight=2 * brian.mV) self._connection.compress()
def main(): tmax = 50e-3 # second cn.set_fs(40e3) # Hz # anf_trains are normally generated by something like # cochlea.run_zilany2009() anf_trains = pd.DataFrame([ {'spikes': np.array([10e-3, 30e-3]), 'duration': tmax}, {'spikes': np.array([20e-3, 40e-3]), 'duration': tmax}, ]) # Generate ANF and GBC groups anfs = cn.make_anf_group(anf_trains) gbcs = cn.make_gbc_group(10) # Connect ANFs and GBCs synapses = brian.Connection( anfs, gbcs, 'ge_syn', ) synapses.connect_random( anfs, gbcs, p=0.5, fixed=True, weight=1e-6*siemens ) # Monitors for the GBCs spikes = brian.SpikeMonitor(gbcs) voltages = brian.StateMonitor(gbcs, 'vm', record=True) # Run the simulation cn.run( duration=tmax, objects=[anfs, gbcs, synapses, spikes, voltages] ) # Present the results print(spikes.spikes) fig, ax = plt.subplots(2,1) plt.sca(ax[0]) brian.raster_plot(spikes) plt.sca(ax[1]) voltages.plot() plt.show()
def cochlea_to_gbc(anf_group, n_gbc): gbc_group = make_gbc_group(n_gbc) p_connect = 40 / n_gbc synapses_i = br.Connection(anf_group, gbc_group, 'ge_syn') synapses_i.connect_random(anf_group, gbc_group, p=p_connect, fixed=True, weight=0.0047561806622803005 * 1e-6 * siemens) return {'neuron_groups': [gbc_group, synapses_i]}
def run_sim(number_neurons=default_number_neurons, connection_probability=default_connection_probability, synaptic_weights=default_synaptic_weights, synaptic_time_constant=default_synaptic_time_constant, tend=300): '''run a simulation of a population of leaky integrate-and-fire excitatory neurons that are randomly connected. The population is injected with a transient current.''' from brian.units import mvolt, msecond, namp, Mohm import brian brian.clear() El = 0 * mvolt tau_m = 30 * msecond tau_syn = synaptic_time_constant * msecond R = 20 * Mohm v_threshold = 30 * mvolt v_reset = 0 * mvolt tau_refractory = 4 * msecond eqs = brian.Equations(''' dv/dt = (-(v - El) + R*I)/tau_m : volt I = I_syn + I_stim : amp dI_syn/dt = -I_syn/tau_syn : amp I_stim : amp ''') external_current = np.zeros(tend) external_current[np.arange(0, 100)] = 5 * namp group = brian.NeuronGroup( model=eqs, N=number_neurons, threshold=v_threshold, reset=v_reset, refractory=tau_refractory) group.I_stim = brian.TimedArray(external_current, dt=1*msecond) connections = brian.Connection(group, group, 'I_syn') connections.connect_random(sparseness=connection_probability, weight=synaptic_weights*namp) spike_monitor = brian.SpikeMonitor(group) population_rate_monitor = brian.PopulationRateMonitor(group, bin=10*msecond) brian.reinit() brian.run(tend * msecond) return spike_monitor, population_rate_monitor
neuron_groups[name + 'i'] = neuron_groups['i'].subgroup(n_i) neuron_groups[name + 'e'].v = v_rest_e - 40. * b.mV neuron_groups[name + 'i'].v = v_rest_i - 40. * b.mV if test_mode or weight_path[-8:] == 'weights/': neuron_groups['e'].theta = np.load(weight_path + 'theta_A' + '.npy') else: neuron_groups['e'].theta = np.ones((n_e)) * 20.0 * b.mV print 'create recurrent connections' for conn_type in recurrent_conn_names: connName = name + conn_type[0] + name + conn_type[1] + ending weightMatrix = get_matrix_from_file(weight_path + '../random/' + connName + '.npy') connections[connName] = b.Connection(neuron_groups[connName[0:2]], neuron_groups[connName[2:4]], structure=conn_structure, state='g' + conn_type[0]) connections[connName].connect(neuron_groups[connName[0:2]], neuron_groups[connName[2:4]], weightMatrix) if ee_STDP_on: if 'ee' in recurrent_conn_names: stdp_methods[name + 'e' + name + 'e'] = b.STDP( connections[name + 'e' + name + 'e' + ending], eqs=eqs_stdp_ee, pre=eqs_stdp_pre_ee, post=eqs_stdp_post_ee, wmin=0., wmax=wmax_ee)
def run_sim(ffExcInputMult=None, ffInhInputMult=None): """Run the cond-based LIF neuron simulation. Takes a few minutes to construct network and run Parameters ---------- ffExcInputMult: scalar: FF input magnitude to E cells. multiply ffInputV by this value and connect to E cells ffInhInputMult: scalar: FF input magnitude to I cells. Returns ------- outDict - spike times, records of continuous values from simulation """ # use helper to get input timecourses (ffInputV, condAddV) = create_input_vectors( doDebugPlot=False) # multiplied by scalars below # setup initial state stT = time.time() brian.set_global_preferences(usecodegen=True) brian.set_global_preferences(useweave=True) brian.set_global_preferences(usecodegenweave=True) brian.clear(erase=True, all=True) brian.reinit_default_clock() clk = brian.Clock(dt=0.05 * ms) ################ # create neurons, define connections neurNetwork = brian.NeuronGroup(nNet, model=eqs, threshold=vthresh, reset=vrest, refractory=absRefractoryMs * msecond, order=1, compile=True, freeze=False, clock=clk) # create neuron pools neurCE = neurNetwork.subgroup(nExc) neurCI = neurNetwork.subgroup(nInh) connCE = brian.Connection(neurCE, neurNetwork, 'ge') connCI = brian.Connection(neurCI, neurNetwork, 'gi') print('n cells: %d, nE,I %d,%d, %s, absRefractoryMs: %d' % (nNet, nExc, nInh, repr(clk), absRefractoryMs)) # connect the network to itself connCE.connect_random(neurCE, neurNetwork, internalSparseness, weight=connENetWeight) connCI.connect_random(neurCI, neurNetwork, internalSparseness, weight=connINetWeight) # connect inputs that change spont rate assert ( spontAddRate <= 0 ), 'Spont add rate should be negative - convention: neg, excite inhibitory cells' spontAddNInpSyn = 100 nTotalSpontNeurons = (spontAddNInpSyn * nInh * 0.02) neurSpont = brian.PoissonGroup(nTotalSpontNeurons, -1.0 * spontAddRate * Hz) connCSpont = brian.Connection(neurSpont, neurCI, 'ge') connCSpont.connect_random( p=spontAddNInpSyn * 1.0 / nTotalSpontNeurons, weight=connENetWeight, # match internal excitatory strengths fixed=True) # connect the feedforward visual (poisson) inputs to excitatory cells (ff E) ffExcInputNInpSyn = 100 nTotalFfNeurons = (ffExcInputNInpSyn * ffExcInputNTargs * 0.02 ) # one pop of input cells for both E and I FF _ffExcInputV = ffExcInputMult * np.abs(a_(ffInputV).copy()) assert (np.all( _ffExcInputV >= 0)), 'Negative FF rates are rectified to zero' neurFfExcInput = brian.PoissonGroup( nTotalFfNeurons, lambda t: _ffExcInputV[int(t * 1000)] * Hz) connCFfExcInput = brian.Connection(neurFfExcInput, neurNetwork, 'ge') connCFfExcInput.connect_random(neurFfExcInput, neurCE[0:ffExcInputNTargs], ffExcInputNInpSyn * 1.0 / nTotalFfNeurons, weight=connENetWeight, fixed=True) # connect the feedforward visual (poisson) inputs to inhibitory cells (ff I) ffInhInputNInpSyn = 100 _ffInhInputV = ffInhInputMult * np.abs(ffInputV.copy()) assert (np.all( _ffInhInputV >= 0)), 'Negative FF rates are rectified to zero' neurFfInhInput = brian.PoissonGroup( nTotalFfNeurons, lambda t: _ffInhInputV[int(t * 1000)] * Hz) connCFfInhInput = brian.Connection(neurFfInhInput, neurNetwork, 'ge') connCFfInhInput.connect_random( neurFfInhInput, neurCI[0:ffInhInputNTargs], ffInhInputNInpSyn * 1.0 / nTotalFfNeurons, # sparseness weight=connENetWeight, fixed=True) # connect added step (ChR2) conductance to excitatory cells condAddAmp = 4.0 gAdd = brian.TimedArray(condAddAmp * condAddV, dt=1 * ms) print('Adding conductance for %d cells (can be slow): ' % len(condAddNeurNs), end=' ') for (iN, tN) in enumerate(condAddNeurNs): neurCE[tN].gAdd = gAdd print('done') # Initialize using some randomness so all neurons don't start in same state. # Alternative: initialize with constant values, give net extra 100-300ms to evolve from initial state. neurNetwork.v = (brian.randn(1) * 5.0 - 65) * mvolt neurNetwork.ge = brian.randn(nNet) * 1.5 + 4 neurNetwork.gi = brian.randn(nNet) * 12 + 20 # Record continuous variables and spikes monSTarg = brian.SpikeMonitor(neurNetwork) if contRecNs is not None: contRecClock = brian.Clock(dt=contRecStepMs * ms) monVTarg = brian.StateMonitor(neurNetwork, 'v', record=contRecNs, clock=contRecClock) monGETarg = brian.StateMonitor(neurNetwork, 'ge', record=contRecNs, clock=contRecClock) monGAddTarg = brian.StateMonitor(neurNetwork, 'gAdd', record=contRecNs, clock=contRecClock) monGITarg = brian.StateMonitor(neurNetwork, 'gi', record=contRecNs, clock=contRecClock) # construct brian.Network before running (so brian explicitly knows what to update during run) netL = [ neurNetwork, connCE, connCI, monSTarg, neurFfExcInput, connCFfExcInput, neurFfInhInput, connCFfInhInput, neurSpont, connCSpont ] if contRecNs is not None: # noinspection PyUnboundLocalVariable netL.append([monVTarg, monGETarg, monGAddTarg, monGITarg]) # cont monitors net = brian.Network(netL) print("Network construction time: %3.1f seconds" % (time.time() - stT)) # run print("Simulation running...") sys.stdout.flush() start_time = time.time() net.run(simRunTimeS * second, report='text', report_period=30.0 * second) durationS = time.time() - start_time print("Simulation time: %3.1f seconds" % durationS) outNTC = collections.namedtuple( 'outNTC', 'vm ge gadd gi clockDtS clockStartS clockEndS spiketimes contRecNs') outNTC.__new__.__defaults__ = (None, ) * len( outNTC._fields) # default to None outNT = outNTC(clockDtS=float(monSTarg.clock.dt), clockStartS=float(monSTarg.clock.start), clockEndS=float(monSTarg.clock.end), spiketimes=a_(monSTarg.spiketimes.values(), dtype='O'), contRecNs=contRecNs) if contRecNs is not None: outNT = outNT._replace(vm=monVTarg.values, ge=monGETarg.values, gadd=monGAddTarg.values, gi=monGITarg.values) return outNT
def __init__(mode, connectivity, weight_dependence, post_pre, conv_size, conv_stride, conv_features, weight_sharing, lattice_structure, random_inhibition_prob, top_percent): ''' Network initialization. ''' # setting input parameters this.mode = mode this.connectivity = connectivity this.weight_dependence = weight_dependence this.post_pre = post_pre this.conv_size = conv_size this.conv_features = conv_features this.weight_sharing = weight_sharing this.lattice_structure = lattice_structure this.random_inhibition_prob = random_inhibition_prob # load training or testing data if mode == 'train': start = time.time() this.data = get_labeled_data(MNIST_data_path + 'training') end = time.time() print 'time needed to load training set:', end - start else: start = time.time() this.data = get_labeled_data(MNIST_data_path + 'testing', bTrain = False) end = time.time() print 'time needed to load test set:', end - start # set parameters for simulation based on train / test mode if test_mode: weight_path = top_level_path + 'weights/conv_patch_connectivity_weights/' this.num_examples = 10000 * 1 this.do_plot_performance = False ee_STDP_on = False else: weight_path = top_level_path + 'random/conv_patch_connectivity_random/' this.num_examples = 60000 * 1 this.do_plot_performance = True ee_STDP_on = True # plotting or not do_plot = True # number of inputs to the network this.n_input = 784 this.n_input_sqrt = int(math.sqrt(n_input)) # number of neurons parameters this.n_e = ((n_input_sqrt - conv_size) / conv_stride + 1) ** 2 this.n_e_total = n_e * conv_features this.n_e_sqrt = int(math.sqrt(n_e)) this.n_i = n_e this.conv_features_sqrt = int(math.sqrt(conv_features)) # time (in seconds) per data example presentation and rest period in between, used to calculate total runtime this.single_example_time = 0.35 * b.second this.resting_time = 0.15 * b.second runtime = num_examples * (single_example_time + resting_time) # set the update interval if test_mode: this.update_interval = num_examples else: this.update_interval = 100 # rest potential parameters, reset potential parameters, threshold potential parameters, and refractory periods v_rest_e, v_rest_i = -65. * b.mV, -60. * b.mV v_reset_e, v_reset_i = -65. * b.mV, -45. * b.mV v_thresh_e, v_thresh_i = -52. * b.mV, -40. * b.mV refrac_e, refrac_i = 5. * b.ms, 2. * b.ms # dictionaries for weights and delays weight, delay = {}, {} # populations, connections, saved connections, etc. input_population_names = [ 'X' ] population_names = [ 'A' ] input_connection_names = [ 'XA' ] save_conns = [ 'XeAe', 'AeAe' ] # weird and bad names for variables, I think input_conn_names = [ 'ee_input' ] recurrent_conn_names = [ 'ei', 'ie', 'ee' ] # setting weight, delay, and intensity parameters weight['ee_input'] = (conv_size ** 2) * 0.175 delay['ee_input'] = (0 * b.ms, 10 * b.ms) delay['ei_input'] = (0 * b.ms, 5 * b.ms) input_intensity = start_input_intensity = 2.0 # time constants, learning rates, max weights, weight dependence, etc. tc_pre_ee, tc_post_ee = 20 * b.ms, 20 * b.ms nu_ee_pre, nu_ee_post = 0.0001, 0.01 wmax_ee = 1.0 exp_ee_post = exp_ee_pre = 0.2 w_mu_pre, w_mu_post = 0.2, 0.2 # setting up differential equations (depending on train / test mode) if test_mode: scr_e = 'v = v_reset_e; timer = 0*ms' else: tc_theta = 1e7 * b.ms theta_plus_e = 0.05 * b.mV scr_e = 'v = v_reset_e; theta += theta_plus_e; timer = 0*ms' offset = 20.0 * b.mV v_thresh_e = '(v>(theta - offset + ' + str(v_thresh_e) + ')) * (timer>refrac_e)' # equations for neurons neuron_eqs_e = ''' dv/dt = ((v_rest_e - v) + (I_synE + I_synI) / nS) / (100 * ms) : volt I_synE = ge * nS * -v : amp I_synI = gi * nS * (-100.*mV-v) : amp dge/dt = -ge/(1.0*ms) : 1 dgi/dt = -gi/(2.0*ms) : 1 ''' if test_mode: neuron_eqs_e += '\n theta :volt' else: neuron_eqs_e += '\n dtheta/dt = -theta / (tc_theta) : volt' neuron_eqs_e += '\n dtimer/dt = 100.0 : ms' neuron_eqs_i = ''' dv/dt = ((v_rest_i - v) + (I_synE + I_synI) / nS) / (10*ms) : volt I_synE = ge * nS * -v : amp I_synI = gi * nS * (-85.*mV-v) : amp dge/dt = -ge/(1.0*ms) : 1 dgi/dt = -gi/(2.0*ms) : 1 ''' # creating dictionaries for various objects this.neuron_groups = {} this.input_groups = {} this.connections = {} this.input_connections = {} this.stdp_methods = {} this.rate_monitors = {} this.spike_monitors = {} this.spike_counters = {} # creating excitatory, inhibitory populations this.neuron_groups['e'] = b.NeuronGroup(n_e_total, neuron_eqs_e, threshold=v_thresh_e, refractory=refrac_e, reset=scr_e, compile=True, freeze=True) this.neuron_groups['i'] = b.NeuronGroup(n_e_total, neuron_eqs_i, threshold=v_thresh_i, refractory=refrac_i, reset=v_reset_i, compile=True, freeze=True) # creating subpopulations of excitatory, inhibitory neurons for name in population_names: print '...creating neuron group:', name # get a subgroup of size 'n_e' from all exc neuron_groups[name + 'e'] = neuron_groups['e'].subgroup(conv_features * n_e) # get a subgroup of size 'n_i' from the inhibitory layer neuron_groups[name + 'i'] = neuron_groups['i'].subgroup(conv_features * n_e) # start the membrane potentials of these groups 40mV below their resting potentials neuron_groups[name + 'e'].v = v_rest_e - 40. * b.mV neuron_groups[name + 'i'].v = v_rest_i - 40. * b.mV print '...creating recurrent connections' for name in population_names: # if we're in test mode / using some stored weights if mode == 'test' or weight_path[-8:] == 'weights/conv_patch_connectivity_weights/': # load up adaptive threshold parameters neuron_groups['e'].theta = np.load(weight_path + 'theta_A' + '_' + ending +'.npy') else: # otherwise, set the adaptive additive threshold parameter at 20mV neuron_groups['e'].theta = np.ones((n_e_total)) * 20.0 * b.mV for conn_type in recurrent_conn_names: if conn_type == 'ei': # create connection name (composed of population and connection types) conn_name = name + conn_type[0] + name + conn_type[1] # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # instantiate the created connection for feature in xrange(conv_features): for n in xrange(n_e): connections[conn_name][feature * n_e + n, feature * n_e + n] = 10.4 elif conn_type == 'ie': # create connection name (composed of population and connection types) conn_name = name + conn_type[0] + name + conn_type[1] # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # instantiate the created connection for feature in xrange(conv_features): for other_feature in xrange(conv_features): if feature != other_feature: for n in xrange(n_e): connections[conn_name][feature * n_e + n, other_feature * n_e + n] = 17.4 if random_inhibition_prob != 0.0: for feature in xrange(conv_features): for other_feature in xrange(conv_features): for n_this in xrange(n_e): for n_other in xrange(n_e): if n_this != n_other: if b.random() < random_inhibition_prob: connections[conn_name][feature * n_e + n_this, other_feature * n_e + n_other] = 17.4 elif conn_type == 'ee': # create connection name (composed of population and connection types) conn_name = name + conn_type[0] + name + conn_type[1] # get weights from file if we are in test mode if mode == 'test': weight_matrix = get_matrix_from_file(weight_path + conn_name + '_' + ending + '.npy', conv_features * n_e, conv_features * n_e) # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # instantiate the created connection if connectivity == 'all': for feature in xrange(conv_features): for other_feature in xrange(conv_features): if feature != other_feature: for this_n in xrange(n_e): for other_n in xrange(n_e): if is_lattice_connection(n_e_sqrt, this_n, other_n): if mode == 'test': connections[conn_name][feature * n_e + this_n, other_feature * n_e + other_n] = weight_matrix[feature * n_e + this_n, other_feature * n_e + other_n] else: connections[conn_name][feature * n_e + this_n, other_feature * n_e + other_n] = (b.random() + 0.01) * 0.3 elif connectivity == 'pairs': for feature in xrange(conv_features): if feature % 2 == 0: for this_n in xrange(n_e): for other_n in xrange(n_e): if is_lattice_connection(n_e_sqrt, this_n, other_n): if mode == 'test': connections[conn_name][feature * n_e + this_n, (feature + 1) * n_e + other_n] = weight_matrix[feature * n_e + this_n, (feature + 1) * n_e + other_n] else: connections[conn_name][feature * n_e + this_n, (feature + 1) * n_e + other_n] = (b.random() + 0.01) * 0.3 elif feature % 2 == 1: for this_n in xrange(n_e): for other_n in xrange(n_e): if is_lattimode == 'test'ce_connection(n_e_sqrt, this_n, other_n): if mode == 'test': connections[conn_name][feature * n_e + this_n, (feature - 1) * n_e + other_n] = weight_matrix[feature * n_e + this_n, (feature - 1) * n_e + other_n] else: connections[conn_name][feature * n_e + this_n, (feature - 1) * n_e + other_n] = (b.random() + 0.01) * 0.3 elif connectivity == 'none': pass # if STDP from excitatory -> excitatory is on and this connection is excitatory -> excitatory if ee_STDP_on and 'ee' in recurrent_conn_names: stdp_methods[name + 'e' + name + 'e'] = b.STDP(connections[name + 'e' + name + 'e'], eqs=eqs_stdp_ee, pre=eqs_stdp_pre_ee, post=eqs_stdp_post_ee, wmin=0., wmax=wmax_ee) print '...creating monitors for:', name # spike rate monitors for excitatory and inhibitory neuron populations rate_monitors[name + 'e'] = b.PopulationRateMonitor(neuron_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second) rate_monitors[name + 'i'] = b.PopulationRateMonitor(neuron_groups[name + 'i'], bin=(single_example_time + resting_time) / b.second) spike_counters[name + 'e'] = b.SpikeCounter(neuron_groups[name + 'e']) # record neuron population spikes if specified spike_monitors[name + 'e'] = b.SpikeMonitor(neuron_groups[name + 'e']) spike_monitors[name + 'i'] = b.SpikeMonitor(neuron_groups[name + 'i'])
def build_network(): global fig_num, assignments neuron_groups['e'] = b.NeuronGroup(n_e_total, neuron_eqs_e, threshold=v_thresh_e, refractory=refrac_e, reset=scr_e, compile=True, freeze=True) neuron_groups['i'] = b.NeuronGroup(n_e_total, neuron_eqs_i, threshold=v_thresh_i, refractory=refrac_i, reset=v_reset_i, compile=True, freeze=True) for name in population_names: print '...Creating neuron group:', name # get a subgroup of size 'n_e' from all exc neuron_groups[name + 'e'] = neuron_groups['e'].subgroup(conv_features * n_e) # get a subgroup of size 'n_i' from the inhibitory layer neuron_groups[name + 'i'] = neuron_groups['i'].subgroup(conv_features * n_e) # start the membrane potentials of these groups 40mV below their resting potentials neuron_groups[name + 'e'].v = v_rest_e - 40. * b.mV neuron_groups[name + 'i'].v = v_rest_i - 40. * b.mV print '...Creating recurrent connections' for name in population_names: # if we're in test mode / using some stored weights if test_mode: # load up adaptive threshold parameters if save_best_model: neuron_groups['e'].theta = np.load( os.path.join(best_weights_dir, '_'.join(['theta_A', ending + '_best.npy']))) else: neuron_groups['e'].theta = np.load( os.path.join(end_weights_dir, '_'.join(['theta_A', ending + '_end.npy']))) else: # otherwise, set the adaptive additive threshold parameter at 20mV neuron_groups['e'].theta = np.ones((n_e_total)) * 20.0 * b.mV for conn_type in recurrent_conn_names: if conn_type == 'ei': # create connection name (composed of population and connection types) conn_name = name + conn_type[0] + name + conn_type[1] # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection( neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # instantiate the created connection for feature in xrange(conv_features): for n in xrange(n_e): connections[conn_name][feature * n_e + n, feature * n_e + n] = 10.4 elif conn_type == 'ie': # create connection name (composed of population and connections types) conn_name = name + conn_type[0] + name + conn_type[ 1] + '_' + ending # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection( neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # instantiate the created connection with the 'weightMatrix' loaded from file for feature in xrange(conv_features): for other_feature in xrange(conv_features): if feature != other_feature: for n in xrange(n_e): connections[conn_name][feature * n_e + n, other_feature * n_e + n] = 17.4 print '...Creating monitors for:', name # spike rate monitors for excitatory and inhibitory neuron populations rate_monitors[name + 'e'] = b.PopulationRateMonitor( neuron_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second) rate_monitors[name + 'i'] = b.PopulationRateMonitor( neuron_groups[name + 'i'], bin=(single_example_time + resting_time) / b.second) spike_counters[name + 'e'] = b.SpikeCounter(neuron_groups[name + 'e']) # record neuron population spikes if specified if record_spikes or plot: spike_monitors[name + 'e'] = b.SpikeMonitor(neuron_groups[name + 'e']) spike_monitors[name + 'i'] = b.SpikeMonitor(neuron_groups[name + 'i']) if record_spikes and plot: b.figure(fig_num, figsize=(8, 6)) fig_num += 1 b.ion() b.subplot(211) b.raster_plot(spike_monitors['Ae'], refresh=1000 * b.ms, showlast=1000 * b.ms, title='Excitatory spikes per neuron') b.subplot(212) b.raster_plot(spike_monitors['Ai'], refresh=1000 * b.ms, showlast=1000 * b.ms, title='Inhibitory spikes per neuron') b.tight_layout() # creating Poission spike train from input image (784 vector, 28x28 image) for name in input_population_names: input_groups[name + 'e'] = b.PoissonGroup(n_input, 0) rate_monitors[name + 'e'] = b.PopulationRateMonitor( input_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second) # creating connections from input Poisson spike train to excitatory neuron population(s) for name in input_connection_names: print '\n...Creating connections between', name[0], 'and', name[1] # for each of the input connection types (in this case, excitatory -> excitatory) for conn_type in input_conn_names: # saved connection name conn_name = name[0] + conn_type[0] + name[1] + conn_type[1] # get weight matrix depending on training or test phase if test_mode: if save_best_model: weight_matrix = np.load( os.path.join( best_weights_dir, '_'.join([conn_name, ending + '_best.npy']))) else: weight_matrix = np.load( os.path.join( end_weights_dir, '_'.join([conn_name, ending + '_end.npy']))) # create connections from the windows of the input group to the neuron population input_connections[conn_name] = b.Connection(input_groups['Xe'], neuron_groups[name[1] + conn_type[1]], \ structure='sparse', state='g' + conn_type[0], delay=True, max_delay=delay[conn_type][1]) if test_mode: for feature in xrange(conv_features): for n in xrange(n_e): for idx in xrange(conv_size**2): input_connections[conn_name][convolution_locations[n][idx], feature * n_e + n] = \ weight_matrix[convolution_locations[n][idx], feature * n_e + n] else: for feature in xrange(conv_features): for n in xrange(n_e): for idx in xrange(conv_size**2): input_connections[conn_name][ convolution_locations[n][idx], feature * n_e + n] = (b.random() + 0.01) * 0.3 if test_mode: if plot: plot_weights_and_assignments(assignments) fig_num += 1 # if excitatory -> excitatory STDP is specified, add it here (input to excitatory populations) if not test_mode: print '...Creating STDP for connection', name # STDP connection name conn_name = name[0] + conn_type[0] + name[1] + conn_type[1] # create the STDP object stdp_methods[conn_name] = b.STDP(input_connections[conn_name], eqs=eqs_stdp_ee, \ pre=eqs_stdp_pre_ee, post=eqs_stdp_post_ee, wmin=0., wmax=wmax_ee) print '\n'
def main(): Fs = float(100e3) #The entire simulation must be done at a common sampling frequency. As the Zilany model takes a minimum sampling frequency of 100KHz, everything is upsampled to that #Extracting speech signal and pre-processing: dictsample = sio.loadmat( 'resampled.mat' ) #Resampled.mat is simply a speech signal resampled to 100KHz, here you can have anything that is sampled at 100KHz, be it a segment of a song or any arbritary signal. There is no file path because we assume that this file is the same directory sample = dictsample['newstory'] #newstory is just the key title for the array sample = sample.flatten() #making the signal a Row vector duration = float(2) #This is how long you want your sample to be, we take 2 seconds of the speech signal because it contains significant amount of vowel sounds but you can change this number to anything index = int(duration * Fs) #Converting the duration wanted into an index value using the sampling frequency mysample = sample[0:index] #Selecting the desired duration of signal interms of index value wv.set_dbspl( mysample, 78 ) #setting the level to 78 dB SPL. SPL is the sound pressure level. This is an arbritary number and can be changed. brian.defaultclock.dt = 0.01 * ms #This coincides with the desired sampling frequency of 100KHz # Generate spike trains from Auditory Nerve Fibres using Cochlea Package anf_trains = cochlea.run_zilany2014( sound=mysample, fs=Fs, anf_num=( 13, 3, 1 ), # (Amount of High spike rate, Medium spike rate, Low spike rate fibres. You can choose these as you want but these numbers are taken from the Verhulst et.al 2015 paper) cf=(125, 8000, 30), species='human', #This can be changed to cat as well seed=0, powerlaw= 'approximate' #The latest implementation of the Zilany model includes the power law representation however we do not want this to be too computationally intensive. Therefore we pick approximate ) # Generate ANF and GBC groups in Brian using inbuilt functions in the Cochlear Nucleus package anfs = cn.make_anf_group( anf_trains ) #The amount of neurons for the Auditory Nerve = 30 * Amount of Fibres gbcs = cn.make_gbc_group( 200 ) #200 is the number of neurons for globular bushy cells in the cochlear nucleus. You can change this to any number but it doesn't affect the result # Connect ANFs and GBCs using the synapses class in Brian synapses = brian.Connection( anfs, gbcs, 'ge_syn', delay=5 * ms #This is important to make sure that there is a delay between the groups ) #this value of convergence is taken from the Cochlear Nucleus documentation convergence = 20 weight = cn.synaptic_weight(pre='anf', post='gbc', convergence=convergence) #Initiating the synaptic connections to be random with a fixed probability p that is proportional to the synaptic convergence synapses.connect_random( anfs, gbcs, p=convergence / len(anfs), fixed=True, weight=weight, ) # Monitors for the GBCs. Brian Spike Monitors are objects that basically collect the amount of spikes in a neuron group gbc_spikes = brian.SpikeMonitor(gbcs) # Run the simulation using the run function in the CN package. This basically uses Brian's run function cn.run( duration=duration, objects=[anfs, gbcs, synapses, gbc_spikes] #include ANpop and CN pop in this if you need to ) gbc_trains = th.make_trains(gbc_spikes) #Extracting the spike times for both AN and CN groups CNspikes = gbc_trains['spikes'] ANspikes = anf_trains['spikes'] #Converting dict format to array format so that spike train data is basically a one dimensional array or row vector of times where spikes occur CN_spikes = np.asarray(CNspikes) AN_spikes = np.asarray(ANspikes) #Saving it in the appropriate format so that we can do further processing in MATLAB data = {'CN': CN_spikes, 'AN': AN_spikes} sio.savemat('Spiketrains', data) #If you want to plot the rasters of these spike trains in MATPLOTLIB, you can use the following code: fig, ax = plt.subplots(2, 1) th.plot_raster(anf_trains, ax=ax[0]) th.plot_raster(gbc_trains, ax=ax[1]) plt.show() plt.tight_layout()
if test_mode: # load up adaptive threshold parameters neuron_groups['e'].theta = np.load( os.path.join(weights_dir, '_'.join(['theta_A', ending + '.npy']))) else: # otherwise, set the adaptive additive threshold parameter at 20mV neuron_groups['e'].theta = np.ones((n_e_total)) * 20.0 * b.mV for conn_type in recurrent_conn_names: if conn_type == 'ei': # create connection name (composed of population and connections types) conn_name = name + conn_type[0] + name + conn_type[1] # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection( neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # instantiate the created connection with the 'weightMatrix' loaded from file for feature in xrange(conv_features): for n in xrange(n_e): connections[conn_name][feature * n_e + n, feature * n_e + n] = 10.4 elif conn_type == 'ie': # create connection name (composed of population and connections types) conn_name = name + conn_type[0] + name + conn_type[1] # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection( neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]],
compile=True, freeze=True) neuron_groups['inhibitory'].v = v_rest_i - 40. * b.mV # DEFINE CONNECTIONS connections = {} # sensory -> spiking neurons weight_matrix_input = load_weights delay_input_excitatory = (0 * b.ms, 10 * b.ms) connections['input'] = b.Connection(neuron_groups['poisson'], neuron_groups['spiking'], structure='dense', state='ge', delay=True, max_delay=delay_input_excitatory[1]) connections['input'].connect(neuron_groups['poisson'], neuron_groups['spiking'], weight_matrix_input, delay=delay_input_excitatory) connections['input'].delay[:, :] = load_delay # spiking -> inhibitory weight_matrix_excitatory = np.identity(spiking_neurons) weight_matrix_excitatory *= 10.4 connections['excitatory'] = b.Connection(neuron_groups['spiking'], neuron_groups['inhibitory'],
def build_network(): global fig_num neuron_groups['e'] = b.NeuronGroup(n_e_total, neuron_eqs_e, threshold=v_thresh_e, \ refractory=refrac_e, reset=scr_e, compile=True, freeze=True) neuron_groups['i'] = b.NeuronGroup(n_e_total, neuron_eqs_i, threshold=v_thresh_i, \ refractory=refrac_i, reset=v_reset_i, compile=True, freeze=True) for name in population_names: print '...Creating neuron group:', name # get a subgroup of size 'n_e' from all exc neuron_groups[name + 'e'] = neuron_groups['e'].subgroup(conv_features * n_e) # get a subgroup of size 'n_i' from the inhibitory layer neuron_groups[name + 'i'] = neuron_groups['i'].subgroup(conv_features * n_e) # start the membrane potentials of these groups 40mV below their resting potentials neuron_groups[name + 'e'].v = v_rest_e - 40. * b.mV neuron_groups[name + 'i'].v = v_rest_i - 40. * b.mV print '...Creating recurrent connections' for name in population_names: neuron_groups['e'].theta = np.load( os.path.join(best_weights_dir, '_'.join(['theta_A', ending + '_best.npy']))) for conn_type in recurrent_conn_names: if conn_type == 'ei': # create connection name (composed of population and connection types) conn_name = name + conn_type[0] + name + conn_type[1] # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection( neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # instantiate the created connection for feature in xrange(conv_features): for n in xrange(n_e): connections[conn_name][feature * n_e + n, feature * n_e + n] = 10.4 elif conn_type == 'ie': # create connection name (composed of population and connection types) conn_name = name + conn_type[0] + name + conn_type[1] # load weight matrix weight_matrix = np.load( os.path.join(best_weights_dir, '_'.join([conn_name, ending, 'best.npy']))) # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection( neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # define the actual synaptic connections and strengths for feature in xrange(conv_features): for other_feature in xrange(conv_features): if feature != other_feature: for n in xrange(n_e): connections[conn_name][feature * n_e + n, other_feature * n_e + n] = inhibition_level print '...Creating monitors for:', name # spike rate monitors for excitatory and inhibitory neuron populations rate_monitors[name + 'e'] = b.PopulationRateMonitor( neuron_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second) rate_monitors[name + 'i'] = b.PopulationRateMonitor( neuron_groups[name + 'i'], bin=(single_example_time + resting_time) / b.second) spike_counters[name + 'e'] = b.SpikeCounter(neuron_groups[name + 'e']) # record neuron population spikes if specified if record_spikes and do_plot: spike_monitors[name + 'e'] = b.SpikeMonitor(neuron_groups[name + 'e']) spike_monitors[name + 'i'] = b.SpikeMonitor(neuron_groups[name + 'i']) if record_spikes and do_plot: b.figure(fig_num, figsize=(8, 6)) b.ion() b.subplot(211) b.raster_plot(spike_monitors['Ae'], refresh=1000 * b.ms, showlast=1000 * b.ms, title='Excitatory spikes per neuron') b.subplot(212) b.raster_plot(spike_monitors['Ai'], refresh=1000 * b.ms, showlast=1000 * b.ms, title='Inhibitory spikes per neuron') b.tight_layout() fig_num += 1 # creating Poission spike train from input image (784 vector, 28x28 image) for name in input_population_names: input_groups[name + 'e'] = b.PoissonGroup(n_input, 0) rate_monitors[name + 'e'] = b.PopulationRateMonitor( input_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second) # creating connections from input Poisson spike train to excitatory neuron population(s) for name in input_connection_names: print '\n...Creating connections between', name[0], 'and', name[1] # for each of the input connection types (in this case, excitatory -> excitatory) for conn_type in input_conn_names: # saved connection name conn_name = name[0] + conn_type[0] + name[1] + conn_type[1] # get weight matrix depending on training or test phase weight_matrix = np.load( os.path.join(best_weights_dir, '_'.join([conn_name, ending + '_best.npy']))) # create connections from the windows of the input group to the neuron population input_connections[conn_name] = b.Connection(input_groups['Xe'], neuron_groups[name[1] + conn_type[1]], \ structure='sparse', state='g' + conn_type[0], delay=True, max_delay=delay[conn_type][1]) for feature in xrange(conv_features): for n in xrange(n_e): for idx in xrange(conv_size**2): input_connections[conn_name][convolution_locations[n][idx], feature * n_e + n] = \ weight_matrix[convolution_locations[n][idx], feature * n_e + n] if do_plot: plot_2d_input_weights() fig_num += 1 print '\n'
if name == 'CONV1': for conn_type in recurrent_connection_types: if conn_type == 'ei': connection_name = name + conn_type[0] + name + conn_type[ 1] + '_' + ending # get the corresponding stored weights from file weight_matrix = get_matrix_from_file( data_path + 'random/conv_full_conn_random/' + connection_name + '.npy', n_src=conv_features * n_e_patch, n_tgt=conv_features) # create a connection from the first group in conn_name with the second group connections[connection_name] = b.Connection( neuron_groups[name + 'e'], neuron_groups[name + 'i'], structure='sparse', state='g' + conn_type[0]) # instantiate the created connection with the 'weightMatrix' loaded from file for feature in xrange(conv_features): for n in xrange(n_e_patch): connections[connection_name][ feature * n_e_patch + n, feature] = weight_matrix[feature * n_e_patch + n, feature] elif conn_type == 'ie': connection_name = name + conn_type[0] + name + conn_type[ 1] + '_' + ending # get the corresponding stored weights from file weight_matrix = get_matrix_from_file(
neuron_groups['Ae'] = neuron_groups['e'].subgroup(n_e) neuron_groups['Ai'] = neuron_groups['i'].subgroup(n_i) neuron_groups['Ae'].v = v_rest_e - 40. * b.mV neuron_groups['Ai'].v = v_rest_i - 40. * b.mV neuron_groups['e'].theta = np.ones((n_e)) * 20.0 * b.mV # recurrent connections connName = 'AeAi' tmp = np.load('../random/AeAi.npy') avg = np.average(tmp) std = np.std(tmp) weightMatrix = np.random.normal(avg, std, size=(n_e, n_i)) connections[connName] = b.Connection(neuron_groups['Ae'], neuron_groups['Ai'], structure=conn_structure, state='g' + 'e') connections[connName].connect(neuron_groups['Ae'], neuron_groups['Ai'], weightMatrix) ''' connName = 'AeAi' weightMatrix = get_matrix_from_file('../random/AeAi.npy') connections[connName] = b.Connection(neuron_groups['Ae'], neuron_groups['Ai'], structure= conn_structure, state = 'g'+'e') connections[connName].connect(neuron_groups['Ae'], neuron_groups['Ai'], weightMatrix) ''' connName = 'AiAe' tmp = np.load('../random/AiAe.npy') avg = np.average(tmp) std = np.std(tmp) weightMatrix = np.random.normal(avg, std, size=(n_i, n_e))
def __init__(self, input_dim, output_dim, dtype, input_scaling=100, input_conn_frac=.5, dt=1, we_scaling=2, wi_scaling=.5, we_sparseness=.1, wi_sparseness=.1): super(BrianIFReservoirNode, self).__init__(input_dim=input_dim, output_dim=output_dim, dtype=dtype) self.taum = 20 * brian.ms self.taue = 5 * brian.ms self.taui = 10 * brian.ms self.Vt = 15 * brian.mV self.Vr = 0 * brian.mV self.frac_e = .75 self.input_scaling = input_scaling self.input_conn_frac = input_conn_frac self.dt = dt self.we_scaling = we_scaling self.wi_scaling = wi_scaling self.we_sparseness = we_sparseness self.wi_sparseness = wi_sparseness self.eqs = brian.Equations(''' dV/dt = (I-V+ge-gi)/self.taum : volt dge/dt = -ge/self.taue : volt dgi/dt = -gi/self.taui : volt I: volt ''') self.G = brian.NeuronGroup(N=output_dim, model=self.eqs, threshold=self.Vt, reset=self.Vr) self.Ge = self.G.subgroup(int(scipy.floor( output_dim * self.frac_e))) # Excitatory neurons self.Gi = self.G.subgroup( int(scipy.floor(output_dim * (1 - self.frac_e)))) self.internal_conn = brian.Connection(self.G, self.G) self.we = self.we_scaling * scipy.random.rand(len(self.Ge), len( self.G)) * brian.nS self.wi = self.wi_scaling * scipy.random.rand(len(self.Ge), len( self.G)) * brian.nS self.Ce = brian.Connection(self.Ge, self.G, 'ge', sparseness=self.we_sparseness, weight=self.we) self.Ci = brian.Connection(self.Gi, self.G, 'gi', sparseness=self.wi_sparseness, weight=self.wi) #self.internal_conn.connect(self.G, self.G, self.w_res) self.Mv = brian.StateMonitor(self.G, 'V', record=True, timestep=10) self.Ms = brian.SpikeMonitor(self.G, record=True) self.w_in = self.input_scaling * (scipy.random.rand( self.output_dim, self.input_dim)) * (scipy.random.rand( self.output_dim, self.input_dim) < self.input_conn_frac) self.network = brian.Network(self.G, self.Ce, self.Ci, self.Ge, self.Gi, self.Mv, self.Ms)
def main(): fs = 100e3 # s cf = 600 # Hz duration = 50e-3 # s # Simulation sampling frequency cn.set_fs(40e3) # Hz # Generate sound sound = wv.ramped_tone( fs=fs, freq=cf, duration=duration, dbspl=30, ) # Generate ANF trains anf_trains = cochlea.run_zilany2014( sound=sound, fs=fs, anf_num=(300, 0, 0), # (HSR, MSR, LSR) cf=cf, species='cat', seed=0, ) # Generate ANF and GBC groups anfs = cn.make_anf_group(anf_trains) gbcs = cn.make_gbc_group(100) # Connect ANFs and GBCs synapses = brian.Connection( anfs, gbcs, 'ge_syn', ) convergence = 20 weight = cn.synaptic_weight(pre='anf', post='gbc', convergence=convergence) synapses.connect_random(anfs, gbcs, p=convergence / len(anfs), fixed=True, weight=weight) # Monitors for the GBCs spikes = brian.SpikeMonitor(gbcs) # Run the simulation cn.run(duration=len(sound) / fs, objects=[anfs, gbcs, synapses, spikes]) # Present the results gbc_trains = th.make_trains(spikes) fig, ax = plt.subplots(2, 1) th.plot_raster(anf_trains, ax=ax[0]) th.plot_raster(gbc_trains, ax=ax[1]) plt.show()
} """.replace('SCALAR', precision)) stateupdate_and_threshold = mod.get_function("stateupdate_and_threshold") reset = mod.get_function("reset") V = gpuarray.to_gpu( numpy.array(numpy.random.rand(N) * 0.01 - 0.06, dtype=mydtype)) ge = gpuarray.to_gpu(numpy.zeros(N, dtype=mydtype)) gi = gpuarray.to_gpu(numpy.zeros(N, dtype=mydtype)) we = 0.00162 wi = -0.009 P = brian.NeuronGroup(N, model='V:1') Pe = P.subgroup(Ne) Pi = P.subgroup(Ni) Ce = brian.Connection(Pe, P, sparseness=sparseness, weight=we).W.connection_matrix() Ci = brian.Connection(Pi, P, sparseness=sparseness, weight=wi).W.connection_matrix() Ce.alldata = numpy.array(Ce.alldata, dtype=mydtype) Ci.alldata = numpy.array(Ci.alldata, dtype=mydtype) Ce_alldata = drv.mem_alloc(Ce.alldata.nbytes) drv.memcpy_htod(Ce_alldata, Ce.alldata) Ce_allj = drv.mem_alloc(Ce.allj.nbytes) drv.memcpy_htod(Ce_allj, Ce.allj) Ce_rowind = drv.mem_alloc(Ce.rowind.nbytes) drv.memcpy_htod(Ce_rowind, Ce.rowind) Ci_alldata = drv.mem_alloc(Ci.alldata.nbytes) drv.memcpy_htod(Ci_alldata, Ci.alldata) Ci_allj = drv.mem_alloc(Ci.allj.nbytes) drv.memcpy_htod(Ci_allj, Ci.allj) Ci_rowind = drv.mem_alloc(Ci.rowind.nbytes)
def build_network(): global fig_num neuron_groups['e'] = b.NeuronGroup(n_e_total, neuron_eqs_e, threshold=v_thresh_e, refractory=refrac_e, reset=scr_e, compile=True, freeze=True) neuron_groups['i'] = b.NeuronGroup(n_e_total, neuron_eqs_i, threshold=v_thresh_i, refractory=refrac_i, reset=v_reset_i, compile=True, freeze=True) for name in ['A']: print '...Creating neuron group:', name # get a subgroup of size 'n_e' from all exc neuron_groups[name + 'e'] = neuron_groups['e'].subgroup(conv_features * n_e) # get a subgroup of size 'n_i' from the inhibitory layer neuron_groups[name + 'i'] = neuron_groups['i'].subgroup(conv_features * n_e) # start the membrane potentials of these groups 40mV below their resting potentials neuron_groups[name + 'e'].v = v_rest_e - 40. * b.mV neuron_groups[name + 'i'].v = v_rest_i - 40. * b.mV print '...Creating recurrent connections' for name in ['A']: neuron_groups['e'].theta = np.load( os.path.join(best_weights_dir, '_'.join(['theta_A', ending + '_best.npy']))) for conn_type in ['ei', 'ie']: if conn_type == 'ei': # create connection name (composed of population and connection types) conn_name = name + conn_type[0] + name + conn_type[1] # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], \ neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # instantiate the created connection for feature in xrange(conv_features): for n in xrange(n_e): connections[conn_name][feature * n_e + n, feature * n_e + n] = 10.4 elif conn_type == 'ie' and not remove_inhibition: # create connection name (composed of population and connection types) conn_name = name + conn_type[0] + name + conn_type[1] # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], \ neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # define the actual synaptic connections and strengths for feature in xrange(conv_features): if inhib_scheme in ['far', 'strengthen']: for other_feature in set(range(conv_features)) - set( neighbor_mapping[feature]): if inhib_scheme == 'far': for n in xrange(n_e): connections[conn_name][feature * n_e + n, other_feature * n_e + n] = 17.4 elif inhib_scheme == 'strengthen': if n_e == 1: x, y = feature // np.sqrt( n_e_total), feature % np.sqrt( n_e_total) x_, y_ = other_feature // np.sqrt( n_e_total), other_feature % np.sqrt( n_e_total) else: x, y = feature // np.sqrt( conv_features), feature % np.sqrt( conv_features) x_, y_ = other_feature // np.sqrt( conv_features ), other_feature % np.sqrt(conv_features) for n in xrange(n_e): connections[conn_name][feature * n_e + n, other_feature * n_e + n] = \ min(17.4, inhib_const * np.sqrt(euclidean([x, y], [x_, y_]))) elif inhib_scheme == 'increasing': for other_feature in xrange(conv_features): if n_e == 1: x, y = feature // np.sqrt( n_e_total), feature % np.sqrt(n_e_total) x_, y_ = other_feature // np.sqrt( n_e_total), other_feature % np.sqrt( n_e_total) else: x, y = feature // np.sqrt( conv_features), feature % np.sqrt( conv_features) x_, y_ = other_feature // np.sqrt( conv_features), other_feature % np.sqrt( conv_features) if feature != other_feature: for n in xrange(n_e): connections[conn_name][feature * n_e + n, other_feature * n_e + n] = \ min(17.4, inhib_const * np.sqrt(euclidean([x, y], [x_, y_]))) else: raise Exception( 'Expecting one of "far", "increasing", or "strengthen" for argument "inhib_scheme".' ) # spike rate monitors for excitatory and inhibitory neuron populations rate_monitors[name + 'e'] = b.PopulationRateMonitor( neuron_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second) rate_monitors[name + 'i'] = b.PopulationRateMonitor( neuron_groups[name + 'i'], bin=(single_example_time + resting_time) / b.second) spike_counters[name + 'e'] = b.SpikeCounter(neuron_groups[name + 'e']) # record neuron population spikes if specified if record_spikes: spike_monitors[name + 'e'] = b.SpikeMonitor(neuron_groups[name + 'e']) spike_monitors[name + 'i'] = b.SpikeMonitor(neuron_groups[name + 'i']) if record_spikes and do_plot: if reset_state_vars: time_window = single_example_time * 1000 else: time_window = (single_example_time + resting_time) * 1000 b.figure(fig_num, figsize=(8, 6)) b.ion() b.subplot(211) b.raster_plot(spike_monitors['Ae'], refresh=time_window * b.ms, showlast=time_window * b.ms, title='Excitatory spikes per neuron') b.subplot(212) b.raster_plot(spike_monitors['Ai'], refresh=time_window * b.ms, showlast=time_window * b.ms, title='Inhibitory spikes per neuron') b.tight_layout() fig_num += 1 # creating Poission spike train from input image (784 vector, 28x28 image) for name in ['X']: input_groups[name + 'e'] = b.PoissonGroup(n_input, 0) rate_monitors[name + 'e'] = b.PopulationRateMonitor( input_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second) # creating connections from input Poisson spike train to convolution patch populations for name in ['XA']: print '\n...Creating connections between', name[0], 'and', name[1] # for each of the input connection types (in this case, excitatory -> excitatory) for conn_type in ['ee_input']: # saved connection name conn_name = name[0] + conn_type[0] + name[1] + conn_type[1] # get weight matrix depending on training or test phase weight_matrix = np.load( os.path.join(best_weights_dir, '_'.join([conn_name, ending + '_best.npy']))) # create connections from the windows of the input group to the neuron population input_connections[conn_name] = b.Connection(input_groups['Xe'], neuron_groups[name[1] + \ conn_type[1]], structure='sparse', state='g' + conn_type[0], delay=True, max_delay=delay[conn_type][1]) for feature in xrange(conv_features): for n in xrange(n_e): for idx in xrange(conv_size**2): input_connections[conn_name][convolution_locations[n][idx], feature * n_e + n] = \ weight_matrix[convolution_locations[n][idx], feature * n_e + n] if do_plot: plot_2d_input_weights() fig_num += 1
refractory=refrac_i, reset=v_reset_i, compile=True, freeze=True) groups['Ae'] = groups['e'].subgroup(6400) groups['Ai'] = groups['i'].subgroup(6400) groups['Ae'].v = v_rest_e - 40. * b.mV groups['Ai'].v = v_rest_i - 40. * b.mV groups['e'].theta = np.ones(6400) * 20.0 * b.mV print('Creating connections between excitatory and inhibitory layers.') connections = {} connections['Ae_Ai'] = b.Connection(groups['Ae'], groups['Ai'], state='ge') connections['Ae_Ai'].connect_full(groups['Ae'], groups['Ai'], 10.4) connections['Ai_Ae'] = b.Connection(groups['Ai'], groups['Ae'], state='gi') w = 17.4 * np.ones([6400, 6400]) - 17.4 * np.diag(np.ones(6400)) connections['Ai_Ae'].connect(groups['Ai'], groups['Ae'], w) print('Creating Poisson input group.') groups['X'] = b.PoissonGroup(784, 0) print('Creating connection between input and excitatory layer.') connections['X_Ae'] = b.Connection(groups['X'], groups['Ae'], state='ge', delay=True, max_delay=10 * b.ms)
def __init__(self, n_input=784, conv_size=16, conv_stride=4, conv_features=50, connectivity='all', weight_dependence=False, post_pre=True, weight_sharing=False, lattice_structure='4', random_lattice_prob=0.0, random_inhibition_prob=0.0): ''' Constructor for the spiking convolutional neural network model. n_input: (flattened) dimensionality of the input data conv_size: side length of convolution windows used conv_stride: stride (horizontal and vertical) of convolution windows used conv_features: number of convolution features (or patches) used connectivity: connection style between patches; one of 'none', 'pairs', all'; more to be added weight_dependence: whether to use weight STDP with weight dependence post_pre: whether to use STDP with both post- and pre-synpatic traces weight_sharing: whether to impose that all neurons within a convolution patch share a common set of weights lattice_structure: lattice connectivity pattern between patches; one of 'none', '4', '8', and 'all' random_lattice_prob: probability of adding random additional lattice connections between patches random_inhibition_prob: probability of adding random additional inhibition edges from the inhibitory to excitatory population ''' self.n_input, self.conv_size, self.conv_stride, self.conv_features, self.connectivity, self.weight_dependence, \ self.post_pre, self.weight_sharing, self.lattice_structure, self.random_lattice_prob, self.random_inhibition_prob = \ n_input, conv_size, conv_stride, conv_features, connectivity, weight_dependence, post_pre, weight_sharing, lattice_structure, \ random_lattice_prob, random_inhibition_prob # number of inputs to the network self.n_input_sqrt = int(math.sqrt(self.n_input)) self.n_excitatory_patch = ( (self.n_input_sqrt - self.conv_size) / self.conv_stride + 1)**2 self.n_excitatory = self.n_excitatory_patch * self.conv_features self.n_excitatory_patch_sqrt = int(math.sqrt(self.n_excitatory_patch)) self.n_inhibitory_patch = self.n_excitatory_patch self.n_inhibitory = self.n_excitatory self.conv_features_sqrt = int(math.ceil(math.sqrt(self.conv_features))) # time (in seconds) per data example presentation and rest period in between self.single_example_time = 0.35 * b.second self.resting_time = 0.15 * b.second # set update intervals self.update_interval = 100 self.weight_update_interval = 10 self.print_progress_interval = 10 # rest potential parameters, reset potential parameters, threshold potential parameters, and refractory periods v_rest_e, v_rest_i = -65. * b.mV, -60. * b.mV v_reset_e, v_reset_i = -65. * b.mV, -45. * b.mV v_thresh_e, v_thresh_i = -52. * b.mV, -40. * b.mV refrac_e, refrac_i = 5. * b.ms, 2. * b.ms # time constants, learning rates, max weights, weight dependence, etc. tc_pre_ee, tc_post_ee = 20 * b.ms, 20 * b.ms nu_ee_pre, nu_ee_post = 0.0001, 0.01 exp_ee_post = exp_ee_pre = 0.2 w_mu_pre, w_mu_post = 0.2, 0.2 # parameters for neuron equations tc_theta = 1e7 * b.ms theta_plus = 0.05 * b.mV scr_e = 'v = v_reset_e; theta += theta_plus; timer = 0*ms' offset = 20.0 * b.mV v_thresh_e = '(v>(theta - offset + ' + str( v_thresh_e) + ')) * (timer>refrac_e)' # equations for neurons neuron_eqs_e = ''' dv / dt = ((v_rest_e - v) + (I_synE + I_synI) / nS) / (100 * ms) : volt I_synE = ge * nS * - v : amp I_synI = gi * nS * (-100. * mV - v) : amp dge / dt = -ge / (1.0*ms) : 1 dgi / dt = -gi / (2.0*ms) : 1 dtheta / dt = -theta / (tc_theta) : volt dtimer / dt = 100.0 : ms ''' neuron_eqs_i = ''' dv/dt = ((v_rest_i - v) + (I_synE + I_synI) / nS) / (10*ms) : volt I_synE = ge * nS * -v : amp I_synI = gi * nS * (-85.*mV-v) : amp dge/dt = -ge/(1.0*ms) : 1 dgi/dt = -gi/(2.0*ms) : 1 ''' # STDP synaptic traces eqs_stdp_ee = ''' dpre / dt = -pre / tc_pre_ee : 1.0 dpost / dt = -post / tc_post_ee : 1.0 ''' # dictionaries for weights and delays self.weight, self.delay = {}, {} # setting weight, delay, and intensity parameters self.weight['ee_input'] = (conv_size**2) * 0.175 self.delay['ee_input'] = (0 * b.ms, 10 * b.ms) self.delay['ei_input'] = (0 * b.ms, 5 * b.ms) self.input_intensity = self.start_input_intensity = 2.0 self.wmax_ee = 1.0 # populations, connections, saved connections, etc. self.input_population_names = ['X'] self.population_names = ['A'] self.input_connection_names = ['XA'] self.save_connections = ['XeAe', 'AeAe'] self.input_connection_names = ['ee_input'] self.recurrent_connection_names = ['ei', 'ie', 'ee'] # setting STDP update rule if weight_dependence: if post_pre: eqs_stdp_pre_ee = 'pre = 1.; w -= nu_ee_pre * post * w ** exp_ee_pre' eqs_stdp_post_ee = 'w += nu_ee_post * pre * (wmax_ee - w) ** exp_ee_post; post = 1.' else: eqs_stdp_pre_ee = 'pre = 1.' eqs_stdp_post_ee = 'w += nu_ee_post * pre * (wmax_ee - w) ** exp_ee_post; post = 1.' else: if post_pre: eqs_stdp_pre_ee = 'pre = 1.; w -= nu_ee_pre * post' eqs_stdp_post_ee = 'w += nu_ee_post * pre; post = 1.' else: eqs_stdp_pre_ee = 'pre = 1.' eqs_stdp_post_ee = 'w += nu_ee_post * pre; post = 1.' print '\n' # for filesaving purposes stdp_input = '' if self.weight_dependence: stdp_input += 'weight_dependence_' else: stdp_input += 'no_weight_dependence_' if self.post_pre: stdp_input += 'post_pre' else: stdp_input += 'no_post_pre' if self.weight_sharing: use_weight_sharing = 'weight_sharing' else: use_weight_sharing = 'no_weight_sharing' # set ending of filename saves self.ending = self.connectivity + '_' + str(self.conv_size) + '_' + str(self.conv_stride) + '_' + str(self.conv_features) + \ '_' + str(self.n_excitatory_patch) + '_' + stdp_input + '_' + \ use_weight_sharing + '_' + str(self.lattice_structure) + '_' + str(self.random_lattice_prob) + \ '_' + str(self.random_inhibition_prob) self.fig_num = 1 # creating dictionaries for various objects self.neuron_groups, self.input_groups, self.connections, self.input_connections, self.stdp_methods, self.rate_monitors, \ self.spike_monitors, self.spike_counters, self.output_numbers = {}, {}, {}, {}, {}, {}, {}, {}, {} # creating convolution locations inside the input image self.convolution_locations = {} for n in xrange(self.n_excitatory_patch): self.convolution_locations[n] = [ ((n % self.n_excitatory_patch_sqrt) * self.conv_stride + (n // self.n_excitatory_patch_sqrt) \ * self.n_input_sqrt * self.conv_stride) + (x * self.n_input_sqrt) + y \ for y in xrange(self.conv_size) for x in xrange(self.conv_size) ] # instantiating neuron spike / votes monitor self.result_monitor = np.zeros( (self.update_interval, self.conv_features, self.n_excitatory_patch)) # creating overarching neuron populations self.neuron_groups['e'] = b.NeuronGroup(self.n_excitatory, neuron_eqs_e, threshold=v_thresh_e, \ refractory=refrac_e, reset=scr_e, compile=True, freeze=True) self.neuron_groups['i'] = b.NeuronGroup(self.n_inhibitory, neuron_eqs_i, threshold=v_thresh_i, \ refractory=refrac_i, reset=v_reset_i, compile=True, freeze=True) # create neuron subpopulations for name in self.population_names: print '...creating neuron group:', name # get a subgroup of size 'n_e' from all exc self.neuron_groups[name + 'e'] = self.neuron_groups['e'].subgroup( self.conv_features * self.n_excitatory_patch) # get a subgroup of size 'n_i' from the inhibitory layer self.neuron_groups[name + 'i'] = self.neuron_groups['i'].subgroup( self.conv_features * self.n_excitatory_patch) # start the membrane potentials of these groups 40mV below their resting potentials self.neuron_groups[name + 'e'].v = v_rest_e - 40. * b.mV self.neuron_groups[name + 'i'].v = v_rest_i - 40. * b.mV print '...creating recurrent connections' for name in self.population_names: # set the adaptive additive threshold parameter at 20mV self.neuron_groups['e'].theta = np.ones( (self.n_excitatory)) * 20.0 * b.mV for connection_type in self.recurrent_connection_names: if connection_type == 'ei': # create connection name (composed of population and connection types) connection_name = name + connection_type[ 0] + name + connection_type[1] # create a connection from the first group in conn_name with the second group self.connections[connection_name] = b.Connection(self.neuron_groups[connection_name[0:2]], \ self.neuron_groups[connection_name[2:4]], structure='sparse', state='g' + conn_type[0]) # instantiate the created connection for feature in xrange(self.conv_features): for n in xrange(self.n_excitatory_patch): self.connections[conn_name][feature * self.n_excitatory_patch + n, \ feature * self.n_excitatory_patch + n] = 10.4 elif connection_type == 'ie': # create connection name (composed of population and connection types) connection_name = name + connection_type[ 0] + name + connection_type[1] # create a connection from the first group in conn_name with the second group self.connections[connection_name] = b.Connection(self.neuron_groups[connection_name[0:2]], \ self.neuron_groups[connection_name[2:4]], structure='sparse', state='g' + conn_type[0]) # instantiate the created connection for feature in xrange(self.conv_features): for other_feature in xrange(self.conv_features): if feature != other_feature: for n in xrange(self.n_excitatory_patch): self.connections[connection_name][feature * self.n_excitatory_patch + n, \ other_feature * self.n_excitatory_patch + n] = 17.4 # adding random inhibitory connections as specified if self.random_inhibition_prob != 0.0: for feature in xrange(self.conv_features): for other_feature in xrange(self.conv_features): for n_this in xrange(self.n_excitatory_patch): for n_other in xrange( self.n_excitatory_patch): if n_this != n_other: if b.random( ) < self.random_inhibition_prob: self.connections[connection_name][feature * self.n_excitatory_patch + n_this, \ other_feature * self.n_excitatory_patch + n_other] = 17.4 elif connection_type == 'ee': # create connection name (composed of population and connection types) connection_name = name + connection_type[ 0] + name + connection_type[1] # create a connection from the first group in conn_name with the second group self.connections[connection_name] = b.Connection(self.neuron_groups[connection_name[0:2]], \ self.neuron_groups[connection_name[2:4]], structure='sparse', state='g' + connection_type[0]) # instantiate the created connection if self.connectivity == 'all': for feature in xrange(self.conv_features): for other_feature in xrange(self.conv_features): if feature != other_feature: for this_n in xrange( self.n_excitatory_patch): for other_n in xrange( self.n_excitatory_patch): if is_lattice_connection( self. n_excitatory_patch_sqrt, this_n, other_n): self.connections[connection_name][feature * self.n_excitatory_patch + this_n, \ other_feature * self.n_excitatory_patch + other_n] = \ (b.random() + 0.01) * 0.3 elif self.connectivity == 'pairs': for feature in xrange(self.conv_features): if feature % 2 == 0: for this_n in xrange(self.n_excitatory_patch): for other_n in xrange( self.n_excitatory_patch): if is_lattice_connection( self.n_excitatory_patch_sqrt, this_n, other_n): self.connections[connection_name][feature * self.n_excitatory_patch + this_n, \ (feature + 1) * self.n_excitatory_patch + other_n] = (b.random() + 0.01) * 0.3 elif feature % 2 == 1: for this_n in xrange(self.n_excitatory_patch): for other_n in xrange( self.n_excitatory_patch): if is_lattice_connection( self.n_excitatory_patch_patch, this_n, other_n): self.connections[connection_name][feature * self.n_excitatory_patch + this_n, \ (feature - 1) * self.n_excitatory_patch + other_n] = (b.random() + 0.01) * 0.3 elif connectivity == 'linear': for feature in xrange(self.conv_features): if feature != self.conv_features - 1: for this_n in xrange(self.n_excitatory_patch): for other_n in xrange( self.n_excitatory_patch): if is_lattice_connection( self.n_excitatory_patch_sqrt, this_n, other_n): self.connections[connection_name][feature * self.n_excitatory_patch + this_n, \ (feature + 1) * self.n_excitatory_patch + other_n] = \ (b.random() + 0.01) * 0.3 if feature != 0: for this_n in xrange(self.n_excitatory_patch): for other_n in xrange( self.n_excitatory_patch): if is_lattice_connection( self.n_excitatory_patch_sqrt, this_n, other_n): self.connections[connection_name][feature * self.n_excitatory_patch + this_n, \ (feature - 1) * self.n_excitatory_patch + other_n] = \ (b.random() + 0.01) * 0.3 elif self.connectivity == 'none': pass # if STDP from excitatory -> excitatory is on and this connection is excitatory -> excitatory if 'ee' in self.recurrent_conn_names: self.stdp_methods[name + 'e' + name + 'e'] = b.STDP(self.connections[name + 'e' + name + 'e'], \ eqs=eqs_stdp_ee, pre=eqs_stdp_pre_ee, \ post=eqs_stdp_post_ee, wmin=0., wmax=self.wmax_ee) print '...creating monitors for:', name # spike rate monitors for excitatory and inhibitory neuron populations self.rate_monitors[name + 'e'] = b.PopulationRateMonitor(self.neuron_groups[name + 'e'], \ bin=(self.single_example_time + self.resting_time) / b.second) self.rate_monitors[name + 'i'] = b.PopulationRateMonitor(self.neuron_groups[name + 'i'], \ bin=(self.single_example_time + self.resting_time) / b.second) self.spike_counters[name + 'e'] = b.SpikeCounter( self.neuron_groups[name + 'e']) # record neuron population spikes self.spike_monitors[name + 'e'] = b.SpikeMonitor( self.neuron_groups[name + 'e']) self.spike_monitors[name + 'i'] = b.SpikeMonitor( self.neuron_groups[name + 'i']) if do_plot: b.figure(self.fig_num) fig_num += 1 b.ion() b.subplot(211) b.raster_plot(self.spike_monitors['Ae'], refresh=1000 * b.ms, showlast=1000 * b.ms) b.subplot(212) b.raster_plot(self.spike_monitors['Ai'], refresh=1000 * b.ms, showlast=1000 * b.ms) # specifying locations of lattice connections self.lattice_locations = {} if self.connectivity == 'all': for this_n in xrange(self.conv_features * self.n_excitatory_patch): self.lattice_locations[this_n] = [ other_n for other_n in xrange(self.conv_features * self.n_excitatory_patch) \ if is_lattice_connection(self.n_excitatory_patch_sqrt, \ this_n % self.n_excitatory_patch, other_n % self.n_excitatory_patch) ] elif self.connectivity == 'pairs': for this_n in xrange(self.conv_features * self.n_excitatory_patch): self.lattice_locations[this_n] = [] for other_n in xrange(self.conv_features * self.n_excitatory_patch): if this_n // self.n_excitatory_patch % 2 == 0: if is_lattice_connection(self.n_excitatory_patch_sqrt, this_n % self.n_excitatory_patch, \ other_n % self.n_excitatory_patch) and \ other_n // self.n_excitatory_patch == this_n // self.n_excitatory_patch + 1: self.lattice_locations[this_n].append(other_n) elif this_n // self.n_excitatory_patch % 2 == 1: if is_lattice_connection(self.n_excitatory_patch_sqrt, this_n % self.n_excitatory_patch, \ other_n % self.n_excitatory_patch) and \ other_n // self.n_excitatory_patch == this_n // self.n_excitatory_patch - 1: self.lattice_locations[this_n].append(other_n) elif self.connectivity == 'linear': for this_n in xrange(self.conv_features * self.n_excitatory_patch): self.lattice_locations[this_n] = [] for other_n in xrange(conv_features * self.n_excitatory_patch): if this_n // self.n_excitatory_patch != self.conv_features - 1: if is_lattice_connection(self.n_excitatory_patch_sqrt, this_n % self.n_excitatory_patch, \ other_n % self.n_excitatory_patch) and \ other_n // self.n_excitatory_patch == this_n // self.n_excitatory_patch + 1: self.lattice_locations[this_n].append(other_n) elif this_n // self.n_excitatory_patch != 0: if is_lattice_connection(self.n_excitatory_patch_sqrt, this_n % self.n_excitatory_patch, \ other_n % self.n_excitatory_patch) and \ other_n // self.n_excitatory_patch == this_n // self.n_excitatory_patch - 1: self.lattice_locations[this_n].append(other_n) # setting up parameters for weight normalization between patches num_lattice_connections = sum( [len(value) for value in lattice_locations.values()]) self.weight['ee_recurr'] = (num_lattice_connections / self.conv_features) * 0.15 # creating Poission spike train from input image (784 vector, 28x28 image) for name in self.input_population_names: self.input_groups[name + 'e'] = b.PoissonGroup(self.n_input, 0) self.rate_monitors[name + 'e'] = b.PopulationRateMonitor(self.input_groups[name + 'e'], \ bin=(self.single_example_time + self.resting_time) / b.second) # creating connections from input Poisson spike train to convolution patch populations for name in self.input_connection_names: print '\n...creating connections between', name[0], 'and', name[1] # for each of the input connection types (in this case, excitatory -> excitatory) for connection_type in self.input_conn_names: # saved connection name connection_name = name[0] + connection_type[0] + name[ 1] + connection_type[1] # create connections from the windows of the input group to the neuron population self.input_connections[connection_name] = b.Connection(self.input_groups['Xe'], \ self.neuron_groups[name[1] + connection_type[1]], structure='sparse', \ state='g' + connection_type[0], delay=True, max_delay=self.delay[connection_type][1]) for feature in xrange(self.conv_features): for n in xrange(self.n_excitatory_patch): for idx in xrange(self.conv_size**2): self.input_connections[connection_name][self.convolution_locations[n][idx], \ feature * self.n_excitatory_patch + n] = (b.random() + 0.01) * 0.3 # if excitatory -> excitatory STDP is specified, add it here (input to excitatory populations) print '...creating STDP for connection', name # STDP connection name connection_name = name[0] + connection_type[0] + name[ 1] + connection_type[1] # create the STDP object self.stdp_methods[connection_name] = b.STDP(self.input_connections[connection_name], \ eqs=eqs_stdp_ee, pre=eqs_stdp_pre_ee, post=eqs_stdp_post_ee, wmin=0., wmax=self.wmax_ee) print '\n'