TransWikia.com

Dimension of output in Dense layer Keras

Data Science Asked by data_person on May 16, 2021

I have the sample following model

from tensorflow.keras import models
from tensorflow.keras import layers
sample_model = models.Sequential()
sample_model.add(layers.Dense(32, input_shape=(4,)))
sample_model.add(layers.Dense(16, input_shape = (44,)))

sample_model.compile(loss="binary_crossentropy", optimizer="adam", metrics = ["accuracy"])

IP for the model:

sam_x = np.random.rand(10,4)
sam_y = np.array([0,1,1,0,1,0,0,1,0,1,])
sample_model.fit(sam_x,sam_y)

The confusion is the fit should have thrown an error of shape mismatch as the input_shape for the 2nd Dense Layer is given as (None,44) but the output for the 1st Dense Layer (which is the input of the 2nd Dense Layer) will be of shape (None,32). But it ran successfully.

I dont understand why there was no error. Any clarifications will be helpful

One Answer

The answer can be found by just printing

sample_model.summary()

giving

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 32)                160       
_________________________________________________________________
dense_1 (Dense)              (None, 16)                528       
=================================================================
Total params: 688
Trainable params: 688
Non-trainable params: 0
_________________________________________________________________

Indeed, the input_shape argument in a layer that is not the first one is ignored in a Sequential model.

Correct answer by Oscar on May 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP