title: How to calculate the number of parameters for convolutional neural network?
body: <p>I'm using Lasagne to create a CNN for the MNIST dataset. I'm following closely to this example: <a href="http://blog.christianperone.com/2015/08/convolutional-neural-networks-and-feature-extraction-with-python/" rel="noreferrer">Convolutional Neural Networks and Feature Extraction with Python</a>.</p>
  
  <p>The CNN architecture I have at the moment, which doesn't include any dropout layers, is:</p>
  
  <pre><code>NeuralNet(
      layers=[('input', layers.InputLayer),        # Input Layer
              ('conv2d1', layers.Conv2DLayer),     # Convolutional Layer
              ('maxpool1', layers.MaxPool2DLayer), # 2D Max Pooling Layer
              ('conv2d2', layers.Conv2DLayer),     # Convolutional Layer
              ('maxpool2', layers.MaxPool2DLayer), # 2D Max Pooling Layer
              ('dense', layers.DenseLayer),        # Fully connected layer
              ('output', layers.DenseLayer),       # Output Layer
              ],
      # input layer
      input_shape=(None, 1, 28, 28),
  
      # layer conv2d1
      conv2d1_num_filters=32,
      conv2d1_filter_size=(5, 5),
      conv2d1_nonlinearity=lasagne.nonlinearities.rectify,
  
      # layer maxpool1
      maxpool1_pool_size=(2, 2),
  
      # layer conv2d2
      conv2d2_num_filters=32,
      conv2d2_filter_size=(3, 3),
      conv2d2_nonlinearity=lasagne.nonlinearities.rectify,
  
      # layer maxpool2
      maxpool2_pool_size=(2, 2),
  
  
      # Fully Connected Layer
      dense_num_units=256,
      dense_nonlinearity=lasagne.nonlinearities.rectify,
  
     # output Layer
      output_nonlinearity=lasagne.nonlinearities.softmax,
      output_num_units=10,
  
      # optimization method params
      update= momentum,
      update_learning_rate=0.01,
      update_momentum=0.9,
      max_epochs=10,
      verbose=1,
      )
  </code></pre>
  
  <p>This outputs the following Layer Information:</p>
  
  <pre><code>  #  name      size
  ---  --------  --------
    0  input     1x28x28
    1  conv2d1   32x24x24
    2  maxpool1  32x12x12
    3  conv2d2   32x10x10
    4  maxpool2  32x5x5
    5  dense     256
    6  output    10
  </code></pre>
  
  <p>and outputs the number of learnable parameters as <strong>217,706</strong></p>
  
  <p>I'm wondering how this number is calculated? I've read a number of resources, including this StackOverflow's <a href="https://stackoverflow.com/questions/28232235/how-to-calculate-the-number-of-parameters-of-convolutional-neural-networkscnns">question</a>, but none clearly generalizes the calculation.</p>
  
  <p>If possible, <em>can the calculation of the learnable parameters per layer be generalised?</em> </p>
  
  <p>For example, convolutional layer: number of filters x filter width x filter height.</p>