Skip to content

Convolution and Pooling#

Convolution and pooling operations are provided in the module ANNarchy.extensions.convolution. They must be explicitly imported:

from ANNarchy import *
from ANNarchy.extensions.convolution import *

ANNarchy.extensions.convolution.Convolution #

Bases: SpecificProjection

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.

Source code in ANNarchy/extensions/convolution/Convolve.py
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
class Convolution(SpecificProjection):
    """
    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:

    ```python
    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.
    """

    def __init__(self, pre, post, target, psp="pre.r * w", operation="sum", name=None, copied=False):
        """
        :param pre: pre-synaptic population (either its name or a ``Population`` object).
        :param post: post-synaptic population (either its name or a ``Population`` object).
        :param target: type of the connection
        :param psp: continuous influence of a single synapse on the post-synaptic neuron (default for rate-coded: ``w*pre.r``).
        :param operation: operation (sum, max, min, mean) performed by the kernel (default: sum).
        """
        # Create the description, but it will not be used for generation
        SpecificProjection.__init__(
            self,
            pre,
            post,
            target,
            synapse=SharedSynapse(psp=psp, operation=operation, name="Convolution operation", description="Convoluted kernel over the pre-synaptic population."),
            name=name,
            copied=copied
        )

        # Disable saving
        self._saveable = False

        # For copy
        self._used_single_filter = False
        self._used_bank_of_filters = False
        self.operation = operation

    @property
    def weights(self):
        if not self.initialized:
            return self.init["weights"]
        else:
            return np.array(self.cyInstance.get_w())

    @weights.setter
    def weights(self, value):
        if not self.initialized:
            self.init["weights"]=value
        else:
            if self.dim_kernel != value.ndim:
                raise AttributeError("Mismatch between filter dimensions")

            self.cyInstance.set_w(value)

    def connect_filter(self, weights, delays=0.0, keep_last_dimension=False, padding=0.0, subsampling=None):
        """
        Applies a single filter on the pre-synaptic population.

        :param weights: numpy array or list of lists representing the matrix of weights for the filter.
        :param delays: delay in synaptic transmission (default: dt). Can only be the same value for all neurons.
        :param 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.
        :param 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.
        :param subsampling: list for each post-synaptic neuron of coordinates in the pre-synaptic population defining the center of the kernel/filter. Default: None.
        """

        # Process the weights
        self.weights = np.array(weights)

        # Process the delays
        self.delays = float(delays)
        if not isinstance(delays, (int, float)):
            Global._error('Convolutions can only have constant delays.')

        self.subsampling = subsampling
        self.keep_last_dimension = keep_last_dimension
        self.padding = padding
        self.multiple = False

        # Check dimensions of populations and weight matrix
        self.dim_kernel = self.weights.ndim
        self.dim_pre = self.pre.dimension
        self.dim_post = self.post.dimension

        if self.dim_post > 4:
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: Too many dimensions for the post-synaptic population (maximum 4).')

        if self.dim_pre > 4:
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: Too many dimensions for the pre-synaptic population (maximum 4).')

        if self.dim_kernel > 5  or (not self.multiple and self.dim_kernel > 4):
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: Too many dimensions for the kernel (maximum 4).')

        # Check if the last axes match for parallel convolution (e.g. 3-2-3)
        if self.dim_kernel < self.dim_pre:
            if not self.keep_last_dimension:
                print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
                Global._error('Convolution: If the kernel has less dimensions than the pre-synaptic population, you need to set the flag keep_last_dimension to True.')

            if self.pre.geometry[-1] != self.post.geometry[-1]:
                print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
                Global._error('Convolution: If the kernel has fewer dimensions than the two populations (keep_last_dimension=True), these must have the same number of neurons in the last dimension.')

        # If the last dim of the kernel matches the last dim of the pre-pop, the last pop can have one dimension less.
        if self.dim_post < self.dim_pre: # OK, but check the last dimension of the kernel has the same size as the post-population
            if self.weights.shape[-1] != self.pre.geometry[-1]:
                print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
                Global._error('Convolution: If the post-synaptic population has less dimensions than the pre-synaptic one, the last dimension of the filter must be equal to the last of the pre-synaptic population.')

        # Check if it is a bank of filters
        if self.dim_kernel > self.dim_pre:
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: If the kernel has more dimensions than the pre-synaptic population, you need to use the connect_filters() method.')


        # Generate the pre-synaptic coordinates
        self._generate_pre_coordinates()

        # Finish building the synapses
        self._create()

        # For copy
        self._used_single_filter = True

        return self


    def connect_filters(self, weights, delays=0.0, keep_last_dimension=False, padding=0.0, subsampling=None):
        """
        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.


        :param weights: numpy array or list of lists representing the matrix of weights for the filter.
        :param delays: delay in synaptic transmission (default: dt). Can only be the same value for all neurons.
        :param 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.
        :param 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.
        :param subsampling: list for each post-synaptic neuron of coordinates in the pre-synaptic population defining the center of the kernel/filter. Default: None.
        """

        # Process the weights
        self.weights = np.array(weights)

        # Process the delays
        self.delays = float(delays)
        if not isinstance(delays, (int, float)):
            Global._error('Convolutions can only have constant delays.')

        self.subsampling = subsampling
        self.keep_last_dimension = keep_last_dimension
        self.padding = padding
        self.multiple = True

        # Check dimensions of populations and weight matrix
        self.dim_kernel = self.weights.ndim
        self.dim_pre = self.pre.dimension
        self.dim_post = self.post.dimension


        if self.dim_post > 4:
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: Too many dimensions for the post-synaptic population (maximum 4).')

        if self.dim_pre > 4:
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: Too many dimensions for the pre-synaptic population (maximum 4).')

        if self.dim_kernel > 5  or (not self.multiple and self.dim_kernel > 4):
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: Too many dimensions for the kernel (maximum 4).')

        # Check if the last axes match for parallel convolution (e.g. 3-2-3)
        if self.dim_kernel < self.dim_pre:
            if not self.keep_last_dimension:
                print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
                Global._error('Convolution: If the kernel has less dimensions than the pre-synaptic population, you need to set the flag keep_last_dimension to True.')

            if self.pre.geometry[-1] != self.post.geometry[-1]:
                print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
                Global._error('Convolution: If the kernel has fewer dimensions than the two populations (keep_last_dimension=True), these must have the same number of neurons in the last dimension.')

        # If the last dim of the kernel matches the last dim of the pre-pop, the last pop can have one dimension less.
        if self.dim_post < self.dim_pre: # OK, but check the last dimension of the kernel has the same size as the post-population
            if self.weights.shape[-1] != self.pre.geometry[-1]:
                print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
                Global._error('Convolution: If the post-synaptic population has less dimensions than the pre-synaptic one, the last dimension of the filter must be equal to the last of the pre-synaptic population.')

        # The last dimension of the post population must correspond to the number of filters
        if self.weights.shape[0] != self.post.geometry[-1]:
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: For multiple filters, the last dimension of the post-synaptic population must have as many neurons as there are filters.')

        # Generate the pre-synaptic coordinates
        self._generate_pre_coordinates_bank()

        # Finish building the synapses
        self._create()

        # For copy
        self._used_bank_of_filters = True

        return self

    def _copy(self, pre, post):
        "Returns a copy of the projection when creating networks.  Internal use only."
        copied_proj = Convolution(pre=pre, post=post, target=self.target,
                                  psp=self.synapse_type.psp, operation=self.operation,
                                  name=self.name, copied=True)

        copied_proj.delays = self.delays
        copied_proj.weights = self.weights

        copied_proj.subsampling = self.subsampling
        copied_proj.keep_last_dimension = self.keep_last_dimension
        copied_proj.padding = self.padding
        copied_proj.multiple = self.multiple
        copied_proj.dim_kernel = self.weights.ndim
        copied_proj.dim_pre = self.pre.dimension
        copied_proj.dim_post = self.post.dimension

        if self._used_single_filter:
            copied_proj._generate_pre_coordinates()
        elif self._used_bank_of_filters:
            copied_proj._generate_pre_coordinates_bank()
        else:
            raise ValueError("Either use single filter or bank of filter must be True! (Missing connect?)")

        copied_proj._create()
        copied_proj._connection_method = self._connection_method
        copied_proj._connection_args = self._connection_args
        copied_proj._connection_delay = self._connection_delay
        copied_proj._storage_format = self._storage_format
        return copied_proj

    def _create(self):
        # create fake LIL object, just for compilation.
        try:
            from ANNarchy.core.cython_ext.Connector import LILConnectivity
        except Exception as e:
            Global._print(e)
            Global._error('ANNarchy was not successfully installed.')

        lil = LILConnectivity()
        lil.max_delay = self.delays
        lil.uniform_delay = self.delays
        self.connector_name = "Convolution"
        self.connector_description = "Convolution"
        self._store_connectivity(self._load_from_lil, (lil, ), self.delays, storage_format="lil", storage_order="post_to_pre")

    ################################
    ### Create connection pattern
    ################################
    def _connect(self, module):
        """
        Builds up dendrites either from list or dictionary. Called by instantiate().
        """
        if not self._connection_method:
            Global._error('Convolution: The projection between ' + self.pre.name + ' and ' + self.post.name + ' is declared but not connected.')

        # Create the Cython instance
        proj = getattr(module, 'proj'+str(self.id)+'_wrapper')
        self.cyInstance = proj(self.pre_coordinates, self.weights)

        # Set delays after instantiation
        if self.delays > 0.0:
            self.cyInstance.set_delay(self.delays/Global.config['dt'])

        return True

    def _generate_pre_coordinates(self):
        " Returns a list for each post neuron of the corresponding center coordinates."

        # Check if the list is already defined:
        if self.subsampling:
            try:
                shape = np.array(self.subsampling).shape
            except:
                Global._error('Convolution: The sub-sampling list must have', self.post.size, 'elements of size', self.pre.dimension)
                return
            if shape != (self.post.size, self.pre.dimension):
                Global._error('Convolution: The sub-sampling list must have', self.post.size, 'elements of size', self.pre.dimension)
                return
            self.pre_coordinates = self.subsampling
            return

        # Otherwise create it, possibly with sub-sampling
        coords = [[] for i in range(self.post.size)]

        # Compute pre-indices
        idx_range= []
        for dim in range(self.dim_pre):
            if dim < self.dim_post:
                pre_size = int(self.pre.geometry[dim])
                post_size = int(self.post.geometry[dim])
                sample = int(pre_size/post_size)
                if post_size * sample != pre_size:
                    Global._error('Convolution: The pre-synaptic dimensions must be a multiple of the post-synaptic ones for down-sampling to work.')

                idx_range.append([int((sample-1)/2) + sample * i for i in range(post_size)])
            else: # extra dimension
                if self.keep_last_dimension:
                    idx_range.append(range(self.post.geometry[dim]))
                else:
                    idx_range.append([self._center_filter(self.weights.shape[dim])])

        # Generates coordinates TODO: Find a more robust way!
        if self.dim_pre == 1 :
            rk = 0
            for i in idx_range[0]:
                coords[rk] = [i]
                rk += 1
        elif self.dim_pre == 2 :
            rk = 0
            for i in idx_range[0]:
                for j in idx_range[1]:
                    coords[rk] = [i, j]
                    rk += 1
        elif self.dim_pre == 3 :
            rk = 0
            for i in idx_range[0]:
                for j in idx_range[1]:
                    for k in idx_range[2]:
                        coords[rk] = [i, j, k]
                        rk += 1
        elif self.dim_pre == 4 :
            rk = 0
            for i in idx_range[0]:
                for j in idx_range[1]:
                    for k in idx_range[2]:
                        for l in idx_range[3]:
                            coords[rk] = [i, j, k, l]
                            rk += 1

        # Save the result
        self.pre_coordinates = coords

    def _generate_pre_coordinates_bank(self):
        " Returns a list for each post neuron of the corresponding center coordinates, when the filter is a bank."

        self.nb_filters = self.weights.shape[0]
        self.dim_single_filter = self.weights.shape[1:]

        # Check if the list is already defined:
        if self.subsampling:
            try:
                shape = np.array(self.subsampling).shape
            except:
                Global._error('Convolution: The sub-sampling list must have', self.post.size / self.post.geometry[-1], 'elements of size', self.pre.dimension)
                return
            if shape != (self.post.size/ self.post.geometry[-1], self.pre.dimension):
                Global._error('Convolution: The sub-sampling list must have', self.post.size/ self.post.geometry[-1], 'elements of size', self.pre.dimension)
                return
            self.pre_coordinates = [c + [d] for c in self.subsampling  for d  in range(self.nb_filters)]
            return

        # Otherwise create it, possibly with sub-sampling
        coords = [[] for i in range(self.post.size)]

        # Compute pre-indices
        idx_range= []
        for dim in range(self.dim_pre):
            if dim < self.dim_post -1:
                pre_size = self.pre.geometry[dim]
                post_size = self.post.geometry[dim]
                sample = int(pre_size/post_size)
                if post_size * sample != pre_size:
                    Global._error('Convolution: The pre-synaptic dimensions must be a multiple of the post-synaptic ones for down-sampling to work.')

                idx_range.append([int((sample-1)/2) + sample * i for i in range(post_size)])
            else: # extra dimension
                if self.keep_last_dimension:
                    idx_range.append(range(self.post.geometry[dim]))
                else:
                    idx_range.append([self._center_filter(self.weights.shape[dim+1])])


        # Generates coordinates TODO: Find a more robust way!
        if self.dim_pre == 1 :
            rk = 0
            for i in idx_range[0]:
                for d in range(self.nb_filters):
                    coords[rk] = [i, d]
                    rk += 1
        elif self.dim_pre == 2 :
            rk = 0
            for i in idx_range[0]:
                for j in idx_range[1]:
                    for d in range(self.nb_filters):
                        coords[rk] = [i, j, d ]
                        rk += 1
        elif self.dim_pre == 3 :
            rk = 0
            for i in idx_range[0]:
                for j in idx_range[1]:
                    for k in idx_range[2]:
                        for d in range(self.nb_filters):
                            coords[rk] = [i, j, k, d]
                            rk += 1
        elif self.dim_pre == 4 :
            rk = 0
            for i in idx_range[0]:
                for j in idx_range[1]:
                    for k in idx_range[2]:
                        for l in idx_range[3]:
                            for d in range(self.nb_filters):
                                coords[rk] = [i, j, k, l, d]
                                rk += 1

        # Save the result
        self.pre_coordinates = coords

    ################################
    # Code generation
    ################################
    def _generate(self):
        """
        Overrides default code generation. This function is called during the code generation procedure.
        """
        # Filter definition
        filter_definition, filter_pyx_definition = self._filter_definition()

        # On CPUs we have a pre-load on the inner-most sub-vector
        use_inner_line = Global._check_paradigm("openmp")

        # Convolve_code
        if not self.multiple:
            convolve_code, sum_code = self._generate_convolve_code(pre_load_inner_line=use_inner_line)
        else:
            convolve_code, sum_code = self._generate_bank_code(pre_load_inner_line=use_inner_line)

        if Global._check_paradigm("openmp"):
            self._generate_omp(filter_definition, filter_pyx_definition, convolve_code, sum_code)
        elif Global._check_paradigm("cuda"):
            self._generate_cuda(filter_definition, filter_pyx_definition, convolve_code, sum_code)
        else:
            raise NotImplementedError

    def _generate_omp(self, filter_definition, filter_pyx_definition, convolve_code, sum_code, kernel=True):
        """
        OpenMP code generation.
        """
        # Basic ids
        base_ids = {
            'id_proj': self.id,
            'size_post': self.post.size,
            'float_prec': Global.config['precision']
        }

        # Fill the basic definitions
        conv_dict = deepcopy(convolve_template_omp)
        for key, value in conv_dict.items():
            value = value % base_ids
            conv_dict[key] = value
        self._specific_template.update(conv_dict)

        # Kernel-based method: specify w with the correct dimension
        if kernel:
            # The number of dimension influences the type
            cpp_type_w = filter_definition.replace(' w;', '')
            pyx_type_w = filter_pyx_definition.replace(' w', '')

            # Fill the code templates
            self._specific_template['declare_parameters_variables'] = tabify(filter_definition.strip(), 1)
            self._specific_template['export_parameters_variables'] = ""
            self._specific_template['access_parameters_variables'] = conv_filter_template["openmp"]["access"] % {'type_w': cpp_type_w}
            self._specific_template['export_connectivity'] += conv_filter_template["pyx_wrapper"]["export"] % {'type_w': pyx_type_w}
            self._specific_template['wrapper_args'] += conv_filter_template["pyx_wrapper"]["args"]
            self._specific_template['wrapper_init_connectivity'] += conv_filter_template["pyx_wrapper"]["init"] % {'id_proj': self.id}
            self._specific_template['wrapper_access_connectivity'] += conv_filter_template["pyx_wrapper"]["access"] % {'id_proj': self.id, 'float_prec': Global.config['precision']}

        # Override the monitor to avoid recording the weights
        self._specific_template['monitor_class'] = ""
        self._specific_template['monitor_export'] = ""
        self._specific_template['monitor_wrapper'] = ""

        # Clean-up
        self._specific_template['clear_container'] = convolve_template_omp["clear"]

        # OMP code
        omp_code = ""
        if Global.config['num_threads'] > 1:
            omp_code = """
        #pragma omp for private(sum, rk_pre, coord) %(psp_schedule)s""" % {'psp_schedule': "" if not 'psp_schedule' in self._omp_config.keys() else self._omp_config['psp_schedule']}

        # HD ( 16.10.2015 ):
        # pre-load delayed firing rate in a local array, so we
        # prevent multiple accesses to pop%(id_pre)s._delayed_r[delay-1]
        # wheareas delay is set available as variable
        # TODO HD: wouldn't it be much better to reduce delay globaly, instead of the substraction here???
        if self.delays > Global.config['dt']:
            pre_load_r = """
        // pre-load delayed firing rate
        auto delayed_r = pop%(id_pre)s._delayed_r[delay-1];
        """% {'id_pre': self.pre.id}
        else:
            pre_load_r = ""

        # Target variable depends on neuron type
        target_code = "_sum_%(target)s" if self.post.neuron_type.type=="rate" else "g_%(target)s"
        target_code %= {'target': self.target}

        # Compute sum
        wsum =  """
        if ( _transmission && pop%(id_pre)s._active ) {
            int* coord;
""" + pre_load_r + """
            %(omp_code)s
            for(int i = 0; i < %(size_post)s; i++){
                coord = pre_coords[i].data();

                // perform the convolution
""" + tabify(convolve_code, 1) + """

                // store result
                pop%(id_post)s.%(target)s[i] += """ + sum_code + """;
            } // for
        } // if
"""

        # Finalize the processing code
        self._specific_template['psp_code'] = wsum % \
        {   'id_proj': self.id,
            'target': target_code,
            'id_pre': self.pre.id, 'name_pre': self.pre.name, 'size_pre': self.pre.size,
            'id_post': self.post.id, 'name_post': self.post.name, 'size_post': self.post.size,
            'omp_code': omp_code,
            'convolve_code': convolve_code
        }

    def _generate_cuda(self, filter_definition, filter_pyx_definition, convolve_code, sum_code, kernel=True):
        """
        CUDA code generation.
        """
        # Basic ids
        base_ids = {
            'id_proj': self.id,
            'size_post': self.post.size,
            'float_prec': Global.config['precision']
        }

        # Fill the basic definitions
        conv_dict = deepcopy(convolve_template_cuda)
        for key, value in conv_dict.items():
            value = value % base_ids
            conv_dict[key] = value
        self._specific_template.update(conv_dict)

        # Kernel-based method: specify w with the correct dimension
        if kernel:
            # The number of dimension influences the type
            cpp_type_w = filter_definition.replace(' w;', '')
            pyx_type_w = filter_pyx_definition.replace(' w', '')

            # Fill the code templates
            self._specific_template['declare_parameters_variables'] = conv_filter_template["cuda"]["declare"] % {'cpu_side_filter': filter_definition.strip(), 'float_prec': Global.config["precision"]}
            self._specific_template['export_parameters_variables'] = ""
            self._specific_template['access_parameters_variables'] = conv_filter_template["cuda"]["access"] % {'type_w': cpp_type_w, 'id_proj': self.id}
            self._specific_template['export_connectivity'] += conv_filter_template["pyx_wrapper"]["export"] % {'type_w': pyx_type_w}
            self._specific_template['wrapper_args'] += conv_filter_template["pyx_wrapper"]["args"]
            self._specific_template['wrapper_init_connectivity'] += conv_filter_template["pyx_wrapper"]["init"] % {'id_proj': self.id}
            self._specific_template['wrapper_access_connectivity'] += conv_filter_template["pyx_wrapper"]["access"] % {'id_proj': self.id, 'float_prec': Global.config['precision']}

            # Memory transfer of variables
            dim_pre = self.dim_pre
            if self.multiple:
               dim_pre += 1
            self._specific_template['host_device_transfer'] += conv_filter_template["cuda"]["host_device_transfer"] % {'ctype': Global.config["precision"], 'id_proj': self.id, 'pre_dim': dim_pre}

            # Other fields
            self._specific_template['size_in_bytes'] = ""

        # Override the monitor to avoid recording the weights
        self._specific_template['monitor_class'] = ""
        self._specific_template['monitor_export'] = ""
        self._specific_template['monitor_wrapper'] = ""

        # Clean-up
        self._specific_template['clear_container'] = convolve_template_cuda["clear"]

        # Add pre-synaptic variables to argument list
        pre_variables_header = ""
        pre_variables_invoke = ""
        pre_variables_call = ""
        for pre_dep in self.synapse_type.description['dependencies']['pre']:
            # HD (TODO): the type to float precision works for now, but one should
            #            look up the type in the pre-synaptic neuron type...
            pre_id_dict = {
                'id_pre': self.pre.id,
                'name': pre_dep,
                'type': Global.config["precision"]
            }
            pre_variables_header += ", const %(type)s* __restrict__ pre_%(name)s" % pre_id_dict
            pre_variables_invoke += ", pre_%(name)s" % pre_id_dict
            pre_variables_call += ", pop%(id_pre)s.gpu_%(name)s" % pre_id_dict

        # Finalize code templates
        code_ids = {
            'id_proj': self.id,
            'target': self.target,
            'id_post': self.post.id,
            'pre_dim': self.dim_pre,
            'convolve_code': convolve_code,
            'float_prec': Global.config["precision"],
            'pre_variables_header': pre_variables_header,
            'pre_variables_invoke': pre_variables_invoke,
            'pre_variables_call': pre_variables_call,
            'pre_variable': "pre_%(name)s" % pre_id_dict,
            'convolve_code': convolve_code
        }

        # Finalize the processing code
        if not self.multiple:
            # Convolution
            self._specific_template['psp_body'] = cuda_convolution_single_filter["body"] % code_ids
            self._specific_template['psp_invoke'] = cuda_convolution_single_filter["invoke"] % code_ids
            self._specific_template['psp_header'] = cuda_convolution_single_filter["header"] % code_ids
            self._specific_template['psp_call'] = cuda_convolution_single_filter["call"] % code_ids

        else:
            num_elem_per_filter = 1
            for i in self.weights.shape[1:]:
                num_elem_per_filter *= i
            code_ids.update({
                'filter_dim': self.dim_kernel,
                'num_elem_filter': num_elem_per_filter
            })

            # Bank of filters
            if len(self.weights.shape)==3 and len(self.pre.geometry)==2:
                code_ids.update({
                    'post_size': self.post.size,
                    'filter_dim_i': self.weights.shape[1],
                    'filter_dim_j': self.weights.shape[2],
                    'pre_offset_i': self._center_filter(self.weights.shape[1]),
                    'pre_offset_j': self._center_filter(self.weights.shape[2]),
                    'pre_border_i': self.pre.geometry[0]-1,
                    'pre_border_j': self.pre.geometry[1]-1,
                    'pre_dim_j': self.pre.geometry[1]
                })
                self._specific_template['psp_body'] = cuda_convolution_bank_of_filter_3d["body"] % code_ids
                self._specific_template['psp_invoke'] = cuda_convolution_bank_of_filter_3d["invoke"] % code_ids
                self._specific_template['psp_header'] = cuda_convolution_bank_of_filter_3d["header"] % code_ids
                self._specific_template['psp_call'] = cuda_convolution_bank_of_filter_3d["call"] % code_ids

            elif len(self.weights.shape)==4 and len(self.pre.geometry)==3:
                code_ids.update({
                    'post_size': self.post.size,
                    'filter_dim_i': self.weights.shape[1],
                    'filter_dim_j': self.weights.shape[2],
                    'filter_dim_k': self.weights.shape[3],
                    'pre_offset_i': self._center_filter(self.weights.shape[1]),
                    'pre_offset_j': self._center_filter(self.weights.shape[2]),
                    'pre_offset_k': self._center_filter(self.weights.shape[3]),
                    'pre_border_i': self.pre.geometry[0]-1,
                    'pre_border_j': self.pre.geometry[1]-1,
                    'pre_border_k': self.pre.geometry[2]-1,
                    'pre_dim_j': self.pre.geometry[1],
                    'pre_dim_k': self.pre.geometry[2]
                })
                self._specific_template['psp_body'] = cuda_convolution_bank_of_filter_4d["body"] % code_ids
                self._specific_template['psp_invoke'] = cuda_convolution_bank_of_filter_4d["invoke"] % code_ids
                self._specific_template['psp_header'] = cuda_convolution_bank_of_filter_4d["header"] % code_ids
                self._specific_template['psp_call'] = cuda_convolution_bank_of_filter_4d["call"] % code_ids

            else:
                self._specific_template['psp_body'] = cuda_convolution_bank_of_filter["body"] % code_ids
                self._specific_template['psp_invoke'] = cuda_convolution_bank_of_filter["invoke"] % code_ids
                self._specific_template['psp_header'] = cuda_convolution_bank_of_filter["header"] % code_ids
                self._specific_template['psp_call'] = cuda_convolution_bank_of_filter["call"] % code_ids

        # Post-neuron is a spike neuron (e.g., part of ANN-to-SNN conversion)
        if self.post.neuron_type.type == "spike":
            self._specific_template['psp_call'] = self._specific_template['psp_call'].replace("gpu__sum_"+self.target, "gpu_g_"+self.target)

        # Remove trailing spaces
        self._specific_template['psp_body'] = remove_trailing_spaces(self._specific_template['psp_body'])

    ################################
    ### Utilities
    ################################
    def _center_filter(self, i):
        return int(i/2) if i%2==1 else int(i/2)-1

    def _filter_definition(self):
        dim = self.dim_kernel
        cpp = Global.config['precision']
        pyx = Global.config['precision']
        for d in range(dim):
            cpp = 'std::vector< ' + cpp + ' >'
            pyx = 'vector[' + pyx + ']'
        cpp += ' w;'
        pyx += ' w'
        return cpp, pyx

    def _coordinates_to_rank(self, name, geometry):

        dim = len(geometry)

        txt = ""

        for d in range(dim):
            if txt == "" : # first coordinate is special
                txt = indices[0] + "_" + name
            else:
                txt = str(geometry[d]) + '*(' + txt + ') + ' + indices[d]  + '_' + name

        return txt

    def _filter_coordinates_to_index(self, name, filter_dim):
        dim = len(filter_dim)

        txt = ""

        for d in range(dim):
            if txt == "" : # first coordinate is special
                txt = indices[0] + "_" + name
            else:
                txt = str(filter_dim[d]) + '*(' + txt + ') + ' + indices[d]  + '_' + name

        return txt

    def _generate_convolve_code(self, pre_load_inner_line=True):
        """
        Generate the loop for the convolution case.

        Parameters:

        * pre_load_inner_line: for CPU-code it's useful to have a local variable for accessing the innermost sub-vector
        """

        # Operation to be performed: sum, max, min, mean
        operation = self.synapse_type.operation

        # Main code
        code = tabify("sum = 0.0;\n", 3)

        # Generate for loops
        for dim in range(self.dim_kernel):
            if pre_load_inner_line:
                if dim == self.dim_kernel-1:
                    inner_idx = ""
                    for i in range(self.dim_kernel-1):
                        inner_idx += "["+indices[i]+"_w]"
                    code += "auto inner_line = w"+inner_idx+".data();\n"

            code += tabify("""
            for(int %(index)s_w = 0; %(index)s_w < %(size)s;%(index)s_w++) {
            """ % { 'index': indices[dim], 'size': self.weights.shape[dim]}, dim)

            # Compute indices
            if dim < self.dim_kernel:
                code += tabify(
                    """int %(index)s_pre = coord[%(dim)s] %(operator)s (%(index)s_w - %(center)s);""" %
                        {
                            'id_proj': self.id,
                            'index': indices[dim],
                            'dim': dim,
                            'operator': '+' ,
                            'center': self._center_filter(self.weights.shape[dim])
                        }, 1)
            else:
                code += tabify(
                    """int %(index)s_pre = coord[%(dim)s];""" %
                        {
                            'id_proj': self.id,
                            'index': indices[dim],
                            'dim': dim
                        }, 1)

            # Check indices
            if operation in ['sum', 'mean']:
                if isinstance(self.padding, str): # 'border'
                        code += tabify("""
                if (%(index)s_pre < 0) %(index)s_pre = 0 ;
                if (%(index)s_pre > %(max_size)s) %(index)s_pre = %(max_size)s ;
                """ % { 'index': indices[dim], 'dim': dim, 'max_size': self.pre.geometry[dim] -1}, dim)
                else:
                    code += tabify("""
                if ((%(index)s_pre < 0) || (%(index)s_pre > %(max_size)s)){
                    sum += %(padding)s;
                    continue;
                }
                """ % { 'index': indices[dim], 'padding': self.padding, 'max_size': self.pre.geometry[dim] -1}, dim)

            else: # min, max
                code += """
                if ((%(index)s_pre < 0) || (%(index)s_pre > %(max_size)s)) {
                    continue;
                }
                """ % { 'index': indices[dim], 'max_size': self.pre.geometry[dim] -1}

        # if True, we need to take the last dimension from coords
        if self.keep_last_dimension:
            id_dict = {
                'index': indices[self.dim_kernel],
                'dim': self.dim_kernel
            }
            code += "int %(index)s_pre = coord[%(dim)s];" % id_dict

        # Compute pre-synaptic rank
        code += tabify("""
                rk_pre = %(value)s;""" % {'value': self._coordinates_to_rank('pre', self.pre.geometry)}, dim)
        if not pre_load_inner_line:
            code += tabify("""
                w_idx = %(value)s;""" % {'value': self._filter_coordinates_to_index('w', self.weights.shape)}, dim)

        # Compute the increment
        index = ""
        for dim in range(self.dim_kernel):
            index += '[' + indices[dim] + '_w]'

        # Indices etc. depends on the target platform
        inc_dict = {
            'id_pre': self.pre.id,
            'id_post': self.post.id,
        }
        if Global._check_paradigm("openmp"):
            inc_dict.update({
                'global_index': '[i]',
                'local_index': index,
                'pre_index': '[rk_pre]',
                'post_index': '[rk_post]',
                'pre_prefix': 'pop'+str(self.pre.id)+'.',
                'post_prefix': 'pop'+str(self.post.id)+'.'
            })
        elif Global._check_paradigm("cuda"):
            inc_dict.update({
                'global_index': '',
                'local_index': '[w_idx]',
                'pre_index': '[rk_pre]',
                'post_index': '',
                'pre_prefix': 'pre_',
                'post_prefix': 'post_'
            })
        else:
            raise NotImplementedError

        # Fill the code template
        increment = self.synapse_type.description['psp']['cpp'] % inc_dict

        # Delays
        if self.delays > Global.config['dt']:
            increment = increment.replace(
                'pop%(id_pre)s.r[rk_pre]' % {'id_pre': self.pre.id},
                'delayed_r[rk_pre]'
            )

        # Apply the operation
        if operation == "sum":
            if self.dim_kernel == 1:
                code += tabify("""
                sum += %(increment)s""" % {'increment': increment}, dim)
            else:
                if pre_load_inner_line:
                    code += tabify("""
                    sum += %(increment)s""" % {'increment': increment.replace('w'+inner_idx, 'inner_line')}, dim)
                else:
                    code += tabify("""
                    sum += %(increment)s""" % {'increment': increment}, dim)
        elif operation == "max":
            code += tabify("""
                %(float_prec)s _psp = %(increment)s
                if(_psp > sum) sum = _psp;""" % {'increment': increment, 'float_prec': Global.config['precision']}, dim)
        elif operation == "min":
            code += tabify("""
                %(float_prec)s _psp = %(increment)s
                if(_psp < sum) sum = _psp;""" % {'increment': increment, 'float_prec': Global.config['precision']}, dim)
        elif operation == "mean":
            code += tabify("""
                sum += %(increment)s""" % {'increment': increment}, dim)
        else:
            Global._error('Convolution: Operation', operation, 'is not implemented yet for shared projections.')

        # Close for loops
        for dim in range(self.dim_kernel):
            code += tabify("""
            }""", self.dim_kernel-1-dim)

        impl_code = code % {'id_proj': self.id,
            'target': self.target,
            'id_pre': self.pre.id,
            'name_pre': self.pre.name,
            'size_pre': self.pre.size,
            'id_post': self.post.id,
            'name_post': self.post.name,
            'size_post': self.post.size
          }

        # sum code
        self.weights.size
        if operation == "mean":
            sum_code = """sum/%(filter_size)s""" % {'filter_size': self.weights.size}
        else:
            sum_code = "sum"

        return impl_code, sum_code

    def _generate_bank_code(self, pre_load_inner_line=True):
        """
        Generate the loop for the bank of filters case.

        Parameters:

        * pre_load_inner_line: for CPU-code it's useful to have a local variable for accessing the innermost sub-vector
        """

        # Operation to be performed: sum, max, min, mean
        operation = self.synapse_type.operation

        # Main code
        code = tabify("sum = 0.0;\n", 3)

        # Generate for loops
        for dim in range(self.dim_kernel-1):
            if pre_load_inner_line:
                if dim == self.dim_kernel-2:
                    inner_idx = ""
                    for i in range(self.dim_kernel-2):
                        inner_idx += "["+indices[i]+"_w]"
                    code += tabify("""
                const %(float_prec)s* w_inner_line = w[coord[%(dim_pre)s]]%(inner_idx)s.data();
    """ % {'float_prec': Global.config["precision"], 'inner_idx': inner_idx, 'dim_pre': self.dim_pre}, dim)

            code += tabify("""
            for (int %(index)s_w = 0; %(index)s_w < %(size)s;%(index)s_w++) {
            """ % { 'index': indices[dim], 'size': self.weights.shape[dim+1]}, dim)

            # Compute indices
            if dim < self.dim_kernel:
                code += tabify(
                    """int %(index)s_pre = coord[%(dim)s] %(operator)s (%(index)s_w - %(center)s);""" %
                    {
                        'id_proj': self.id,
                        'index': indices[dim],
                        'dim': dim,
                        'operator': '+',
                        'center': self._center_filter(self.weights.shape[dim+1])
                    }, 1)
            else:
                code += tabify(
                    """int %(index)s_pre = coord[%(dim)s];""" %
                    {
                        'id_proj': self.id,
                        'index': indices[dim],
                        'dim': dim
                    }, 1)

            # Check indices
            if operation in ['sum', 'mean']:
                if isinstance(self.padding, str): # 'border'
                    code += tabify("""
            if (%(index)s_pre < 0) %(index)s_pre = 0 ;
            if (%(index)s_pre > %(max_size)s) %(index)s_pre = %(max_size)s ;
            """ % { 'index': indices[dim], 'dim': dim, 'max_size': self.pre.geometry[dim] -1}, 1+dim)
                else:
                    code += tabify("""
            if ((%(index)s_pre < 0) || (%(index)s_pre > %(max_size)s)) {
                sum += %(padding)s;
                continue;
            }
            """ % { 'index': indices[dim], 'padding': self.padding, 'max_size': self.pre.geometry[dim] -1}, 1+dim)

            else: # min, max
                code += tabify("""
            if ((%(index)s_pre < 0) || (%(index)s_pre > %(max_size)s)){
                continue;
            }
            """ % { 'index': indices[dim], 'max_size': self.pre.geometry[dim] -1}, 1+dim)

        # Compute pre-synaptic rank
        code +=tabify("""
            rk_pre = %(value)s;""" % {'value': self._coordinates_to_rank('pre', self.pre.geometry)}, 1+dim)
        if not pre_load_inner_line:
            code += tabify("""
                w_idx = %(value)s;""" % {'value': self._filter_coordinates_to_index('w', self.weights.shape[1:])}, dim)

        # Compute the increment
        if pre_load_inner_line:
            index = "_inner_line["+indices[self.dim_kernel-2]+"_w]"
        else:
            index = "[coord["+str(self.dim_pre)+"]]"
            for dim in range(self.dim_kernel-1):
                index += '[' + indices[dim] + '_w]'

        # Indices etc. depend on target platform
        inc_dict = {
            'id_pre': self.pre.id,
            'id_post': self.post.id,
        }
        if Global._check_paradigm("openmp"):
            inc_dict.update({
                'local_index': index,
                'global_index': '[i]',
                'pre_index': '[rk_pre]',
                'post_index': '[rk_post]',
                'pre_prefix': 'pop'+str(self.pre.id)+'.',
                'post_prefix': 'pop'+str(self.post.id)+'.'
            })
        elif Global._check_paradigm("cuda"):
            inc_dict.update({
                'local_index': "[w_idx]",
                'global_index': '[bIdx]',
                'pre_index': '[rk_pre]',
                'post_index': '[bIdx]',
                'pre_prefix': 'pre_',
                'post_prefix': 'post_'
            })
        else:
            raise NotImplementedError

        # Pixel-wise applied operation
        increment = self.synapse_type.description['psp']['cpp']
        if Global._check_paradigm("cuda"):
            increment = increment.replace("w%(local_index)s", "w_bank%(local_index)s")
        increment %= inc_dict

        # Delays
        if self.delays > Global.config['dt']:
            increment = increment.replace(
                'pop%(id_pre)s.r[rk_pre]' % {'id_pre': self.pre.id},
                'delayed_r[rk_pre]'
            )

        # Apply the operation
        if operation == "sum":
            code += tabify("""
            sum += %(increment)s""" % {'increment': increment}, 1+dim)
        elif operation == "max":
            code += tabify("""
            %(float_prec)s _psp = %(increment)s
            if(_psp > sum) sum = _psp;""" % {'increment': increment, 'float_prec': Global.config['precision']}, 1+dim)
        elif operation == "min":
            code += tabify("""
            %(float_prec)s _psp = %(increment)s
            if(_psp < sum) sum = _psp;""" % {'increment': increment, 'float_prec': Global.config['precision']}, 1+dim)
        elif operation == "mean":
            code += tabify("""
            sum += %(increment)s""" % {'increment': increment}, 1+dim)
        else:
            Global._error('SharedProjection: Operation', operation, 'is not implemented yet for shared projections.')

        # Close for loops
        for dim in range(self.dim_kernel-1):
            code += tabify("""
        }""", self.dim_kernel-1-dim)

        impl_code = code % {'id_proj': self.id,
            'target': self.target,
            'id_pre': self.pre.id, 'name_pre': self.pre.name, 'size_pre': self.pre.size,
            'id_post': self.post.id, 'name_post': self.post.name, 'size_post': self.post.size
        }

        # sum code
        if operation == "mean":
            sum_code = """sum/%(filter_size)s""" % {'filter_size': self.weights.size}
        else:
            sum_code = "sum"

        return impl_code, sum_code

    ##############################
    ## Override useless methods
    ##############################
    def _data(self):
        "Disable saving."
        desc = {}
        desc['post_ranks'] = self.post_ranks
        desc['attributes'] = self.attributes
        desc['parameters'] = self.parameters
        desc['variables'] = self.variables

        desc['dendrites'] = []
        desc['number_of_synapses'] = 0
        return desc

    def save_connectivity(self, filename):
        "Not available."
        Global._warning('Convolutional projections can not be saved.')
    def save(self, filename):
        "Not available."
        Global._warning('Convolutional projections can not be saved.')
    def load(self, filename):
        "Not available."
        Global._warning('Convolutional projections can not be loaded.')
    def receptive_fields(self, variable = 'w', in_post_geometry = True):
        "Not available."
        Global._warning('Convolutional projections can not display receptive fields.')
    def connectivity_matrix(self, fill=0.0):
        "Not available."
        Global._warning('Convolutional projections can not display connectivity matrices.')

