Skip to content

Commit

Permalink
Tidy up scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgait committed May 12, 2023
1 parent 72d5d93 commit e3aedfd
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 33 deletions.
29 changes: 22 additions & 7 deletions eprop_testing/create_pops_for_incremental_learning.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import spynnaker8 as pynn
import pyNN.spiNNaker as pynn
from spynnaker.pyNN.spynnaker_external_device_plugin_manager import SpynnakerExternalDevicePluginManager
from eprop_testing.plot_graph import draw_graph_from_list, plot_learning_curve
from eprop_testing.incremental_config import *
from plot_graph import draw_graph_from_list, plot_learning_curve
from incremental_config import *

def load_connections(npy_label, pop_size, rec=True):
in_conn = [list(ele) for ele in np.load(npy_label+' in.npy').tolist()]
Expand Down Expand Up @@ -66,6 +66,7 @@ def range_connector(pre_min, pre_max, post_min, post_max, weight=1.5, delay_offs

def first_create_pops():
pynn.setup(timestep=1)
pynn.set_number_of_neurons_per_core(pynn.extra_models.EPropAdaptive, 6)
input_pop = pynn.Population(input_size,
pynn.SpikeSourcePoisson(rate=rates),
label='input_pop')
Expand All @@ -81,7 +82,13 @@ def first_create_pops():
label="readout_pop"
)

SpynnakerExternalDevicePluginManager.add_edge(readout_pop._get_vertex, input_pop._get_vertex, "CONTROL")
# SpynnakerExternalDevicePluginManager.add_edge(readout_pop._get_vertex, input_pop._get_vertex, "CONTROL")
poisson_control_edge = SpynnakerExternalDevicePluginManager.add_edge(
readout_pop._vertex, input_pop._vertex, "CONTROL")
# pynn.external_devices.activate_live_output_to(
# readout_pop, input_pop, "CONTROL")
input_pop._vertex.set_live_poisson_control_edge(poisson_control_edge)
# pynn.external_devices.add_poisson_live_rate_control(input_pop)

eprop_learning_neuron = pynn.STDPMechanism(
timing_dependence=pynn.extra_models.TimingDependenceEprop(),
Expand Down Expand Up @@ -179,7 +186,13 @@ def next_create_pops(from_list_in, from_list_rec, from_list_out):
label="readout_pop"
)

SpynnakerExternalDevicePluginManager.add_edge(readout_pop._get_vertex, input_pop._get_vertex, "CONTROL")
# SpynnakerExternalDevicePluginManager.add_edge(readout_pop._get_vertex, input_pop._get_vertex, "CONTROL")
poisson_control_edge = SpynnakerExternalDevicePluginManager.add_edge(
readout_pop._vertex, input_pop._vertex, "CONTROL")
# pynn.external_devices.activate_live_output_to(
# readout_pop, input_pop, "CONTROL")
input_pop._vertex.set_live_poisson_control_edge(poisson_control_edge)
# pynn.external_devices.add_poisson_live_rate_control(input_pop)

eprop_learning_neuron = pynn.STDPMechanism(
timing_dependence=pynn.extra_models.TimingDependenceEprop(),
Expand Down Expand Up @@ -290,14 +303,16 @@ def run_until(experiment_label, runtime, pynn, in_proj, recurrent_proj, out_proj
pynn.run(window_size)
# in_spikes = input_pop.get_data('spikes', clear=True)
# neuron_res = neuron.get_data('all', clear=True)
readout_res = readout_pop.get_data('all', clear=True)
readout_res = readout_pop.get_data('all') #, clear=True)

for cycle in range(window_cycles):
cycle_error.append(0.0)
correct_or_not.append([])
for time_index in range(cycle_time):
instantaneous_error = np.abs(float(
readout_res.segments[0].filter(name='gsyn_inh')[0][time_index + ((cycle+current_iter) * cycle_time)][0]))
readout_res.segments[0].filter(
name='gsyn_inh')[0][time_index + (
(cycle+current_iter) * cycle_time)][0]))
cycle_error[-1] += instantaneous_error
if cycle_error[-1] < 75:
correct_or_not[-1] = 1
Expand Down
6 changes: 3 additions & 3 deletions eprop_testing/incremental_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
readout_neuron_params = {
"v": 0,
"v_thresh": 30, # controls firing rate of error neurons
"poisson_pop_size": input_size / 4,
"poisson_pop_size": input_size // 4,
"rate_on": 100,
"rate_off": 0,
"w_fb": [1, -1, 0],
Expand All @@ -39,7 +39,7 @@
}
rates = []
for i in range(input_size):
if i >= (3 * input_size) / 4:
if i >= (3 * input_size) // 4:
rates.append(10)
else:
rates.append(0)
Expand All @@ -49,7 +49,7 @@
ratio_of_LIF = 0.5
beta = []
for i in range(neuron_pop_size):
if i < neuron_pop_size / 2:
if i < neuron_pop_size // 2:
beta.append(threshold_beta)
else:
beta.append(threshold_beta)
Expand Down
30 changes: 20 additions & 10 deletions eprop_testing/incremental_graph_drawing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import spynnaker8 as pynn
import numpy as np
import matplotlib.pyplot as plt
from PyNN8Examples.eprop_testing.frozen_poisson import build_input_spike_train, frozen_poisson_variable_hz
from frozen_poisson import build_input_spike_train, frozen_poisson_variable_hz
from pyNN.random import NumpyRNG, RandomDistribution
from pyNN.utility.plotting import Figure, Panel
from spynnaker.pyNN.spynnaker_external_device_plugin_manager import SpynnakerExternalDevicePluginManager
from PyNN8Examples.eprop_testing.plot_graph import draw_graph_from_list, plot_learning_curve
from plot_graph import draw_graph_from_list, plot_learning_curve

