# Using custom synaptic weight matrix

**URL:** <https://brian.discourse.group/t/using-custom-synaptic-weight-matrix/485>\
**Category:** Support\
**Tags:** synapses\
**Created:** [5 October 2021 13:39 UTC](https://brian.discourse.group/t/using-custom-synaptic-weight-matrix/485 "2021-10-05T13:39:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pshah95](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/pshah95/32/262_2.png) [@pshah95](https://brian.discourse.group/u/pshah95)\
**Post date:** [5 October 2021 13:39 UTC](https://brian.discourse.group/t/using-custom-synaptic-weight-matrix/485/1 "2021-10-05T13:39:01Z")

</div>

# Description of problem

Hello, I need to implement custom synaptic weight matrices into my network. I have found a way of doing this using a for loop that goes through all the source and target neurons in the network and sets the synaptic weight directly for each synapse. However, the loop takes a very long time to complete for a 10^3 network size. Is there a faster or more correct way of setting custom synaptic weight matrix throughout the network? Is there a direct method to specify a custom synaptic weight matrix in the Synapses group?

# Minimal code to reproduce problem

```auto
N = 4000 # number of neurons in network

eqs = """
dv/dt = (ge+gi-(v-El))/taum : volt (unless refractory)
dge/dt = -ge/taue : volt
dgi/dt = -gi/taui : volt
"""

P = NeuronGroup(N, eqs, threshold='v>Vt', reset='v = Vr', refractory=5*ms,
                method='exact')

W = mtx # N x N custom connectivity matrix

Ce = Synapses(P, P, 'w_e: volt', on_pre='ge += w_e')
Ce.connect(p=1.0)
for i in range(N):
    for j in range(N):
        msg = 'connecting neurons %s and %s' % (i, j)
        print(msg, end='\r')
        Ce.w_e[i, j] = W[i,j]*mV

```

Thank you for any advice you might have.

Prajay Shah

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/adam/32/197_2.png) [@adam](https://brian.discourse.group/u/adam)\
**Post date:** [5 October 2021 14:36 UTC](https://brian.discourse.group/t/using-custom-synaptic-weight-matrix/485/2 "2021-10-05T14:36:27Z")

</div>

Hi Prajay,

I’m a brian amateur, so I don’t know in advance whether or not this will be faster, but could you try the following:  
[from the documentation on synapses](https://brian2.readthedocs.io/en/stable/user/synapses.html#synaptic-connection-weight-matrices)

```auto
sources, targets = W.nonzero()
Ce_synapses = Synapses(P, P, 'w_e: volt', on_pre='ge += w_e')
Ce_synapses.connect(i=sources, j=targets)

```

at the very least this will mean only the nonzero elements of W will actually be connected (instead of some being connected with weight 0)

if the nonzero elements of W have different values from each other you’ll need some way of setting those weight values individually, perhaps making creative use of the string specification for w [(see distance-dependent weight here)](https://brian2.readthedocs.io/en/stable/resources/tutorials/2-intro-to-brian-synapses.html), but if they’re all the same you could do something like

```auto
w_val = 1.0 #or whatever the value. is
Ce_synapses.w_e = w_val * mV

```

as another practical note, those print statements are likely to add a lot of time! simply removing those (or having the option to turn them off when not debugging) might be sufficient to solve your problem  
-Adam

---

<div class="post-metadata">

**Author:** ![pshah95](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/pshah95/32/262_2.png) [@pshah95](https://brian.discourse.group/u/pshah95)\
**Post date:** [5 October 2021 14:55 UTC](https://brian.discourse.group/t/using-custom-synaptic-weight-matrix/485/3 "2021-10-05T14:55:29Z")

</div>

Hi Adam,

Thanks for your tips!

I have looked into the .connect documentation, and the issue is that it only allows for binary 1 or 0 connections between sources and targets.

I have a weight matrix that has a continuous distribution of synaptic weights values. My key constraint is that I have a very specific method of building the synaptic connectivity weight matrix (I take the outer product of two vectors), so I can’t think of any string specification for w that would allow me to build the matrix I need.

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/adam/32/197_2.png) [@adam](https://brian.discourse.group/u/adam)\
**Post date:** [5 October 2021 16:24 UTC](https://brian.discourse.group/t/using-custom-synaptic-weight-matrix/485/4 "2021-10-05T16:24:06Z")

</div>

I wonder… would

```auto
Ce_synapses.w_e = 'W[i,j]'

```

work? (you may need to multiply W by mV in advance to get the units right)

---

<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:** [5 October 2021 18:11 UTC](https://brian.discourse.group/t/using-custom-synaptic-weight-matrix/485/5 "2021-10-05T18:11:15Z")

</div>

Hi @pshah95 . Unfortunately we only mention the other direction (getting a weight matrix out of Brian’s internal structure) in the documentation, but if you have a fully-connected network, then using a weight matrix to set the weights is actually quite simple. After connecting things you can use:

```auto
Ce.w[:] = W.flatten()*mV

```

This works because the internal ordering of synapses is exactly the same as for a flattened matrix.

Hope that helps, best  
Marcel

PS: It is doing exactly the same thing, but the more “canonical” way of asking for an all-to-all connectivity would be `Ce.connect(True)` or `Ce.connect()` instead of `Ce.connet(p=1.0)`.

---

<div class="post-metadata">

**Author:** ![pshah95](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/pshah95/32/262_2.png) [@pshah95](https://brian.discourse.group/u/pshah95)\
**Post date:** [5 October 2021 18:50 UTC](https://brian.discourse.group/t/using-custom-synaptic-weight-matrix/485/6 "2021-10-05T18:50:23Z")

</div>

Thanks @mstimberg ! This is working great.