__init__(pre, post, target, psp='pre.r * w', operation='sum', name=None, copied=False) #

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).

Source code in ANNarchy/extensions/convolution/Convolve.py
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def __init__(self, pre, post, target, psp="pre.r * w", operation="sum", name=None, copied=False):
    """
    :param pre: pre-synaptic population (either its name or a ``Population`` object).
    :param post: post-synaptic population (either its name or a ``Population`` object).
    :param target: type of the connection
    :param psp: continuous influence of a single synapse on the post-synaptic neuron (default for rate-coded: ``w*pre.r``).
    :param operation: operation (sum, max, min, mean) performed by the kernel (default: sum).
    """
    # Create the description, but it will not be used for generation
    SpecificProjection.__init__(
        self,
        pre,
        post,
        target,
        synapse=SharedSynapse(psp=psp, operation=operation, name="Convolution operation", description="Convoluted kernel over the pre-synaptic population."),
        name=name,
        copied=copied
    )

    # Disable saving
    self._saveable = False

    # For copy
    self._used_single_filter = False
    self._used_bank_of_filters = False
    self.operation = operation

connect_filter(weights, delays=0.0, keep_last_dimension=False, padding=0.0, subsampling=None) #

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.

Source code in ANNarchy/extensions/convolution/Convolve.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def connect_filter(self, weights, delays=0.0, keep_last_dimension=False, padding=0.0, subsampling=None):
    """
    Applies a single filter on the pre-synaptic population.

    :param weights: numpy array or list of lists representing the matrix of weights for the filter.
    :param delays: delay in synaptic transmission (default: dt). Can only be the same value for all neurons.
    :param 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.
    :param 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.
    :param subsampling: list for each post-synaptic neuron of coordinates in the pre-synaptic population defining the center of the kernel/filter. Default: None.
    """

    # Process the weights
    self.weights = np.array(weights)

    # Process the delays
    self.delays = float(delays)
    if not isinstance(delays, (int, float)):
        Global._error('Convolutions can only have constant delays.')

    self.subsampling = subsampling
    self.keep_last_dimension = keep_last_dimension
    self.padding = padding
    self.multiple = False

    # Check dimensions of populations and weight matrix
    self.dim_kernel = self.weights.ndim
    self.dim_pre = self.pre.dimension
    self.dim_post = self.post.dimension

    if self.dim_post > 4:
        print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
        Global._error('Convolution: Too many dimensions for the post-synaptic population (maximum 4).')

    if self.dim_pre > 4:
        print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
        Global._error('Convolution: Too many dimensions for the pre-synaptic population (maximum 4).')

    if self.dim_kernel > 5  or (not self.multiple and self.dim_kernel > 4):
        print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
        Global._error('Convolution: Too many dimensions for the kernel (maximum 4).')

    # Check if the last axes match for parallel convolution (e.g. 3-2-3)
    if self.dim_kernel < self.dim_pre:
        if not self.keep_last_dimension:
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: If the kernel has less dimensions than the pre-synaptic population, you need to set the flag keep_last_dimension to True.')

        if self.pre.geometry[-1] != self.post.geometry[-1]:
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: If the kernel has fewer dimensions than the two populations (keep_last_dimension=True), these must have the same number of neurons in the last dimension.')

    # If the last dim of the kernel matches the last dim of the pre-pop, the last pop can have one dimension less.
    if self.dim_post < self.dim_pre: # OK, but check the last dimension of the kernel has the same size as the post-population
        if self.weights.shape[-1] != self.pre.geometry[-1]:
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: If the post-synaptic population has less dimensions than the pre-synaptic one, the last dimension of the filter must be equal to the last of the pre-synaptic population.')

    # Check if it is a bank of filters
    if self.dim_kernel > self.dim_pre:
        print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
        Global._error('Convolution: If the kernel has more dimensions than the pre-synaptic population, you need to use the connect_filters() method.')


    # Generate the pre-synaptic coordinates
    self._generate_pre_coordinates()

    # Finish building the synapses
    self._create()

    # For copy
    self._used_single_filter = True

    return self

