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

最后都变了- 提交于 2020-03-04 18:55:12

问题


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.


回答1:


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




回答2:


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?



来源:https://stackoverflow.com/questions/60226423/valueerror-error-when-checking-input-expected-dense-1-input-to-have-shape-180

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!