Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Understanding neural nets: sequential class

... understanding how each layer of a neural network changes the shape of the data as it flows through the network is a key part of truly understanding the mechanics of deep learning ~ David Foster, Generative Deep Learning

In this notebook we look at 3 ways of finding the output shape and number of parameters layer by layer:

  1. Keras’ built-in method: model.summary();

  2. our home-made method: mysummary(model);

  3. manual back-of-the-envelope calculation.

We beginning by defining our home-made method, mysummary(model). Worked examples of different network architectures, of fully connected and 2D convolution layers, will follow later.

This notebook is the sequential class version, which matches cell-by-cell with the companion function API version. Sequential class and functional API are two different ways of defining a neural network architecture in Keras.

Using TensorFlow backend.

Worked examples

In each cell that follows, we define a network architecture using the sequential class, and proceed to

  1. call Keras’ built-in method: model.summary();

  2. call mysummary(model) which we just defined;

  3. markup inline with our manual back-of-the-envelop calculation of the output shape and number of parameters layer by layer.

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 32)                25120     
_________________________________________________________________
dense_2 (Dense)              (None, 32)                1056      
=================================================================
Total params: 26,176
Trainable params: 26,176
Non-trainable params: 0
_________________________________________________________________
OUTPUT FROM mysummary(model):
dense_1                      (None, 32)                 25120
dense_2                      (None, 32)                 1056
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_3 (Dense)              (None, 16)                160016    
_________________________________________________________________
dense_4 (Dense)              (None, 16)                272       
_________________________________________________________________
dense_5 (Dense)              (None, 1)                 17        
=================================================================
Total params: 160,305
Trainable params: 160,305
Non-trainable params: 0
_________________________________________________________________
OUTPUT FROM mysummary(model):
dense_3                      (None, 16)                 160016
dense_4                      (None, 16)                 272
dense_5                      (None, 1)                  17
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_6 (Dense)              (None, 64)                640064    
_________________________________________________________________
dense_7 (Dense)              (None, 64)                4160      
_________________________________________________________________
dense_8 (Dense)              (None, 46)                2990      
=================================================================
Total params: 647,214
Trainable params: 647,214
Non-trainable params: 0
_________________________________________________________________
OUTPUT FROM mysummary(model):
dense_6                      (None, 64)                 640064
dense_7                      (None, 64)                 4160
dense_8                      (None, 46)                 2990
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_9 (Dense)              (None, 512)               401920    
_________________________________________________________________
dense_10 (Dense)             (None, 10)                5130      
=================================================================
Total params: 407,050
Trainable params: 407,050
Non-trainable params: 0
_________________________________________________________________
OUTPUT FROM mysummary(model):
dense_9                      (None, 512)                401920
dense_10                     (None, 10)                 5130
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 24, 24, 64)        18496     
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 22, 22, 64)        36928     
=================================================================
Total params: 55,744
Trainable params: 55,744
Non-trainable params: 0
_________________________________________________________________
OUTPUT FROM mysummary(model):
conv2d_1                     (None, 26, 26, 32)         320
conv2d_2                     (None, 24, 24, 64)         18496
conv2d_3                     (None, 22, 22, 64)         36928
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_4 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 13, 13, 32)        0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 11, 11, 64)        18496     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 5, 5, 64)          0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 3, 3, 64)          36928     
_________________________________________________________________
flatten_1 (Flatten)          (None, 576)               0         
_________________________________________________________________
dense_11 (Dense)             (None, 64)                36928     
_________________________________________________________________
dense_12 (Dense)             (None, 10)                650       
=================================================================
Total params: 93,322
Trainable params: 93,322
Non-trainable params: 0
_________________________________________________________________
OUTPUT FROM mysummary(model):
conv2d_4                     (None, 26, 26, 32)         320
max_pooling2d_1              (None, 13, 13, 32)         0
conv2d_5                     (None, 11, 11, 64)         18496
max_pooling2d_2              (None, 5, 5, 64)           0
conv2d_6                     (None, 3, 3, 64)           36928
flatten_1                    (None, 576)                0
dense_11                     (None, 64)                 36928
dense_12                     (None, 10)                 650
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_2 (Flatten)          (None, 3072)              0         
_________________________________________________________________
dense_13 (Dense)             (None, 200)               614600    
_________________________________________________________________
dense_14 (Dense)             (None, 150)               30150     
_________________________________________________________________
dense_15 (Dense)             (None, 10)                1510      
=================================================================
Total params: 646,260
Trainable params: 646,260
Non-trainable params: 0
_________________________________________________________________
OUTPUT FROM mysummary(model):
flatten_2                    (None, 3072)               0
dense_13                     (None, 200)                614600
dense_14                     (None, 150)                30150
dense_15                     (None, 10)                 1510
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_7 (Conv2D)            (None, 16, 16, 10)        490       
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 8, 8, 20)          1820      
_________________________________________________________________
flatten_3 (Flatten)          (None, 1280)              0         
_________________________________________________________________
dense_16 (Dense)             (None, 10)                12810     
=================================================================
Total params: 15,120
Trainable params: 15,120
Non-trainable params: 0
_________________________________________________________________
OUTPUT FROM mysummary(model):
conv2d_7                     (None, 16, 16, 10)         490
conv2d_8                     (None, 8, 8, 20)           1820
flatten_3                    (None, 1280)               0
dense_16                     (None, 10)                 12810
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_9 (Conv2D)            (None, 148, 148, 32)      896       
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 74, 74, 32)        0         
_________________________________________________________________
conv2d_10 (Conv2D)           (None, 72, 72, 64)        18496     
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 36, 36, 64)        0         
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 34, 34, 128)       73856     
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 17, 17, 128)       0         
_________________________________________________________________
conv2d_12 (Conv2D)           (None, 15, 15, 128)       147584    
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 7, 7, 128)         0         
_________________________________________________________________
flatten_4 (Flatten)          (None, 6272)              0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 6272)              0         
_________________________________________________________________
dense_17 (Dense)             (None, 512)               3211776   
_________________________________________________________________
dense_18 (Dense)             (None, 1)                 513       
=================================================================
Total params: 3,453,121
Trainable params: 3,453,121
Non-trainable params: 0
_________________________________________________________________
OUTPUT FROM mysummary(model):
conv2d_9                     (None, 148, 148, 32)       896
max_pooling2d_3              (None, 74, 74, 32)         0
conv2d_10                    (None, 72, 72, 64)         18496
max_pooling2d_4              (None, 36, 36, 64)         0
conv2d_11                    (None, 34, 34, 128)        73856
max_pooling2d_5              (None, 17, 17, 128)        0
conv2d_12                    (None, 15, 15, 128)        147584
max_pooling2d_6              (None, 7, 7, 128)          0
flatten_4                    (None, 6272)               0
dropout_1                    (None, 6272)               0
dense_17                     (None, 512)                3211776
dense_18                     (None, 1)                  513
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
block1_conv2d_1 (Conv2D)     (None, 150, 150, 64)      1792      
_________________________________________________________________
block1_conv2d_2 (Conv2D)     (None, 150, 150, 64)      36928     
_________________________________________________________________
block1_max_pooling2d (MaxPoo (None, 75, 75, 64)        0         
_________________________________________________________________
block2_conv2d_1 (Conv2D)     (None, 75, 75, 128)       73856     
_________________________________________________________________
block2_conv2d_2 (Conv2D)     (None, 75, 75, 128)       147584    
_________________________________________________________________
block2_max_pooling2d (MaxPoo (None, 37, 37, 128)       0         
_________________________________________________________________
block3_conv2d_1 (Conv2D)     (None, 37, 37, 256)       295168    
_________________________________________________________________
block3_conv2d_2 (Conv2D)     (None, 37, 37, 256)       590080    
_________________________________________________________________
block3_conv2d_3 (Conv2D)     (None, 37, 37, 256)       590080    
_________________________________________________________________
block3_max_pooling2d (MaxPoo (None, 18, 18, 256)       0         
_________________________________________________________________
block4_conv2d_1 (Conv2D)     (None, 18, 18, 512)       1180160   
_________________________________________________________________
block4_conv2d_2 (Conv2D)     (None, 18, 18, 512)       2359808   
_________________________________________________________________
block4_conv2d_3 (Conv2D)     (None, 18, 18, 512)       2359808   
_________________________________________________________________
block4_max_pooling2d (MaxPoo (None, 9, 9, 512)         0         
_________________________________________________________________
block5_conv2d_1 (Conv2D)     (None, 9, 9, 512)         2359808   
_________________________________________________________________
block5_conv2d_2 (Conv2D)     (None, 9, 9, 512)         2359808   
_________________________________________________________________
block5_conv2d_3 (Conv2D)     (None, 9, 9, 512)         2359808   
_________________________________________________________________
block5_max_pooling2d (MaxPoo (None, 4, 4, 512)         0         
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0
_________________________________________________________________
OUTPUT FROM mysummary(model):
block1_conv2d_1              (None, 150, 150, 64)       1792
block1_conv2d_2              (None, 150, 150, 64)       36928
block1_max_pooling2d         (None, 75, 75, 64)         0
block2_conv2d_1              (None, 75, 75, 128)        73856
block2_conv2d_2              (None, 75, 75, 128)        147584
block2_max_pooling2d         (None, 37, 37, 128)        0
block3_conv2d_1              (None, 37, 37, 256)        295168
block3_conv2d_2              (None, 37, 37, 256)        590080
block3_conv2d_3              (None, 37, 37, 256)        590080
block3_max_pooling2d         (None, 18, 18, 256)        0
block4_conv2d_1              (None, 18, 18, 512)        1180160
block4_conv2d_2              (None, 18, 18, 512)        2359808
block4_conv2d_3              (None, 18, 18, 512)        2359808
block4_max_pooling2d         (None, 9, 9, 512)          0
block5_conv2d_1              (None, 9, 9, 512)          2359808
block5_conv2d_2              (None, 9, 9, 512)          2359808
block5_conv2d_3              (None, 9, 9, 512)          2359808
block5_max_pooling2d         (None, 4, 4, 512)          0
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         (None, 150, 150, 3)       0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 150, 150, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 150, 150, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 75, 75, 64)        0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 75, 75, 128)       73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 75, 75, 128)       147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, 37, 37, 128)       0         
_________________________________________________________________
block3_conv1 (Conv2D)        (None, 37, 37, 256)       295168    
_________________________________________________________________
block3_conv2 (Conv2D)        (None, 37, 37, 256)       590080    
_________________________________________________________________
block3_conv3 (Conv2D)        (None, 37, 37, 256)       590080    
_________________________________________________________________
block3_pool (MaxPooling2D)   (None, 18, 18, 256)       0         
_________________________________________________________________
block4_conv1 (Conv2D)        (None, 18, 18, 512)       1180160   
_________________________________________________________________
block4_conv2 (Conv2D)        (None, 18, 18, 512)       2359808   
_________________________________________________________________
block4_conv3 (Conv2D)        (None, 18, 18, 512)       2359808   
_________________________________________________________________
block4_pool (MaxPooling2D)   (None, 9, 9, 512)         0         
_________________________________________________________________
block5_conv1 (Conv2D)        (None, 9, 9, 512)         2359808   
_________________________________________________________________
block5_conv2 (Conv2D)        (None, 9, 9, 512)         2359808   
_________________________________________________________________
block5_conv3 (Conv2D)        (None, 9, 9, 512)         2359808   
_________________________________________________________________
block5_pool (MaxPooling2D)   (None, 4, 4, 512)         0         
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0
_________________________________________________________________
OUTPUT FROM mysummary(model):
input_2                      (None, 150, 150, 3)        0
block1_conv1                 (None, 150, 150, 64)       1792
block1_conv2                 (None, 150, 150, 64)       36928
block1_pool                  (None, 75, 75, 64)         0
block2_conv1                 (None, 75, 75, 128)        73856
block2_conv2                 (None, 75, 75, 128)        147584
block2_pool                  (None, 37, 37, 128)        0
block3_conv1                 (None, 37, 37, 256)        295168
block3_conv2                 (None, 37, 37, 256)        590080
block3_conv3                 (None, 37, 37, 256)        590080
block3_pool                  (None, 18, 18, 256)        0
block4_conv1                 (None, 18, 18, 512)        1180160
block4_conv2                 (None, 18, 18, 512)        2359808
block4_conv3                 (None, 18, 18, 512)        2359808
block4_pool                  (None, 9, 9, 512)          0
block5_conv1                 (None, 9, 9, 512)          2359808
block5_conv2                 (None, 9, 9, 512)          2359808
block5_conv3                 (None, 9, 9, 512)          2359808
block5_pool                  (None, 4, 4, 512)          0