connect_filters(weights, delays=0.0, keep_last_dimension=False, padding=0.0, subsampling=None) #

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.

Source code in ANNarchy/extensions/convolution/Convolve.py
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
def connect_filters(self, weights, delays=0.0, keep_last_dimension=False, padding=0.0, subsampling=None):
    """
    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.


    :param weights: numpy array or list of lists representing the matrix of weights for the filter.
    :param delays: delay in synaptic transmission (default: dt). Can only be the same value for all neurons.
    :param 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.
    :param 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.
    :param subsampling: list for each post-synaptic neuron of coordinates in the pre-synaptic population defining the center of the kernel/filter. Default: None.
    """

    # Process the weights
    self.weights = np.array(weights)

    # Process the delays
    self.delays = float(delays)
    if not isinstance(delays, (int, float)):
        Global._error('Convolutions can only have constant delays.')

    self.subsampling = subsampling
    self.keep_last_dimension = keep_last_dimension
    self.padding = padding
    self.multiple = True

    # Check dimensions of populations and weight matrix
    self.dim_kernel = self.weights.ndim
    self.dim_pre = self.pre.dimension
    self.dim_post = self.post.dimension


    if self.dim_post > 4:
        print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
        Global._error('Convolution: Too many dimensions for the post-synaptic population (maximum 4).')

    if self.dim_pre > 4:
        print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
        Global._error('Convolution: Too many dimensions for the pre-synaptic population (maximum 4).')

    if self.dim_kernel > 5  or (not self.multiple and self.dim_kernel > 4):
        print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
        Global._error('Convolution: Too many dimensions for the kernel (maximum 4).')

    # Check if the last axes match for parallel convolution (e.g. 3-2-3)
    if self.dim_kernel < self.dim_pre:
        if not self.keep_last_dimension:
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: If the kernel has less dimensions than the pre-synaptic population, you need to set the flag keep_last_dimension to True.')

        if self.pre.geometry[-1] != self.post.geometry[-1]:
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: If the kernel has fewer dimensions than the two populations (keep_last_dimension=True), these must have the same number of neurons in the last dimension.')

    # If the last dim of the kernel matches the last dim of the pre-pop, the last pop can have one dimension less.
    if self.dim_post < self.dim_pre: # OK, but check the last dimension of the kernel has the same size as the post-population
        if self.weights.shape[-1] != self.pre.geometry[-1]:
            print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
            Global._error('Convolution: If the post-synaptic population has less dimensions than the pre-synaptic one, the last dimension of the filter must be equal to the last of the pre-synaptic population.')

    # The last dimension of the post population must correspond to the number of filters
    if self.weights.shape[0] != self.post.geometry[-1]:
        print("Convolution:", self.dim_pre, '*', self.dim_kernel, '->', self.dim_post)
        Global._error('Convolution: For multiple filters, the last dimension of the post-synaptic population must have as many neurons as there are filters.')

    # Generate the pre-synaptic coordinates
    self._generate_pre_coordinates_bank()

    # Finish building the synapses
    self._create()

    # For copy
    self._used_bank_of_filters = True

    return self

