[英]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).我的学习模型如下(使用 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,).我的输入数据 X_train 是一个形状数组 (180,),相应的包含标签的 y_train 也是一个形状数组 (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:当我运行model.fit()时,我遇到了以下错误:
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,)
:在您的情况下,第一层中定义的 input_shape 应该是
(1,)
:
X_train.shape[0]
is the number of samples, each sample has for shape (1,)
. X_train.shape[0]
是样本数,每个样本具有形状(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).此外,您对 fit 函数的调用将不起作用,因为您的输出具有形状
(2,)
( Dense(2)
) 而每个目标样本的形状是(1,)
(您有 180 个)。
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.正如@Thomas Schillaci 所写,问题在于,如果您编写
X_train.shape[0]
您将考虑数据集的样本数量。 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.但是在那一行代码想知道你有多少特征,所以你必须改变
X_train.shape[1]
以获得输入的 n°。 How many labels do you have?你有多少标签?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.