Debian Codegraph @

examples/synapses/spike_based_homeostasis.py

raw
1 #!/usr/bin/env python3
2 """Brian2 simulation of spike-based homeostasis
3
4 Following
5 O. Breitwieser: "Towards a Neuromorphic Implementation of Spike-Based Expectation Maximization"
6
7 Two poisson stimuli are connected to a neuron. One with a varying rate
8 and the other with a fixed rate. The synaptic weight from the varying
9 rate stimulus to the neuron is fixed. The synaptic weight from the
10 fixed rate stimulus to the neuron is plastic and tries to keep the
11 neuron at a firing rate that is determined by the parameters of the
12 plasticity rule.
13
14 Sebastian Schmitt, 2021
15 """
16
17 import itertools
18 import numpy as np
19 import matplotlib.pyplot as plt
20
21 from brian2 import TimedArray, PoissonGroup, NeuronGroup, Synapses, StateMonitor, PopulationRateMonitor
22 from brian2 import defaultclock, run
23 from brian2 import Hz, ms, second
24
25 # The synaptic weight from the steady stimulus is plastic
26 steady_stimulus = TimedArray([50]*Hz, dt=40*second)
27 steady_poisson = PoissonGroup(1, rates='steady_stimulus(t)')
28
29 # The synaptic weight from the varying stimulus is static
30 varying_stimulus = TimedArray([25*Hz, 50*Hz, 0*Hz, 35*Hz, 0*Hz], dt=10*second)
31 varying_poisson = PoissonGroup(1, rates='varying_stimulus(t)')
32
33 # dw_plus/dw_minus determines scales the steady stimulus rate to the target firing rate, must not be larger 1
34 # the magntude of dw_plus and dw_minus determines the "speed" of the homeostasis
35 parameters = {
36 'tau': 10*ms, # membrane time constant
37 'dw_plus': 0.05, # weight increment on pre spike
38 'dw_minus': 0.05, # weight increment on post spike
39 'w_max': 2, # maximum plastic weight
40 'w_initial': 0 # initial plastic weight
41 }
42
43 eqs = 'dv/dt = (0 - v)/tau : 1 (unless refractory)'
44
45 neuron_with_homeostasis = NeuronGroup(1, eqs,
46 threshold='v > 1', reset='v = -1',
47 method='euler', refractory=1*ms,
48 namespace=parameters)
49 neuron_without_homeostasis = NeuronGroup(1, eqs,
50 threshold='v > 1', reset='v = -1',
51 method='euler', refractory=1*ms,
52 namespace=parameters)
53
54 plastic_synapse = Synapses(steady_poisson, neuron_with_homeostasis,
55 'w : 1',
56 on_pre='''
57 v_post += w
58 w = clip(w + dw_plus, 0, w_max)
59 ''',
60 on_post='''
61 w = clip(w - dw_minus, 0, w_max)
62 ''', namespace=parameters)
63 plastic_synapse.connect()
64 plastic_synapse.w = parameters['w_initial']
65
66 non_plastic_synapse_neuron_without_homeostasis = Synapses(varying_poisson,
67 neuron_without_homeostasis,
68 'w : 1', on_pre='v_post += w')
69 non_plastic_synapse_neuron_without_homeostasis.connect()
70 non_plastic_synapse_neuron_without_homeostasis.w = 2
71
72 non_plastic_synapse_neuron = Synapses(varying_poisson, neuron_with_homeostasis,
73 'w : 1', on_pre='v_post += w')
74 non_plastic_synapse_neuron.connect()
75 non_plastic_synapse_neuron.w = 2
76
77 M = StateMonitor(neuron_with_homeostasis, 'v', record=True)
78 M2 = StateMonitor(plastic_synapse, 'w', record=True)
79 M_rate_neuron_with_homeostasis = PopulationRateMonitor(neuron_with_homeostasis)
80 M_rate_neuron_without_homeostasis = PopulationRateMonitor(neuron_without_homeostasis)
81
82 duration = 40*second
83 defaultclock.dt = 0.1*ms
84 run(duration, report='text')
85
86 fig, axes = plt.subplots(3, sharex=True)
87
88 axes[0].plot(M2.t/second, M2.w[0], label="homeostatic weight")
89 axes[0].set_ylabel("weight")
90 axes[0].legend()
91
92 # dt is in second
93 dts = np.arange(0., len(varying_stimulus.values)*varying_stimulus.dt, varying_stimulus.dt)
94 x = list(itertools.chain(*zip(dts, dts)))
95 y = list(itertools.chain(*zip(varying_stimulus.values/Hz, varying_stimulus.values/Hz)))
96 axes[1].plot(x, [0] + y[:-1], label="varying stimulus")
97 axes[1].set_ylabel("rate [Hz]")
98 axes[1].legend()
99
100 # in ms
101 smooth_width = 100*ms
102 axes[2].plot(M_rate_neuron_with_homeostasis.t/second,
103 M_rate_neuron_with_homeostasis.smooth_rate(width=smooth_width)/Hz,
104 label="with homeostasis")
105 axes[2].plot(M_rate_neuron_without_homeostasis.t/second,
106 M_rate_neuron_without_homeostasis.smooth_rate(width=smooth_width)/Hz,
107 label="without homeostasis")
108 axes[2].set_ylabel("firing rate [Hz]")
109 axes[2].legend()
110
111 plt.xlabel('Time (s)')
112 plt.show()