I have expensive summed variable code running on synapses that I only need at spike time. I’ve tried leaving the variable undefined in model and updating it in pre, but I get
`ValueError: Equations of type 'parameter' cannot have a flag 'summed', only the following flags are allowed: ['constant', 'shared']
This is the code I wished could work
import brian2.only as b2
from brian2 import np
ng = b2.NeuronGroup(10, 'x_sum : 1', threshold='rand() < 0.1', reset='')
syn = b2.Synapses(ng, ng, 'x_sum_post : 1 (summed)', on_pre='x_sum_post = rand()')
syn.connect()
mon = b2.StateMonitor(ng, 'x_sum', record=True)
net = b2.Network(ng, syn, mon)
net.run(10 * b2.ms)
mon.x_sum
I’ve also tried putting the (summed) in the on-pre statements. Also setting x_sum to 0 in model, but updating in on_pre, which doesn’t do the summing.
Hi @kjohnsen. An on_pre statement that adds something to a post-synaptic variable is almost equivalent to an event-based summed variable – the only thing that is missing is to set the variable to 0 before starting the summing. The most simple general solution would be to use run_regularly at every time step for this – this is a bit wasteful since it only needs to be set to 0 on events, but I don’t think that slows down things a lot. So in your example, this would look like:
ng = b2.NeuronGroup(10, 'x_sum : 1', threshold='rand() < 0.1', reset='')
ng.run_regulary("x_sum = 0", when="start")
syn = b2.Synapses(ng, ng, on_pre='x_sum_post += rand()')
syn.connect()
Actually, in this example, the group ng is both the source and target of the synapse, so you could also use its reset statement to set x_sum to 0. You’d have to be careful with the order of operations, though, depending on what you want to do with x_sum (see Running a simulation — Brian 2 0.0.post128 documentation).
Thank you! This makes sense: reset the variable to 0 in a neuron_group reset (or other on_event statement) then sum up in the synapse on_pre, making sure to schedule in the right order. My weight training procedure broke when I tried this, but I don’t have time to figure out why. For now I have things working simply with the summed variable updating every step, even if it’s a bit more expensive.
1 Like