connectivity_matrix(fill=0.0) #

Not available.

Source code in ANNarchy/extensions/convolution/Convolve.py
1155
1156
1157
def connectivity_matrix(self, fill=0.0):
    "Not available."
    Global._warning('Convolutional projections can not display connectivity matrices.')

load(filename) #

Not available.

Source code in ANNarchy/extensions/convolution/Convolve.py
1149
1150
1151
def load(self, filename):
    "Not available."
    Global._warning('Convolutional projections can not be loaded.')

receptive_fields(variable='w', in_post_geometry=True) #

Not available.

Source code in ANNarchy/extensions/convolution/Convolve.py
1152
1153
1154
def receptive_fields(self, variable = 'w', in_post_geometry = True):
    "Not available."
    Global._warning('Convolutional projections can not display receptive fields.')

save(filename) #

Not available.

Source code in ANNarchy/extensions/convolution/Convolve.py
1146
1147
1148
def save(self, filename):
    "Not available."
    Global._warning('Convolutional projections can not be saved.')

save_connectivity(filename) #

Not available.

Source code in ANNarchy/extensions/convolution/Convolve.py
1143
1144
1145
def save_connectivity(self, filename):
    "Not available."
    Global._warning('Convolutional projections can not be saved.')

ANNarchy.extensions.convolution.Pooling #

