Home > Mobile >  Keras Sequential - ValueError: Dimensions must be equal
Keras Sequential - ValueError: Dimensions must be equal

Time:12-19

I use X and y data (which are both np arrays) with the following shape:

print(X.shape)
print(y.shape)
(1075, 5, 50)
(1075, 5)

The model:

# define the keras model
model = Sequential()
model.add(Dense(512, input_shape=(5, 50), activation='relu'))
model.add(Dense(64, activation='relu'))

model.compile(loss='categorical_crossentropy', optimizer="Adam", metrics=['accuracy'])

history = model.fit(X, y, epochs=100, batch_size=10, validation_split=0.2)

And i get the following error:

ValueError                                
Traceback (most recent call last)
Cell In[79], line 1
----> 1 history = model.fit(X, y, epochs=100, batch_size=10, validation_split=0.2, callbacks=[callback])

ValueError: Shapes (10, 5) and (10, 5, 64) are incompatible

If I change the last layers density from 64 to 10, the error ouput changes:

model.add(Dense(10, activation='relu'))
ValueError: Shapes (10, 5) and (10, 5, 10) are incompatibl

To what dimension do i have to chnage my last layer, so it fits to the shape of my y data?

CodePudding user response:

You have two problems in your code. First the output shape of your model doesn't match your 'y' so your last layer to be something like that (I added softmax because I guess your are doing multi class classification)

model.add(tf.keras.layers.Dense(5, activation='softmax'))

but with that last layer, you still have a 2D shape into a 1D shape, so you need to flatten it before passing to the last layer

The full working exemple is

X = tf.ones((1075, 5, 50))
y = tf.ones((1075, 5))
print(X.shape)
print(y.shape)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(512, input_shape=(5, 50), activation='relu'))
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Flatten(input_shape=(5,5)))
model.add(tf.keras.layers.Dense(5, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer="Adam", metrics=['accuracy'])
print(model.summary())
history = model.fit(X, y, epochs=100, batch_size=10, validation_split=0.2)
  • Related