3.17. Convolution and Pooling¶
Convolution and pooling operations are provided in the module ANNarchy.extensions.convolution
. They must be explicitely imported:
from ANNarchy import *
from ANNarchy.extensions.convolution import *
3.17.1. Class Convolution¶

class
ANNarchy.extensions.convolution.
Convolution
(pre, post, target, psp='pre.r * w', operation='sum', name=None, copied=False)[source]¶ Performs a convolution of a weight kernel on the presynaptic population.
Despite its name, the operation performed is actually a crosscorrelation, as is usual in computer vision and convolutional neural networks:
\[g(x) = \sum_{k=n}^n h(k) \, f(x + k)\]The convolution operation benefits from giving a multidimensional geometry to the populations and filters, for example in 2D:
inp = Population(geometry=(100, 100), neuron=Neuron(parameters="r = 0.0")) pop = Population(geometry=(100, 100), neuron=Neuron(equations="r = sum(exc)")) proj = Convolution(inp, pop, 'exc') proj.connect_filter( [ [1., 0., 1.], [1., 0., 1.], [1., 0., 1.] ])
The maximum number of dimensions for populations and filters is 4, an error is thrown otherwise.
Depending on the number of dimensions of the pre and postsynaptic populations, as well as of the kernel, the convolution is implemented differentely.
Method connect_filter()
If the pre and postpopulations have the same dimension as the kernel, the convolution is regular. Example:
(100, 100) * (3, 3) > (100, 100)
If the postpopulation has one dimension less than the presynaptic one, the last dimension of the kernel must match the last one of the presynaptic population. Example:
(100, 100, 3) * (3, 3, 3) > (100, 100)
If the kernel has less dimensions than the two populations, the number of neurons in the last dimension of the populations must be the same. The convolution will be calculated for each feature map in the last dimension. In this case, you must set
keep_last_dimension
toTrue
. Example:(100, 100, 16) * (3, 3) > (100, 100, 16)
Method connect_filters()
If the kernel has more dimensions than the presynaptic population, this means a bank of different filters will be applied on the presynaptic population (like a convolutional layer in a CNN). Attention: the first index of
weights
corresponds to the different filters, while the result will be accessible in the last dimension of the postsynaptic population. You must set themultiple
argument to True. Example:(100, 100) * (16, 3, 3) > (100, 100, 16)
The convolution always uses padding for elements that would be outside the array (no equivalent of
valid
in tensorflow). It is 0.0 by default, but can be changed using thepadding
argument. Settingpadding
to the stringborder
will repeat the value of the border elements.Subsampling will be automatically performed according to the populations’ geometry. If these geometries do not match, an error will be thrown. Example:
(100, 100) * (3, 3) > (50, 50)You can redefine the subsampling by providing a list
subsampling
as argument, defining for each postsynaptic neuron the coordinates of the presynaptic neuron which will be the center of the filter/kernel.Parameters:  pre – presynaptic population (either its name or a
Population
object).  post – postsynaptic population (either its name or a
Population
object).  target – type of the connection
 psp – continuous influence of a single synapse on the postsynaptic neuron (default for ratecoded:
w*pre.r
).  operation – operation (sum, max, min, mean) performed by the kernel (default: sum).

connect_filter
(weights, delays=0.0, keep_last_dimension=False, padding=0.0, subsampling=None)[source]¶ Applies a single filter on the presynaptic population.
Parameters:  weights – numpy array or list of lists representing the matrix of weights for the filter.
 delays – delay in synaptic transmission (default: dt). Can only be the same value for all neurons.
 keep_last_dimension – defines if the last dimension of the pre and postsynaptic will be convolved in parallel. The weights matrix must have one dimension less than the presynaptic population, and the number of neurons in the last dimension of the pre and postsynaptic populations must match. Default: False.
 padding – value to be used for the rates outside the presynaptic population. If it is a floating value, the presynaptic population is virtually extended with this value above its boundaries. If it is equal to ‘border’, the values on the boundaries are repeated. Default: 0.0.
 subsampling – list for each postsynaptic neuron of coordinates in the presynaptic population defining the center of the kernel/filter. Default: None.

