ValueError:检查输入时出错:预期density_1_input的形状为(180,),但数组的形状为(1,) [英] ValueError: Error when checking input: expected dense_1_input to have shape (180,) but got array with shape (1,)

查看:111
本文介绍了ValueError:检查输入时出错:预期density_1_input的形状为(180,),但数组的形状为(1,)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的学习模型如下(使用Keras).

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

我的输入数据X_train是形状(180,)的数组,而对应的y_train包含标签的也是形状(180,)的数组.我尝试按以下步骤编译和拟合模型.

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)

当我运行model.fit()时,遇到以下错误:

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.

推荐答案

在您的情况下,在第一层中定义的input_shape应该为(1,):

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

X_train.shape[0]是样本数,每个样本的形状为(1,).

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

此外,由于输出的形状为(2,)(Dense(2)),因此对fit函数的调用将不起作用,而每个目标样本的形状为(1,)(其中有180个).

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

这篇关于ValueError:检查输入时出错:预期density_1_input的形状为(180,),但数组的形状为(1,)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