python - Keras 和 Python 3.6 : Error when checking input for Sequential model

标签 python machine-learning neural-network keras deep-learning

不知何故,我在 Keras 库中发现了一个非常奇怪的错误。

我的学习方法包括一个三层神经网络:一个有 130,517 个单元(输入大小)的输入层,一个有 10,000 个单元的隐藏层,以及一个有 2 个单元的输出层。

在代码过程中,我运行了批量学习(我使用了 partial_fit 函数),但代码反复抛出相同的错误:

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

我再次检查了输入的维度,发现确实如我所想,有130,517个维度。

这是调试时变量的图片,如您所见,np.array(X[0]) 的形状是 130,517:

enter image description here

对于任何情况,我都附上了神经网络初始化的代码,以及调用partial_fit的代码:

    def initClassifier(self):
       self.classifier.add(Dense(100000, input_dim=130517, activation='relu'))
       self.classifier.add(Dense(2, activation='softmax'))
       self.classifier.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])


    def partial_fit(self, X, y, classes):
        self.classifier.train_on_batch(np.array(X[0]), np.array(y))

有人有解决办法吗?
这可能是 Keras 代码中的错误吗?

最佳答案

在训练时,Keras 希望您的数据包含批量大小的维度。就您而言,这意味着数据的形状应为 (batch_size, 130517)。但是,您传递的是形状为 (130517)np 数组,这会导致错误。您可以 reshape 数据以包含批处理形状,如下所示:

X_reshape = X[0].reshape(1, -1)

关于python - Keras 和 Python 3.6 : Error when checking input for Sequential model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51968293/

相关文章:

python - 谷歌日历与 Django 集成

python - 随机森林过拟合

python - 使用 pandas/python 将函数应用于 MultiIndex 数据框

machine-learning - 通过 GridSearchCV() 探索 svm.SVC() 超参数的最佳值范围是多少?

neural-network - 通过神经网络进行时间序列预测

python - 获取 pytorch 数据集的子集

python - Python 局部变量与全局变量

machine-learning - 使用深度学习进行图像分类的最佳批量大小

image - reshape 灰度图像以进行神经网络训练 - 如何正确执行此操作

python - 训练后如何同时获得分数和准确率