SecretIndividual
SecretIndividual

Reputation: 2549

Keras: ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis

I am trying to build a neural network using Keras but am getting the error:

ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 25168 but received input with shape (None, 34783)

I defined the model to be:

model = Sequential()
model.add(Dense(1024, input_dim = len(X), activation = 'relu'))
model.add(Dense(6, activation='softmax'))

In this, X is the result of using scikit-learn it's CountVectorizer() (after it is trained) as follows:

X = count_vectorizer.transform(X).todense()

Is there any method to fix this? Looking around I found that I might need to reshape the data, however I have no idea how and where.

Upvotes: 1

Views: 89

Answers (1)

Marco Cerliani
Marco Cerliani

Reputation: 22031

You are using as input_dim the sample dimensionality: len(X) (the same as X.shape[0]) which is wrong.

Keras expects as input the number of dimensions of the features which, in your case of 2D input, is X.shape[-1]

Upvotes: 1

Related Questions