# Memristor stdp rule

**URL:** <https://brian.discourse.group/t/memristor-stdp-rule/906>\
**Category:** Science, Projects, and Showcases\
**Tags:** synapses, equations\
**Created:** [14 February 2023 10:33 UTC](https://brian.discourse.group/t/memristor-stdp-rule/906 "2023-02-14T10:33:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![serge25](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@serge25](https://brian.discourse.group/u/serge25)\
**Post date:** [14 February 2023 10:33 UTC](https://brian.discourse.group/t/memristor-stdp-rule/906/1 "2023-02-14T10:33:36Z")

</div>

Hello, everybody! Can anybody help me with understanding of implementation of memristor-like stdp rule at Brian2.

 ![Selection_005](https://global.discourse-cdn.com/free1/uploads/brian/original/1X/4833ca9f21215342b7f05eff430d2b614ca5db99.png)

Link to the article with equations [Modeling the Dynamics of Spiking Networks with  
Memristor-Based STDP to Solve Classification Tasks](https://www.mdpi.com/2227-7390/9/24/3237)

Dear @mstimberg , can you help me with the implementation of this stdp rule in the synapse of the Brian2 simulator.

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [14 February 2023 14:43 UTC](https://brian.discourse.group/t/memristor-stdp-rule/906/2 "2023-02-14T14:43:15Z")

</div>

Hi @serge25. You can use the same approach I mentioned in this comment: [Inhibitory STDP (Borges 2017 equations) - #2 by mstimberg](https://brian.discourse.group/t/inhibitory-stdp-borges-2017-equations/646/2) By referring to the difference between `lastspike_pre` and `lastspike_post`, you get the \Delta t that you can then plug into the equations. Here’s an example (see the other comment for more explications about the code):

```python
from brian2 import *

A_plus = 0.074; A_minus = -0.047
mu_plus = 26.7*ms; mu_minus = -22.3*ms
tau_plus = 9.3*ms; tau_minus = 10.8*ms

G = NeuronGroup(101, '''
spike_when : second (constant)
lastspike : second # remove when model uses refractory
''',
threshold='timestep(t, dt) == timestep(spike_when, dt)') # spike at pre-defined time
G.lastspike = -1e9*second
G.spike_when[0] = 100*ms
G.spike_when[1:] = np.linspace(0, 250, 100)*ms

S = Synapses(G, G, 'w : 1',
# the case of dt < 0
on_pre='''
lastspike_pre = t
delta_t = (lastspike_post - lastspike_pre)
w += A_minus * w * (1 + tanh((delta_t - mu_minus)/tau_minus))
''',
# the case of dt > 0
on_post='''
lastspike_post = t
delta_t = (lastspike_post - lastspike_pre)
w += A_plus * w * (1 + tanh(-(delta_t - mu_plus)/tau_plus))
''',
)
S.connect(i=0, j=np.arange(1, 101)) # Connect first neuron to all other neurons
w_init = 0.4
S.w = w_init # initial value for weights
run(251*ms)

# Plot difference to inital value
plt.plot((G.spike_when[1:] - G.spike_when[0])/ms, S.w[:] - w_init, color='r', label=f"w = {w_init:.2f}")
plt.legend()
plt.axhline(linestyle=':', color='k')
plt.axvline(linestyle=':', color='k')
plt.show()

```

![Figure_1](https://global.discourse-cdn.com/free1/uploads/brian/original/1X/8f8a26f5e93ed82e3e147b1cfe1afd6f5b6ead83.png)

---

<div class="post-metadata">

**Author:** ![serge25](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@serge25](https://brian.discourse.group/u/serge25)\
**Post date:** [16 February 2023 08:54 UTC](https://brian.discourse.group/t/memristor-stdp-rule/906/3 "2023-02-16T08:54:08Z")

</div>

@mstimberg thank you very much.

---

<div class="post-metadata">

**Author:** ![serge25](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@serge25](https://brian.discourse.group/u/serge25)\
**Post date:** [28 February 2023 13:56 UTC](https://brian.discourse.group/t/memristor-stdp-rule/906/4 "2023-02-28T13:56:31Z")

</div>

Dear @mstimberg it is possible and how define lastspike for poisson group (at situation when we have stdp connection between PG and neuron group)?

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [28 February 2023 15:16 UTC](https://brian.discourse.group/t/memristor-stdp-rule/906/5 "2023-02-28T15:16:21Z")

</div>

Hi @serge25 . You cannot define `lastspike` for a `PoissonGroup`, but a `PoissonGroup` is just a convenience for a `NeuronGroup` with `threshold='rand()<rates*dt'`, i.e. the following would work as a replacement:

```python
poisson_group = NeuronGroup(N, ''''rates : Hz (constant)
                                   lastspike : second''',
                            threshold='rand()<rates*dt',
                            reset='lastspike = t')
poisson_group.rates = ...
poisson_group.lastspike = -1e9*second

```
