def build_basic_network(input_stimuli_rates, input_stimuli_duration, class_stimuli_rates, class_stimuli_duration, record, ioffset, train, sim): # Create main input and class populations input_populations = [create_input_population(CLASS_POP_SIZE, record, sim) for _ in input_stimuli_rates] if isinstance(ioffset, list) or isinstance(ioffset, np.ndarray): assert len(ioffset) == len(class_stimuli_rates) class_populations = [create_class_population(CLASS_POP_SIZE, record, o, train, sim) for i, o in enumerate(ioffset)] else: print ioffset class_populations = [create_class_population(CLASS_POP_SIZE, record, ioffset, train, sim) for _ in class_stimuli_rates] # Create pre-synaptic stimuli populations pre_stimuli_connector = sim.OneToOneConnector() pre_stimuli_synapse = sim.StaticSynapse(weight=PRE_STIMULI_WEIGHT) for i, (rate, input_pop) in enumerate(zip(input_stimuli_rates, input_populations)): # Convert stimuli into spike times spike_times = generate_stimuli_spike_times(rate, input_stimuli_duration, CLASS_POP_SIZE) # Build spike source array with these times stim_pop = sim.Population(CLASS_POP_SIZE, sim.SpikeSourceArray(spike_times=spike_times), label="pre_stimuli_%u" % i) # Connect spike source to input sim.Projection(stim_pop, input_pop, pre_stimuli_connector, pre_stimuli_synapse, receptor_type="excitatory", label="%s-%s" % (stim_pop.label, input_pop.label)) # Create training spike source array populations post_stimuli_connector = sim.OneToOneConnector() post_stimuli_synapse = sim.StaticSynapse(weight=POST_STIMULI_WEIGHT) for i, (rate, class_pop) in enumerate(zip(class_stimuli_rates, class_populations)): # Convert stimuli into spike times spike_times = generate_stimuli_spike_times(rate, class_stimuli_duration, CLASS_POP_SIZE) # Build spike source array with these times stim_pop = sim.Population(CLASS_POP_SIZE, sim.SpikeSourceArray, {"spike_times": spike_times}, label="post_stimuli_%u" % i) # Connect spike source to input sim.Projection(stim_pop, class_pop, post_stimuli_connector, post_stimuli_synapse, receptor_type="excitatory", label="%s-%s" % (stim_pop.label, class_pop.label)) # Return created populations return input_populations, class_populations
def train(sepal_length, sepal_length_unit_mean_sd, sepal_width, sepal_width_unit_mean_sd, petal_length, petal_length_unit_mean_sd, petal_width, petal_width_unit_mean_sd, unique_species, species): # SpiNNaker setup sim.setup(timestep=1.0, min_delay=1.0, max_delay=10.0, spinnaker_hostname="192.168.1.1") # Calculate input rates input_rates = [] calculate_stim_rates(sepal_length, sepal_length_unit_mean_sd, input_rates, MAX_FREQUENCY) calculate_stim_rates(sepal_width, sepal_width_unit_mean_sd, input_rates, MAX_FREQUENCY) calculate_stim_rates(petal_length, petal_length_unit_mean_sd, input_rates, MAX_FREQUENCY) calculate_stim_rates(petal_width, petal_width_unit_mean_sd, input_rates, MAX_FREQUENCY) # Calculate class rates class_rates = [] for u, _ in enumerate(unique_species): class_rates.append(list((species == u) * MAX_FREQUENCY)) # Build basic network with orthogonal stimulation of both populations input_populations, class_populations = build_basic_network(input_rates, STIMULUS_TIME, class_rates, STIMULUS_TIME, False, 0.0, True, sim) # Create BCPNN model with weights disabled bcpnn_synapse = bcpnn.BCPNNSynapse( tau_zi=BCPNN_TAU_PRIMARY, tau_zj=BCPNN_TAU_PRIMARY, tau_p=BCPNN_TAU_ELIGIBILITY, f_max=MAX_FREQUENCY, w_max=BCPNN_MAX_WEIGHT, weights_enabled=False, plasticity_enabled=True, weight=0.0) # Create all-to-all connector to connect inputs to classes input_class_connector = sim.AllToAllConnector() # Loop through all pairs of input populations and classes plastic_connections = [] for (i, c) in itertools.product(input_populations, class_populations): # Connect input to class with all-to-all plastic synapse connection = sim.Projection(i, c, input_class_connector, bcpnn_synapse, receptor_type="excitatory", label="%s-%s" % (i.label, c.label)) plastic_connections.append(connection) # Run simulation sim.run(STIMULUS_TIME * len(sepal_length)) # Read biases # **HACK** investigate where out by 1000 comes from! learnt_biases = [c.get_data().segments[0].filter(name="bias")[0][-1,:] * 0.001 for c in class_populations] # Read plastic weights learnt_weights = [p.get("weight", format="array") for p in plastic_connections] return learnt_biases, learnt_weights
def test(learnt_weights, learnt_biases): # SpiNNaker setup sim.setup(timestep=1.0, min_delay=1.0, max_delay=10.0, spinnaker_hostname="192.168.1.1") # Generate testing stimuli patters testing_stimuli_rates = [ [MAX_FREQUENCY, MIN_FREQUENCY, MAX_FREQUENCY, MIN_FREQUENCY], [MIN_FREQUENCY, MAX_FREQUENCY, MAX_FREQUENCY, MIN_FREQUENCY], ] # Generate uncertain class stimuli pattern uncertain_stimuli_rates = [ [MAX_FREQUENCY * 0.5], [MAX_FREQUENCY * 0.5], ] # Build basic network input_populations, class_populations = build_basic_network( testing_stimuli_rates, TESTING_STIMULUS_TIME, uncertain_stimuli_rates, TESTING_TIME, True, learnt_biases, False, sim) # Create BCPNN model with weights disabled bcpnn_synapse = bcpnn.BCPNNSynapse(tau_zi=BCPNN_TAU_PRIMARY, tau_zj=BCPNN_TAU_PRIMARY, tau_p=BCPNN_TAU_ELIGIBILITY, f_max=MAX_FREQUENCY, w_max=BCPNN_MAX_WEIGHT, weights_enabled=True, plasticity_enabled=False) for ((i, c), w) in zip(itertools.product(input_populations, class_populations), learnt_weights): # Convert learnt weight matrix into a connection list connections = convert_weights_to_list(w, 1.0, 7.0) # Create projections sim.Projection(i, c, sim.FromListConnector(connections), bcpnn_synapse, receptor_type="excitatory", label="%s-%s" % (i.label, c.label)) # Run simulation sim.run(TESTING_TIME) # Read spikes from input and class populations input_data = [i.get_data() for i in input_populations] class_data = [c.get_data() for c in class_populations] # End simulation on SpiNNaker sim.end() # Return spikes return input_data, class_data
def test(sepal_length, sepal_length_unit_mean_sd, sepal_width, sepal_width_unit_mean_sd, petal_length, petal_length_unit_mean_sd, petal_width, petal_width_unit_mean_sd, num_species, learnt_biases, learnt_weights): # SpiNNaker setup sim.setup(timestep=1.0, min_delay=1.0, max_delay=10.0, spinnaker_hostname="192.168.1.1") # Calculate input rates input_rates = [] calculate_stim_rates(sepal_length, sepal_length_unit_mean_sd, input_rates, MAX_FREQUENCY) calculate_stim_rates(sepal_width, sepal_width_unit_mean_sd, input_rates, MAX_FREQUENCY) calculate_stim_rates(petal_length, petal_length_unit_mean_sd, input_rates, MAX_FREQUENCY) calculate_stim_rates(petal_width, petal_width_unit_mean_sd, input_rates, MAX_FREQUENCY) # Generate uncertain class pattern uncertain_class_rates = [[MAX_FREQUENCY * (1.0 / num_species)] for s in range(num_species)] # Build basic network testing_time = STIMULUS_TIME * len(sepal_length) input_populations, class_populations = build_basic_network(input_rates, STIMULUS_TIME, uncertain_class_rates, testing_time, True, learnt_biases, False, sim) # Create BCPNN model with weights disabled bcpnn_synapse = bcpnn.BCPNNSynapse( tau_zi=BCPNN_TAU_PRIMARY, tau_zj=BCPNN_TAU_PRIMARY, tau_p=BCPNN_TAU_ELIGIBILITY, f_max=MAX_FREQUENCY, w_max=BCPNN_MAX_WEIGHT, weights_enabled=True, plasticity_enabled=False) for ((i, c), w) in zip(itertools.product(input_populations, class_populations), learnt_weights): # Convert learnt weight matrix into a connection list connections = convert_weights_to_list(w, 1.0, 7.0 * (30.0 / float(CLASS_POP_SIZE))) # Create projections sim.Projection(i, c, sim.FromListConnector(connections), bcpnn_synapse, receptor_type="excitatory", label="%s-%s" % (i.label, c.label)) # Run simulation sim.run(testing_time) # Read spikes from input and class populations input_data = [i.get_data() for i in input_populations] class_data = [c.get_data() for c in class_populations] # End simulation on SpiNNaker sim.end() # Return spikes return input_data, class_data
def __init__(self, sim, pre_hcu, post_hcu, ampa_connector, nmda_connector, ampa_synapse, nmda_synapse, record_ampa, record_nmda): self.record_ampa = record_ampa self.record_nmda = record_nmda # Create connection self.ampa_connection = sim.Projection(pre_hcu.e_cells, post_hcu.e_cells, ampa_connector, ampa_synapse, receptor_type="excitatory", label="%s->%s (AMPA)" % (pre_hcu.e_cells.label, post_hcu.e_cells.label)) self.nmda_connection = sim.Projection(pre_hcu.e_cells, post_hcu.e_cells, nmda_connector, nmda_synapse, receptor_type="excitatory2", label="%s->%s (NMDA)" % (pre_hcu.e_cells.label, post_hcu.e_cells.label))
def __init__(self, name, sim, rng, num_excitatory, num_inhibitory, JE, JI, e_cell_model, i_cell_model, e_cell_params, i_cell_params, e_cell_flush_time, e_cell_mean_firing_rate, stim_spike_times, wta, background_weight, background_rate, stim_weight, simtime, record_bias, record_spikes, record_membrane): logger.info("Creating HCU:%s" % name) logger.debug("num excitatory:%u, num inhibitory:%u", num_excitatory, num_inhibitory) # compute number of excitatory synapses on neuron num_excitatory_synapses = int(epsilon * num_excitatory) # Cache recording flags self.record_bias = record_bias self.record_spikes = record_spikes self.record_membrane = record_membrane self.wta = wta logger.debug("Membrane potentials uniformly distributed between %g mV and %g mV.", -80, U0) membrane_voltage_distribution = RandomDistribution("uniform", low=-80.0, high=U0, rng=rng) logger.debug("Creating excitatory population with %d neurons.", num_excitatory) self.e_cells = sim.Population(num_excitatory, e_cell_model(**e_cell_params), label="%s - e_cells" % name) self.e_cells.initialize(v=membrane_voltage_distribution) # Set e cell mean firing rate self.e_cells.spinnaker_config.mean_firing_rate = e_cell_mean_firing_rate # **HACK** issue #18 means that we end up with 1024 wide clusters # which needs a lot of 256-wide neuron and synapse cores self.e_cells.spinnaker_config.max_cluster_width = 512 # Set flush time self.e_cells.spinnaker_config.flush_time = e_cell_flush_time # **YUCK** record spikes actually entirely ignores # sampling interval but throws exception if it is not set if self.record_spikes: self.e_cells.record("spikes", sampling_interval=1000.0) if self.record_bias: self.e_cells.record("bias", sampling_interval=1000.0) if self.record_membrane: self.e_cells.record("v", sampling_interval=1000.0) e_poisson = sim.Population(num_excitatory, sim.SpikeSourcePoisson(rate=background_rate, duration=simtime), label="%s - e_poisson" % name) logger.debug("Creating background->E AMPA connection weight %g nA.", background_weight) sim.Projection(e_poisson, self.e_cells, sim.OneToOneConnector(), sim.StaticSynapse(weight=background_weight, delay=delay), receptor_type="excitatory") if self.wta: logger.debug("Creating inhibitory population with %d neurons.", num_inhibitory) self.i_cells = sim.Population(num_inhibitory, i_cell_model, i_cell_params, label="%s - i_cells" % name) self.i_cells.initialize(v=membrane_voltage_distribution) # Inhibitory cells generally fire at a low rate self.i_cells.spinnaker_config.mean_firing_rate = 5.0 if self.record_spikes: self.i_cells.record("spikes") i_poisson = sim.Population(num_inhibitory, sim.SpikeSourcePoisson(rate=background_rate, duration=simtime), label="%s - i_poisson" % name) logger.debug("Creating I->E GABA connection with connection probability %g, weight %g nA and delay %g ms.", epsilon, JI, delay) I_to_E = sim.Projection(self.i_cells, self.e_cells, sim.FixedProbabilityConnector(p_connect=epsilon, rng=rng), sim.StaticSynapse(weight=JI, delay=delay), receptor_type="inhibitory") logger.debug("Creating E->I AMPA connection with connection probability %g, weight %g nA and delay %g ms.", epsilon, JE, delay) sim.Projection(self.e_cells, self.i_cells, sim.FixedProbabilityConnector(p_connect=epsilon, rng=rng), sim.StaticSynapse(weight=JE, delay=delay), receptor_type="excitatory") logger.debug("Creating I->I GABA connection with connection probability %g, weight %g nA and delay %g ms.", epsilon, JI, delay) sim.Projection(self.i_cells, self.i_cells, sim.FixedProbabilityConnector(p_connect=epsilon, rng=rng), sim.StaticSynapse(weight=JI, delay=delay), receptor_type="inhibitory") logger.debug("Creating background->I AMPA connection weight %g nA.", background_weight) sim.Projection(i_poisson, self.i_cells, sim.OneToOneConnector(), sim.StaticSynapse(weight=background_weight, delay=delay), receptor_type="excitatory") # Create a spike source capable of stimulating entirely excitatory population stim_spike_source = sim.Population(num_excitatory, sim.SpikeSourceArray(spike_times=stim_spike_times)) # Connect one-to-one to excitatory neurons sim.Projection(stim_spike_source, self.e_cells, sim.OneToOneConnector(), sim.StaticSynapse(weight=stim_weight, delay=delay), receptor_type="excitatory")
def train(): # SpiNNaker setup sim.setup(timestep=1.0, min_delay=1.0, max_delay=10.0, spinnaker_hostname="192.168.1.1") # Generate orthogonal input stimuli orthogonal_stimuli_rates = [] num_inputs = len(INPUT_NAMES) for i in range(num_inputs): input_stimuli = [] for s in range(TRAINING_TIME / TRAINING_STIMULUS_TIME): input_stimuli.append(MIN_FREQUENCY if ( s % num_inputs) == i else MAX_FREQUENCY) orthogonal_stimuli_rates.append(input_stimuli) # Build basic network with orthogonal stimulation of both populations input_populations, class_populations = build_basic_network( orthogonal_stimuli_rates, TRAINING_STIMULUS_TIME, orthogonal_stimuli_rates, TRAINING_STIMULUS_TIME, False, 0.0, True, sim) # Create BCPNN model with weights disabled bcpnn_synapse = bcpnn.BCPNNSynapse(tau_zi=BCPNN_TAU_PRIMARY, tau_zj=BCPNN_TAU_PRIMARY, tau_p=BCPNN_TAU_ELIGIBILITY, f_max=MAX_FREQUENCY, w_max=BCPNN_MAX_WEIGHT, weights_enabled=False, plasticity_enabled=True, weight=0.0) # Create all-to-all conector to connect inputs to classes input_class_connector = sim.AllToAllConnector() # Loop through all pairs of input populations and classes plastic_connections = [] for (i, c) in itertools.product(input_populations, class_populations): # Connect input to class with all-to-all plastic synapse connection = sim.Projection(i, c, input_class_connector, bcpnn_synapse, receptor_type="excitatory", label="%s-%s" % (i.label, c.label)) plastic_connections.append(connection) # Run simulation sim.run(TRAINING_TIME) # Plot bias evolution num_classes = len(CLASS_NAMES) #bias_figure, bias_axes = pylab.subplots() # **HACK** Extract learnt biases from gsyn channel learnt_biases = [] plotting_times = range(TRAINING_TIME) for i, c in enumerate(class_populations): # Read bias from class bias = c.get_data().segments[0].filter(name="bias")[0] ''' # Loop through plotting times to get mean biases mean_pj = [] for t in plotting_times: # Slice out the rows for all neurons at this time time_rows = gsyn[t::TRAINING_TIME] time_bias = zip(*time_rows)[2] mean_pj.append(numpy.average(numpy.exp(numpy.divide(time_bias,BCPNN_PHI)))) bias_axes.plot(plotting_times, mean_pj, label=c.label) ''' # Add final bias column to list # **HACK** investigate where out by 1000 comes from! learnt_biases.append(bias[-1, :] * 0.001) ''' bias_axes.set_title("Mean final bias") bias_axes.set_ylim((0.0, 1.0)) bias_axes.set_ylabel("Pj") bias_axes.set_xlabel("Time/ms") bias_axes.legend() ''' # Plot weights weight_figure, weight_axes = pylab.subplots(num_inputs, num_classes) # Loop through plastic connections learnt_weights = [] for i, c in enumerate(plastic_connections): # Extract weights and calculate mean weights = c.get("weight", format="array") mean_weight = numpy.average(weights) # Add weights to list learnt_weights.append(weights) # Plot mean weight in each panel axis = weight_axes[i % num_inputs][i / num_classes] axis.matshow([[mean_weight]], cmap=pylab.cm.gray) #axis.set_title("%s: %fuS" % (c.label, mean_weight)) axis.set_title("%u->%u: %f" % (i % num_inputs, i / num_classes, mean_weight)) axis.get_xaxis().set_visible(False) axis.get_yaxis().set_visible(False) # Show figures pylab.show() # End simulation on SpiNNaker sim.end() # Return learnt weights return learnt_weights, learnt_biases
stdp_model = sim.STDPMechanism( timing_dependence=sim.SpikePairRule(tau_plus=5.0, tau_minus=5.0, A_plus=0.000001, A_minus=1.0), weight_dependence=sim.AdditiveWeightDependence(w_min=0.0, w_max=instant_spike_weight), dendritic_delay_fraction=1.0) ''' stdp_model = sim.StaticSynapse() ''' # Create connector proj = sim.Projection(neurons, neurons, sim.FromListConnector(conn_list), stdp_model, receptor_type="excitatory") # Stimulate stim neuron stim = sim.Population(1, sim.SpikeSourceArray(spike_times=[2.0]), label="stim") sim.Projection( stim, neurons, sim.FromListConnector([ (0, get_neuron_index(stim_x, stim_y, cost_image.shape[1]), instant_spike_weight, 1.0) ]), sim.StaticSynapse()) # Run network sim.run(duration)