def run( self ): print( "starting simulation. length: %d" % self.time ) self.populations['pop1'].record() self.populations['pop2'].record() self.populations['pop3'].record() p.run( self.time ) self.plot_spikes( populations['pop1'], "population 1" ) self.plot_spikes( populations['pop2'], "population 2" ) self.plot_spikes( populations['pop3'], "population 3" )
def main(): # setup timestep of simulation and minimum and maximum synaptic delays setup(timestep=simulationTimestep, min_delay=minSynapseDelay, max_delay=maxSynapseDelay, threads=4) # create a spike sources retinaLeft = createSpikeSource("Retina Left") retinaRight = createSpikeSource("Retina Right") # create network and attach the spike sources network = createCooperativeNetwork(retinaLeft=retinaLeft, retinaRight=retinaRight) # run simulation for time in milliseconds print "Simulation started..." run(simulationTime) print "Simulation ended." # plot results plotExperiment(retinaLeft, retinaRight, network) # finalise program and simulation end()
'tau_syn_I' : 5.0, #The inhibitory input current decay time-constant 'v_reset' : -70.0, #The voltage to set the neuron at immediately after a spike 'v_rest' : -65.0, #The ambient rest voltage of the neuron 'v_thresh' : -50.0 #The threshold voltage at which the neuron will spike. } stim_pop1 = p.Population(n_neurons, p.SpikeSourcePoisson,{"rate": Hz,"start":0, "duration":pattern_length } ) #Stimuluspopultn ip_pop = p.Population(n_neurons, p.IF_curr_exp, cell_params_lif, label="inputneurons") project_stim_pop_ip_pop = p.Projection( stim_pop1,ip_pop, p.OneToOneConnector(weights=10, delays=1.0), target="excitatory") ip_pop.record() stim_pop1.record() p.run(70) spikes4 = ip_pop.getSpikes() spikes5=stim_pop1.getSpikes() with open('/home/ruthvik/Desktop/spikefile_'+str(Neurons)+'_'+str(pattern_length)+'ms'+'_'+str(Hz)+'Hz','w') as f: ##make a pickle of spike data pickle.dump(spikes4,f) spike_time4 = [i[1] for i in spikes4] spike_id4 = [i[0] for i in spikes4] pylab.plot(spike_time4, spike_id4, ".") pylab.xlabel("Time (ms)_stim_population") pylab.ylabel("Neuron ID") pylab.axis([0,70,0,Neurons]) pylab.show()
def run_sim(self): """ Sets up and runs the simulation """ num_neurons = 1471 # total neurons in network num_inputs = 14 # number of neurons considered inputs num_runs = 1 # number of times to loop the learning num_samples = 1 # number of samples to learn` sim_time = 1000.0 # time to run sim for` inhibitory_split = 0.2 connection_probability_factor = 0.02 plot_spikes = True save_figures = True show_figures = True sim_start_time = strftime("%Y-%m-%d_%H:%M") cell_params_lif = {'cm': 0.25, 'i_offset': 0.0, 'tau_m': 10.0, 'tau_refrac': 2.0, 'tau_syn_E': 3.0, 'tau_syn_I': 3.0, 'v_reset': -65.0, 'v_rest': -65.0, 'v_thresh': -50.0} # Create the 3d structure of the NeuCube based on the user's given structure file network_structure = NetworkStructure() network_structure.load_structure_file() network_structure.load_input_location_file() # Calculate the inter-neuron distance to be used in the small world connections network_structure.calculate_distances() # Generate two connection matrices for excitatory and inhibitory neurons based on your defined split network_structure.calculate_connection_matrix(inhibitory_split, connection_probability_factor) # Get these lists to be used when connecting the neurons later excitatory_connection_list = network_structure.get_excitatory_connection_list() inhibitory_connection_list = network_structure.get_inhibitory_connection_list() # Choose the correct neurons to connect them to, based on your a-priori knowledge of the data source -- eg, EEG # to 10-20 locations, fMRI to voxel locations, etc. input_neuron_indexes = network_structure.find_input_neurons() # Make the input connections based on this new list input_weight = 4.0 input_connection_list = [] for index, input_neuron_index in enumerate(input_neuron_indexes): input_connection_list.append((index, input_neuron_index, input_weight, 0)) for run_number in xrange(num_runs): excitatory_weights = [] inhibitory_weights = [] for sample_number in xrange(num_samples): # At the moment with the limitations of the SpiNNaker hardware we have to reinstantiate EVERYTHING # each run. In future there will be some form of repetition added, where the structure stays in memory # on the SpiNNaker and only the input spikes need to be updated. data_prefix = sim_start_time + "_r" + str(run_number + 1) + "-s" + str(sample_number + 1) # Set up the hardware - min_delay should never be less than the timestep. # Timestep should = 1.0 (ms) for normal realtime applications p.setup(timestep=1.0, min_delay=1.0) p.set_number_of_neurons_per_core("IF_curr_exp", 100) # Create a population of neurons for the reservoir neurons = p.Population(num_neurons, p.IF_curr_exp, cell_params_lif, label="Reservoir") # Setup excitatory STDP timing_rule_ex = p.SpikePairRule(tau_plus=20.0, tau_minus=20.0) weight_rule_ex = p.AdditiveWeightDependence(w_min=0.1, w_max=1.0, A_plus=0.02, A_minus=0.02) stdp_model_ex = p.STDPMechanism(timing_dependence=timing_rule_ex, weight_dependence=weight_rule_ex) # Setup inhibitory STDP timing_rule_inh = p.SpikePairRule(tau_plus=20.0, tau_minus=20.0) weight_rule_inh = p.AdditiveWeightDependence(w_min=0.0, w_max=0.6, A_plus=0.02, A_minus=0.02) stdp_model_inh = p.STDPMechanism(timing_dependence=timing_rule_inh, weight_dependence=weight_rule_inh) # record the spikes from that population neurons.record('spikes') # Generate a population of SpikeSourceArrays containing the encoded input spike data # eg. spike_sources = p.Population(14, p.SpikeSourceArray, {'spike_times': [[]]}) # for the moment I'm going to cheat and just use poisson trains as I don't have data with me spike_sources = p.Population(num_inputs, p.SpikeSourcePoisson, {'rate': rand.randint(20, 80)}, label="Poisson_pop_E") # Connect the input spike sources with the "input" neurons connected_inputs = p.Projection(spike_sources, neurons, p.FromListConnector(input_connection_list)) # If we have weights saved/recorded from a previous run of this network, load them into the structure # population.set(weights=weights_list) and population.setWeights(weight_list) are not supported in # SpiNNaker at the moment so we have to do this manually. if excitatory_weights and inhibitory_weights: for index, ex_connection in enumerate(excitatory_connection_list): ex_connection[2] = excitatory_weights[index] for index, in_connection in enumerate(inhibitory_connection_list): in_connection[2] = inhibitory_weights[index] # Setup the connectors excitatory_connector = p.FromListConnector(excitatory_connection_list) inhibitory_connector = p.FromListConnector(inhibitory_connection_list) # Connect the excitatory and inhibitory neuron populations connected_excitatory_neurons = p.Projection(neurons, neurons, excitatory_connector, synapse_dynamics=p.SynapseDynamics(slow=stdp_model_ex), target="excitatory") connected_inhibitory_neurons = p.Projection(neurons, neurons, inhibitory_connector, synapse_dynamics=p.SynapseDynamics(slow=stdp_model_inh), target="inhibitory") # Set up recording the spike trains of all the neurons neurons.record() spike_sources.record() # Run the actual simulation p.run(sim_time) # Save the output spikes spikes_out = neurons.getSpikes(compatible_output=True) input_spikes_out = spike_sources.getSpikes(compatible_output=True) # Get the synaptic weights of all the neurons excitatory_weights = connected_excitatory_neurons.getWeights() inhibitory_weights = connected_inhibitory_neurons.getWeights() # when we're all done, clean up p.end() # Make some plots, save them if required. Check if you need to either save or show them, because if not, # there's no point wasting time plotting things nobody will ever see. if plot_spikes and (save_figures or show_figures): plot = Plot(save_figures, data_prefix) # Plot the 3D structure of the network plot.plot_structure(network_structure.get_positions(), figure_number=0) # Plot the spikes plot.plot_spike_raster(spikes_out, sim_time, num_neurons, figure_number=1) # Plot the weights plot.plot_both_weights(excitatory_weights, inhibitory_weights, figure_number=2) # If we want to show the figures, show them now, otherwise ignore and move on if show_figures: # Show them all at once plot.show_plots() plot.clear_figures() plot = None
spikeArray = {'spike_times': [[0, 1050]]} populations.append(p.Population(nNeurons, p.IF_curr_exp, cell_params_lif, label='pop_1')) populations.append(p.Population(1, p.SpikeSourceArray, spikeArray, label='inputSpikes_1')) projections.append(p.Projection(populations[0], populations[0], p.FromListConnector(loopConnections))) projections.append(p.Projection(populations[1], populations[0], p.FromListConnector(injectionConnection))) populations[0].record_v() populations[0].record_gsyn() populations[0].record() p.run(runtime) v = None gsyn = None spikes = None v = populations[0].get_v(compatible_output=True) gsyn = populations[0].get_gsyn(compatible_output=True) spikes = populations[0].getSpikes(compatible_output=True) if spikes is not None: print spikes pylab.figure() pylab.plot([i[1] for i in spikes], [i[0] for i in spikes], ".") pylab.xlabel('Time/ms') pylab.ylabel('spikes')
p.get_db().set_number_of_neurons_per_core('IF_curr_exp', nNeurons) # this will set 256 neurons per core cell_params = { 'i_offset' : .1, 'tau_refrac' : 3.0, 'v_rest' : -65.0, 'v_thresh' : -51.0, 'tau_syn_E' : 2.0, 'tau_syn_I': 5.0, 'v_reset' : -70.0, 'e_rev_E' : 0., 'e_rev_I' : -80.} left_cochlea_ear = p.Population(nNeurons, p.ProxyNeuron, {'x_source':254, 'y_source':254}, label='left_cochlea_ear') left_cochlea_ear.set_mapping_constraint({'x':0, 'y':0}) left_cochlea_ear.record() # this should record spikes from the cochlea right_cochlea_ear = p.Population(nNeurons, p.SpikeSource, {}, label='right_cochlea_ear') right_cochlea_ear = p.Population(nNeurons, p.ProxyNeuron, {'x_source':254, 'y_source':255}, label='left_cochlea_ear') right_cochlea_ear.set_mapping_constraint({'x':0, 'y':0}) right_cochlea_ear.record() # this should record spikes from the cochlea ifcell = p.Population(nNeurons, p.IF_curr_exp, cell_params, label='IF_curr_exp') ifcell.record_v() p1 = p.Projection(left_cochlea_ear, ifcell, p.OneToOneConnector(weights=1, delays=1), target='excitatory') p2 = p.Projection(right_cochlea_ear, ifcell, p.OneToOneConnector(weights=1, delays=1), target='excitatory') p.run(200.0) p.end()
ssa1_times = {'spike_times': [[i+10, i+50] for i in range(10)]} ssa2_times = {'spike_times': [[14, 54]]} lif1 = p.Population(10, p.IF_curr_exp, cell_params_lif, label='lif1') lif2 = p.Population(1, p.IF_curr_exp, cell_params_lif, label='lif2') ssa1 = p.Population(10, p.SpikeSourceArray, ssa1_times, label='ssa1') ssa2 = p.Population(1, p.SpikeSourceArray, ssa2_times, label='ssa2') t_rule = p.SpikePairRule (tau_plus=1, tau_minus=1) w_rule = p.AdditiveWeightDependence (w_min=0, w_max=weight_to_spike, A_plus=weight_to_spike/26.0, A_minus=weight_to_spike/26.0) stdp_model = p.STDPMechanism (timing_dependence = t_rule, weight_dependence = w_rule) s_d = p.SynapseDynamics(slow = stdp_model) input_proj = p.Projection(lif1, lif2, p.AllToAllConnector(weights=weight_to_spike/26.0, delays=1), synapse_dynamics = s_d, target="excitatory") start_proj = p.Projection(ssa1, lif1, p.OneToOneConnector(weights=weight_to_spike, delays=1), target="excitatory") teaching_proj = p.Projection(ssa2, lif2, p.AllToAllConnector(weights=weight_to_spike, delays=1), target="excitatory") lif1.record() lif2.record() p.run(200) spikes1 = lif1.getSpikes() spikes2 = lif2.getSpikes() weights = input_proj.getWeights() print "Spikes generated by lif1: ", spikes1 print "Spikes generated by lif2: ", spikes2 print "final synaptic weights: ", weights
if i == 1: projections.append(p.Projection( populations[i - 1], dual_stim_population, p.OneToOneConnector( weights=weight_to_spike, delays=10), target='excitatory')) projections.append(p.Projection( populations[i], dual_stim_population, p.OneToOneConnector( weights=weight_to_spike, delays=10), target='excitatory2')) if i > 0: projections.append(p.Projection( populations[i - 1], populations[i], p.OneToOneConnector( weights=weight_to_spike, delays=10))) populations[i].record_v() populations[i].record() p.run(simulation_time) id_accumulator = 0 colour_divider = 0x600 / n_pop for i in range(n_pop): colour = 0x000000 colour_scale = (i / 6) * colour_divider if (i % 6) < 2 or (i % 6) == 5: colour += 0xFF0000 - (colour_scale * 0x10000) if (i % 6) > 0 and (i % 6) < 4: colour += 0x00FF00 - (colour_scale * 0x100) if (i % 6) > 2: colour += 0x0000FF - colour_scale data = numpy.asarray(populations[i].getSpikes()) if len(data) > 0: py_plot.scatter(
def run(simTime): print 'Model run starting at sim time ', spynnaker.get_current_time() spynnaker.run(simTime) print 'Model run ended at sim time ', spynnaker.get_current_time()
import pyNN.spiNNaker as p import spynnaker_external_devices_plugin.pyNN as q import pylab p.setup(1.0) injector = p.Population( 10, q.SpikeInjector, {"virtual_key": 0x4200, "port": 18000}, label="injector") pop = p.Population(10, p.IF_curr_exp, {}, label="pop") pop.record() p.Projection(injector, pop, p.OneToOneConnector(weights=5.0)) p.run(7000) spikes = pop.getSpikes() spike_time = [i[1] for i in spikes] spike_id = [i[0] for i in spikes] pylab.plot(spike_time, spike_id, ".") pylab.xlabel("Time (ms)") pylab.ylabel("Neuron ID") pylab.axis([0, 7000, -1, 10]) pylab.show()
p.Population(nNeurons, p.IF_curr_exp, cell_params_lif, label='pop_1')) populations.append( p.Population(1, p.SpikeSourceArray, spikeArray, label='inputSpikes_1')) projections.append( p.Projection(populations[0], populations[0], p.FromListConnector(loopConnections))) projections.append( p.Projection(populations[1], populations[0], p.FromListConnector(injectionConnection))) populations[0].record_v() populations[0].record_gsyn() populations[0].record() p.run(3000) v = None gsyn = None spikes = None v = populations[0].get_v(compatible_output=True) gsyn = populations[0].get_gsyn(compatible_output=True) spikes = populations[0].getSpikes(compatible_output=True) if spikes is not None: print spikes pylab.figure() pylab.plot([i[1] for i in spikes], [i[0] for i in spikes], ".") pylab.xlabel('Time/ms') pylab.ylabel('spikes')
spikeArray = {'spike_times': [[0, 1050]]} populations.append(p.Population(nNeurons, p.IF_curr_exp, cell_params_lif, label='pop_1')) populations.append(p.Population(1, p.SpikeSourceArray, spikeArray, label='inputSpikes_1')) projections.append(p.Projection(populations[0], populations[0], p.FromListConnector(loopConnections))) projections.append(p.Projection(populations[1], populations[0], p.FromListConnector(injectionConnection))) populations[0].record_v() populations[0].record_gsyn() populations[0].record() p.run(runtime) p.reset() populations.append(p.Population(nNeurons, p.IF_curr_exp, cell_params_lif, label='pop_2')) injectionConnection = [(nNeurons-1, 0, weight_to_spike, 1)] projections.append(p.Projection(populations[0], populations[2], p.FromListConnector(injectionConnection))) p.run(runtime) v = populations[0].get_v(compatible_output=True) gsyn = populations[0].get_gsyn(compatible_output=True) spikes = populations[0].getSpikes(compatible_output=True) if spikes is not None:
import pyNN.spiNNaker as p import pylab p.setup(1.0) input = p.Population( 1, p.SpikeSourceArray, {"spike_times": [0.0]}, label="input") synfire_pop = p.Population(100, p.IF_curr_exp, {}, label="synfire") p.Projection(input, synfire_pop, p.FromListConnector([(0, 0, 5.0, 1.0)])) synfire_list = [] for n in range(0, 100): synfire_list.append((n, (n + 1) % 100, 5.0, 5.0)) p.Projection(synfire_pop, synfire_pop, p.FromListConnector(synfire_list)) synfire_pop.record() p.run(2000) spikes = synfire_pop.getSpikes() spike_time = [i[1] for i in spikes] spike_id = [i[0] for i in spikes] pylab.plot(spike_time, spike_id, ".") pylab.xlabel("Time (ms)") pylab.ylabel("Neuron ID") pylab.axis([0, 2000, 0, 100]) pylab.show()
def get_outgoing_edge_constraints(self, partitioned_edge, graph_mapper): constraints = AbstractOutgoingEdgeSameContiguousKeysRestrictor\ .get_outgoing_edge_constraints( self, partitioned_edge, graph_mapper) constraints.append( KeyAllocatorFixedKeyAndMaskConstraint( [KeyAndMask(0x42000000, 0xFFFF0000)])) return constraints def is_virtual_vertex(self): return True def model_name(self): return "My External Device" import pyNN.spiNNaker as p from pacman.model.partitionable_graph.multi_cast_partitionable_edge \ import MultiCastPartitionableEdge p.setup(1.0) device = p.Population(20, MyExternalDevice, {"spinnaker_link_id": 0}, label="external device") pop = p.Population(20, p.IF_curr_exp, {}, label="population") p.Projection(device, pop, p.OneToOneConnector()) p.run(10)
p.SpikeSource, {}, label='right_cochlea_ear') right_cochlea_ear = p.Population(nNeurons, p.ProxyNeuron, { 'x_source': 254, 'y_source': 255 }, label='left_cochlea_ear') right_cochlea_ear.set_mapping_constraint({'x': 0, 'y': 0}) right_cochlea_ear.record() # this should record spikes from the cochlea ifcell = p.Population(nNeurons, p.IF_curr_exp, cell_params, label='IF_curr_exp') ifcell.record_v() p1 = p.Projection(left_cochlea_ear, ifcell, p.OneToOneConnector(weights=1, delays=1), target='excitatory') p2 = p.Projection(right_cochlea_ear, ifcell, p.OneToOneConnector(weights=1, delays=1), target='excitatory') p.run(200.0) p.end()
'tau_syn_E' : 5.0, #The excitatory input current decay time-constant 'tau_syn_I' : 5.0, #The inhibitory input current decay time-constant 'v_reset' : -70.0, #The voltage to set the neuron at immediately after a spike 'v_rest' : -65.0, #The ambient rest voltage of the neuron 'v_thresh' : -50.0 #The threshold voltage at which the neuron will spike. } stim_pop1 = p.Population(n_neurons, p.SpikeSourcePoisson,{"rate": 60.0,"start":0, "duration":50 } ) #Stimuluspopultn ip_pop = p.Population(n_neurons, p.IF_curr_exp, cell_params_lif, label="inputneurons") project_stim_pop_ip_pop = p.Projection( stim_pop1,ip_pop, p.OneToOneConnector(weights=10, delays=0.0), target="excitatory") stim_pop1.record() p.run(50) spikes4 = stim_pop1.getSpikes() with open('/home/ruthvik/Desktop/spikefile_800_50ms','w') as f: ##make a pickle of spike data pickle.dump(spikes4,f) spike_time4 = [i[1] for i in spikes4] spike_id4 = [i[0] for i in spikes4] pylab.plot(spike_time4, spike_id4, ".") pylab.xlabel("Time (ms)") pylab.ylabel("Neuron ID") pylab.axis([0,50,0,800]) pylab.show()
def lancement_sim(cellSourceSpikes, max_time=800000, path="default", TIME_STEP=TIME_STEP, input_n=input_n, nb_neuron_int=nb_neuron_int, nb_neuron_out=nb_neuron_out, delay=delay, p_conn_in_int=p_conn_in_int, p_conn_int_out=p_conn_int_out, a_minus=0.6, a_plus=0.6, tau_minus=12.0, tau_plus=10.0, v_tresh=10.0): simulator = 'spinnaker' sim.setup(timestep=TIME_STEP, min_delay=delay, max_delay=delay * 2) randoms = np.random.rand(100, 1) lif_curr_exp_params = { 'cm': 1.0, # The capacitance of the LIF neuron in nano-Farads 'tau_m': 20.0, # The time-constant of the RC circuit, in milliseconds 'tau_refrac': 5.0, # The refractory period, in milliseconds 'v_reset': -65.0, # The voltage to set the neuron at immediately after a spike 'v_rest': -65.0, # The ambient rest voltage of the neuron 'v_thresh': -(v_tresh), # The threshold voltage at which the neuron will spike 'tau_syn_E': 5.0, # The excitatory input current decay time-constant 'tau_syn_I': 5.0, # The inhibitory input current decay time-constant 'i_offset': 0.0, # A base input current to add each timestep } Input = sim.Population(input_n, sim.SpikeSourceArray(spike_times=cellSourceSpikes), label="Input") Input.record("spikes") LIF_Intermediate = sim.IF_curr_exp(**lif_curr_exp_params) Intermediate = sim.Population(nb_neuron_int, LIF_Intermediate, label="Intermediate") Intermediate.record(("spikes", "v")) LIF_Output = sim.IF_curr_exp(**lif_curr_exp_params) Output = sim.Population(nb_neuron_out, LIF_Output, label="Output") Output.record(("spikes", "v")) LIF_delayer = sim.IF_curr_exp(**lif_curr_exp_params) Delay_n = sim.Population(1, LIF_delayer, label="Delay") Delay_n.record(("spikes", "v")) python_rng = NumpyRNG(seed=98497627) delay = delay # (ms) synaptic time delay # Définition du fonctionnement de la stdp stdp_proj = sim.STDPMechanism( timing_dependence=sim.SpikePairRule(tau_plus=tau_plus, tau_minus=tau_minus, A_plus=a_plus, A_minus=a_minus), weight_dependence=sim.AdditiveWeightDependence(w_min=0.1, w_max=6), weight=RandomDistribution('normal', (3, 2.9), rng=python_rng), delay=delay) Conn_input_inter = sim.Projection( Input, Intermediate, connector=sim.FixedProbabilityConnector(p_conn_in_int, allow_self_connections=False), # synapse type set avec la définition de la stdp pour l'aprentsissage synapse_type=stdp_proj, receptor_type="excitatory", label="Connection input to intermediate") # second projection with stdp Conn_inter_output = sim.Projection( Intermediate, Output, # pre and post population connector=sim.FixedProbabilityConnector(p_conn_int_out, allow_self_connections=False), synapse_type=stdp_proj, receptor_type="excitatory", label="Connection intermediate to output") FixedInhibitory_WTA = sim.StaticSynapse(weight=6) WTA_INT = sim.Projection( Intermediate, Intermediate, connector=sim.AllToAllConnector(allow_self_connections=False), synapse_type=FixedInhibitory_WTA, receptor_type="inhibitory", label="Connection WTA") WTA_OUT = sim.Projection( Output, Output, connector=sim.AllToAllConnector(allow_self_connections=False), synapse_type=FixedInhibitory_WTA, receptor_type="inhibitory", label="Connection WTA") FixedInhibitory_delayer = sim.StaticSynapse(weight=2) Delay_out = sim.Projection( Delay_n, Output, connector=sim.AllToAllConnector(allow_self_connections=False), synapse_type=FixedInhibitory_delayer, receptor_type="inhibitory", label="Connection WTA") Delay_inter = sim.Projection( Intermediate, Delay_n, connector=sim.AllToAllConnector(allow_self_connections=False), synapse_type=FixedInhibitory_delayer, receptor_type="inhibitory", label="Connection WTA") sim.set_number_of_neurons_per_core(sim.IF_curr_exp, 255) # Définition des callbacks pour la récupération de l'écart-type sur les connexions entrée-intermédiaire, intermédiaire-sortie weight_recorder1 = WeightRecorder(sampling_interval=1000.0, projection=Conn_input_inter) weight_recorder2 = WeightRecorder(sampling_interval=1000.0, projection=Conn_inter_output) simtime = ceil(max_time) # Initialisation des tableaux pour la récupération des poids weights_int = [] weights_out = [] try: sim.run(simtime, callbacks=[weight_recorder1, weight_recorder2]) neo = Output.get_data(variables=["spikes", "v"]) spikes = neo.segments[0].spiketrains #print(spikes) v = neo.segments[0].filter(name='v')[0] weights_int = Conn_input_inter.get(["weight"], format="list") neo_in = Input.get_data(variables=["spikes"]) spikes_in = neo_in.segments[0].spiketrains #print(spikes_in) neo_intermediate = Intermediate.get_data(variables=["spikes", "v"]) spikes_intermediate = neo_intermediate.segments[0].spiketrains #print(spikes_intermediate) v_intermediate = neo_intermediate.segments[0].filter(name='v')[0] #print(v_intermediate) weights_out = Conn_inter_output.get(["weight"], format="list") sim.reset() sim.end() except: v = 0 spikes = 0 if (isinstance(spikes, list) and isinstance(v, AnalogSignal)): # Récupération des écart-types standard_deviation_out = weight_recorder2.get_standard_deviations() standard_deviation_int = weight_recorder1.get_standard_deviations() t = np.arange(0., max_time, 1.) # Création et sauvegarde des graphs sur les spikes et écart-types savePath = "./Generated_data/training/" + path + "/intermediate_layer_standard_deviation.png" plt.plot(standard_deviation_int) plt.xlabel("callbacks tick (1s)") plt.ylabel("standard deviation of the weights( wmax=6, wmin=0.1 )") plt.savefig(savePath) plt.clf() savePath = "./Generated_data/training/" + path + "/output_layer_standard_deviation.png" plt.plot(standard_deviation_out) plt.xlabel("callbacks tick") plt.ylabel("standard deviation ( wmax=6, wmin=0.1 )") plt.savefig(savePath) plt.clf() savePath = "./Generated_data/training/" + path + "/output_layer_membrane_voltage_and_spikes.png" plot.Figure( # plot voltage for first ([0]) neuron plot.Panel(v, ylabel="Membrane potential (mV)", data_labels=[Output.label], yticks=True, xlim=(0, simtime)), # plot spikes (or in this case spike) plot.Panel(spikes, yticks=True, markersize=5, xlim=(0, simtime)), title="Spiking activity of the output layer during training", annotations="Simulated with {}".format(sim.name())).save(savePath) savePath = "./Generated_data/training/" + path + "/intermediate_layer_membrane_voltage_and_spikes.png" plot.Figure( # plot voltage for first ([0]) neuron plot.Panel(v_intermediate, ylabel="Membrane potential (mV)", data_labels=[Output.label], yticks=True, xlim=(0, simtime)), # plot spikes (or in this case spike) plot.Panel(spikes_intermediate, yticks=True, markersize=5, xlim=(0, simtime)), title="Spiking activity of the intermediate layer during training", annotations="Simulated with {}".format(sim.name())).save(savePath) return v, spikes, weights_int, weights_out else: print( "simulation failed with parmaters parameters : (l'affichage des paramètres ayant causés le disfonctionnement de la simulation sera traitée à une date ultérieur, merci!)" ) return 0, 0, 0, 0
what_statement_gate.record() who_query_gate.record() who_query_gate.record_v() who_query_gate.record_gsyn() where_query_gate.record() what_query_gate.record() dst_who.record() dst_where.record() dst_what.record() who.record() where.record() what.record() p.run(15 * 1000) src_pop_spikes = src_pop.getSpikes(compatible_output=True) who_statement_gate_spikes = who_statement_gate.getSpikes(compatible_output=True) who_statement_gate_v = who_statement_gate.get_v() who_statement_gate_i = who_statement_gate.get_gsyn() where_statement_gate_spikes = where_statement_gate.getSpikes(compatible_output=True) what_statement_gate_spikes = what_statement_gate.getSpikes(compatible_output=True) who_query_gate_spikes = who_query_gate.getSpikes(compatible_output=True) who_query_gate_v = who_query_gate.get_v() who_query_gate_i = who_query_gate.get_gsyn() where_query_gate_spikes = where_query_gate.getSpikes(compatible_output=True) what_query_gate_spikes = what_query_gate.getSpikes(compatible_output=True) dst_who_spikes = dst_who.getSpikes(compatible_output=True) dst_where_spikes = dst_where.getSpikes(compatible_output=True) dst_what_spikes = dst_what.getSpikes(compatible_output=True)
# Layer 2 (hidden layer) populations: populations.append(p.Population(nL2ExcitNeurons, p.IF_curr_exp, cell_params_lif, label='L2Excit')) ## Projections # Starting in layer 1: projections.append(p.Projection(populations[L1E], populations[L1I], p.FixedProbabilityConnector(p_connect=pL1E2I, weights=L1MeanWeightX2X, delays=E2ImeanDelay), target='excitatory')) # From excit to its own inhib neurons projections.append(p.Projection(populations[L1E], populations[L2E], p.FixedProbabilityConnector(p_connect=pL1E2E, weights=L1MeanWeightX2X, delays=E2ImeanDelay), target='excitatory')) # Feedforward excitation #projections.append(p.Projection(populations[L1I], populations[L2E], p.FixedProbabilityConnector(p_connect=pL1I2E, weights=distWeightL1I2E, delays=I2EmeanDelay), target='inhibitory')) # Feedforward inhibition #populations[L1E].record() #populations[L1I].record() #populations[L2E].record() populations[L1I].record_v() populations[L2E].record_v() p.run(runTime) v1I = populations[L1I].get_v(compatible_output=True) v2E = populations[L2E].get_v(compatible_output=True) gsyn = None spikes = None #spikesL1E = populations[L1E].getSpikes(compatible_output=True) #spikesL1I = populations[L1I].getSpikes(compatible_output=True) #spikesL2E = populations[L2E].getSpikes(compatible_output=True) print "Potential info:" print v1I print "Length: ", len(v1I) print "Single potential info:" print v1I[1]
p.Projection(stim_pop_pattern, input,p.OneToOneConnector(weights = 10.0,delays = 0),target = "excitatory") ##setting up the parameters for STDP t_rule = p.SpikePairRule (tau_plus=16.8, tau_minus=33.7) #The 2 parameters of this class identify the exponential decay rate of the STDP function(Curve) w_rule = p.AdditiveWeightDependence (w_min=0.0, w_max=weight_to_spike, A_plus=0.03125, A_minus=0.85*A_plus) stdp_model = p.STDPMechanism (timing_dependence = t_rule, weight_dependence = w_rule) #STDP mechanism involving weight and timing. s_d = p.SynapseDynamics(slow = stdp_model)#instantial of synaptic plasticity project_ip_op = p.Projection( input,op_pop, p.AllToAllConnector(weights=.475, delays=0), synapse_dynamics = s_d, target="excitatory") input.record() op_pop.record() input.record_v() p.run(15500) import pylab v = input.getSpikes() spikes1 = input.getSpikes() spikes2 = op_pop.getSpikes() weights = project_ip_op.getWeights() print "final synaptic weight: ", weights spike_time = [i[1] for i in spikes1] spike_id = [i[0] for i in spikes1] pylab.plot(spike_time, spike_id, ".") pylab.xlabel("Time(ms)") pylab.ylabel("NeuronID")
str(prediction * (100 - current_poucentage))) current_poucentage += 1 if (df['x'].iloc[i] % 4 == 0 & df['y'].iloc[i] % 4 == 0): x = df['x'].iloc[i] y = df['y'].iloc[i] time = df['ts'].iloc[i] * 1.e-3 if (timemax < time): timemax = time index = y * x + x - 2 sourceArray[index] = sourceArray[index] + [time] # Pour la desambiguisation sur SpiNNaker sourceArray = [list(elem) for elem in sourceArray] # lancement d'un simulation ultra simple pour vérifier l'acceptation du spikeSourceArray import pyNN.spiNNaker as sim simulator = 'spinnaker' sim.setup(timestep=10, min_delay=20, max_delay=30) sources = sim.SpikeSourceArray(spike_times=sourceArray) spikeSource = sim.Population(1024, sources) spikeSource.record(['spikes']) sim.run(simtime=10000) spikeSources = spikeSource.get_data() #.segments[0].spiketrains S_spikes = spikeSources.segments[0].spiketrains sim.end()
A_plus=0.005, A_minus=0.005), weight_dependence=sim.AdditiveWeightDependence(w_min=0.0, w_max=0.0175), weight=start_w) projections.append( sim.Projection(pre_pop, post_pop, sim.OneToOneConnector(), synapse_type=stdp_model)) print("Simulating for %us" % (sim_time / 1000)) # Run simulation sim.run(sim_time) # Get weight from each projection end_w = [p.get('weight', 'list', with_address=False)[0] for p in projections] # End simulation on SpiNNaker sim.end() # ------------------------------------------------------------------- # Plot curve # ------------------------------------------------------------------- # Calculate deltas from end weights delta_w = [(w - start_w) / start_w for w in end_w] # Plot STDP curve figure, axis = pylab.subplots()
spikeArray = {'spike_times': [[0]]} populations.append(p.Population(nNeurons, p.IF_curr_exp, cell_params_lif, label='pop_1')) populations.append(p.Population(1, p.SpikeSourceArray, spikeArray, label='inputSpikes_1')) projections.append(p.Projection(populations[0], populations[0], p.FromListConnector(loopConnections))) projections.append(p.Projection(populations[1], populations[0], p.FromListConnector(injectionConnection))) populations[0].record_v() populations[0].record_gsyn() populations[0].record() p.run(526) p.run(526) p.run(526) p.run(526) p.run(526) p.run(370) v = None gsyn = None spikes = None v = populations[0].get_v(compatible_output=True) gsyn = populations[0].get_gsyn(compatible_output=True) spikes = populations[0].getSpikes(compatible_output=True) if spikes is not None:
def train_snn( ### Settings data="load", cls="load", save=True, # True to save all parameters of the network randomness=True, reverse_src_del=False, use_old_weights=True, rand_data=False, ### Parameters n_training=2, # How many times the samples will be iterated ts=1., # Timestep of Spinnaker (ms) trial_num=10, # Number of features (= 4 features * 20 neurons) # => 20 neuros: resolution of encoding n_feature=80, # Weights wei_src_enc=.2, # From Source Array at input to Encoding Layer(Exc) wei_enc_filt=.6, # From Encoding Layer to Filtering Layer Exc neurons (Exc) wei_filt_inh=0.03, # From Filtering Layer Inh neurons to Exc neurons (Inh) wei_init_stdp=.0, # From Filtering Layer Exc neurons to Output Layer (Exc) wei_cls_exc=0.9, # From Output Layer Exc neurons to Inh neurons (Exc) wei_cls_inh=0.1, #,10 # From Output Layer Inh neurons to Exc neurons (Inh) wei_source_outp=10., # From Source Array at output to Output Layer Exc neurons (Exc) wei_noise_poi=0.02, # Delays del_init_stdp=1., del_source_outp=1., del_noise_poi=1., # Connection Probabilities prob_filt_inh=.4, # Prob of connectivity inhibitory connections at FilT_Layer prob_stdp=1., # Prob of STDP connections prob_output_inh=.7, # Prob of inhibitory connections at Output Layer prob_noise_poi_conn=0.02, ## STDP Parameters tau_pl=5., stdp_w_max=0.4, # default 0.4 stdp_w_min=0.0, # default 0.0 stdp_A_pl=0.02, # 0.01, # default 0.01 (below 0.01 weights don't change) # => minus in order to get symmetric curve # Data Extraction scale_data=2.): # Scale features into [0-scale_data] range # BUG fix: # n_feature is somehow a tuple try: trial_num = trial_num[0] except Exception as e: pass ############################################################################ ## Function Definitions ############################################################################ def gaussian(x, mu, sig): return np.float16( np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))) def calc_pop_code(feature, rng1, rng2, num): interval = np.float(rng2 - rng1) / num means = np.arange(rng1 + interval, rng2 + interval, interval) pop_code = [gaussian(feature, mu, 0.025) for mu in means] return pop_code def PoissonTimes2(t_str=0., t_end=100., rate=10., seed=1.): times = [t_str] rng = np.random.RandomState(seed=seed) cont = True while cont == True: t_next = np.floor(times[-1] + 1000. * next_spike_times(rng, rate)) if t_next < t_end - 30: times.append(t_next[0]) else: cont = False return times[1:] def PoissonTimes(t_str=0., t_end=100., rate=10., seed=1.): if rate > 0: interval = (t_end - t_str + 0.) / rate times = np.arange(t_str + 30, t_end - 40, interval) return list(times) else: return [] def next_spike_times(rng, rate): return -np.log(1.0 - rng.rand(1)) / rate def ismember(a, b): b = [b] bind = {} for i, elt in enumerate(b): if elt not in bind: bind[elt] = i aa = [bind.get(itm, -1) for itm in a] return sum(np.array(aa) + 1.) def get_data(trial_num, test_num=10): # trial_num: number of training samples # test_num: number of test samples pass def rand_sample_of_train_set(n): # n: number of features # Return: np.array containing n samples of the training set X = np.load('data/X_iris_train.npy') y = np.load('data/y_iris_train.npy') idx = np.random.randint(len(X), size=n) return X[idx], y[idx] def PCA_dim_red(X, var_desired): """ Dimensionality reduction using PCA X: matrix (2d np.array) var_desired: desired preserved variance Returns X with reduced dimesnions """ # PCA pca = PCA(n_components=X.shape[1] - 1) pca.fit(X) print('pca.explained_variance_ratio_:\n', pca.explained_variance_ratio_) var_sum = pca.explained_variance_ratio_.sum() var = 0 for n, v in enumerate(pca.explained_variance_ratio_): var += v if var / var_sum >= var_desired: X_reduced = PCA(n_components=n + 1).fit_transform(X) print( "Reached Variance: {:1.3f} at {}-Dimensions. New shape: {}" .format(var / var_sum, n + 1, X_reduced.shape)) return X_reduced ############################################################################ ## Parameters ############################################################################ # Load training data # only load n_rand_data features of training set if rand_data == True: data, cls = rand_sample_of_train_set(trial_num) # load all features of training set else: # Only read data if not given as argument if data == "load" and cls == "load": #data = np.load('data/X_iris_train.npy') #cls = np.load('data/y_iris_train.npy') data = np.load('data_eeg/X_train_zied.npy') cls = np.load('data_eeg/y_train_zied.npy') if 1: data = PCA_dim_red(data, var_desired=0.9) # Simulation Parameters trial_num = len( cls) # How many samples (trials) from data will be presented #n_training = 1 # How many times the samples will be iterated n_trials = n_training * trial_num # Total trials time_int_trials = 200. # (ms) Time to present each trial data SIM_TIME = n_trials * time_int_trials # Total simulation time (ms) #ts = 1. # Timestep of Spinnaker (ms) min_del = ts max_del = 144 * ts p.setup(timestep=ts, min_delay=min_del, max_delay=max_del) ## Neuron Numbers #n_feature = 80 # Number of features (= 4 features * 20 neurons) # => 20 neuros: resolution of encoding n_pop = 4 # Number of neurons in one population n_cl = 2 # Number of classes at the output ## Connection Parameters # Weights # wei_src_enc = .2 # From Source Array at input to Encoding Layer(Exc) # wei_enc_filt = .6 # From Encoding Layer to Filtering Layer Exc neurons (Exc) # wei_filt_inh = 0.03 # From Filtering Layer Inh neurons to Exc neurons (Inh) # wei_init_stdp = .0 # From Filtering Layer Exc neurons to Output Layer (Exc) # wei_cls_exc = 0.9 # From Output Layer Exc neurons to Inh neurons (Exc) # wei_cls_inh = 10 # 0.1 # From Output Layer Inh neurons to Exc neurons (Inh) # wei_source_outp = 10. # From Source Array at output to Output Layer Exc neurons (Exc) # wei_noise_poi = 0.02 # Delays if randomness == True: # if True: calculate "del_src_enc" (randomly) new # if False: load previously saved "del_src_enc" if reverse_src_del == True: # calc delays erversly proportional to feature value del_src_enc = np.zeros(n_feature * n_pop) else: del_src_enc = [ int(np.random.randint(n_pop) + 1) for _ in range(n_feature * n_pop) ] np.save("output_files/del_src_enc.npy", del_src_enc) else: #del_src_enc = np.load("output_files/del_src_enc.npy") del_src_enc = np.ones(n_feature * n_pop).astype( int) #[1 for _ in range(n_feature*n_pop)] del_enc_filt = ts del_filt_inh = ts # del_init_stdp = 1. del_cls_exc = ts del_cls_inh = ts # del_source_outp = 1. # del_noise_poi = 1. # Firing Rates noise_poi_rate = 10. max_fr_input = 100. # maximum firing rate at the input layer max_fr_rate_output = 20. # Maximum firing rate at output (supervisory signal) ## Connection Probabilities # prob_filt_inh = .4 # Prob of connectivity inhibitory connections at FilT_Layer # prob_stdp = 1. # Prob of STDP connections # prob_output_inh = .7 # Prob of inhibitory connections at Output Layer # prob_noise_poi_conn = 0.02 ## STDP Parameters # tau_pl = 0.3 # (0.2 - 0.3 works) tau_min = tau_pl # default tau_pl # stdp_w_max = 0.4 # default 0.4 # stdp_w_min = 0.0 # default 0.0 # stdp_A_pl = 0.01 # default 0.01 (below 0.01 weights don't change) stdp_A_min = -stdp_A_pl # default - stdp_A_pl # => minus in order to get symmetric curve ## Neuron Parameters cell_params_lif = { 'cm': 0.25, #1., 'i_offset': 0.0, 'tau_m': 20., 'tau_refrac': 2.0, 'tau_syn_E': 5.0, 'tau_syn_I': 5.0, 'v_reset': -70.0, 'v_rest': -65.0, 'v_thresh': -50 #-65.0 } ############################################################################ ## Data Extraction ############################################################################ ## Extract Feature Data # scale_data = 2. # Scale features into [0-scale_data] range r, c = np.shape(data) data_rates = np.reshape(data, (1, r * c))[0] # Threshold (to keep spikes in range) thr_data_plus = 30 thr_data_minus = -10 #dd = [d if d<thr_data_plus else thr_data_plus for d in data_rates] #dd = [d if d>thr_data_minus else thr_data_minus for d in dd] # Shift and normalize data #dd2 = np.array(dd) - min(dd) dd2 = np.array(data_rates) - min(data_rates) dd2 = dd2 / max(dd2) * 2 new_data_rates = [] for r in dd2: new_data_rates += calc_pop_code(r, 0., scale_data, n_feature / (n_pop + 0.0)) data_rates = list(max_fr_input * np.array(new_data_rates)) ## Extract Class Data # load class vector #cls = np.load(path_y) cls = np.reshape(cls, (len(cls), 1)) # create col vector r_cl, c_cl = np.shape(cls) #cls = list(np.reshape(cls, (1, r_cl * c_cl))[0] - 1) cls = list(np.reshape(cls, (1, r_cl * c_cl))[0]) ## The class and rate infromation to be used during the simulation outputs = n_training * cls[0:trial_num] # positiv, ints poi_rate = n_training * data_rates[0:trial_num * n_feature] ## Save parameters to be used in test parameter_dict = { "n_feature": n_feature, "n_pop": n_pop, "n_cl": n_cl, "wei_src_enc": wei_src_enc, "wei_enc_filt": wei_enc_filt, "wei_filt_inh": wei_filt_inh, "wei_cls_exc": wei_cls_exc, "wei_cls_inh": wei_cls_inh, "del_enc_filt": del_enc_filt, "del_init_stdp": del_init_stdp, "del_cls_exc": del_cls_exc, "del_cls_inh": del_cls_inh, "trial_num": trial_num, "time_int_trials": time_int_trials, "scale_data": scale_data, "ts": ts, "max_fr_input": max_fr_input, "max_fr_rate_output": max_fr_rate_output, "noise_poi_rate": noise_poi_rate, "max_fr_input": max_fr_input, "max_fr_rate_output": max_fr_rate_output, "prob_filt_inh": prob_filt_inh, "prob_stdp": prob_stdp, "prob_output_inh": prob_output_inh, "prob_noise_poi_conn": prob_noise_poi_conn, "tau_pl": tau_pl, "stdp_w_max": stdp_w_max, "stdp_w_min": stdp_w_min, "stdp_A_pl": stdp_A_pl, "wei_noise_poi": wei_noise_poi, "del_noise_poi": del_noise_poi, "thr_data_plus": thr_data_plus, "thr_data_minus": thr_data_minus } if save == True: np.save("output_files/parameters1", parameter_dict) np.save("output_files/parameters2", del_src_enc) ############################################################################ ## Create populations for different layers ############################################################################ poi_layer = [] enc_layer = [] filt_layer_exc = [] out_layer_exc = [] out_layer_inh = [] out_spike_source = [] # Calculate spike times at the input using the rate information coming from features spike_times = [[] for i in range(n_feature)] for i in range(n_trials): t_st = i * time_int_trials t_end = t_st + time_int_trials ind = i * n_feature for j in range(n_feature): times = PoissonTimes(t_st, t_end, poi_rate[ind + j], np.random.randint(100)) for t in times: spike_times[j].append(t) if randomness == True: # if True: calculate "spike_times" (randomly) new # uf False: load previously saved "spike_times" np.save('output_files/spike_times_train.npy', spike_times) else: spike_times = np.load('output_files/spike_times_train.npy') # Calculate spike times at the output (as supervisory signal) out_spike_times = [[] for i in range(n_cl)] for i in range(n_trials): t_st = i * time_int_trials t_end = t_st + time_int_trials ind = outputs[i] times = PoissonTimes(t_st, t_end, max_fr_rate_output, np.random.randint(100)) for t in times: out_spike_times[int(ind)].append(t) if randomness == True: # if True: calculate "out_spike_times" (randomly) new # uf False: load previously saved "out_spike_times" np.save('output_files/out_spike_times.npy', out_spike_times) else: out_spike_times = np.load('output_files/out_spike_times.npy') # Spike source of input layer spike_source = p.Population(n_feature, p.SpikeSourceArray, {'spike_times': spike_times}, label='spike_source') # Spike source of output layer (Supervisory signal) for i in range(n_cl): out_spike_source.append( p.Population(1, p.SpikeSourceArray, {'spike_times': [out_spike_times[i]]}, label='out_spike_source')) # Encoding layer and Filtering Layer definitions enc_layer = p.Population(n_feature * n_pop, p.IF_curr_exp, cell_params_lif, label='enc_layer') filt_layer = p.Population(n_feature * n_pop, p.IF_curr_exp, cell_params_lif, label='filt_layer') # Excitatory and Inhibitory population definitions at the output for i in range(n_cl): out_layer_exc.append( p.Population(n_pop, p.IF_curr_exp, cell_params_lif, label='out_layer_exc{}'.format(i))) out_layer_inh.append( p.Population(n_pop, p.IF_curr_exp, cell_params_lif, label='out_layer_inh{}'.format(i))) out_layer_exc[i].record() # Noisy poisson population at the input poisson_input = p.Population(n_pop * 2, p.SpikeSourcePoisson, {"rate": noise_poi_rate}) # Record Spikes enc_layer.record() filt_layer.record() #enc_layer.initialize('v',p.RandomDistribution('uniform',[-51.,-69.])) #filt_layer.initialize('v',p.RandomDistribution('uniform',[-51.,-69.])) ############################################################################ ## Projections ############################################################################ ## Connection List from Spike Source Array to Encoding Layer conn_inp_enc = [] for i in range(n_feature): ind = i * n_pop for j in range(n_pop): conn_inp_enc.append( [i, ind + j, wei_src_enc, del_src_enc[ind + j]]) if save == True: np.save("output_files/conn_inp_enc", conn_inp_enc) ## Connection List for Filtering Layer Inhibitory if randomness == True: # if True: calculate conn_filt_inh (randomly) new # uf False: load previously saved conn_filt_inh conn_filt_inh = [] for i in range(n_feature): rng1 = i * n_pop rng2 = rng1 + n_pop inp = range(rng1, rng2) outp = range(0, rng1) + range(rng2, n_feature * n_pop) for ii in inp: for jj in outp: if prob_filt_inh > np.random.rand(): conn_filt_inh.append( [ii, jj, wei_filt_inh, del_filt_inh]) if save == True: np.save('output_files/conn_filt_inh.npy', conn_filt_inh) else: conn_filt_inh = np.load('output_files/conn_filt_inh.npy') ## STDP Connection List if randomness == True: # if True: calculate conn_stdp_list (randomly) new # uf False: load previously saved conn_stdp_list conn_stdp_list = [[] for i in range(n_cl)] for i in range(n_cl): # For each population at output layer if use_old_weights == True: cl_weights = np.load( "output_files/stdp_weights{}.npy".format(i)) w = 0 for ii in range(n_pop * n_feature): # For each neuron in filtering layer for jj in range( n_pop ): # For each neuron in each population of output layer if prob_stdp > np.random.rand( ): # If the prob of connection is satiesfied # Make the connection if use_old_weights == True: conn_stdp_list[i].append( [ii, jj, cl_weights[w], del_init_stdp]) w += 1 else: conn_stdp_list[i].append( [ii, jj, wei_init_stdp, del_init_stdp]) if use_old_weights == False or save == True: np.save('output_files/conn_stdp_list.npy', conn_stdp_list) else: conn_stdp_list = np.load('output_files/conn_stdp_list.npy') ## Output Layer Inhibitory Connection List if randomness == True: # if True: calculate conn_stdp_list (randomly) new # uf False: load previously saved conn_stdp_list conn_output_inh = [[] for i in range(n_cl) for j in range(n_cl) if i != j] c = 0 for i in range(n_cl): for j in range(n_cl): if i != j: for ii in range(n_pop): for jj in range(n_pop): if prob_output_inh > np.random.rand(): conn_output_inh[c].append( [ii, jj, wei_cls_inh, del_cls_inh]) c += 1 if save == True: np.save("output_files/conn_output_inh.npy", conn_output_inh) else: conn_output_inh = np.load("output_files/conn_output_inh.npy") ## Spike Source to Encoding Layer p.Projection(spike_source, enc_layer, p.FromListConnector(conn_inp_enc)) ## Encoding Layer to Filtering Layer p.Projection( enc_layer, filt_layer, p.OneToOneConnector(weights=wei_enc_filt, delays=del_enc_filt)) ## Filtering Layer Inhibitory p.Projection(filt_layer, filt_layer, p.FromListConnector(conn_filt_inh), target="inhibitory") ## STDP Connection between Filtering Layer and Output Layer timing_rule = p.SpikePairRule(tau_plus=tau_pl, tau_minus=tau_min) weight_rule = p.AdditiveWeightDependence(w_max=stdp_w_max, w_min=stdp_w_min, A_plus=stdp_A_pl, A_minus=stdp_A_min) stdp_model = p.STDPMechanism(timing_dependence=timing_rule, weight_dependence=weight_rule) # STDP connection stdp_proj = [] for j in range(n_cl): stdp_proj.append( p.Projection(filt_layer, out_layer_exc[j], p.FromListConnector(conn_stdp_list[j]), synapse_dynamics=p.SynapseDynamics(slow=stdp_model))) ## Connection between Output Layer neurons c = 0 for i in range(n_cl): p.Projection( out_layer_exc[i], out_layer_inh[i], p.OneToOneConnector(weights=wei_cls_exc, delays=del_cls_exc)) iter_array = [j for j in range(n_cl) if j != i] for j in iter_array: p.Projection(out_layer_exc[i], out_layer_exc[j], p.FromListConnector(conn_output_inh[c]), target="inhibitory") c += 1 ## Spike Source Array to Output for i in range(n_cl): p.Projection( out_spike_source[i], out_layer_exc[i], p.AllToAllConnector(weights=wei_source_outp, delays=del_source_outp)) iter_array = [j for j in range(n_cl) if j != i] for j in iter_array: p.Projection(out_spike_source[i], out_layer_exc[j], p.AllToAllConnector(weights=wei_source_outp, delays=del_source_outp), target="inhibitory") #for i in range(n_cl): # p.Projection(out_spike_source[i], out_layer_exc[i], p.AllToAllConnector\ # (weights=wei_source_outp, delays=del_source_outp)) # p.Projection(out_spike_source[i], out_layer_exc[1-i], p.AllToAllConnector\ # (weights=wei_source_outp, delays=del_source_outp),target="inhibitory") ## Noisy poisson connection to encoding layer if randomness == True: # if True: connect noise to network # if False: don't use noise in network p.Projection( poisson_input, enc_layer, p.FixedProbabilityConnector(p_connect=prob_noise_poi_conn, weights=wei_noise_poi, delays=del_noise_poi)) ############################################################################ ## Simulation ############################################################################ p.run(SIM_TIME) Enc_Spikes = enc_layer.getSpikes() Filt_Exc_Spikes = filt_layer.getSpikes() Out_Spikes = [[] for i in range(n_cl)] for i in range(n_cl): Out_Spikes[i] = out_layer_exc[i].getSpikes() wei = [] for i in range(n_cl): ww = stdp_proj[i].getWeights() if save == True: np.save("output_files/stdp_weights{}".format(i), ww) wei.append(ww) p.end() ############################################################################ ## Plot ############################################################################ ## Plot 1: Encoding Layer Raster Plot if 0: pylab.figure() pylab.xlabel('Time (ms)') pylab.ylabel('Neuron ID') pylab.title('Encoding Layer Raster Plot') pylab.hold(True) pylab.plot([i[1] for i in Enc_Spikes], [i[0] for i in Enc_Spikes], ".b") pylab.hold(False) #pylab.axis([-10,c*SIM_TIME+100,-1,numInp+numOut+numInp+3]) pylab.show() ## Plot 2-1: Filtering Layer Raster Plot if 0: pylab.figure() pylab.xlabel('Time (ms)') pylab.ylabel('Neuron ID') pylab.title('Filtering Layer Raster Plot') pylab.plot([i[1] for i in Filt_Exc_Spikes], [i[0] for i in Filt_Exc_Spikes], ".b") #pylab.axis([-10,c*SIM_TIME+100,-1,numInp+numOut+numInp+3]) pylab.show() ## Plot 2-2: Filtering Layer Layer Raster Plot if 0: pylab.figure() pylab.xlabel('Time (ms)') pylab.ylabel('Neuron ID') pylab.title('Filtering Layer Layer Raster Plot') pylab.hold(True) pylab.plot([i[1] for i in Filt_Exc_Spikes], [i[0] for i in Filt_Exc_Spikes], ".b") time_ind = [i * time_int_trials for i in range(len(outputs))] for i in range(len(time_ind)): pylab.plot([time_ind[i], time_ind[i]], [0, 2000], "r") pylab.hold(False) #pylab.axis([-10,c*SIM_TIME+100,-1,numInp+numOut+numInp+3]) pylab.show() ## Plot 3-1: Output Layer Raster Plot if 0: pylab.figure() pylab.xlabel('Time (ms)') pylab.ylabel('Neuron') pylab.title('Output Layer Raster Plot') pylab.hold(True) c = 0 for array in Out_Spikes: pylab.plot([i[1] for i in array], [i[0] + c for i in array], ".b") c += 0.2 pylab.hold(False) pylab.axis([-10, SIM_TIME + 100, -1, n_pop + 3]) pylab.show() ## Plot 4: STDP WEIGHTS if 1: pylab.figure() pylab.xlabel('Weight ID') pylab.ylabel('Weight Value') pylab.title('STDP weights at the end') #pylab.title('STDP weights at the end' + ' (trail_num=' + str(trial_num) + ')') pylab.hold(True) for i in range(n_cl): pylab.plot(wei[i]) pylab.hold(False) pylab.axis([ -10, n_pop * n_feature * n_pop * 0.5 + 10, -stdp_w_max, 2 * stdp_w_max ]) str_legend = ["To Cl {}".format(i + 1) for i in range(n_cl)] pylab.legend(str_legend) #pylab.show() fname = 'plots/weights_1.png' while True: if os.path.isfile(fname): # if file already exists new_num = int(fname.split('.')[0].split('_')[1]) + 1 fname = fname.split('_')[0] + '_' + str(new_num) + '.png' else: pylab.savefig(fname) break #pylab.figure() #pylab.xlabel('Weight ID') #pylab.ylabel('Weight Value') #pylab.title('STDP weights at the end') #pylab.hold(True) #pylab.plot(wei[0], "b") #pylab.plot(wei[1], "g") #pylab.hold(False) #pylab.axis([-10, n_pop * n_feature * n_pop * 0.5 + 10, # -stdp_w_max, 2 * stdp_w_max]) #pylab.legend(['To Cl 1','To Cl 2']) #pylab.show() ## Plot 5: Spike Source Spiking Times if 0: pylab.figure() pylab.hold(True) pylab.plot(out_spike_times[0], [1 for i in range(len(out_spike_times[0]))], "x") pylab.plot(out_spike_times[1], [1.05 for i in range(len(out_spike_times[1]))], "x") pylab.hold(False) pylab.title("Spike Source Spiking Times") pylab.axis([-100, SIM_TIME + 100, -2, 3]) pylab.show() ## Calculate spiking activity of each neuron to each class inputs sum_filt = [[0 for i in range(n_feature * n_pop)] for j in range(n_cl)] sum_filt = np.array(sum_filt) for i in range(n_trials): t_st = i * time_int_trials t_end = t_st + time_int_trials cl = outputs[i] for n, t in Filt_Exc_Spikes: if t >= t_st and t < t_end: sum_filt[int(cl), int(n)] = sum_filt[int(cl), int(n)] + 1 a4 = sum_filt[0] b4 = sum_filt[1] thr = 20 diff_vec = np.abs(a4 - b4) diff_thr = [i if i > thr else 0. for i in diff_vec] diff_ind = [i for i in range(len(diff_thr)) if diff_thr[i] != 0] if save == True: np.save("output_files/diff_ind_filt", diff_ind) diff2 = a4 - b4 diff_thr2 = [i if i > thr or i < -thr else 0. for i in diff2] diff_ind2 = [i for i in range(len(diff_thr2)) if diff_thr2[i] != 0] if save == True: np.save("output_files/diff_ind_filt2", diff_ind2) np.save("output_files/diff_thr2", diff_thr2) ## Plot 6: Total Spiking Activity of Neurons at Decomposition Layer for Each Class if 0: a4 = sum_filt[0] b4 = sum_filt[1] pylab.figure() pylab.hold(True) pylab.plot(a4, "b") pylab.plot(b4, "r") pylab.xlabel('Neuron ID') pylab.ylabel('Total Firing Rates Through Trials') pylab.title( "Total Spiking Activity of Neurons at Decomposition Layer for Each Class" ) pylab.hold(False) pylab.legend(["Activity to AN1", "Activity to AN2"]) pylab.show()
AbstractOutgoingEdgeSameContiguousKeysRestrictor.__init__(self) def get_outgoing_edge_constraints(self, partitioned_edge, graph_mapper): constraints = AbstractOutgoingEdgeSameContiguousKeysRestrictor\ .get_outgoing_edge_constraints( self, partitioned_edge, graph_mapper) constraints.append(KeyAllocatorFixedKeyAndMaskConstraint( [KeyAndMask(0x42000000, 0xFFFF0000)])) return constraints def is_virtual_vertex(self): return True def model_name(self): return "My External Device" import pyNN.spiNNaker as p from pacman.model.partitionable_graph.multi_cast_partitionable_edge \ import MultiCastPartitionableEdge p.setup(1.0) device = p.Population(20, MyExternalDevice, {"spinnaker_link_id": 0}, label="external device") pop = p.Population(20, p.IF_curr_exp, {}, label="population") p.Projection(device, pop, p.OneToOneConnector()) p.run(10)
for trial, j in enumerate(test_data): if firstrun: firstrun = False else: pynn.reset() try: for i in range(len(network)-1): network[i+1].initialize(v=0.0) x_flat = np.ravel(j) rates = 1000 * x_flat / rescale_fac network[0].set(rate=rates) #run simulation pynn.run(sim_time) #get spikes shape = (5, int(sim_time/dt)) spiketrains = network[-1].get_data().segments[-1].spiketrains spiketrains_flat = np.zeros(shape) for k, spiketrain in enumerate(spiketrains): for t in spiketrain: spiketrains_flat[k, int(t / dt)] = 1 spikesum = np.sum(spiketrains_flat, axis = 1) pred_labels.append(np.eye(5)[np.argmax(spikesum)]) print(spikesum) print('estimate = ' + str(np.argmax(spikesum)))
import pyNN.spiNNaker as p import spynnaker_external_devices_plugin.pyNN as q from spinnman.messages.eieio.eieio_type import EIEIOType p.setup(1.0) #pop = p.Population(4, p.SpikeSourceArray, {"spike_times": [[0], [1000], [2000], [3000]]}) pop = p.Population(4, p.SpikeSourcePoisson, {"rate": 5}) q.activate_live_output_for(pop, port=18000, message_type=EIEIOType.KEY_16_BIT, payload_as_time_stamps=False, use_payload_prefix=False) p.run(5000)
#~ return count #~ # raise NotImplementedError print("\n\n") total_connections = 0 for k in sorted(connections.keys()): num_conn = len(connections[k]) print( "%s\t\tconnections: %s"%(k, num_conn) ) total_connections += num_conn print("-------------------------------") print("Total connections: %s"%(total_connections)) print("\n\n") time.sleep(1) ############ start sim sim.run(sim_runtime) ############ get data out any_spikes_recorded = True sim_spikes = {} try: #~ sim_spikes['in'] = {} #~ for l in input_layer: #~ sim_spikes['in'][l] = input_layer[l].getSpikes(compatible_output=True) sim_spikes['id'] = id_layer.getSpikes(compatible_output=True) sim_spikes['exc'] = learn_layer['exc'].getSpikes(compatible_output=True) sim_spikes['inh'] = learn_layer['inh'].getSpikes(compatible_output=True)
hostname="192.168.240.254" #ipaddress of packets to be sent to strip_sdp=True #p.activate_live_output_for(populations[0], portNo, hostname, tag, strip_sdp, use_prefix, right_shift, # payload_as_time_stamps, use_payload_prefix, # payload_prefix, payload_right_shift, # number_of_packets_sent_per_time_step) p.activate_live_output_for(populations[0], portNo, hostname,tag=1, strip_sdp=strip_sdp) #populations[0].set_constraint( p.PartitionerMaximumSizeConstraint(5)) # 4 chips addressed (0,0) (0,1) (1,0) (1,1) so (2,2) out of range #populations[0].set_mapping_constraint({'x':2, 'y':2, 'p':0}) #However (1,1) should work, but doesn't? #populations[0].set_mapping_constraint({'x':1, 'y':1, 'p':0}) #same problem as above #populations[0].set_constraint( p.PlacerChipAndCoreConstraint(2,2,0)) #populations[0].set_constraint( p.PlacerChipAndCoreConstraint(0,0,0)) p.run(400) # p.end()
#!/usr/bin/env python import IPython import pyNN.spiNNaker as p from pylab import * p.setup(timestep=1.0, min_delay=1.0, max_delay=10.0) pois1 = p.Population(100, p.SpikeSourceRemote, { 'max_rate': 50, 'overlap': 0.2 }) pois1.record() p.run(1000.) spk = pois1.getSpikes() figure() plot(spk[:, 0], spk[:, 1], "s") figure() hist(spk[:, 1]) #IPython.embed() show()
grcpc_weights_distribution = p.RandomDistribution("uniform", [0.05, 0.5], rng) pro_grcpcsynapsis_connector = p.FixedProbabilityConnector(0.8, weights=grcpc_weights_distribution) pro_grcpcsynapsis_left = p.Projection( prepop, postpop, pro_grcpcsynapsis_connector, target="excitatory", synapse_dynamics=syndyn_grcpcsynapsis, label="grcpcsynapsis", ) proj = pro_grcpcsynapsis_left proj.projection_edge.postvertex.custom_max_atoms_per_core = max(1, 4000 / proj.projection_edge.prevertex.atoms) # plasticsyn.projection_edge.postvertex.custom_max_atoms_per_core = 100 p.run(duration) # IPython.embed() prespikes = prepop.getSpikes() if prespikes != None: plot(prespikes[:, 0], prespikes[:, 1], "gd", markersize=10, alpha=0.6) teachspikes = teachpop.getSpikes() if teachspikes != None: plot(teachspikes[:, 0], teachspikes[:, 1], "ro", markersize=10, alpha=0.6) postspikes = postpop.getSpikes() if postspikes != None: plot(postspikes[:, 0], postspikes[:, 1], "bs", markersize=10, alpha=0.6)
for i in range(8): for x in range(8): weighted_connections_2ndLayer.append( (x, i, float(wList_2ndLayer[x + cont]), delay)) cont += 8 lif_1_to_lif_2_proj = p.Projection( lif_output, second_lif_layer, p.FromListConnector(weighted_connections_2ndLayer), target="excitatory") lif_output.record() second_lif_layer.record() p.run(endTime * 2) spikes_output_def = lif_output.getSpikes() spikes_output_def_2nd = second_lif_layer.getSpikes() weights = lif_to_lif_output_proj.getWeights() neuronFirings = [0 for i in range(8)] neuronTotal = 0 neuronFirings_2nd = [0 for i in range(8)] neuronTotal_2nd = 0 for x in spikes_output_def: neuronTotal += 1 if x[0] == 0: neuronFirings[0] += 1 elif x[0] == 1:
# stimulate the first population with a single spike (3 times) spike_times = [0, 1000, 2000] in_0 = pynn.Population(1, pynn.SpikeSourceArray, {'spike_times': spike_times}) pynn.Projection(in_0, all_pops[0], con_alltoall, target="excitatory") def shift(seq, n): n = n % len(seq) return seq[n:] + seq[:n] # connect all populations, but don't close the chain for pop_a, pop_b in zip(all_pops, shift(all_pops, 1)[:-1]): pynn.Projection(pop_a, pop_b, con_fixednumberpre, target='excitatory') pynn.run(duration) spikes = None # Collect and record spikes for pop in all_pops: new_spikes = pop.getSpikes(compatible_output=True) if new_spikes is not None: numpy.fliplr(new_spikes) new_spikes = new_spikes / [1, 1000.0] if spikes is None: spikes = new_spikes else: new_spikes = new_spikes + [len(spikes), 0] spikes = numpy.concatenate((spikes, new_spikes), axis=0) if spikes is None:
import pyNN.spiNNaker as p from pylab import * p.setup(timestep=1.0,min_delay=1.0,max_delay=1.0) cell_params = { 'i_offset' : .1, 'tau_refrac' : 3.0, 'v_rest' : -65.0, 'v_thresh' : -51.0, 'tau_syn_E' : 2.0, 'tau_syn_I': 5.0, 'v_reset' : -70.0, 'e_rev_E' : 0., 'e_rev_I' : -80.} # setup test population if_pop = p.Population(1,p.IF_cond_exp,cell_params) # setup spike sources exc_pop = p.Population(1,p.SpikeSourceArray,{'spike_times':[20.,40.,60.]}) inh_pop = p.Population(1,p.SpikeSourceArray,{'spike_times':[120.,140.,160.]}) # setup excitatory and inhibitory connections listcon = p.FromListConnector([(0,0,0.01,1.0)]) exc_pro = p.Projection(exc_pop,if_pop,listcon,target='excitatory') inh_pro = p.Projection(inh_pop,if_pop,listcon,target='inhibitory') # setup recorder if_pop.record_v() p.run(200.) #read out voltage and plot V = if_pop.get_v() plot(V[:,1],V[:,2],'.',label=p.__name__) p.end() legend() show()
poispops=[] for k,(mini,maxi) in [(0xFEFFFE20,(1220.,2880.)), (0xFEFFFE03,(-20.,500.)), (0xFEFFFE07,(-20.,500.)), (0xFEFFFE02,(0.,1000.)), (0xFEFFFE06,(0.,1000.)), (0xFEFFFE00,(-50.,50.)), (0xFEFFFE04,(-50.,50.))]: poi = p.Population(32,p.SpikeSourceRemote,{'max_rate':100,'min_rate':.1,'overlap':10./(maxi-mini),'sensormin':mini,'sensormax':maxi, 'src_type': 'rbf_pois', 'listen_key': k}) poi.record() poispops.append(poi) #errorprop=p.Projection(myopop,pois1,p.OneToOneConnector(weights=1.0,delays=1.0)) p.run(15000) figure() for i,pois1 in enumerate(poispops): spk=pois1.getSpikes() # subplot(len(poispops),1,i) plot(spk[:,0],spk[:,1],"s",alpha=0.4) #figure() #hist(spk[:,1],bins=32) IPython.embed() p.end()
A_minus=0.5), #weight_dependence=sim.MultiplicativeWeightDependence(w_min=wMin, w_max=wMax), weight_dependence=sim.AdditiveWeightDependence(w_min=0, w_max=5), #weight_dependence=sim.AdditiveWeightDependence(w_min=0, w_max=0.4), dendritic_delay_fraction=1.0) stdp_proj = sim.Projection(layer1, post, sim.AllToAllConnector(), synapse_type=stdp) layer1.record(['spikes']) layer2.record(['spikes']) post.record(['v', 'spikes']) sim.run(runTime) weight_list = [ stdp_proj.get('weight', 'list'), stdp_proj.get('weight', format='list', with_address=False) ] neo = post.get_data(["spikes", "v"]) spikes = neo.segments[0].spiketrains v = neo.segments[0].filter(name='v')[0] neoinput = layer1.get_data(["spikes"]) spikesinput = neoinput.segments[0].spiketrains neooutput = layer2.get_data(["spikes"]) spikesoutput = neooutput.segments[0].spiketrains plt.close('all') pplt.Figure(
#~ print("-----------------------------------------------------------------") #~ print("-----------------------------------------------------------------") #~ print("Inh to Exc Weights") #~ print("-----------------------------------------------------------------") #~ print(i2e_proj.getWeights()) print("-----------------------------------------------------------------") print("-----------------------------------------------------------------") print("Stim to Inh Weights") print("-----------------------------------------------------------------") print(s2i_proj.getWeights()) exc_pop.record() inh_pop.record() sim.run(runtime) exc_spikes_found = True try: exc_spikes = exc_pop.getSpikes(compatible_output=True) except IndexError: print("No spikes?") exc_spikes_found = False inh_spikes_found = True try: inh_spikes = inh_pop.getSpikes(compatible_output=True) except IndexError: print("No spikes?") inh_spikes_found = False
import pyNN.spiNNaker as p import spynnaker_external_devices_plugin.pyNN as q import pylab p.setup(1.0) injector = p.Population(10, q.SpikeInjector, { "virtual_key": 0x4200, "port": 18000 }, label="injector") pop = p.Population(10, p.IF_curr_exp, {}, label="pop") pop.record() p.Projection(injector, pop, p.OneToOneConnector(weights=5.0)) p.run(7000) spikes = pop.getSpikes() spike_time = [i[1] for i in spikes] spike_id = [i[0] for i in spikes] pylab.plot(spike_time, spike_id, ".") pylab.xlabel("Time (ms)") pylab.ylabel("Neuron ID") pylab.axis([0, 7000, -1, 10]) pylab.show()
def test(spikeTimes, trained_weights, label): #spikeTimes = extractSpikes(sample) runTime = int(max(max(spikeTimes))) + 100 ########################################## sim.setup(timestep=1) pre_pop = sim.Population(input_size, sim.SpikeSourceArray, {'spike_times': spikeTimes}, label="pre_pop") post_pop = sim.Population(output_size, sim.IF_curr_exp, cell_params_lif, label="post_pop") if len(trained_weights) > input_size: weigths = [[0 for j in range(output_size)] for i in range(input_size)] #np array? size 1024x25 k = 0 for i in range(input_size): for j in range(output_size): weigths[i][j] = trained_weights[k] k += 1 else: weigths = trained_weights connections = [] #k = 0 for n_pre in range(input_size): # len(untrained_weights) = input_size for n_post in range( output_size ): # len(untrained_weight[0]) = output_size; 0 or any n_pre #connections.append((n_pre, n_post, weigths[n_pre][n_post]*(wMax), __delay__)) connections.append((n_pre, n_post, weigths[n_pre][n_post] * (wMax) / max(trained_weights), __delay__)) # #k += 1 prepost_proj = sim.Projection( pre_pop, post_pop, sim.FromListConnector(connections), synapse_type=sim.StaticSynapse(), receptor_type='excitatory') # no more learning !! #inhib_proj = sim.Projection(post_pop, post_pop, sim.AllToAllConnector(), synapse_type=sim.StaticSynapse(weight=inhibWeight, delay=__delay__), receptor_type='inhibitory') # no more lateral inhib post_pop.record(['v', 'spikes']) sim.run(runTime) neo = post_pop.get_data(['v', 'spikes']) spikes = neo.segments[0].spiketrains v = neo.segments[0].filter(name='v')[0] f1 = pplt.Figure( # plot voltage pplt.Panel(v, ylabel="Membrane potential (mV)", xticks=True, yticks=True, xlim=(0, runTime + 100)), # raster plot pplt.Panel(spikes, xlabel="Time (ms)", xticks=True, yticks=True, markersize=2, xlim=(0, runTime + 100)), title='Test with label ' + str(label), annotations='Test with label ' + str(label)) f1.save('plot1/' + str(trylabel) + str(label) + '_test.png') f1.fig.texts = [] print("Weights:{}".format(prepost_proj.get('weight', 'list'))) weight_list = [ prepost_proj.get('weight', 'list'), prepost_proj.get('weight', format='list', with_address=False) ] #predict_label= sim.end() return spikes
weighted_connections_2ndLayer = [] cont = 0 for i in range (8): for x in range(8): weighted_connections_2ndLayer.append((x, i, float(wList_2ndLayer[x + cont]), delay)) cont += 8 lif_1_to_lif_2_proj = p.Projection(lif_output, second_lif_layer, p.FromListConnector(weighted_connections_2ndLayer), target="excitatory") lif_output.record() second_lif_layer.record() p.run(endTime*2) spikes_output_def = lif_output.getSpikes() spikes_output_def_2nd = second_lif_layer.getSpikes() weights = lif_to_lif_output_proj.getWeights() neuronFirings = [0 for i in range(8)] neuronTotal = 0 neuronFirings_2nd = [0 for i in range(8)] neuronTotal_2nd = 0 for x in spikes_output_def: neuronTotal += 1 if x[0] == 0: neuronFirings[0] += 1
def train(label, untrained_weights=None): organisedStim = {} labelSpikes = [] spikeTimes = generate_data(label) for i in range(output_size): labelSpikes.append([]) labelSpikes[label] = [(input_len - 1) * v_co + 1, (input_len - 1) * v_co * 2 + 1, (input_len - 1) * v_co * 3 + 1] if untrained_weights == None: untrained_weights = RandomDistribution('uniform', low=wMin, high=wMaxInit).next(input_size * output_size) #untrained_weights = RandomDistribution('normal_clipped', mu=0.1, sigma=0.05, low=wMin, high=wMaxInit).next(input_size*output_size) untrained_weights = np.around(untrained_weights, 3) #saveWeights(untrained_weights, 'untrained_weightssupmodel1traj') print("init!") print "length untrained_weights :", len(untrained_weights) if len(untrained_weights) > input_size: training_weights = [[0 for j in range(output_size)] for i in range(input_size) ] #np array? size 1024x25 k = 0 for i in range(input_size): for j in range(output_size): training_weights[i][j] = untrained_weights[k] k += 1 else: training_weights = untrained_weights connections = [] for n_pre in range(input_size): # len(untrained_weights) = input_size for n_post in range( output_size ): # len(untrained_weight[0]) = output_size; 0 or any n_pre connections.append((n_pre, n_post, training_weights[n_pre][n_post], __delay__)) #index runTime = int(max(max(spikeTimes))) + 100 ##################### sim.setup(timestep=1) #def populations layer1 = sim.Population(input_size, sim.SpikeSourceArray, {'spike_times': spikeTimes}, label='inputspikes') layer2 = sim.Population(output_size, sim.IF_curr_exp, cellparams=cell_params_lif, label='outputspikes') supsignal = sim.Population(output_size, sim.SpikeSourceArray, {'spike_times': labelSpikes}, label='supersignal') #def learning rule stdp = sim.STDPMechanism( weight=untrained_weights, #weight=0.02, # this is the initial value of the weight #delay="0.2 + 0.01*d", timing_dependence=sim.SpikePairRule(tau_plus=tauPlus, tau_minus=tauMinus, A_plus=aPlus, A_minus=aMinus), #weight_dependence=sim.MultiplicativeWeightDependence(w_min=wMin, w_max=wMax), weight_dependence=sim.AdditiveWeightDependence(w_min=wMin, w_max=wMax), #weight_dependence=sim.AdditiveWeightDependence(w_min=0, w_max=0.4), dendritic_delay_fraction=1.0) #def projections #stdp_proj = sim.Projection(layer1, layer2, sim.FromListConnector(connections), synapse_type=stdp) stdp_proj = sim.Projection(layer1, layer2, sim.AllToAllConnector(), synapse_type=stdp) inhibitory_connections = sim.Projection( layer2, layer2, sim.AllToAllConnector(allow_self_connections=False), synapse_type=sim.StaticSynapse(weight=inhibWeight, delay=__delay__), receptor_type='inhibitory') stim_proj = sim.Projection(supsignal, layer2, sim.OneToOneConnector(), synapse_type=sim.StaticSynapse( weight=stimWeight, delay=__delay__)) layer1.record(['spikes']) layer2.record(['v', 'spikes']) supsignal.record(['spikes']) sim.run(runTime) print("Weights:{}".format(stdp_proj.get('weight', 'list'))) weight_list = [ stdp_proj.get('weight', 'list'), stdp_proj.get('weight', format='list', with_address=False) ] neo = layer2.get_data(["spikes", "v"]) spikes = neo.segments[0].spiketrains v = neo.segments[0].filter(name='v')[0] neostim = supsignal.get_data(["spikes"]) print(label) spikestim = neostim.segments[0].spiketrains neoinput = layer1.get_data(["spikes"]) spikesinput = neoinput.segments[0].spiketrains plt.close('all') pplt.Figure(pplt.Panel(v, ylabel="Membrane potential (mV)", xticks=True, yticks=True, xlim=(0, runTime)), pplt.Panel(spikesinput, xticks=True, yticks=True, markersize=2, xlim=(0, runTime)), pplt.Panel(spikestim, xticks=True, yticks=True, markersize=2, xlim=(0, runTime)), pplt.Panel(spikes, xticks=True, xlabel="Time (ms)", yticks=True, markersize=2, xlim=(0, runTime)), title="Training" + str(label), annotations="Training" + str(label)).save('plot1/' + str(trylabel) + str(label) + '_training.png') #plt.hist(weight_list[1], bins=100) plt.close('all') plt.hist([ weight_list[1][0:input_size], weight_list[1][input_size:input_size * 2], weight_list[1][input_size * 2:] ], bins=20, label=['neuron 0', 'neuron 1', 'neuron 2'], range=(0, wMax)) plt.title('weight distribution') plt.xlabel('Weight value') plt.ylabel('Weight count') #plt.show() #plt.show() sim.end() return weight_list[1]
else: plt.ylabel("Voltage") plt.show(block=False) while running: if last_spike_fast < total_run_time + time_to_run: fast_spikes = list(islice(fast_spike_iter, 10)) last_spike_fast = fast_spikes[-1] fast_injector.set("spike_times", [fast_spikes] + [[]] * 9) if last_spike_slow < total_run_time + time_to_run: slow_spikes = list(islice(slow_spike_iter, 10)) last_spike_slow = slow_spikes[-1] slow_injector.set("spike_times", [slow_spikes] + [[]] * 9) sim.run(time_to_run) total_run_time += time_to_run plt.xlim(max(0, total_run_time - 5*time_to_run), total_run_time) if mode == "spikes": plt.ylim(-1, 101) all_spikes = populations[-1].getSpikes() print "Total spikes %d" % len(all_spikes) spikes = list(takewhile(lambda x: x[1] > total_run_time - time_to_run, all_spikes)) plt.plot([i[1] for i in spikes], [i[0] for i in spikes], ".", markersize=2) else: plt.ylim(v_reset - 5, v_thresh + 5) voltages = list(ifilter(lambda x: x[0] == 1 and x[1] >= total_run_time - time_to_run, reversed(populations[0].get_v()))) plt.plot([i[1] for i in voltages], [i[2] for i in voltages], "b-", markersize=1)
label="plastic_projection") # +-------------------------------------------------------------------+ # | Simulation and results | # +-------------------------------------------------------------------+ # Record neurons' potentials pre_pop.record_v() post_pop.record_v() # Record spikes pre_pop.record() post_pop.record() # Run simulation sim.run(simtime) print("Weights:", plastic_projection.getWeights()) def plot_spikes(spikes, title): if spikes is not None: pylab.figure() pylab.xlim((0, simtime)) pylab.plot([i[1] for i in spikes], [i[0] for i in spikes], ".") pylab.xlabel('Time/ms') pylab.ylabel('spikes') pylab.title(title) else: print "No spikes received"
# (0xFEFFFE00,(-800.,800.)), # omega = delta PWM, not useful! # (0xFEFFFE04,(-800.,800.)), # could go much higher (~PWM=800/4000) # (0xFEFFFE01,((-2<<18) + 1, (2<<18) - 1)), # spindle encoders # (0xFEFFFE05,((-2<<18) + 1, (2<<18) - 1))]: # these will be auto-downscaled poi = p.Population(32,p.SpikeSourceRemote,{'max_rate':50,'min_rate':.1,'gauss_width':0.666,'sensormin':mini,'sensormax':maxi, 'src_type': 'rbf_det', 'listen_key': k}, label=hex(k)+"_PLOT") poi.record() poispops.append(poi) iosource = p.Population(32,p.SpikeSourceRemote,{'max_rate':10,'min_rate':.1,'sensormin':1220,'sensormax':2880, 'src_type': 'glob_pois', 'listen_key': 0xFEFFFE30}, label=hex(k)+"_PLOT") iosource.record() #pois1 = p.Population(100,p.SpikeSourceRemote,{'max_rate':100,'overlap':0.2}) #pois1.record() #errorprop=p.Projection(myopop,pois1,p.OneToOneConnector(weights=1.0,delays=1.0)) p.run(rt) #myospikes=myopop.getSpikes() #inpspikes=inppop.getSpikes() #testspikes=testpop.getSpikes() #plot(inpspikes[:,0],inpspikes[:,1],"s") #plot(testspikes[:,0],testspikes[:,1],"s") figure() for i,pois1 in enumerate(poispops): spk=pois1.getSpikes() # subplot(len(poispops),1,i) plot(spk[:,0],spk[:,1],"s",alpha=0.4,label=pois1.vertex.label)
def pause_stop_commands(self): return [MultiCastCommand(0x1), MultiCastCommand(0x2, 0x0)] @property def timed_commands(self): return [] class MySpiNNakerLinkDeviceDataHolder(DataHolder): def __init__(self, spinnaker_link_id, label=None): DataHolder.__init__(self, { "spinnaker_link_id": spinnaker_link_id, "label": label }) @staticmethod def build_model(): return MySpiNNakerLinkDevice p.setup(1.0) pop = p.Population(1, p.IF_curr_exp()) device = p.Population(1, MySpiNNakerLinkDeviceDataHolder(spinnaker_link_id=0)) p.Projection(device, pop, p.OneToOneConnector(), p.StaticSynapse(weight=1.0)) p.run(100) p.end()
##PROJECTING IP_POP ONTO OP_POP WITH STDP DYNAMIC SYNAPSES project_ip_op = p.Projection( ip_pop,op_pop, p.AllToAllConnector(weights=weights, delays=1), synapse_dynamics = s_d, target="excitatory") ########################$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$END OF STDPMechanism #################################################################### ################################################## #################################### ################ ##############################################$$$$$$$$$$$$$$$$$$$$PLOTTING AND RECORDING ####################################################### stimlus_pop.record() ip_pop.record() op_pop.record() op_pop.record_v() p.run((pattern_gap+k)*n) c=stimlus_pop.getSpikes() v=ip_pop.getSpikes() l=op_pop.getSpikes() vo=op_pop.get_v() print "Output Population voltage", vo print "Output Population voltage", vo[1] spike_id = [i[0] for i in v] spike_time = [i[1] for i in v] #***pylab.subplot(3,1,1) pylab.plot(spike_time, spike_id, ".") pylab.xlabel("Time(ms)")
import pyNN.spiNNaker as sim sim.setup() p1 = sim.Population(3, sim.SpikeSourceArray, {"spike_times": [1.0, 2.0, 3.0]}) p2 = sim.Population(3, sim.SpikeSourceArray, {"spike_times": [[10.0], [20.0], [30.0]]}) p3 = sim.Population(4, sim.IF_cond_exp, {}) sim.Projection(p2, p3, sim.FromListConnector([ (0, 0, 0.1, 1.0), (1, 1, 0.1, 1.0), (2, 2, 0.1, 1.0)])) #sim.Projection(p1, p3, sim.FromListConnector([(0, 3, 0.1, 1.0)])) # works if this line is added sim.run(100.0)
what_statement_gate.record() who_query_gate.record() who_query_gate.record_v() who_query_gate.record_gsyn() where_query_gate.record() what_query_gate.record() dst_who.record() dst_where.record() dst_what.record() who.record() where.record() what.record() p.run(10 * 1000) # src_pop_spikes = src_pop.getSpikes(compatible_output=True) # who_statement_gate_spikes = who_statement_gate.getSpikes(compatible_output=True) # who_statement_gate_v = who_statement_gate.get_v() # who_statement_gate_i = who_statement_gate.get_gsyn() # where_statement_gate_spikes = where_statement_gate.getSpikes(compatible_output=True) # what_statement_gate_spikes = what_statement_gate.getSpikes(compatible_output=True) # who_query_gate_spikes = who_query_gate.getSpikes(compatible_output=True) # who_query_gate_v = who_query_gate.get_v() # who_query_gate_i = who_query_gate.get_gsyn() # where_query_gate_spikes = where_query_gate.getSpikes(compatible_output=True) # what_query_gate_spikes = what_query_gate.getSpikes(compatible_output=True) # dst_who_spikes = dst_who.getSpikes(compatible_output=True) # dst_where_spikes = dst_where.getSpikes(compatible_output=True) # dst_what_spikes = dst_what.getSpikes(compatible_output=True)
label='pop_1')) populations.append(p.Population(1, p.SpikeSourceArray, spikeArray, label='inputSpikes_1')) projections.append(p.Projection(populations[0], populations[0], p.FromListConnector(loopConnections))) projections.append(p.Projection(populations[1], populations[0], p.FromListConnector(injectionConnection))) populations[0].record_v() populations[0].record_gsyn() populations[0].record() run_time = (max_delay * nNeurons) print "Running for {} ms".format(run_time) p.run(run_time) v = None gsyn = None spikes = None v = populations[0].get_v(compatible_output=True) gsyn = populations[0].get_gsyn(compatible_output=True) spikes = populations[0].getSpikes(compatible_output=True) if spikes is not None: print spikes pylab.figure() pylab.plot([i[1] for i in spikes], [i[0] for i in spikes], ".") pylab.xlabel('Time/ms') pylab.ylabel('spikes')
(0xFEFFFE00, (-50., 50.)), (0xFEFFFE04, (-50., 50.))]: poi = p.Population( 32, p.SpikeSourceRemote, { 'max_rate': 100, 'min_rate': .1, 'overlap': 10. / (maxi - mini), 'sensormin': mini, 'sensormax': maxi, 'src_type': 'rbf_pois', 'listen_key': k }) poi.record() poispops.append(poi) #errorprop=p.Projection(myopop,pois1,p.OneToOneConnector(weights=1.0,delays=1.0)) p.run(15000) figure() for i, pois1 in enumerate(poispops): spk = pois1.getSpikes() # subplot(len(poispops),1,i) plot(spk[:, 0], spk[:, 1], "s", alpha=0.4) #figure() #hist(spk[:,1],bins=32) IPython.embed() p.end()
spikeArray = {'spike_times': [[0]]} populations.append(p.Population(nNeurons, p.IF_curr_exp, cell_params_lif, label='pop_1')) populations.append(p.Population(1, p.SpikeSourceArray, spikeArray, label='inputSpikes_1')) projections.append(p.Projection(populations[0], populations[0], p.FromListConnector(loopConnections))) projections.append(p.Projection(populations[1], populations[0], p.FromListConnector(injectionConnection))) populations[0].record_v() populations[0].record_gsyn() populations[0].record() p.run(runtime) v = populations[0].get_v(compatible_output=True) gsyn = populations[0].get_gsyn(compatible_output=True) spikes = populations[0].getSpikes(compatible_output=True) if spikes is not None: print spikes pylab.figure() pylab.plot([i[1] for i in spikes], [i[0] for i in spikes], ".") pylab.xlabel('Time/ms') pylab.ylabel('spikes') pylab.title('spikes') pylab.show() else:
import pyNN.spiNNaker as sim # import pyNN.utility.plotting as plot # import matplotlib.pyplot as plt sim.setup(timestep=1.0) pop_1 = sim.Population(1, sim.IF_curr_exp(), label="pop_1") pop_1.record(["spikes", "v"]) sim.run(10)
def run(simTime): print "Model run starting at sim time ", spynnaker.get_current_time() spynnaker.run(simTime) print "Model run ended at sim time ", spynnaker.get_current_time()
inputToPlot[i][ j] += 20 # Track segmentation signal locations # Set a firing positive firing rate for concerned units of the segmentation top-down signal if len(surfaceOffTarget) > 0: # SurfaceSegmentationOff[surfaceOffTarget].inject(sim.DCSource(amplitude=1.0, start=segmentationSignalStart, stop=simTime)) SurfaceSegmentationOff[surfaceOffTarget].set('i_offset', 2.0) if len(surfaceOnTarget) > 0: # SurfaceSegmentationOn [surfaceOnTarget] .inject(sim.DCSource(amplitude=1.0, start=segmentationSignalStart, stop=simTime)) SurfaceSegmentationOn[surfaceOnTarget].set('i_offset', 2.0) if len(boundaryOnTarget) > 0: # BoundarySegmentationOn[boundaryOnTarget].inject(sim.DCSource(amplitude=1.0, start=segmentationSignalStart, stop=simTime)) BoundarySegmentationOn[boundaryOnTarget].set('i_offset', 2.0) # Actual run of the network, using the input and the segmentation signals sim.run(stepDuration) # To store results for later plotting plotDensityLGNBright = [[0 for j in range(ImageNumPixelColumns)] for i in range(ImageNumPixelRows)] plotDensityLGNDark = [[0 for j in range(ImageNumPixelColumns)] for i in range(ImageNumPixelRows)] plotDensityOrientationV1 = [[[0 for j in range(numPixelColumns)] for i in range(numPixelRows)] for k in range(numOrientations)] plotDensityOrientationV2 = [[[[0 for j in range(numPixelColumns)] for i in range(numPixelRows)] for h in range(numSegmentationLayers)] for k in range(numOrientations)] plotDensityBrightnessV4 = [[[0 for j in range(ImageNumPixelColumns)] for i in range(ImageNumPixelRows)]