How to Calculate the Number of Parameters in Keras Models

Original article was published by Yong Cui, Ph.D. on Artificial Intelligence on Medium

Calculation of Parameters

MaxPooling2D Layers

Let’s start with something simple. As you can notice, the number of parameters for all MaxPooling2D layers is all 0. The reason is that this layer doesn’t learn anything. What it does is to reduce the complexity of the model and to extract local features by finding the maximum values for each 2 x 2 pool.

As mentioned previously, the MaxPooling2D layer uses the output from the previous layer. Thus, the max_pooling2d layer’s input has a shape of (26, 26, 32), which is output from the conv2d layer. The max pooling is applied to each filter (n=32) with a shape of (26, 26). In the model, for the max_pooling2d layer, the size of the pool is 2 x 2, and thus the shape of the data will become (13, 13), which is (26 / 2, 26 / 2).

Similarly, for the second MaxPooling2D layer (i.e., max_pooling2d_1), the input shape is (11, 11, 64). By applying a 2 x 2 pooling, the resulting output shape becomes (5, 5, 64), as shown in the “Output Shape” column.