Danush Aaditya
Danush Aaditya

Reputation: 73

ValueError: Error when checking input: expected dense_1_input to have shape (180,) but got array with shape (1,)

My learning model is as follows (using Keras).

model = Sequential()
model.add(Dense(100, activation='relu', input_shape = (X_train.shape[0],)))
model.add(Dense(500, activation='relu'))
model.add(Dense(2, activation='softmax'))

My input data X_train is an array of shape (180,) and the corresponding y_train containing labels is also an array of shape (180,). I tried to compile and fit the model as follows.

model.compile(loss="sparse_categorical_crossentropy",
             optimizer="adam",
             metrics=['accuracy'])

model.fit(X_train, y_train, epochs = 200)

When I run the model.fit(), I encountered the following error:

ValueError: Error when checking input: expected dense_1_input to have
shape (180,) but got array with shape (1,)

I'm not sure what I'm doing wrong since I'm pretty new to deep learning. Any help is appreciated. Thanks.

Upvotes: 0

Views: 90

Answers (2)

Gabriele Valvo
Gabriele Valvo

Reputation: 306

As @Thomas Schillaci wrote, the problem is that if you write X_train.shape[0] you are taking into account the number of samples of your dataset. But in that line the code want to know how many features you have, so you have to change in X_train.shape[1] in order to have the n° of input. How many labels do you have?

Upvotes: 0

Thomas Schillaci
Thomas Schillaci

Reputation: 2453

In your case, the input_shape defined in the first layer, should be (1,):

X_train.shape[0] is the number of samples, each sample has for shape (1,).

Moreover, your call to the fit function won't work as your output has for shape (2,) (Dense(2)) whereas the shape of each target sample is (1,) (you have 180 of those).

Upvotes: 1

Related Questions