connect_filters
(weights, delays=0.0, keep_last_dimension=False, padding=0.0, subsampling=None)[source]¶ Applies a set of different filters on the presynaptic population.
The weights matrix must have one dimension more than the presynaptic populations, and the number of neurons in the last dimension of the postsynaptic population must be equal to the number of filters.
Parameters:  weights – numpy array or list of lists representing the matrix of weights for the filter.
 delays – delay in synaptic transmission (default: dt). Can only be the same value for all neurons.
 keep_last_dimension – defines if the last dimension of the pre and postsynaptic will be convolved in parallel. The weights matrix must have one dimension less than the presynaptic population, and the number of neurons in the last dimension of the pre and postsynaptic populations must match. Default: False.
 padding – value to be used for the rates outside the presynaptic population. If it is a floating value, the presynaptic population is virtually extended with this value above its boundaries. If it is equal to ‘border’, the values on the boundaries are repeated. Default: 0.0.
 subsampling – list for each postsynaptic neuron of coordinates in the presynaptic population defining the center of the kernel/filter. Default: None.
3.17.2. Class Pooling¶

class
ANNarchy.extensions.convolution.
Pooling
(pre, post, target, operation='max', name=None, copied=False)[source]¶ Performs a pooling operation (e.g. max.pooling) on the presynaptic population.
Each postsynaptic neuron covers a specific region (
extent
) of the presynaptic population, over which the result of the operation on firing rates will be assigned to sum(target).The extent is automatically computed using the geometry of the populations, but can be specified in the connect_pooling()` methods.
Example:
inp = Population(geometry=(100, 100), neuron=Neuron(parameters="r = 0.0")) pop = Population(geometry=(50, 50), neuron=Neuron(equations="r = sum(exc)")) proj = Pooling(inp, pop, 'exc', operation='max') # maxpooling proj.connect_pooling() # extent=(2, 2) is implicit
Parameters:  pre – presynaptic population (either its name or a
Population
object).  post – postsynaptic population (either its name or a
Population
object).  target – type of the connection
 operation – pooling function to be applied (“max”, “min”, “mean”)

connect_pooling
(extent=None, delays=0.0)[source]¶ Parameters:  extent – extent of the pooling area expressed in the geometry of the presynaptic population (e.g
(2, 2)
). In each dimension, the product of this extent with the number of neurons in the postsynaptic population must be equal to the number of presynaptic neurons. Default: None.  delays – synaptic delay in ms
 extent – extent of the pooling area expressed in the geometry of the presynaptic population (e.g
 pre – presynaptic population (either its name or a
3.17.3. Class Copy¶

class
ANNarchy.extensions.convolution.
Copy
(pre, post, target, psp='pre.r * w', operation='sum', name=None, copied=False)[source]¶ Creates a virtual projection reusing the weights and delays of an alreadydefined projection.
Although the original projection can be learnable, this one can not. Changes in the original weights will be reflected in this projection. The only possible modifications are
psp
andoperation
.The pre and postsynaptic populations of both projections must have the same geometry.
Example:
proj = Projection(pop1, pop2, "exc") proj.connect_fixed_probability(0.1, 0.5) copy_proj = Copy(pop1, pop3, "exc") copy_proj.connect_copy(proj)
Parameters:  pre – presynaptic population (either its name or a
Population
object).  post – postsynaptic population (either its name or a
Population
object).  target – type of the connection
 psp – continuous influence of a single synapse on the postsynaptic neuron (default for ratecoded:
w*pre.r
).  operation – operation (sum, max, min, mean) performed by the kernel (default: sum).
 pre – presynaptic population (either its name or a