Search code examples
python-3.xkerastensorflow2.0keras-layer

Noob question: Keras shapes are incompatible


I'm a student in datascience and I'm trying to work with deep learning. The model I have is as follows:

Model: "sequential_32"

Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_26 (Conv2D)           (None, 27, 27, 32)        288       
_________________________________________________________________
max_pooling2d_12 (MaxPooling (None, 13, 13, 32)        0         
_________________________________________________________________
dense_13 (Dense)             (None, 13, 13, 128)       4224      
_________________________________________________________________
dropout_6 (Dropout)          (None, 13, 13, 128)       0         
_________________________________________________________________
dense_14 (Dense)             (None, 13, 13, 10)        1290      
=================================================================
Total params: 5,802
Trainable params: 5,802
Non-trainable params: 0
_________________________________________________________________

Now I'm trying to fit some data into it with the following code:

print(x_train.shape)
print(x_test.shape)
result = model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))

Which outputs the following:

(60000, 28, 28, 1)
(10000, 28, 28, 1)
# some error codes followed by:
ValueError: Shapes (32, 10) and (32, 13, 13, 11) are incompatible

I feel like it really is an easy mistake to fix but I just can't see it. Any help and/ or explanation is welcome!


Solution

  • Yes, Thymen's response helped me. Thank you. The model now looks like this:

    Model: "sequential_35"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    conv2d_29 (Conv2D)           (None, 27, 27, 32)        160       
    _________________________________________________________________
    max_pooling2d_15 (MaxPooling (None, 13, 13, 32)        0         
    _________________________________________________________________
    flatten (Flatten)            (None, 5408)              0         
    _________________________________________________________________
    dense_17 (Dense)             (None, 128)               692352    
    _________________________________________________________________
    dropout_8 (Dropout)          (None, 128)               0         
    _________________________________________________________________
    dense_18 (Dense)             (None, 10)                1290      
    =================================================================
    Total params: 693,802
    Trainable params: 693,802
    Non-trainable params: 0
    _________________________________________________________________