a.set_title(l.name) a.set_ylabel("Spike number") a.set_xlim((example * PRESENT_TIMESTEPS, (example + 1) * PRESENT_TIMESTEPS)) a.set_ylim((-1, l.size + 1)) # Add an x-axis label axes[-1].set_xlabel("Time [ms]") # axes[-1].hlines(testing_labels[0], xmin=0, xmax=PRESENT_TIMESTEPS, # linestyle="--", color="gray", alpha=0.2) # Show plot save_filename = os.path.join('example' + str(example) + '.png') plt.savefig(save_filename) model.pull_var_from_device("out", "SpikeCount") true_label = y[example] classwise_total_spikes = np.add.reduceat( output_spike_count, np.arange(0, len(output_spike_count), OUTPUT_NEURON_NUM)) print(classwise_total_spikes) predicted_label = np.argmax(classwise_total_spikes) print("\tExample=%u, true label=%u, predicted label=%u" % (example, true_label, predicted_label)) if predicted_label == true_label: num_correct += 1 print("\n")
#testing_images = np.load("testing_images.npy") #testing_labels = np.load("testing_labels.npy") # Load MNIST data training_images, training_labels = get_training_data() testing_images, testing_labels = get_testing_data() single_example_timesteps = NUM_PRESENT_TIMESTEPS + NUM_REST_TIMESTEPS # Get views to efficiently access state variables pn_input_current_view = pn_input.vars["magnitude"].view pn_refrac_time_view = pn.vars["RefracTime"].view ggn_v_view = ggn.vars["V"].view kc_mbon_view = kc_mbon.vars["g"].view model.pull_var_from_device("kc_mbon", "g") before = np.copy(kc_mbon_view) plot = True label_spikes = np.zeros((NUM_MBON, NUM_MBON), dtype=int) pn_spikes = None kc_spikes = None mbon_spikes = None NUM_STIM = 100 while model.timestep < (single_example_timesteps * NUM_STIM): # Calculate the timestep within the presentation timestep_in_example = model.timestep % single_example_timesteps example = int(model.timestep // single_example_timesteps)
x = [] while model.timestep < PRESENT_TIMESTEPS: model.step_time() # Record presynaptic spikes presyn.pull_current_spikes_from_device() pre_spikes.append(np.copy(presyn.current_spikes)) # Record postsynaptic spikes postsyn.pull_current_spikes_from_device() post_spikes.append(np.copy(postsyn.current_spikes)) # Record value of postsyn_V model.pull_var_from_device("postsyn", "V") post_v.append(np.copy(postsyn.vars["V"].view)) # Record value of X pre2post.pull_var_from_device("X") x.append(np.copy(pre2post.get_var_values("X"))) # Record value of C pre2post.pull_var_from_device("C") c.append(np.copy(pre2post.post_vars["C"].view)) pre_spike_times = get_spike_times(pre_spikes) post_spike_times = get_spike_times(post_spikes) post_v = np.concatenate(post_v) c = np.concatenate(c)
non_target_spike_rates.append(non_target_rate) target_spike_rates.append(target_rate) print("Completed training.") target_final = sum(target_spike_rates) / len(target_spike_rates) non_target_final = sum(non_target_spike_rates) / len( non_target_spike_rates) # print("Target neurons spiking at: " + str(target_final) + " Hz.") # print("Non-target neurons spiking at: " + str(non_target_final) + " Hz.") CSV_DATA = [EXPERIMENT, target_final, non_target_final] model.pull_var_from_device(inp2out.name, "g") weight_values = inp2out.get_var_values("g") WT_FILENAME = os.path.join(WEIGHTS_PATH, EXPERIMENT + ".npy") np.save(WT_FILENAME, weight_values) ########### TESTING ############## # ---------------------------------------------------------------------------- # Build model # ---------------------------------------------------------------------------- # Create GeNN model model = GeNNModel("float", "tutorial_1") model.dT = TIMESTEP neurons_count = { "inp": NUM_INPUT,
# Manually 'reset' voltage v[:] = 0.0 # Upload model.push_var_to_device(l.name, "V") # Zero spike count output_spike_count[:] = 0 model.push_var_to_device(neuron_layers[-1].name, "SpikeCount") # Advance simulation model.step_time() # If this is the LAST timestep of presenting the example if timestep_in_example == (PRESENT_TIMESTEPS - 1): # Download spike count from last layer model.pull_var_from_device(neuron_layers[-1].name, "SpikeCount") # Find which neuron spiked the most to get prediction predicted_label = np.argmax(output_spike_count) true_label = testing_labels[example] print("\tExample=%u, true label=%u, predicted label=%u" % (example, true_label, predicted_label)) if predicted_label == true_label: num_correct += 1 print("Accuracy %f%%" % ((num_correct / float(testing_images.shape[0])) * 100.0))
# ---------------------------------------------------------------------------- # Training # ---------------------------------------------------------------------------- # Get views to efficiently access state variables current_input_magnitude = current_input.vars["magnitude"].view current_output_magnitude = current_output.vars["magnitude"].view layer_voltages = [l.vars["V"].view for l in neuron_layers] exp = "vogels" prefix = "vogels" save_png_dir = "/home/manvi/Documents/pygenn_ml_tutorial/imgs/" + exp print("Experiment: " + prefix) model.pull_var_from_device(synapses[1].name, "g") weight_values = synapses[1].get_var_values("g") print("max: ") print(np.amax(weight_values)) print("min: ") print(np.amin(weight_values)) # Simulate while model.timestep < (PRESENT_TIMESTEPS * X.shape[0]): # Calculate the timestep within the presentation timestep_in_example = model.timestep % PRESENT_TIMESTEPS example = int(model.timestep // PRESENT_TIMESTEPS) # If this is the first timestep of presenting the example if timestep_in_example == 0:
var_name_types=[("rate", "scalar"), ("spikeCount", "scalar")], sim_code=""" """, reset_code=""" $(spikeCount) += 1; """, threshold_condition_code="$(gennrand_uniform) >= exp(-$(rate) * 0.001 * DT)" ) TIMESTEP = 1.0 PRESENT_TIMESTEPS = 1000 model = GeNNModel("float", "tutorial_1") model.dT = TIMESTEP poisson_init = {"rate": 30.0, "spikeCount": 0.0} p = model.add_neuron_population("p", 1, poisson_model, {}, poisson_init) model.build() model.load() while model.timestep < PRESENT_TIMESTEPS: model.step_time() model.pull_var_from_device("p", "spikeCount") spikeNum = p.vars["spikeCount"].view print("Spike Rate: ") print(spikeNum)