2
$\begingroup$

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()
$\endgroup$
1
  • 1
    $\begingroup$ Here is a related (if not duplicate) question. $\endgroup$
    – nbro
    Commented Dec 28, 2021 at 22:37

1 Answer 1

2
$\begingroup$

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

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.

$\endgroup$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .