3.17. Weight Sharing¶
Shared projections (either using the same set of weights for all postsynaptic neurons, or sharing its weight with another projection) are provided in the module ANNarchy.extensions.weightsharing
. They must be explicitely imported:
from ANNarchy import *
from ANNarchy.extensions.weightsharing import *
3.17.1. Class SharedProjection¶

class
ANNarchy.extensions.weightsharing.
SharedProjection
(pre, post, target, psp='w * pre.r', operation='sum', name=None, copied=False)[source]¶ Projection based on shared weights: each postsynaptic neuron uses the same weights, so they need to be instantiated only once to save memory.
Learning is not possible for now. The
synapse
argument is removed, replaced by a singlepsp
argument to modified what is summed andoperation
to replace the summation operation by maxpooling or similar..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: function to be summed. By default:
w * pre.r
 operation: function applied on
psp
(“sum”, “max”, “min”, “mean”). “sum” is the default.

center
(*args, **kwds)[source]¶ Returns the coordinates in the presynaptic population of the center of the kernel corresponding to the postsynaptic with the given rank or coordinates.
Parameters
 rank: rank or coordinates of the postsynaptic neuron. If only one argument is given, it is a rank. If it is a tuple, it is coordinates.

connectivity_matrix
(fill=0.0)[source]¶ Returns a dense connectivity matrix (2D Numpy array) representing the connections between the pre and postpopulations.
The first index of the matrix represents postsynaptic neurons, the second the presynaptic ones.
If PopulationViews were used for creating the projection, the matrix is expanded to the whole populations by default.
Parameters:
 fill: value to put in the matrix when there is no connection (default: 0.0).

convolve
(weights, delays=0.0, method='convolution', keep_last_dimension=False, multiple=False, padding=0.0, subsampling=None)[source]¶ Builds the shared connection pattern that will perform a convolution of the weights kernel on the presynaptic population.
Depending on the number of dimensions of the pre and postsynaptic populations, as well as the kernel, the convolution can be implemented differentely.
 If the pre and postpopulations have the same dimension as the kernel, the convolution is regular.
 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. For example, filtering a N*M*3 image with a 3D filter (3 elements in the third dimension) results into a 2D population.
 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 position in the last dimension (parallel convolution, useful if the presynaptic population is a stack of feature maps, for example). In this case, you must set
keep_last_dimension
to True.  If the kernel has more dimensions than the presynaptic population, this means a bank of different filters will be applied on the presynaptic population. 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.
Subsampling will be automatically performed according to the populations’ geometry. If these geometries do not match, an error will be thrown. You can force 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:
 weights: Numpy array or list of lists representing the matrix of weights for the filter/kernel.
 delays: delay in synaptic transmission (default: dt). Can only be the same value for all neurons.
 method: defines if the given weights are filterbased (dotproduct between the filter and subregion: ‘filter’) or kernelbased (regular convolution: ‘convolution’).. Default: ‘convolution’.
 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.
 multiple: defines if the weights matrix describes a bank of filters which have to applied in parallel. 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.
 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.

copy
(projection)[source]¶ Creates a virtual connection pattern 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 each projection must have the same geometry.
Parameters:
 projection: the projection to reuse.

load
(filename)[source]¶ Loads the saved state of the projection by Projection.save().
Warning: Matlab data can not be loaded.
Parameters:
 filename: the file name with relative or absolute path.
Example:
proj.load('proj1.npz') proj.load('proj1.txt') proj.load('proj1.txt.gz')

pooling
(delays=0.0, extent=None, overlap=None)[source]¶ Builds the shared connection pattern that will perform a pooling operation over the presynaptic population.
Each postsynaptic neuron is associated to a region of the presynaptic one, over which the result of the operation on firing rates will be assigned to sum(target).
If the SharedProjection does not define an operation, the default is “sum”. If you want maxpooling, you should set it to “max”.
Parameters:
 delays: delays (in ms) in synaptic transmission. Must be a single value for all neurons.
 extent: Extent of the pooling area expressed in the geometry of the presynaptic population. 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.
 overlap: TODO, not implemented yet.

receptive_fields
(variable='w', in_post_geometry=True)[source]¶ Gathers all receptive fields within this projection.
Parameters:
 variable: name of the variable
 in_post_geometry: if False, the data will be plotted as square grid. (default = True)

save
(filename)[source]¶ Saves all information about the projection (connectivity, current value of parameters and variables) into a file.
 If the file name is ‘.npz’, the data will be saved and compressed using np.savez_compressed (recommended).
 If the file name ends with ‘.gz’, the data will be pickled into a binary file and compressed using gzip.
 If the file name is ‘.mat’, the data will be saved as a Matlab 7.2 file. Scipy must be installed.
 Otherwise, the data will be pickled into a simple binary text file using pickle.
Parameter:
 filename: file name, may contain relative or absolute path.
Warning
The ‘.mat’ data will not be loadable by ANNarchy, it is only for external analysis purpose.
Example:
proj.save('proj1.npz') proj.save('proj1.txt') proj.save('proj1.txt.gz') proj.save('proj1.mat')

save_connectivity
(filename)[source]¶ Saves the connectivity of the projection into a file.
Only the connectivity matrix, the weights and delays are saved, not the other synaptic variables.
The generated data can be used to create a projection in another network:
proj.connect_from_file(filename)
 If the file name is ‘.npz’, the data will be saved and compressed using np.savez_compressed (recommended).
 If the file name ends with ‘.gz’, the data will be pickled into a binary file and compressed using gzip.
 If the file name is ‘.mat’, the data will be saved as a Matlab 7.2 file. Scipy must be installed.
 Otherwise, the data will be pickled into a simple binary text file using pickle.
Parameters:
 filename: file name, may contain relative or absolute path.
 pre: presynaptic population (either its name or a