# 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 pre-synaptic population.

Despite its name, the operation performed is actually a cross-correlation, 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 multi-dimensional 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 post-synaptic populations, as well as of the kernel, the convolution is implemented differentely.

Method connect_filter()

• If the pre- and post-populations have the same dimension as the kernel, the convolution is regular. Example:

(100, 100) * (3, 3) -> (100, 100)

• If the post-population has one dimension less than the pre-synaptic one, the last dimension of the kernel must match the last one of the pre-synaptic 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 to True. Example:

(100, 100, 16) * (3, 3) -> (100, 100, 16)

Method connect_filters()

• If the kernel has more dimensions than the pre-synaptic population, this means a bank of different filters will be applied on the pre-synaptic 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 post-synaptic population. You must set the multiple 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 the padding argument. Setting padding to the string border will repeat the value of the border elements.

Sub-sampling 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 sub-sampling by providing a list subsampling as argument, defining for each post-synaptic neuron the coordinates of the pre-synaptic neuron which will be the center of the filter/kernel.

Parameters: pre – pre-synaptic population (either its name or a Population object). post – post-synaptic population (either its name or a Population object). target – type of the connection psp – continuous influence of a single synapse on the post-synaptic neuron (default for rate-coded: 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 pre-synaptic 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 post-synaptic will be convolved in parallel. The weights matrix must have one dimension less than the pre-synaptic population, and the number of neurons in the last dimension of the pre- and post-synaptic populations must match. Default: False. padding – value to be used for the rates outside the pre-synaptic population. If it is a floating value, the pre-synaptic 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 post-synaptic neuron of coordinates in the pre-synaptic 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 pre-synaptic population.

The weights matrix must have one dimension more than the pre-synaptic populations, and the number of neurons in the last dimension of the post-synaptic 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 post-synaptic will be convolved in parallel. The weights matrix must have one dimension less than the pre-synaptic population, and the number of neurons in the last dimension of the pre- and post-synaptic populations must match. Default: False. padding – value to be used for the rates outside the pre-synaptic population. If it is a floating value, the pre-synaptic 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 post-synaptic neuron of coordinates in the pre-synaptic population defining the center of the kernel/filter. Default: None.
connectivity_matrix(fill=0.0)[source]

Not available.

load(filename)[source]

Not available.

receptive_fields(variable='w', in_post_geometry=True)[source]

Not available.

save(filename)[source]

Not available.

save_connectivity(filename)[source]

Not available.

## 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 pre-synaptic population.

Each post-synaptic neuron covers a specific region (extent) of the pre-synaptic 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') # max-pooling
proj.connect_pooling() # extent=(2, 2) is implicit

Parameters: pre – pre-synaptic population (either its name or a Population object). post – post-synaptic 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 pre-synaptic population (e.g (2, 2)). In each dimension, the product of this extent with the number of neurons in the post-synaptic population must be equal to the number of pre-synaptic neurons. Default: None. delays – synaptic delay in ms
connectivity_matrix(fill=0.0)[source]

Not available.

load(filename)[source]

Not available.

receptive_fields(variable='w', in_post_geometry=True)[source]

Not available.

save(filename)[source]

Not available.

save_connectivity(filename)[source]

Not available.

## 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 already-defined 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 and operation.

The pre- and post-synaptic 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 – pre-synaptic population (either its name or a Population object). post – post-synaptic population (either its name or a Population object). target – type of the connection psp – continuous influence of a single synapse on the post-synaptic neuron (default for rate-coded: w*pre.r). operation – operation (sum, max, min, mean) performed by the kernel (default: sum).
connect_copy(projection)[source]
Parameters: projection – Existing projection to copy.
connectivity_matrix(fill=0.0)[source]

Not available.

generate_omp()[source]

Code generation of CopyProjection object for the openMP paradigm.

load(filename)[source]

Not available.

receptive_fields(variable='w', in_post_geometry=True)[source]

Not available.

save(filename)[source]

Not available.

save_connectivity`(filename)[source]

Not available.