:copyright: Copyright 2013 - now, see AUTHORS. :license: GPLv2, see LICENSE for details.

Pooling #

Bases: SpecificProjection

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
Source code in ANNarchy/extensions/convolution/Pooling.py
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
class Pooling(SpecificProjection):
    """
    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:

    ```python
    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
    ```
    """
    def __init__(self, pre, post, target, psp="pre.r", operation="max", name=None, copied=False):
        """
        :param pre: pre-synaptic population (either its name or a ``Population`` object).
        :param post: post-synaptic population (either its name or a ``Population`` object).
        :param target: type of the connection
        :param operation: pooling function to be applied ("max", "min", "mean")
        """
        # Sanity check
        if not operation in ["max", "mean", "min"]:
            Global._error("Pooling: the operation must be either 'max', 'mean' or 'min'.")
        self.operation = operation

        # Store for _copy
        self.psp = psp

        SpecificProjection.__init__(
            self,
            pre,
            post,
            target,
            synapse=SharedSynapse(psp=psp, operation=operation, name="Pooling operation", description=operation+"-pooling operation over the pre-synaptic population."),
            name=name,
            copied=copied
        )

        # check dimensions of populations, should not exceed 4
        self.dim_pre = self.pre.dimension
        self.dim_post = self.post.dimension
        if self.dim_post > 4:
            Global._error('Pooling: Too many dimensions for the post-synaptic population (maximum 4).')
        if self.dim_pre > 4:
            Global._error('Pooling: Too many dimensions for the pre-synaptic population (maximum 4).')

        # Disable saving
        self._saveable = False



    def connect_pooling(self, extent=None, delays=0.0):
        """
        :param 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.
        :param delays: synaptic delay in ms
        """

        # process extent
        self.extent_init = extent
        if extent is None:  # compute the extent automatically
            if self.pre.dimension != self.post.dimension:
                Global._error(
                    'Pooling: If you do not provide the extent parameter, the two populations must have the same number of dimensions.')

            extent = list(self.pre.geometry)
            for dim in range(self.pre.dimension):
                extent[dim] /= self.post.geometry[dim]
                if self.pre.geometry[dim] != extent[dim] * self.post.geometry[dim]:
                    Global._error(
                        'Pooling: Unable to compute the extent of the pooling area: the number of neurons do not match.')

        elif not isinstance(extent, tuple):
            Global._error('Pooling: You must provide a tuple for the extent of the pooling operation.')

        self.extent = list(extent)
        if len(self.extent) < self.pre.dimension:
            Global._error('Pooling: You must provide a tuple for the extent of the pooling operation.')

        # process delays
        self.delays = delays

        # Generate the pre-synaptic coordinates
        self._generate_extent_coordinates()

        # create fake LIL
        self._create()

        return self

    def _copy(self, pre, post):
        "Returns a copy of the projection when creating networks.  Internal use only."
        copied_proj = Pooling(pre=pre, post=post, target=self.target, psp=self.psp,
                              operation=self.operation, name=self.name, copied=True)

        copied_proj.extent = self.extent
        copied_proj.delays = self.delays

        copied_proj._generate_extent_coordinates()
        copied_proj._create()

        copied_proj._connection_method = self._connection_method
        copied_proj._connection_args = self._connection_args
        copied_proj._connection_delay = self._connection_delay
        copied_proj._storage_format = self._storage_format
        return copied_proj

    def _create(self):
        """
        create fake LIL object, just for compilation process

        :return: no return value
        """
        try:
            from ANNarchy.core.cython_ext.Connector import LILConnectivity
        except Exception as e:
            Global._print(e)
            Global._error('ANNarchy was not successfully installed.')

        lil = LILConnectivity()
        lil.max_delay = self.delays
        lil.uniform_delay = self.delays
        self.connector_name = "Pooling"
        self.connector_description = "Pooling"
        self._store_connectivity(self._load_from_lil, (lil, ), self.delays, storage_format="lil", storage_order="post_to_pre")

    def _connect(self, module):
        """
        Builds up dendrites either from list or dictionary. Called by instantiate().
        """
        if not self._connection_method:
            Global._error(
                'Pooling: The projection between ' + self.pre.name + ' and ' + self.post.name + ' is declared but not connected.')

        # Create the Cython instance
        proj = getattr(module, 'proj' + str(self.id) + '_wrapper')
        self.cyInstance = proj(self.pre_coordinates)

        return True

    def _generate_extent_coordinates(self):
        """
        Generates for each post-neuron the position of the top-left corner, where the pooling should be applied.

        :return:  a list for each post neuron of the corresponding top-left coordinates
        """
        # Generates coordinates TODO: Find a more robust way!
        coords = [[] for i in range(self.post.size)]
        if self.dim_pre == 1:
            rk = 0
            for i in range(self.post.geometry[0]):
                coords[rk] = [i * self.extent[0]]
                rk += 1
        elif self.dim_pre == 2:
            rk = 0
            for i in range(self.post.geometry[0]):
                if self.dim_post > 1:
                    for j in range(self.post.geometry[1]):
                        coords[rk] = [i * self.extent[0], j * self.extent[1]]
                        rk += 1
                else: # over the whole second axis
                    coords[rk] = [i * self.extent[0], 0]
                    rk += 1

        elif self.dim_pre == 3:
            rk = 0
            for i in range(self.post.geometry[0]):
                for j in range(self.post.geometry[1]):
                    if self.dim_post > 2:
                        for k in range(self.post.geometry[2]):
                            coords[rk] = [i * self.extent[0], j * self.extent[1], k * self.extent[2]]
                            rk += 1
                    else: # over the whole third axis
                        coords[rk] = [i * self.extent[0], j * self.extent[1], 0]
                        rk += 1

        elif self.dim_pre == 4: # TODO: post has less than 4 dimensions
            rk = 0
            for i in range(self.post.geometry[0]):
                for j in range(self.post.geometry[1]):
                    for k in range(self.post.geometry[2]):
                        for l in range(self.post.geometry[3]):
                            coords[rk] = [i * self.extent[0], j * self.extent[1], k * self.extent[2], l * self.extent[3]]
                            rk += 1
        # Save the result
        self.pre_coordinates = coords

    def _generate(self):
        """
        Overrides the default code generation.
        """
        # Convolve_code
        convolve_code, sum_code = self._generate_pooling_code()

        # Generate the code
        if Global._check_paradigm("openmp"):
            self._generate_omp(convolve_code, sum_code)
        elif Global._check_paradigm("cuda"):
            self._generate_cuda()
        else:
            Global._error("Pooling: not implemented for the configured paradigm")

    def _generate_pooling_code(self):
        """
        Generate loop statements for the desired pooling operation.
        """
        # Operation to be performed: sum, max, min, mean
        operation = self.synapse_type.operation

        # Main code
        # default value for sum in code depends on operation
        sum_default = "0.0"
        if self.synapse_type.operation == "min":
            sum_default = "std::numeric_limits<%(float_prec)s>::max()" % {'float_prec': Global.config['precision']}
        elif self.synapse_type.operation == "max":
            sum_default = "std::numeric_limits<%(float_prec)s>::min()" % {'float_prec': Global.config['precision']}

        code = """
            sum = %(sum_default)s;
""" % {'sum_default': sum_default}

        # Generate for loops
        for dim in range(self.dim_pre):
            ind_dict = {
                'index': indices[dim],
                'size': self.extent[dim]
            }
            if self.extent[dim] > 1:
                code += """
            for(int %(index)s_w = 0; %(index)s_w < %(size)s; %(index)s_w++){
    """ % ind_dict

        # Compute indices
        for dim in range(self.dim_pre):
            ind_dict = {
                'index': indices[dim],
                'dim': dim
            }
            if self.extent[dim] > 1:
                code += """
                int %(index)s_pre = coord[%(dim)s] + %(index)s_w;""" % ind_dict
            else:
                code += """
                int %(index)s_pre = coord[%(dim)s];""" % ind_dict

        # Check indices
        for dim in range(self.dim_pre):
            ind_dict = {
                'index': indices[dim],
                'max_size': self.pre.geometry[dim] - 1
            }
            code += """
                if ((%(index)s_pre < 0) ||(%(index)s_pre > %(max_size)s)){
                    continue;
                }""" % ind_dict

        # Compute pre-synaptic rank
        code += """
                rk_pre = %(value)s;""" % {'value': self._coordinates_to_rank('pre', self.pre.geometry)}

        # Compute the value to pool
        psp = self.synapse_type.description['psp']['cpp'] % {
            'id_pre': self.pre.id,
            'id_post': self.post.id,
            'local_index': '[i][j]',
            'global_index': '[i]',
            'pre_index': '[rk_pre]',
            'post_index': '[rk_post]',
            'pre_prefix': 'pop'+str(self.pre.id)+'.',
            'post_prefix': 'pop'+str(self.post.id)+'.'
        }

        # Delays
        if self.delays > Global.config['dt']:
            psp = psp.replace(
                'pop%(id_pre)s.r[rk_pre]' % {'id_pre': self.pre.id},
                'pop%(id_pre)s._delayed_r[%(delay)s][rk_pre]' % {'id_pre': self.pre.id, 'delay': str(int(self.delays/Global.config['dt'])-1)}
            )

        # Apply the operation
        if operation == "max":
            code += """
                %(float_prec)s _psp = %(psp)s;
                if(_psp > sum) sum = _psp;"""
        elif operation == "min":
            code += """
                %(float_prec)s _psp = %(psp)s;
                if(_psp < sum) sum = _psp;"""
        elif operation == "sum":
            code += """
                sum += %(psp)s;"""
        elif operation == "mean":
            code += """
                sum += %(psp)s;"""
        else:
            Global._error('SharedProjection: Operation', operation, 'is not implemented yet for shared projections with pooling.')

        # Close for loops
        for dim in range(self.dim_pre):
            if self.extent[dim] > 1:
                code += """
            }"""

        impl_code = code % {
            'id_proj': self.id,
            'target': self.target,
            'id_pre': self.pre.id, 'name_pre': self.pre.name, 'size_pre': self.pre.size,
            'id_post': self.post.id, 'name_post': self.post.name, 'size_post': self.post.size,
            'psp': psp,
            'float_prec': Global.config['precision']
        }

        if operation == "mean":
            size = 1
            for dim in range(self.pre.dimension):
                size *= self.extent[dim]
            sum_code = "sum/" + str(size)
        else:
            sum_code = "sum"

        return impl_code, sum_code

    def _generate_omp(self, convolve_code, sum_code):
        """
        Update the ProjectionGenerator._specific_template structure and bypass the standard openMP code generation.

        :param convolve_code:
        :param sum_code:
        """
        # default value for sum in code depends on operation
        sum_default = "0.0"
        if self.synapse_type.operation == "min":
            sum_default = "std::numeric_limits<%(float_prec)s>::max()" % {'float_prec': Global.config['precision']}
        elif self.synapse_type.operation == "max":
            sum_default = "std::numeric_limits<%(float_prec)s>::min()" % {'float_prec': Global.config['precision']}

        # Specific template for generation
        pool_dict = deepcopy(pooling_template_omp)
        for key, value in pool_dict.items():
            value = value % {
                'id_proj': self.id,
                'size_post': self.post.size,
                'sum_default': sum_default,
                'float_prec': Global.config['precision']
            }
            pool_dict[key] = value
        self._specific_template.update(pool_dict)

        # OMP code
        omp_code = ""
        if Global.config['num_threads'] > 1:
            omp_code = """
        #pragma omp for private(sum, rk_pre, coord) %(psp_schedule)s""" % {
                'psp_schedule': "" if not 'psp_schedule' in self._omp_config.keys() else self._omp_config[
                    'psp_schedule']}

        # HD ( 16.10.2015 ):
        # pre-load delayed firing rate in a local array, so we
        # prevent multiple accesses to pop%(id_pre)s._delayed_r[%(delay)s]
        if self.delays > Global.config['dt']:
            pre_load_r = """
        // pre-load delayed firing rate
        auto delayed_r = pop%(id_pre)s._delayed_r[%(delay)s];
        """ % {'id_pre': self.pre.id, 'delay': str(int(self.delays / Global.config['dt']) - 1)}
        else:
            pre_load_r = ""

        # Target variable depends on neuron type
        target_code = "_sum_%(target)s" if self.post.neuron_type.type=="rate" else "g_%(target)s"
        target_code %= {'target': self.target}

        # Compute sum
        wsum = """
        if ( _transmission && pop%(id_pre)s._active ) {
        std::vector<int> coord;
""" + pre_load_r + """
        %(omp_code)s
        for(int i = 0; i < %(size_post)s; i++){
            coord = pre_coords[i];
""" + convolve_code + """
            pop%(id_post)s.%(target)s[i] += """ + sum_code + """;
        } // for
        } // if
"""

        # Delays
        self._specific_template['wrapper_init_delay'] = ""

        # Dictionary keys
        psp_dict = {
            'id_proj': self.id,
            'target': target_code,
            'id_pre': self.pre.id, 'name_pre': self.pre.name,
            'size_pre': self.pre.size,
            'id_post': self.post.id, 'name_post': self.post.name,
            'size_post': self.post.size,
            'omp_code': omp_code,
            'convolve_code': convolve_code
        }

        # Psp code
        self._specific_template['psp_code'] = wsum % psp_dict
        self._specific_template['size_in_bytes'] = """
        // connectivity
        size_in_bytes += sizeof(std::vector<int>);
        size_in_bytes += pre_coords.capacity() * sizeof(int);

        size_in_bytes += sizeof(std::vector<std::vector<int>>);
        size_in_bytes += pre_coords.capacity() * sizeof(std::vector<int>);
        for (auto it = pre_coords.begin(); it != pre_coords.end(); it++) {
            size_in_bytes += it->capacity() * sizeof(int);
        }
"""

        # Clean-up
        self._specific_template['clear_container'] = pooling_template_omp["clear"]

    def _generate_cuda(self):
        """
        Update the ProjectionGenerator._specific_template structure and bypass the standard CUDA code generation.
        """
        # Extract operation and pre-synaptic variable which should be processed
        pool_operation = self.synapse_type.operation
        pre_var = self.synapse_type.psp.split(".")[1]

        # default value for sum in code depends on operation
        sum_default = "0.0"
        if pool_operation == "min":
            sum_default = "FLT_MAX"
        elif pool_operation == "max":
            sum_default = "FLT_MIN"

        # operation to perform
        pool_op_code = cuda_op_code[pool_operation] % {'float_prec': Global.config['precision']}

        # mean operation requires one additional computation
        if pool_operation == "mean":
            size = 1
            for dim in range(self.pre.dimension):
                size *= self.extent[dim]
            final_result = "psp[bIdx] += local_res/" + str(size) + ";"
        else:
            final_result = "psp[bIdx] += local_res;"

        # result dictionary with code for
        # body, call and header
        pool_template = {}
        base_ids = {
            'id_proj': self.id,
            'id_pre': self.pre.id,
            'id_post': self.post.id,
            'target': self.target,
            'float_prec': Global.config['precision'],
            'size_post': self.post.size # TODO: population views?
        }

        # The correct templates depends on both
        # kernel-geometry and extent
        if len(self.pre.geometry) == 2:
            # For small extents, we compute multiple coords within one warp. If one extent can fill alone
            # a half-warp we switch to the other implementation.
            if self.extent[0] < 6:

                pool_op_reduce_code = cuda_pooling_code_2d_small_extent['reduce_code'][pool_operation] % {
                    'float_prec': Global.config['precision'],
                    'row_extent': int(self.extent[0]),
                    'col_extent': int(self.extent[1])
                }

                pool_dict = deepcopy(base_ids)
                pool_dict.update({
                    'sum_default': sum_default,
                    'row_extent': int(self.extent[0]),
                    'col_extent': int(self.extent[1]),
                    'row_size': int(self.pre.geometry[0]),
                    'col_size': int(self.pre.geometry[1]),
                    'operation': tabify(pool_op_code, 3),
                    'operation_reduce': pool_op_reduce_code,
                    'final_result': final_result
                })

                pool_template['psp_body'] = cuda_pooling_code_2d_small_extent['psp_body'] % pool_dict
                pool_template['psp_invoke'] = cuda_pooling_code_2d_small_extent['psp_invoke'] % pool_dict
                pool_template['psp_header'] = cuda_pooling_code_2d_small_extent['psp_header'] % pool_dict
                pool_template['psp_call'] = cuda_pooling_code_2d_small_extent['psp_call'] % pool_dict

            else:
                pool_op_reduce_code = cuda_pooling_code_2d['reduce_code'][pool_operation] % {
                    'float_prec': Global.config['precision'],
                    'row_extent': int(self.extent[0]),
                    'col_extent': int(self.extent[1])
                }

                pool_dict = deepcopy(base_ids)
                pool_dict.update({
                    'sum_default': sum_default,
                    'row_extent': int(self.extent[0]),
                    'col_extent': int(self.extent[1]),
                    'row_size': int(self.pre.geometry[0]),
                    'col_size': int(self.pre.geometry[1]),
                    'operation': tabify(pool_op_code, 3),
                    'operation_reduce': tabify(pool_op_reduce_code, 2),
                    'final_result': final_result
                })

                pool_template['psp_body'] = remove_trailing_spaces(cuda_pooling_code_2d['psp_body'] % pool_dict)
                pool_template['psp_invoke'] = remove_trailing_spaces(cuda_pooling_code_2d['psp_invoke'] % pool_dict)
                pool_template['psp_header'] = cuda_pooling_code_2d['psp_header'] % pool_dict
                pool_template['psp_call'] = cuda_pooling_code_2d['psp_call'] % pool_dict

        elif len(self.pre.geometry) == 3:

            pool_dict = deepcopy(base_ids)
            pool_dict.update({
                'sum_default': sum_default,
                'row_extent': self.extent[0],
                'col_extent': self.extent[1],
                'plane_extent': self.extent[2],
                'row_size': self.pre.geometry[0],
                'col_size': self.pre.geometry[1],
                'plane_size': self.pre.geometry[2],
                'pre_var': pre_var,
                'operation': tabify(pool_op_code, 4),
                'final_result': final_result
            })

            pool_template['psp_body'] = remove_trailing_spaces(cuda_pooling_code_3d['psp_body'] % pool_dict)
            pool_template['psp_invoke'] = remove_trailing_spaces(cuda_pooling_code_3d['psp_invoke'] % pool_dict)
            pool_template['psp_header'] = cuda_pooling_code_3d['psp_header'] % pool_dict
            pool_template['psp_call'] = cuda_pooling_code_3d['psp_call'] % pool_dict

        else:
            raise NotImplementedError

        # Post-neuron is a spike neuron (e.g., part of ANN-to-SNN conversion)
        if self.post.neuron_type.type == "spike":
            pool_template['psp_call'] = pool_template['psp_call'].replace("gpu__sum_"+self.target, "gpu_g_"+self.target)

        # Update psp fields
        self._specific_template.update(pool_template)

        # Specific template for generation (wrapper, etc)
        pool_dict = deepcopy(pooling_template_cuda)
        for key, value in pool_dict.items():
            value = value % base_ids
            pool_dict[key] = value
        self._specific_template.update(pool_dict)

        self._specific_template['wrapper_connector_call'] = ""
        self._specific_template['access_parameters_variables'] = ""

        self._specific_template['size_in_bytes'] = "//TODO:\n"

        # Clean-up
        self._specific_template['clear_container'] = pooling_template_cuda["clear"]

    @staticmethod
    def _coordinates_to_rank(name, geometry):
        """
        Generate the code for array access, for instance used
        for pre-synaptic ranks.
        """
        dim = len(geometry)

        txt = ""

        for d in range(dim):
            if txt == "":   # first coordinate is special
                txt = indices[0] + "_" + name
            else:
                txt = str(geometry[d]) + '*(' + txt + ') + ' + indices[d] + '_' + name

        return txt

    ##############################
    ## Override useless methods
    ##############################
    def _data(self):
        "Disable saving."
        desc = {}
        desc['post_ranks'] = self.post_ranks
        desc['attributes'] = self.attributes
        desc['parameters'] = self.parameters
        desc['variables'] = self.variables

        desc['dendrites'] = []
        desc['number_of_synapses'] = 0
        return desc

    def save_connectivity(self, filename):
        "Not available."
        Global._warning('Pooling projections can not be saved.')
    def save(self, filename):
        "Not available."
        Global._warning('Pooling projections can not be saved.')
    def load(self, filename):
        "Not available."
        Global._warning('Pooling projections can not be loaded.')
    def receptive_fields(self, variable = 'w', in_post_geometry = True):
        "Not available."
        Global._warning('Pooling projections can not display receptive fields.')
    def connectivity_matrix(self, fill=0.0):
        "Not available."
        Global._warning('Pooling projections can not display connectivity matrices.')

