|
| 1 | +# Copyright (c) 2026 The University of Manchester |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +stdp_mad_recurrent_pre_stochastic_multiplicative |
| 17 | +""" |
| 18 | +import matplotlib.pyplot as plt |
| 19 | +import pyNN.spiNNaker as p |
| 20 | +from pyNN.utility.plotting import Figure, Panel |
| 21 | +# pylint: disable=wrong-spelling-in-comment |
| 22 | + |
| 23 | +p.setup(timestep=1.0, min_delay=1.0) |
| 24 | +p.set_number_of_neurons_per_core(p.IF_curr_exp, 100) |
| 25 | + |
| 26 | +nSourceNeurons = 1 # number of input (excitatory) neurons |
| 27 | +nExcitNeurons = 1 # number of excitatory neurons in the recurrent memory |
| 28 | +nInhibNeurons = 10 # number of inhibitory neurons in the recurrent memory |
| 29 | +nTeachNeurons = 1 |
| 30 | +runTime = 3200 |
| 31 | + |
| 32 | +cell_params_lif = { |
| 33 | + 'cm': 0.25, # nF was 0.25 |
| 34 | + 'i_offset': 0.0, |
| 35 | + 'tau_m': 10.0, |
| 36 | + 'tau_refrac': 2.0, |
| 37 | + 'tau_syn_E': 0.5, |
| 38 | + 'tau_syn_I': 0.5, |
| 39 | + 'v_reset': -70.0, |
| 40 | + 'v_rest': -70.0, |
| 41 | + 'v_thresh': -50.0} |
| 42 | + |
| 43 | +populations = list() |
| 44 | +projections = list() |
| 45 | + |
| 46 | +stimulus = 0 |
| 47 | +inhib = 1 |
| 48 | +excit = 2 |
| 49 | +teacher = 3 |
| 50 | + |
| 51 | +weight_to_force_firing = 15.0 |
| 52 | +baseline_excit_weight = 2.0 |
| 53 | + |
| 54 | +spikes0 = list() |
| 55 | +teachingSpikes = list() |
| 56 | +for i in range(runTime//40): |
| 57 | + spikes0.append(i*40) |
| 58 | +for i in range(runTime//80): |
| 59 | + teachingSpikes.append(i*40+5+120) |
| 60 | + |
| 61 | +arrayEntries = [] |
| 62 | +for i in range(nSourceNeurons): |
| 63 | + newEntry = [] |
| 64 | + for spike in spikes0: |
| 65 | + newEntry.append(spike + i*40.0/100.0) |
| 66 | + arrayEntries.append(newEntry) |
| 67 | +spikeArray = {'spike_times': arrayEntries} |
| 68 | + |
| 69 | +teachlist = list() |
| 70 | +for i in range(nSourceNeurons): |
| 71 | + teachlist.append(teachingSpikes) |
| 72 | +teachingSpikeArray = {'spike_times': teachlist} |
| 73 | +populations.append(p.Population(nSourceNeurons, |
| 74 | + p.SpikeSourceArray(**spikeArray), |
| 75 | + label='excit_pop_ss_array')) # 0 |
| 76 | +populations.append(p.Population(nInhibNeurons, |
| 77 | + p.IF_curr_exp(**cell_params_lif), |
| 78 | + label='inhib_pop')) # 1 |
| 79 | +populations.append(p.Population(nExcitNeurons, |
| 80 | + p.IF_curr_exp(**cell_params_lif), |
| 81 | + label='excit_pop')) # 2 |
| 82 | +populations.append(p.Population(nTeachNeurons, |
| 83 | + p.SpikeSourceArray(**teachingSpikeArray), |
| 84 | + label='teaching_ss_array')) # 3 |
| 85 | + |
| 86 | +stdp_model = p.STDPMechanism( |
| 87 | + timing_dependence=p.extra_models.RecurrentRule( |
| 88 | + accumulator_depression=-6, accumulator_potentiation=3, |
| 89 | + mean_pre_window=10.0, mean_post_window=10.0, dual_fsm=False, |
| 90 | + A_plus=0.2, A_minus=0.2), |
| 91 | + weight_dependence=p.MultiplicativeWeightDependence(w_min=0.0, w_max=16.0), |
| 92 | + weight=baseline_excit_weight, delay=1) |
| 93 | + |
| 94 | +projections.append( |
| 95 | + p.Projection(populations[stimulus], populations[excit], |
| 96 | + p.AllToAllConnector(), synapse_type=stdp_model)) |
| 97 | + |
| 98 | +projections.append( |
| 99 | + p.Projection(populations[teacher], populations[excit], |
| 100 | + p.OneToOneConnector(), receptor_type='excitatory', |
| 101 | + synapse_type=p.StaticSynapse( |
| 102 | + weight=weight_to_force_firing, delay=1))) |
| 103 | + |
| 104 | +populations[inhib].record(['v', 'spikes']) |
| 105 | +populations[excit].record(['v', 'spikes']) |
| 106 | + |
| 107 | +p.run(runTime) |
| 108 | + |
| 109 | +final_weights = projections[0].get('weight', 'list', with_address=False) |
| 110 | +print(f"Final weights: {final_weights}") |
| 111 | + |
| 112 | +v = populations[excit].get_data('v') |
| 113 | +spikes = populations[excit].get_data('spikes') |
| 114 | +vInhib = populations[inhib].get_data('v') |
| 115 | +spikesInhib = populations[inhib].get_data('spikes') |
| 116 | + |
| 117 | +Figure( |
| 118 | + # plot of the neuron spike times |
| 119 | + Panel(spikes.segments[0].spiketrains, |
| 120 | + yticks=True, markersize=0.2, xlim=(0, runTime)), |
| 121 | + # membrane potential of the neurons |
| 122 | + Panel(v.segments[0].filter(name='v')[0], |
| 123 | + ylabel="Membrane potential (mV)", |
| 124 | + data_labels=[populations[excit].label], yticks=True, |
| 125 | + xlim=(0, runTime), xticks=True), |
| 126 | + title="Simple associative memory: spikes and membrane potential", |
| 127 | + annotations=f"Simulated with {p.name()}" |
| 128 | +) |
| 129 | +plt.show() |
| 130 | + |
| 131 | +p.end() |
| 132 | + |
| 133 | +# combined binaries [ |
| 134 | +# IF_curr_exp_stdp_mad_recurrent_pre_stochastic_multiplicative.aplx] |
0 commit comments