def load_connections(npy_label, pop_size, rec=True):
in_conn = [list(ele) for ele in np.load(npy_label+' in.npy').tolist()]
Expand Down Expand Up @@ -72,7 +72,7 @@ def range_connector(pre_min, pre_max, post_min, post_max, weight=1.5, delay_offs
number_of_cues = 1
cycle_time = (number_of_cues*150)+1000+150
num_repeats = 300
pynn.setup(1.0)
# pynn.setup(1.0) # setup called twice?

reg_rate = 0.000
p_connect_in = 1.
Expand All @@ -94,12 +94,13 @@ def range_connector(pre_min, pre_max, post_min, post_max, weight=1.5, delay_offs
weight_string = "i{}-p{}-r{}-o{}".format(in_weight, prompt_weight, rec_weight, out_weight)
pynn.setup(timestep=1)

pynn.set_number_of_neurons_per_core(pynn.extra_models.EPropAdaptive, 6)

input_size = 40
readout_neuron_params = {
"v": 0,
"v_thresh": 30, # controls firing rate of error neurons
"poisson_pop_size": input_size / 4,
"poisson_pop_size": input_size // 4,
"rate_on": 100,
"rate_off": 0,
# "tau_m": tau_a,
Expand All @@ -109,7 +110,7 @@ def range_connector(pre_min, pre_max, post_min, post_max, weight=1.5, delay_offs
}
rates = []
for i in range(input_size):
if i >= (3*input_size) / 4:
if i >= (3*input_size) // 4:
rates.append(10)
else:
rates.append(0)
Expand All @@ -125,7 +126,7 @@ def range_connector(pre_min, pre_max, post_min, post_max, weight=1.5, delay_offs
ratio_of_LIF = 0.5
beta = []
for i in range(neuron_pop_size):
if i < neuron_pop_size/2:
if i < neuron_pop_size//2:
# if i % 2 == 0:
beta.append(threshold_beta)
else:
Expand Down Expand Up @@ -158,7 +159,12 @@ def range_connector(pre_min, pre_max, post_min, post_max, weight=1.5, delay_offs
label="readout_pop"
)

SpynnakerExternalDevicePluginManager.add_edge(readout_pop._get_vertex, input_pop._get_vertex, "CONTROL")
poisson_control_edge = SpynnakerExternalDevicePluginManager.add_edge(
readout_pop._vertex, input_pop._vertex, "CONTROL")
# pynn.external_devices.activate_live_output_to(
# readout_pop, input_pop, "CONTROL")
input_pop._vertex.set_live_poisson_control_edge(poisson_control_edge)
# pynn.external_devices.add_poisson_live_rate_control(input_pop)

eprop_learning_neuron = pynn.STDPMechanism(
timing_dependence=pynn.extra_models.TimingDependenceEprop(),
Expand Down Expand Up @@ -232,7 +238,9 @@ def range_connector(pre_min, pre_max, post_min, post_max, weight=1.5, delay_offs

input_pop.record('spikes')
neuron.record('spikes')
neuron.record(['gsyn_exc', 'v', 'gsyn_inh'], indexes=[i for i in range(int((neuron_pop_size/2)-5), int((neuron_pop_size/2)+5))])
# neuron.record(['gsyn_exc', 'v', 'gsyn_inh'], indexes=[i for i in range(int((neuron_pop_size/2)-5), int((neuron_pop_size/2)+5))])
neuron[[i for i in range(int((neuron_pop_size/2)-5),
int((neuron_pop_size/2)+5))]].record(['gsyn_exc', 'v', 'gsyn_inh'])
readout_pop.record('all')

runtime = cycle_time * num_repeats
Expand All @@ -246,7 +254,7 @@ def range_connector(pre_min, pre_max, post_min, post_max, weight=1.5, delay_offs
pynn.run(window_size)
in_spikes = input_pop.get_data('spikes', clear=True)
neuron_res = neuron.get_data('all', clear=True)
readout_res = readout_pop.get_data('all', clear=True)
readout_res = readout_pop.get_data('all') #, clear=True)
plot_start = (window_size*current_window)
current_window += 1
plot_end = (window_size*current_window)
Expand Down Expand Up @@ -315,7 +323,9 @@ def range_connector(pre_min, pre_max, post_min, post_max, weight=1.5, delay_offs

graph_directory = './graphs/'
draw_graph_from_list(new_connections_in, new_connections_rec, new_connections_out, graph_directory, experiment_label+' {}'.format(current_window), rec_flag=recurrent_connections, save_flag=True)
plot_learning_curve([correct_or_not, cycle_error], graph_directory, experiment_label+' {}'.format(current_window), save_flag=True)
plot_learning_curve(correct_or_not, cycle_error, graph_directory,
experiment_label+' {}'.format(current_window),
save_flag=True)


total_error = 0.0
Expand Down
6 changes: 3 additions & 3 deletions eprop_testing/incremental_learning.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
from eprop_testing.plot_graph import draw_graph_from_list, plot_learning_curve
from eprop_testing.create_pops_for_incremental_learning import first_create_pops, next_create_pops, run_until
from eprop_testing.incremental_config import *
from plot_graph import draw_graph_from_list, plot_learning_curve
from create_pops_for_incremental_learning import first_create_pops, next_create_pops, run_until
from incremental_config import *

np.random.seed(272727)

Expand Down
9 changes: 6 additions & 3 deletions eprop_testing/input_buffer_overflow_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ def range_connector(pre_min, pre_max, post_min, post_max, weight=1.5, delay_offs
label="readout_pop"
)

# SpynnakerExternalDevicePluginManager.add_edge(readout_pop._get_vertex, input_pop._get_vertex, "CONTROL")
pynn.external_devices.activate_live_output_to(
readout_pop, input_pop, "CONTROL")
poisson_control_edge = SpynnakerExternalDevicePluginManager.add_edge(
readout_pop._vertex, input_pop._vertex, "CONTROL")
# pynn.external_devices.activate_live_output_to(
# readout_pop, input_pop, "CONTROL")
input_pop._vertex.set_live_poisson_control_edge(poisson_control_edge)
# pynn.external_devices.add_poisson_live_rate_control(input_pop)

start_w = [weight_distribution(neuron_pop_size*input_size) for i in range(input_size)]
eprop_learning_neuron = pynn.STDPMechanism(
Expand Down
2 changes: 1 addition & 1 deletion eprop_testing/readout_input_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import spynnaker8 as pynn
import pyNN.spiNNaker as pynn
import numpy as np
import matplotlib.pyplot as plt
from frozen_poisson import build_input_spike_train, frozen_poisson_variable_hz
Expand Down
16 changes: 10 additions & 6 deletions eprop_testing/test_left_right_readout.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,12 @@ def probability_connector(pre_pop_size, post_pop_size, prob, offset=0, added_wei
label="readout_pop"
)

# SpynnakerExternalDevicePluginManager.add_edge(
# readout_pop._vertex, input_pop._vertex, "CONTROL")
pynn.external_devices.activate_live_output_to(
readout_pop, input_pop, "CONTROL")
poisson_control_edge = SpynnakerExternalDevicePluginManager.add_edge(
readout_pop._vertex, input_pop._vertex, "CONTROL")
# pynn.external_devices.activate_live_output_to(
# readout_pop, input_pop, "CONTROL")
input_pop._vertex.set_live_poisson_control_edge(poisson_control_edge)
# pynn.external_devices.add_poisson_live_rate_control(input_pop)

# start_w = [weight_distribution(neuron_pop_size*input_size) for i in range(input_size)]
eprop_learning_neuron = pynn.STDPMechanism(
Expand Down Expand Up @@ -207,8 +209,10 @@ def probability_connector(pre_pop_size, post_pop_size, prob, offset=0, added_wei

runtime = cycle_time * num_repeats

experiment_label = "eta:{}/{} - size:{}/{} - reg_rate:{} - p_conn:{}/{}/{} - rec:{} - cycle:{}/{}/{} noresetv 0d b:{} in+{}".format(
readout_neuron_params["eta"], neuron_params["eta"], input_size, neuron_pop_size, reg_rate, p_connect_in, p_connect_rec, p_connect_out, recurrent_connections, cycle_time, window_size, runtime, eprop_beta, added_input_weight)
experiment_label = "eta:{}/{} - size:{}/{} - reg_rate:{} - p_conn:{}/{}/{} - rec:{} - cycle:{}/{}/{} noresetv 0d b:{}".format(
readout_neuron_params["eta"], neuron_params["eta"], input_size, neuron_pop_size,
reg_rate, p_connect_in, p_connect_rec, p_connect_out, recurrent_connections,
cycle_time, window_size, runtime, eprop_beta)
print("\n", experiment_label, "\n")

pynn.run(runtime)
Expand Down

0 comments on commit e3aedfd

Please sign in to comment.