__init__(pre, post, target, psp='pre.r', operation='max', name=None, copied=False) #

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")

Source code in ANNarchy/extensions/convolution/Pooling.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def __init__(self, pre, post, target, psp="pre.r", operation="max", name=None, copied=False):
    """
    :param pre: pre-synaptic population (either its name or a ``Population`` object).
    :param post: post-synaptic population (either its name or a ``Population`` object).
    :param target: type of the connection
    :param operation: pooling function to be applied ("max", "min", "mean")
    """
    # Sanity check
    if not operation in ["max", "mean", "min"]:
        Global._error("Pooling: the operation must be either 'max', 'mean' or 'min'.")
    self.operation = operation

    # Store for _copy
    self.psp = psp

    SpecificProjection.__init__(
        self,
        pre,
        post,
        target,
        synapse=SharedSynapse(psp=psp, operation=operation, name="Pooling operation", description=operation+"-pooling operation over the pre-synaptic population."),
        name=name,
        copied=copied
    )

    # check dimensions of populations, should not exceed 4
    self.dim_pre = self.pre.dimension
    self.dim_post = self.post.dimension
    if self.dim_post > 4:
        Global._error('Pooling: Too many dimensions for the post-synaptic population (maximum 4).')
    if self.dim_pre > 4:
        Global._error('Pooling: Too many dimensions for the pre-synaptic population (maximum 4).')

    # Disable saving
    self._saveable = False

