# How to efficiently modify synnaptic weights for large networks

**URL:** <https://brian.discourse.group/t/how-to-efficiently-modify-synnaptic-weights-for-large-networks/926>\
**Category:** Support\
**Tags:** performance, synapses\
**Created:** [2 March 2023 18:00 UTC](https://brian.discourse.group/t/how-to-efficiently-modify-synnaptic-weights-for-large-networks/926 "2023-03-02T18:00:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nspiller](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/nspiller/32/536_2.png) [@nspiller](https://brian.discourse.group/u/nspiller)\
**Post date:** [2 March 2023 18:00 UTC](https://brian.discourse.group/t/how-to-efficiently-modify-synnaptic-weights-for-large-networks/926/1 "2023-03-02T18:00:28Z")

</div>

Hi

I am working with a large network model with more than 1e6 synapses. I now want to silence neurons by setting some of those connections to zero.

I am trying this via `S.w[i, :] = 0`, where `S` is the `Synapses()` object and `i` the index for the neuron to be silenced.

However, this approach takes really long (I interrupted my code after about 1 h). Is there some more efficient way to address synaptic weights or silence a neuron?

Thanks a lot for your help 🙂

---

<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:** [2 March 2023 19:56 UTC](https://brian.discourse.group/t/how-to-efficiently-modify-synnaptic-weights-for-large-networks/926/2 "2023-03-02T19:56:11Z")

</div>

Hi @nspiller. Just to make sure I understand correctly, what exactly is taking long – the code that sets the weights to 0, or running a simulation where many weights are 0?

---

<div class="post-metadata">

**Author:** ![nspiller](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/nspiller/32/536_2.png) [@nspiller](https://brian.discourse.group/u/nspiller)\
**Post date:** [2 March 2023 22:04 UTC](https://brian.discourse.group/t/how-to-efficiently-modify-synnaptic-weights-for-large-networks/926/3 "2023-03-02T22:04:42Z")

</div>

Hi @mstimberg,

setting the weight to 0, exactly this line of code: `S.w[i, :] = 0`.

Sorry, I did not write that clearly 🙂

---

<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:** [3 March 2023 10:21 UTC](https://brian.discourse.group/t/how-to-efficiently-modify-synnaptic-weights-for-large-networks/926/4 "2023-03-03T10:21:21Z")

</div>

Ok, thanks. I guess it is not literally a single line like this, but rather many such lines, e.g. in a loop? I tried the following code with 1e6 synapses, and setting the weight to 0 for 100 neurons (i.e. 10% of all synapses):

```python
from brian2 import *

source = NeuronGroup(1000, '')
target = NeuronGroup(1000, '')

synapses = Synapses(source, target, 'w : 1')
synapses.connect()
synapses.w = 'rand()' # Not really necessary, but makes it more realistic

for idx in range(100):
    print(idx)
    synapses.w[idx, :] = 0

```

It is indeed not fast, but not _that_ slow either – it takes about 0.4s per iteration, so in total about 40s. The 2-dimensional indexing into synapses is a very general mechanism, though, and it is not optimized for special cases like using `:` for one of the dimensions (we should probably do that!). Using the general “string condition” mechanism is much faster, actually, e.g. if I replace it by

```python
for idx in range(100):
    print(idx)
    synapses.w["idx == i"] = 0

```

it runs in 0.5s _in total_! Finally, getting rid of the loop can speed up things even more, e.g. the above can be replaced by

```python
synapses.w['i < 100'] = 0

```

which basically reduces the speed to that of a single iteration (~0.005s).

Hope that helps!

---

<div class="post-metadata">

**Author:** ![nspiller](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/nspiller/32/536_2.png) [@nspiller](https://brian.discourse.group/u/nspiller)\
**Post date:** [13 March 2023 14:25 UTC](https://brian.discourse.group/t/how-to-efficiently-modify-synnaptic-weights-for-large-networks/926/6 "2023-03-13T14:25:51Z")

</div>

Hi @mstimberg,

thank you so much for the answer. Using this was sufficiently fast for my case:

```
synapses.w["idx == i"] = 0

```

A bit late, but here is an MWE and profiling for my issue:

```
from brian2 import NeuronGroup, Synapses
import numpy as np
from timeit import timeit

N_neu = int(1e5)
N_syn = int(1e6)

neu = NeuronGroup(N_neu, '')
syn = Synapses(neu, neu, 'w : 1')

idx_i = np.random.choice(range(N_neu), size=N_syn)
idx_j = np.random.choice(range(N_neu), size=N_syn)

syn.connect(i=idx_i, j=idx_j)
syn.w = 'rand()'

```

Using the string expression (100 runs):

```
def fun():
    syn.w["0 == i"] = 0

timeit(fun, number=100)

>> 0.5920858001336455

```

Using numpy-like syntax (1 run):

```
def fun():
    syn.w[0, :] = 0

timeit(fun, number=1)

>> 62.030049999943

```

All the best
