From b3ef18607a18cdf06f3cc62b902cd072eea2a3e7 Mon Sep 17 00:00:00 2001 From: Andrew Gait Date: Thu, 14 Sep 2023 19:38:06 +0100 Subject: [PATCH 1/2] Add tests for new IF_curr_delta STDP binaries --- .../test_stdp/test_IF_curr_delta_stdp.py | 223 ++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 spynnaker_integration_tests/test_stdp/test_IF_curr_delta_stdp.py diff --git a/spynnaker_integration_tests/test_stdp/test_IF_curr_delta_stdp.py b/spynnaker_integration_tests/test_stdp/test_IF_curr_delta_stdp.py new file mode 100644 index 0000000000..ec0b758b2b --- /dev/null +++ b/spynnaker_integration_tests/test_stdp/test_IF_curr_delta_stdp.py @@ -0,0 +1,223 @@ +# Copyright (c) 2023 The University of Manchester +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import numpy +import unittest +import pyNN.spiNNaker as sim +from spinnaker_testbase import BaseTestCase + + +class TestIFCurrDeltaSTDP(BaseTestCase): + + def mad_pair_additive_delta(self): + timestep = 1 + spike_current = 20 + input_population_size = 2 + + # Spikes set so the calculated weights for each STDP projection + # should be identical + spike_times1 = [[1,27,55], [1,27,55]] + spike_times2 = [[1,33,59], [1,33,59]] + training_times = [28] + save_spike_times = [90] + runtime = save_spike_times[-1] + 10 + + sim.setup(timestep=timestep) + + IF_curr_delta_model = sim.IF_curr_delta() + + # Set up populations + ssa1 = sim.Population( + input_population_size, sim.SpikeSourceArray(spike_times1), + label='ssa1') + ssa2 = sim.Population( + input_population_size, sim.SpikeSourceArray(spike_times2), + label='ssa2') + save_neuron = sim.Population( + 1, sim.SpikeSourceArray(save_spike_times), label='save_neuron') + injector_neurons_exc = sim.Population( + input_population_size, IF_curr_delta_model, + label='injector_neurons_exc') + injector_neurons_inh = sim.Population( + input_population_size, IF_curr_delta_model, + label='injector_neurons_inh') + teacher_population = sim.Population( + 1, sim.SpikeSourceArray(training_times), + label='teacher_population') + output_neuron = sim.Population( + 1, IF_curr_delta_model, label='output_neuron') + + # Set up projections + static_synapse = sim.StaticSynapse(weight=spike_current, delay=1) + teaching_synapse = sim.StaticSynapse(weight=spike_current, delay=2) + + # SSA -> injectors + sim.Projection( + ssa1, injector_neurons_exc, sim.OneToOneConnector(), + static_synapse, receptor_type='excitatory') + sim.Projection( + ssa2, injector_neurons_inh, sim.OneToOneConnector(), + static_synapse, receptor_type='excitatory') + + # save -> injectors + sim.Projection(save_neuron, injector_neurons_exc, + sim.AllToAllConnector(allow_self_connections=True), + static_synapse, receptor_type='excitatory') + sim.Projection(save_neuron, injector_neurons_inh, + sim.AllToAllConnector(allow_self_connections=True), + static_synapse, receptor_type='excitatory') + + # teacher -> output + sim.Projection(teacher_population, output_neuron, + sim.AllToAllConnector(allow_self_connections=True), + teaching_synapse, receptor_type='excitatory') + + # stdp models for injector -> output + stdp_model = sim.STDPMechanism( + timing_dependence=sim.SpikePairRule( + tau_plus=10, tau_minus=12, A_plus=1, A_minus=-1), + weight_dependence=sim.AdditiveWeightDependence(w_min=0, w_max=20), + weight=0, delay=1) + stdp_model2 = sim.STDPMechanism( + timing_dependence=sim.SpikePairRule( + tau_plus=10, tau_minus=12, A_plus=1, A_minus=-1), + weight_dependence=sim.AdditiveWeightDependence(w_min=0, w_max=20), + weight=0, delay=1) + + injector_proj_exc = sim.Projection( + injector_neurons_exc, output_neuron, + sim.AllToAllConnector(allow_self_connections=True), + stdp_model, receptor_type='excitatory') + injector_proj_inh = sim.Projection( + injector_neurons_inh, output_neuron, + sim.AllToAllConnector(allow_self_connections=True), + stdp_model2, receptor_type='inhibitory') + + sim.run(runtime) + + weights_exc = injector_proj_exc.get(["weight"], "list") + weights_inh = injector_proj_inh.get(["weight"], "list") + + print(weights_exc) + print(weights_inh) + + sim.end() + + self.assertTrue(numpy.allclose(weights_exc, weights_inh, rtol=0.001)) + + def nearest_pair_additive_delta(self): + timestep = 1 + spike_current = 20 + input_population_size = 2 + + # Spikes set so the calculated weights for each STDP projection + # should be identical + spike_times1 = [[1,27,51], [1,27,51]] + spike_times2 = [[1,33,60], [1,33,60]] + training_times = [28] + save_spike_times = [90] + runtime = save_spike_times[-1] + 10 + + sim.setup(timestep=timestep) + + IF_curr_delta_model = sim.IF_curr_delta() + + # Set up populations + ssa1 = sim.Population( + input_population_size, sim.SpikeSourceArray(spike_times1), + label='ssa1') + ssa2 = sim.Population( + input_population_size, sim.SpikeSourceArray(spike_times2), + label='ssa2') + save_neuron = sim.Population( + 1, sim.SpikeSourceArray(save_spike_times), label='save_neuron') + injector_neurons_exc = sim.Population( + input_population_size, IF_curr_delta_model, + label='injector_neurons_exc') + injector_neurons_inh = sim.Population( + input_population_size, IF_curr_delta_model, + label='injector_neurons_inh') + teacher_population = sim.Population( + 1, sim.SpikeSourceArray(training_times), + label='teacher_population') + output_neuron = sim.Population( + 1, IF_curr_delta_model, label='output_neuron') + + # Set up projections + static_synapse = sim.StaticSynapse(weight=spike_current, delay=1) + teaching_synapse = sim.StaticSynapse(weight=spike_current, delay=2) + + # SSA -> injectors + sim.Projection( + ssa1, injector_neurons_exc, sim.OneToOneConnector(), + static_synapse, receptor_type='excitatory') + sim.Projection( + ssa2, injector_neurons_inh, sim.OneToOneConnector(), + static_synapse, receptor_type='excitatory') + + # save -> injectors + sim.Projection(save_neuron, injector_neurons_exc, + sim.AllToAllConnector(allow_self_connections=True), + static_synapse, receptor_type='excitatory') + sim.Projection(save_neuron, injector_neurons_inh, + sim.AllToAllConnector(allow_self_connections=True), + static_synapse, receptor_type='excitatory') + + # teacher -> output + sim.Projection(teacher_population, output_neuron, + sim.AllToAllConnector(allow_self_connections=True), + teaching_synapse, receptor_type='excitatory') + + # stdp models for injector -> output + stdp_model = sim.STDPMechanism( + timing_dependence=sim.extra_models.SpikeNearestPairRule( + tau_plus=10, tau_minus=12, A_plus=1, A_minus=-1), + weight_dependence=sim.AdditiveWeightDependence(w_min=0, w_max=20), + weight=0, delay=1) + stdp_model2 = sim.STDPMechanism( + timing_dependence=sim.extra_models.SpikeNearestPairRule( + tau_plus=10, tau_minus=12, A_plus=1, A_minus=-1), + weight_dependence=sim.AdditiveWeightDependence(w_min=0, w_max=20), + weight=0, delay=1) + + injector_proj_exc = sim.Projection( + injector_neurons_exc, output_neuron, + sim.AllToAllConnector(allow_self_connections=True), + stdp_model, receptor_type='excitatory') + injector_proj_inh = sim.Projection( + injector_neurons_inh, output_neuron, + sim.AllToAllConnector(allow_self_connections=True), + stdp_model2, receptor_type='inhibitory') + + sim.run(runtime) + + weights_exc = injector_proj_exc.get(["weight"], "list") + weights_inh = injector_proj_inh.get(["weight"], "list") + + print(weights_exc) + print(weights_inh) + + sim.end() + + self.assertTrue(numpy.allclose(weights_exc, weights_inh, rtol=0.001)) + + def test_mad_pair_additive_delta(self): + self.runsafe(self.mad_pair_additive_delta) + + def test_nearest_pair_additive_delta(self): + self.runsafe(self.nearest_pair_additive_delta) + + +if __name__ == '__main__': + unittest.main() From 4c56de4c936678b2d18dbc55a1b7add0a2270227 Mon Sep 17 00:00:00 2001 From: Andrew Gait Date: Thu, 14 Sep 2023 19:42:41 +0100 Subject: [PATCH 2/2] flake8 whitespaces --- .../test_stdp/test_IF_curr_delta_stdp.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spynnaker_integration_tests/test_stdp/test_IF_curr_delta_stdp.py b/spynnaker_integration_tests/test_stdp/test_IF_curr_delta_stdp.py index ec0b758b2b..0ce6fb41c8 100644 --- a/spynnaker_integration_tests/test_stdp/test_IF_curr_delta_stdp.py +++ b/spynnaker_integration_tests/test_stdp/test_IF_curr_delta_stdp.py @@ -27,8 +27,8 @@ def mad_pair_additive_delta(self): # Spikes set so the calculated weights for each STDP projection # should be identical - spike_times1 = [[1,27,55], [1,27,55]] - spike_times2 = [[1,33,59], [1,33,59]] + spike_times1 = [[1, 27, 55], [1, 27, 55]] + spike_times2 = [[1, 33, 59], [1, 33, 59]] training_times = [28] save_spike_times = [90] runtime = save_spike_times[-1] + 10 @@ -123,8 +123,8 @@ def nearest_pair_additive_delta(self): # Spikes set so the calculated weights for each STDP projection # should be identical - spike_times1 = [[1,27,51], [1,27,51]] - spike_times2 = [[1,33,60], [1,33,60]] + spike_times1 = [[1, 27, 51], [1, 27, 51]] + spike_times2 = [[1, 33, 60], [1, 33, 60]] training_times = [28] save_spike_times = [90] runtime = save_spike_times[-1] + 10