1
$\begingroup$

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

$\endgroup$
0
$\begingroup$

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.

$\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.