davidADSP/GDL_code

02_01: Sequential and Funtional variant do not give the same summary

Closed this issue · 1 comments

apahl commented

Hi David,

thanks a lot for the book, which I very much enjoy.

I noticed that the Functional example in 02_01 and the corresponding Sequential one (only given in the book) do not give the same model summary. Is that expected?

Functional:

input_layer = Input((32,32,3))

x = Flatten()(input_layer)

x = Dense(200, activation = 'relu')(x)
x = Dense(150, activation = 'relu')(x)

output_layer = Dense(NUM_CLASSES, activation = 'softmax')(x)

model = Model(input_layer, output_layer)

model.summary()

Output:

Model: "model_6"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_9 (InputLayer)         (None, 32, 32, 3)         0         
_________________________________________________________________
flatten_15 (Flatten)         (None, 3072)              0         
_________________________________________________________________
dense_48 (Dense)             (None, 200)               614600    
_________________________________________________________________
dense_49 (Dense)             (None, 150)               30150     
_________________________________________________________________
dense_50 (Dense)             (None, 10)                1510      
=================================================================
Total params: 646,260
Trainable params: 646,260
Non-trainable params: 0

Sequential:

model = Sequential([
    Dense(200, activation="relu", input_shape=(32, 32, 3)),
    Flatten(),
    Dense(150, activation="relu"),
    Dense(10, activation="softmax")
])

model.summary()

Output:

Model: "sequential_11"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_45 (Dense)             (None, 32, 32, 200)       800       
_________________________________________________________________
flatten_14 (Flatten)         (None, 204800)            0         
_________________________________________________________________
dense_46 (Dense)             (None, 150)               30720150  
_________________________________________________________________
dense_47 (Dense)             (None, 10)                1510      
=================================================================
Total params: 30,722,460
Trainable params: 30,722,460
Non-trainable params: 0

Kind regards,
Axel

I think in the Sequential code there is a mistake: the Flatten() should be the first step. So

model = Sequential([
Flatten(),
Dense(200, activation="relu", input_shape=(32, 32, 3)),
Dense(150, activation="relu"),
Dense(10, activation="softmax")
])