connect_pooling(extent=None, delays=0.0) #

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

Source code in ANNarchy/extensions/convolution/Pooling.py
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def connect_pooling(self, extent=None, delays=0.0):
    """
    :param 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.
    :param delays: synaptic delay in ms
    """

    # process extent
    self.extent_init = extent
    if extent is None:  # compute the extent automatically
        if self.pre.dimension != self.post.dimension:
            Global._error(
                'Pooling: If you do not provide the extent parameter, the two populations must have the same number of dimensions.')

        extent = list(self.pre.geometry)
        for dim in range(self.pre.dimension):
            extent[dim] /= self.post.geometry[dim]
            if self.pre.geometry[dim] != extent[dim] * self.post.geometry[dim]:
                Global._error(
                    'Pooling: Unable to compute the extent of the pooling area: the number of neurons do not match.')

    elif not isinstance(extent, tuple):
        Global._error('Pooling: You must provide a tuple for the extent of the pooling operation.')

    self.extent = list(extent)
    if len(self.extent) < self.pre.dimension:
        Global._error('Pooling: You must provide a tuple for the extent of the pooling operation.')

    # process delays
    self.delays = delays

    # Generate the pre-synaptic coordinates
    self._generate_extent_coordinates()

    # create fake LIL
    self._create()

    return self

connectivity_matrix(fill=0.0) #

Not available.

Source code in ANNarchy/extensions/convolution/Pooling.py
624
625
626
def connectivity_matrix(self, fill=0.0):
    "Not available."
    Global._warning('Pooling projections can not display connectivity matrices.')

load(filename) #

Not available.

Source code in ANNarchy/extensions/convolution/Pooling.py
618
619
620
def load(self, filename):
    "Not available."
    Global._warning('Pooling projections can not be loaded.')

receptive_fields(variable='w', in_post_geometry=True) #

Not available.

Source code in ANNarchy/extensions/convolution/Pooling.py
621
622
623
def receptive_fields(self, variable = 'w', in_post_geometry = True):
    "Not available."
    Global._warning('Pooling projections can not display receptive fields.')

save(filename) #

Not available.

Source code in ANNarchy/extensions/convolution/Pooling.py
615
616
617
def save(self, filename):
    "Not available."
    Global._warning('Pooling projections can not be saved.')

save_connectivity(filename) #

Not available.

Source code in ANNarchy/extensions/convolution/Pooling.py
612
613
614
def save_connectivity(self, filename):
    "Not available."
    Global._warning('Pooling projections can not be saved.')

ANNarchy.extensions.convolution.Copy #

:copyright: Copyright 2013 - now, see AUTHORS. :license: GPLv2, see LICENSE for details.

Copy #

