Answers.Body: <p>Calculating the number of parameters in a CNN is very straightforward.</p>
  <p>A CNN is composed of different <strong>filters</strong>, which are essentially 3d tensors. CNN weights are <strong>shared</strong>, meaning they are used multiple times and reused in different locations. Each layer has <span class="math-container">$n$</span> tensors, each with dimension <span class="math-container">$w \times h \times c$</span>, where <span class="math-container">$w$</span> = width, <span class="math-container">$h$</span> = height, <span class="math-container">$c$</span> = channels (the input channel size). Therefore, the number of parameters of a convolutional layer is <span class="math-container">$w * h * c * n$</span>. There is also a bias for each output channel, so the number of biases is <span class="math-container">$n$</span>. At the end the parameter number is calculated with: <span class="math-container">$n * w * h * c + n$</span>. See more about this in hear: <a href="https://towardsdatascience.com/understanding-and-calculating-the-number-of-parameters-in-convolution-neural-networks-cnns-fc88790d530d" rel="nofollow noreferrer">Article</a></p>
  <p>The pooling layer does not have weights, it only has <strong>hyperparameters</strong>. You may have confused the two. There are hyperparameters for the stride, the factor and etc. These are predefined and not trainable.</p>
  <p>For Keras, you can use the solution <a href="https://stackoverflow.com/a/35827171/4886927">here</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-15T15:15:06.447Z
Answers.LastActivityDate: 2021-12-28T22:49:37.587Z
Answers.LastEditDate: 2021-12-28T22:49:37.587Z
Answers.UserDisplayName: Clement
Body: <p>How can we theoretically compute the number of weights considering a convolutional neural network that is used to classify images into two classes:</p>
  <ul>
  <li>INPUT: 100x100 gray-scale images.</li>
  <li>LAYER 1: Convolutional layer with 60 7x7 convolutional filters (stride=1, valid
  padding).</li>
  <li>LAYER 2: Convolutional layer with 100 5x5 convolutional filters (stride=1, valid
  padding).</li>
  <li>LAYER 3: A max pooling layer that down-samples Layer 2 by a factor of 4 (e.g., from 500x500 to 250x250)</li>
  <li>LAYER 4: Dense layer with 250 units</li>
  <li>LAYER 5: Dense layer with 200 units</li>
  <li>LAYER 6: Single output unit</li>
  </ul>
  <p>Assume the existence of biases in each layer. Moreover, the pooling layer has a weight (similar to AlexNet)</p>
  <p>How many weights does this network have?</p>
  <p>Here would be the corresponding model in Keras, but note that I am asking for how to calculate this with a formula, not in Keras.</p>
  <pre><code>import keras
  from keras.models import Sequential
  from keras.layers import Dense
  from keras.layers import Conv2D, MaxPooling2D
  
  model = Sequential()
  model.add(Conv2D(60, (7, 7), input_shape = (100, 100, 1), padding=&quot;same&quot;, activation=&quot;relu&quot;)) # Layer 1
  model.add(Conv2D(100, (5, 5), padding=&quot;same&quot;, activation=&quot;relu&quot;)) # Layer 2
  model.add(MaxPooling2D(pool_size=(2, 2))) # Layer 3
  model.add(Dense(250)) # Layer 4
  model.add(Dense(200)) # Layer 5
  
  model.summary()
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-28T22:37:16.503Z
Comments.Text: [Here](https://ai.stackexchange.com/q/18663/2444) is a related (if not duplicate) question.
Comments.UserDisplayName: nbro
Tags: <neural-networks><convolutional-neural-networks><keras><filters><convolution-arithmetic>
Title: How to compute the number of weights of a CNN?
UserDisplayName: estamos
ThreadText: # How to compute the number of weights of a CNN?
  
  (Asked by: username_0 on 2019-12-14)
  
  How can we theoretically compute the number of weights considering a convolutional neural network that is used to classify images into two classes:
  
  * INPUT: 100x100 gray-scale images.
  * LAYER 1: Convolutional layer with 60 7x7 convolutional filters (stride=1, valid
    padding).
  * LAYER 2: Convolutional layer with 100 5x5 convolutional filters (stride=1, valid
    padding).
  * LAYER 3: A max pooling layer that down-samples Layer 2 by a factor of 4 (e.g., from 500x500 to 250x250)
  * LAYER 4: Dense layer with 250 units
  * LAYER 5: Dense layer with 200 units
  * LAYER 6: Single output unit
  
  Assume the existence of biases in each layer. Moreover, the pooling layer has a weight (similar to AlexNet)
  
  How many weights does this network have?
  
  Here would be the corresponding model in Keras, but note that I am asking for how to calculate this with a formula, not in Keras.
  
  ```
  import keras
  from keras.models import Sequential
  from keras.layers import Dense
  from keras.layers import Conv2D, MaxPooling2D
  
  model = Sequential()
  model.add(Conv2D(60, (7, 7), input_shape = (100, 100, 1), padding="same", activation="relu")) # Layer 1
  model.add(Conv2D(100, (5, 5), padding="same", activation="relu")) # Layer 2
  model.add(MaxPooling2D(pool_size=(2, 2))) # Layer 3
  model.add(Dense(250)) # Layer 4
  model.add(Dense(200)) # Layer 5
  
  model.summary()
  
  ```
  
  **Tags:** neural-networks, convolutional-neural-networks, keras, filters, convolution-arithmetic
  
  > **Comments:**
  > **username_1**: [Here](https://ai.stackexchange.com/q/18663/2444) is a related (if not duplicate) question.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-12-15. Score: 2 | Accepted answer
  
  Calculating the number of parameters in a CNN is very straightforward.
  
  A CNN is composed of different **filters**, which are essentially 3d tensors. CNN weights are **shared**, meaning they are used multiple times and reused in different locations. Each layer has $n$ tensors, each with dimension $w \times h \times c$, where $w$ = width, $h$ = height, $c$ = channels (the input channel size). Therefore, the number of parameters of a convolutional layer is $w \* h \* c \* n$. There is also a bias for each output channel, so the number of biases is $n$. At the end the parameter number is calculated with: $n \* w \* h \* c + n$. See more about this in hear: [Article](https://towardsdatascience.com/understanding-and-calculating-the-number-of-parameters-in-convolution-neural-networks-cnns-fc88790d530d)
  
  The pooling layer does not have weights, it only has **hyperparameters**. You may have confused the two. There are hyperparameters for the stride, the factor and etc. These are predefined and not trainable.
  
  For Keras, you can use the solution [here](https://stackoverflow.com/a/35827171/4886927).
  
  
  ---
  
  