0
$\begingroup$

I trained a model using Keras from this example. The model summary showed me this result

    Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 24, 24, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 12, 12, 64)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 12, 12, 64)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 9216)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               1179776   
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                1290      
=================================================================
Total params: 1,199,882
Trainable params: 1,199,882
Non-trainable params: 0

There are 8 layers and model.get_weights() also shows the first dimension of 8. As I understood correctly things like Pooling and Flatten are "operators" over the input matrices, so why it is presented as a layer? How to understand what is stored in weight array for example in Pooling layer (model.get_weights()[2])?

$\endgroup$
0
$\begingroup$

If you print those shapes using below for loop

weights_m=model.get_weights()
for i in range(8):
  print(weights_m[i].shape)

you will get output as

(3, 3, 1, 32)
(32,)
(3, 3, 32, 64)
(64,)
(9216, 128)
(128,)
(128, 10)
(10,)

so we will get one layer weight and bias. we have a total of 4 layers(2 conv + 2 dense) so 8 weight vectors.

| improve this answer | |
$\endgroup$
  • $\begingroup$ subarrays with only one dimension are biases? $\endgroup$ – anatoly Apr 12 at 18:57
  • $\begingroup$ Yes. Those 1d arrays are biases. For every layer, you are using bias so 4 laters, 4 bias vectors. $\endgroup$ – Uday Apr 12 at 19:01
  • $\begingroup$ ok thx it's clear $\endgroup$ – anatoly Apr 12 at 19:03
0
$\begingroup$

model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))

3x3 weights + 1 bias for each feature map. It has 32 feature map.
This is first 2 lines. (3X3+1)X32. At this points feature size is 26X26(valid padding)

model.add(Conv2D(64, (3, 3), activation='relu'))

3x3 weights connected to 32 previous feature maps + 1 bias for each feature map. It has 64 feature maps.
This is 3rd and 4th lines (3X3X32+1)X64. At this points feature size is 24X24(valid padding)

model.add(MaxPooling2D(pool_size=(2, 2)))

At this points, feature size is 12X12(Pooling)

model.add(Dropout(0.25)) model.add(Flatten())

64 feature maps of size 12X12 are flattened. 144X64=9216 mapped to 128 neurons + 128 biases
This is 5th and 6th lines

model.add(Dense(128, activation='relu')) model.add(Dropout(0.5))

128 neurons are mapped to 10 neurons + 10 biases
This is 7th and 8th lines

model.add(Dense(num_classes, activation='softmax'))




how-to-calculate-the-number-of-parameters-of-convolutional-neural-networks

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.