【问题标题】:Running into key error when building simple feedforward neural network in Keras在 Keras 中构建简单的前馈神经网络时遇到关键错误
【发布时间】:2019-09-18 10:02:46
【问题描述】:

这是我的数据集的快照,包括它的形状:

现在,这是我用来构建 NN 的代码:

# define the architecture of the network
model = Sequential()
model.add(Dense(50, input_dim=X_train.shape[1], init="uniform", activation="relu"))
model.add(Dense(38, activation="relu", kernel_initializer="uniform"))
model.add(Dense(1, activation = 'sigmoid'))

print("[INFO] compiling model...")
adam = Adam(lr=0.01)
model.compile(loss="binary_crossentropy", optimizer=adam,
    metrics=["accuracy"])
model.fit(X_train, Y_train, epochs=50, batch_size=128,
    verbose=1)

当我这样做时,我收到以下错误:

KeyError: '[233946 164308 296688 166151 276165  88219 117980 163503 182033 164328\n 188083  30380  37984 245771 308534   6215 181186 307488 172375  60446\n  29397 166681   5587 243263 103579 262182 107823 234790 258973 116433\n 199283  86118 172148 257334 286452 248407  81280 ...] not in index'

我无法找到解决此问题的方法。任何帮助将非常感激。

【问题讨论】:

  • X_train 和 Y_train 都是 numpy 数组吗?
  • 看起来您的索引引起了问题,如果数据的顺序已经正确,请尝试使用 X_train.values()、Y_train.values()。

标签: python pandas keras neural-network


【解决方案1】:

我相信输入不是 keras 页面上github issue 中描述的 numpy 数组

尝试使用以下方法拟合模型:

model.fit(np.array(X_train), np.array(Y_train), epochs=50, batch_size=128,
    verbose=1)

在拟合数据时会将数组转换为 numpy 数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 2019-12-16
    • 1970-01-01
    • 2015-06-04
    相关资源
    最近更新 更多