Debian Codegraph @

examples/synapses/efficient_gaussian_connectivity.py

raw
1 """
2 An example of turning an expensive `Synapses.connect` operation into
3 three cheap ones using a mathematical trick.
4
5 Consider the connection probability between neurons i and j given by
6 the Gaussian function :math:`p=e^{-\alpha(i-j)^2}` (for some constant
7 :math:`\alpha`). If we want to connect neurons with this probability,
8 we can very simply do::
9
10 S.connect(p='exp(-alpha*(i-j)**2)')
11
12 However, this has a problem. Although we know that this will create
13 :math:`O(N)` synapses if N is the number of neurons, because we
14 have specified ``p`` as a function of i and j, we have to evaluate
15 ``p(i, j)`` for every pair ``(i, j)``, and therefore it takes
16 :math:`O(N^2)` operations.
17
18 Our first option is to take a cutoff, and say that if :math:`p<q` for some
19 small :math:`q`, then we assume that :math:`p\approx 0`. We can work out
20 which j values are compatible with a given value of i by solving
21 :math:`e^{-\alpha(i-j)^2}<q` which gives
22 :math:`|i-j|<\sqrt{-\log(q)/\alpha)}=w`. Now we implement the rule
23 using the generator syntax to only search for values between ``i-w``
24 and ``i+w``, except that some of these values will be outside the
25 valid range of values for j so we set ``skip_if_invalid=True``.
26 The connection code is then::
27
28 S.connect(j='k for k in range(i-w, i+w) if rand()<exp(-alpha*(i-j)**2)',
29 skip_if_invalid=True)
30
31 This is a lot faster (see graph labelled "Limited" for this algorithm).
32
33 However, it may be a problem that we have to specify a cutoff and so
34 we will lose some synapses doing this: it won't be mathematically exact.
35 This isn't a problem for the Gaussian because w grows very slowly with
36 the cutoff probability q, but for other probability distributions with
37 more weight in the tails, it could be an issue.
38
39 If we want to be exact, we can still do a big improvement. For the
40 case :math:`i-w\leq j\leq i+w` we use the same connection code, but
41 we also handle the case :math:`|i-j|>w`. This time, we note that we
42 want to create a synapse with probability :math:`p(i-j)` and we can
43 rewrite this as :math:`p(i-j)/p(w)\cdot p(w)`. If :math:`|i-j|>w`
44 then this is a product of two probabilities :math:`p(i-j)/p(w)`
45 and :math:`p(w)`. So in the region :math:`|i-j|>w` a synapse
46 will be created if two random events both occur, with these
47 two probabilities. This might seem a little strange until you
48 notice that one of the two probabilities :math:`p(w)` doesn't
49 depend on i or j. This lets us use the much more efficient
50 ``sample`` algorithm to generate a set of candidate ``j`` values,
51 and then add the additional test ``rand()<p(i-j)/p(w)``. Here's the
52 code for that::
53
54 w = int(ceil(sqrt(log(q)/-0.1)))
55 S.connect(j='k for k in range(i-w, i+w) if rand()<exp(-alpha*(i-j)**2)',
56 skip_if_invalid=True)
57 pmax = exp(-0.1*w**2)
58 S.connect(j='k for k in sample(0, i-w, p=pmax) if rand()<exp(-alpha*(i-j)**2)/pmax',
59 skip_if_invalid=True)
60 S.connect(j='k for k in sample(i+w, N_post, p=pmax) if rand()<exp(-alpha*(i-j)**2)/pmax',
61 skip_if_invalid=True)
62
63 This "Divided" method is also much faster than the naive method,
64 and is mathematically correct. Note though that this method is still
65 :math:`O(N^2)` but the constants are much, much smaller and this
66 will usually be sufficient. It is possible to take the ideas
67 developed here even further and get even better scaling, but in
68 most cases it's unlikely to be worth the effort.
69
70 The code below shows these examples written out, along with
71 some timing code and plots for different values of N.
72 """
73 from brian2 import *
74 import time
75
76 def naive(N):
77 G = NeuronGroup(N, 'v:1', threshold='v>1', name='G')
78 S = Synapses(G, G, on_pre='v += 1', name='S')
79 S.connect(p='exp(-0.1*(i-j)**2)')
80
81 def limited(N, q=0.001):
82 G = NeuronGroup(N, 'v:1', threshold='v>1', name='G')
83 S = Synapses(G, G, on_pre='v += 1', name='S')
84 w = int(ceil(sqrt(log(q)/-0.1)))
85 S.connect(j='k for k in range(i-w, i+w) if rand()<exp(-0.1*(i-j)**2)', skip_if_invalid=True)
86
87 def divided(N, q=0.001):
88 G = NeuronGroup(N, 'v:1', threshold='v>1', name='G')
89 S = Synapses(G, G, on_pre='v += 1', name='S')
90 w = int(ceil(sqrt(log(q)/-0.1)))
91 S.connect(j='k for k in range(i-w, i+w) if rand()<exp(-0.1*(i-j)**2)', skip_if_invalid=True)
92 pmax = exp(-0.1*w**2)
93 S.connect(j='k for k in sample(0, i-w, p=pmax) if rand()<exp(-0.1*(i-j)**2)/pmax', skip_if_invalid=True)
94 S.connect(j='k for k in sample(i+w, N_post, p=pmax) if rand()<exp(-0.1*(i-j)**2)/pmax', skip_if_invalid=True)
95
96 def repeated_run(f, N, repeats):
97 start_time = time.time()
98 for _ in range(repeats):
99 f(N)
100 end_time = time.time()
101 return (end_time-start_time)/repeats
102
103 N = array([100, 500, 1000, 5000, 10000, 20000])
104 repeats = array([100, 10, 10, 1, 1, 1])*3
105 naive(10)
106 limited(10)
107 divided(10)
108 print('Starting naive')
109 loglog(N, [repeated_run(naive, n, r) for n, r in zip(N, repeats)],
110 label='Naive', lw=2)
111 print('Starting limit')
112 loglog(N, [repeated_run(limited, n, r) for n, r in zip(N, repeats)],
113 label='Limited', lw=2)
114 print('Starting divided')
115 loglog(N, [repeated_run(divided, n, r) for n, r in zip(N, repeats)],
116 label='Divided', lw=2)
117 xlabel('N')
118 ylabel('Time (s)')
119 legend(loc='best', frameon=False)
120 show()