Bases: SpecificProjection

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)
Source code in ANNarchy/extensions/convolution/Copy.py
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
class Copy(SpecificProjection):
    """
    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:

    ```python
    proj = Projection(pop1, pop2, "exc")
    proj.connect_fixed_probability(0.1, 0.5)

    copy_proj = Copy(pop1, pop3, "exc")
    copy_proj.connect_copy(proj)
    ```

    """
    def __init__(self, pre, post, target, psp="pre.r * w", operation="sum", name=None, copied=False):
        """
        :param pre: pre-synaptic population (either its name or a ``Population`` object).
        :param post: post-synaptic population (either its name or a ``Population`` object).
        :param target: type of the connection
        :param psp: continuous influence of a single synapse on the post-synaptic neuron (default for rate-coded: ``w*pre.r``).
        :param operation: operation (sum, max, min, mean) performed by the kernel (default: sum).
        """

        # Create the description, but it will not be used for generation
        SpecificProjection.__init__(
            self,
            pre=pre,
            post=post,
            target=target,
            synapse = SharedSynapse(psp=psp, operation=operation),
            name=name,
            copied=copied
        )

    def connect_copy(self, projection):
        """
        :param projection: Existing projection to copy.
        """
        self.projection = projection

        # Sanity checks
        if not isinstance(self.projection, Projection):
            Global._error('Copy: You must provide an existing projection to copy().')

        if isinstance(self.projection, (Convolution, Pooling)):
            Global._error('Copy: You can only copy regular projections, not shared projections.')

        if not self.pre.geometry == self.projection.pre.geometry or not self.post.geometry == self.projection.post.geometry:
            Global._error('Copy: When copying a projection, the geometries must be the same.')

        # Dummy weights
        self.weights = None
        self.pre_coordinates = []

        # Finish building the synapses
        self._create()

        return self

    def _copy(self, pre, post):
        "Returns a copy of the projection when creating networks. Internal use only."
        raise NotImplementedError

    def _create(self):
        # create fake LIL object, just for compilation.
        try:
            from ANNarchy.core.cython_ext.Connector import LILConnectivity
        except Exception as e:
            Global._print(e)
            Global._error('ANNarchy was not successfully installed.')

        lil = LILConnectivity()
        lil.max_delay = self.delays
        lil.uniform_delay = self.delays
        self.connector_name = "Copy"
        self.connector_description = "Copy projection"
        self._store_connectivity(self._load_from_lil, (lil, ), self.delays)

    def _connect(self, module):
        """
        Builds up dendrites either from list or dictionary. Called by instantiate().
        """
        if not self._connection_method:
            Global._error('Copy: The projection between ' + self.pre.name + ' and ' + self.post.name + ' is declared but not connected.')

        # Create the Cython instance
        proj = getattr(module, 'proj'+str(self.id)+'_wrapper')
        self.cyInstance = proj(self.weights, self.pre_coordinates)

        # Define the list of postsynaptic neurons
        self.post_ranks = list(range(self.post.size))

        # Set delays after instantiation
        if self.delays > 0.0:
            self.cyInstance.set_delay(self.delays/Global.config['dt'])

        return True

    def _generate(self):
        """
        Overrides default code generation. This function is called during the code generation procedure.
        """
        if Global._check_paradigm("openmp"):
            self._generate_omp()
        elif Global._check_paradigm("cuda"):
            self._generate_cuda()
        else:
            raise NotImplementedError

    def generate_omp(self):
        """
        Code generation of CopyProjection object for the openMP paradigm.
        """
        # Set the projection specific parameters
        copy_proj_dict = deepcopy(copy_proj_template)
        for key, value in copy_proj_dict.items():
            value = value % {
                'id_proj': self.id,
                'id_copy': self.projection.id,
                'float_prec': Global.config['precision']
            }
            copy_proj_dict[key] = value

        # Update specific template
        self._specific_template.update(copy_proj_dict)

        # OMP code if more then one thread
        if Global.config['num_threads'] > 1:
            omp_code = '#pragma omp for private(sum)' if self.post.size > Global.OMP_MIN_NB_NEURONS else ''
        else:
            omp_code = ""

        # PSP
        psp = self.synapse_type.description['psp']['cpp']  % {
            'id_pre': self.pre.id,
            'id_post': self.post.id,
            'local_index':'[i][j]',
            'global_index': '[i]',
            'pre_index': '[pre_rank[i][j]]',
            'post_index': '[post_rank[i]]',
            'pre_prefix': 'pop'+str(self.pre.id)+'.',
            'post_prefix': 'pop'+str(self.post.id)+'.'}
        psp = psp.replace('rk_pre', 'pre_rank[i][j]').replace(';', '')

        # Take delays into account if any
        if self.delays > Global.config['dt']:
            psp = psp.replace(
                'pop%(id_pre)s.r[rk_pre]' % {'id_pre': self.pre.id},
                'pop%(id_pre)s._delayed_r[delay-1][rk_pre]' % {'id_pre': self.pre.id}
                # TODO HD: wouldn't it be much better to reduce delay globaly, instead of the substraction here???
            )

        # Select template for operation to be performed: sum, max, min, mean
        try:
            sum_code = copy_sum_template[self.synapse_type.operation]
        except KeyError:
            Global._error("CopyProjection: the operation ", self.synapse_type.operation, ' is not available.')

        # Finalize code
        self.generator['omp']['body_compute_psp'] = sum_code % {
            'id_proj': self.id, 'target': self.target,
            'id_pre': self.pre.id, 'name_pre': self.pre.name,
            'id_post': self.post.id, 'name_post': self.post.name,
            'id': self.projection.id,
            'float_prec': Global.config['precision'],
            'omp_code': omp_code,
            'psp': psp
        }

    def _generate_cuda(self):
        """
        Code generation of CopyProjection object for the CUDA paradigm.

        Note: currently not implemented (TODO HD)
        """
        raise NotImplementedError

    ##############################
    ## Override useless methods
    ##############################
    def _data(self):
        "Disable saving."
        desc = {}
        desc['post_ranks'] = self.post_ranks
        desc['attributes'] = self.attributes
        desc['parameters'] = self.parameters
        desc['variables'] = self.variables

        desc['dendrites'] = []
        desc['number_of_synapses'] = 0
        return desc

    def save_connectivity(self, filename):
        "Not available."
        Global._warning('Copied projections can not be saved.')
    def save(self, filename):
        "Not available."
        Global._warning('Copied projections can not be saved.')
    def load(self, filename):
        "Not available."
        Global._warning('Copied projections can not be loaded.')
    def receptive_fields(self, variable = 'w', in_post_geometry = True):
        "Not available."
        Global._warning('Copied projections can not display receptive fields.')
    def connectivity_matrix(self, fill=0.0):
        "Not available."
        Global._warning('Copied projections can not display connectivity matrices.')

__init__(pre, post, target, psp='pre.r * w', operation='sum', name=None, copied=False) #

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).

Source code in ANNarchy/extensions/convolution/Copy.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def __init__(self, pre, post, target, psp="pre.r * w", operation="sum", name=None, copied=False):
    """
    :param pre: pre-synaptic population (either its name or a ``Population`` object).
    :param post: post-synaptic population (either its name or a ``Population`` object).
    :param target: type of the connection
    :param psp: continuous influence of a single synapse on the post-synaptic neuron (default for rate-coded: ``w*pre.r``).
    :param operation: operation (sum, max, min, mean) performed by the kernel (default: sum).
    """

    # Create the description, but it will not be used for generation
    SpecificProjection.__init__(
        self,
        pre=pre,
        post=post,
        target=target,
        synapse = SharedSynapse(psp=psp, operation=operation),
        name=name,
        copied=copied
    )

connect_copy(projection) #

Parameters:

  • projection

    Existing projection to copy.

Source code in ANNarchy/extensions/convolution/Copy.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def connect_copy(self, projection):
    """
    :param projection: Existing projection to copy.
    """
    self.projection = projection

    # Sanity checks
    if not isinstance(self.projection, Projection):
        Global._error('Copy: You must provide an existing projection to copy().')

    if isinstance(self.projection, (Convolution, Pooling)):
        Global._error('Copy: You can only copy regular projections, not shared projections.')

    if not self.pre.geometry == self.projection.pre.geometry or not self.post.geometry == self.projection.post.geometry:
        Global._error('Copy: When copying a projection, the geometries must be the same.')

    # Dummy weights
    self.weights = None
    self.pre_coordinates = []

    # Finish building the synapses
    self._create()

    return self

connectivity_matrix(fill=0.0) #

Not available.

Source code in ANNarchy/extensions/convolution/Copy.py
225
226
227
def connectivity_matrix(self, fill=0.0):
    "Not available."
    Global._warning('Copied projections can not display connectivity matrices.')

generate_omp() #

Code generation of CopyProjection object for the openMP paradigm.

Source code in ANNarchy/extensions/convolution/Copy.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
def generate_omp(self):
    """
    Code generation of CopyProjection object for the openMP paradigm.
    """
    # Set the projection specific parameters
    copy_proj_dict = deepcopy(copy_proj_template)
    for key, value in copy_proj_dict.items():
        value = value % {
            'id_proj': self.id,
            'id_copy': self.projection.id,
            'float_prec': Global.config['precision']
        }
        copy_proj_dict[key] = value

    # Update specific template
    self._specific_template.update(copy_proj_dict)

    # OMP code if more then one thread
    if Global.config['num_threads'] > 1:
        omp_code = '#pragma omp for private(sum)' if self.post.size > Global.OMP_MIN_NB_NEURONS else ''
    else:
        omp_code = ""

    # PSP
    psp = self.synapse_type.description['psp']['cpp']  % {
        'id_pre': self.pre.id,
        'id_post': self.post.id,
        'local_index':'[i][j]',
        'global_index': '[i]',
        'pre_index': '[pre_rank[i][j]]',
        'post_index': '[post_rank[i]]',
        'pre_prefix': 'pop'+str(self.pre.id)+'.',
        'post_prefix': 'pop'+str(self.post.id)+'.'}
    psp = psp.replace('rk_pre', 'pre_rank[i][j]').replace(';', '')

    # Take delays into account if any
    if self.delays > Global.config['dt']:
        psp = psp.replace(
            'pop%(id_pre)s.r[rk_pre]' % {'id_pre': self.pre.id},
            'pop%(id_pre)s._delayed_r[delay-1][rk_pre]' % {'id_pre': self.pre.id}
            # TODO HD: wouldn't it be much better to reduce delay globaly, instead of the substraction here???
        )

    # Select template for operation to be performed: sum, max, min, mean
    try:
        sum_code = copy_sum_template[self.synapse_type.operation]
    except KeyError:
        Global._error("CopyProjection: the operation ", self.synapse_type.operation, ' is not available.')

    # Finalize code
    self.generator['omp']['body_compute_psp'] = sum_code % {
        'id_proj': self.id, 'target': self.target,
        'id_pre': self.pre.id, 'name_pre': self.pre.name,
        'id_post': self.post.id, 'name_post': self.post.name,
        'id': self.projection.id,
        'float_prec': Global.config['precision'],
        'omp_code': omp_code,
        'psp': psp
    }

load(filename) #

Not available.

Source code in ANNarchy/extensions/convolution/Copy.py
219
220
221
def load(self, filename):
    "Not available."
    Global._warning('Copied projections can not be loaded.')

receptive_fields(variable='w', in_post_geometry=True) #

Not available.

Source code in ANNarchy/extensions/convolution/Copy.py
222
223
224
def receptive_fields(self, variable = 'w', in_post_geometry = True):
    "Not available."
    Global._warning('Copied projections can not display receptive fields.')

save(filename) #

Not available.

Source code in ANNarchy/extensions/convolution/Copy.py
216
217
218
def save(self, filename):
    "Not available."
    Global._warning('Copied projections can not be saved.')

save_connectivity(filename) #

Not available.

Source code in ANNarchy/extensions/convolution/Copy.py
213
214
215
def save_connectivity(self, filename):
    "Not available."
    Global._warning('Copied projections can not be saved.')