简体   繁体   中英

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.

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).

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM