Keras,IndexError:指数超出范围

时间:2017-01-24 21:44:24

标签: tensorflow keras

我试图通过Keras(Tensorflow beckend)实现这个简单的神经网络:

x_train = df_train[["Pclass", "Gender", "Age","SibSp", "Parch"]]
y_train = df_train ["Survived"]

x_test = df_test[["Pclass", "Gender", "Age","SibSp", "Parch"]]
y_test = df_test["Survived"]

y_train = y_train.values
y_test = y_test.values

但是当我运行这部分时:

model = Sequential()
model.add(Dense(input_dim=5, output_dim=1))
model.add(Activation("softmax"))

model.compile(loss='sparse_categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])

model.fit(x_train, y_train)

我收到此错误: IndexError:indices is out-of-bounds 。我假设它是关于 model.fit(x_train,y_train)中的参数。我试图通过 .values 将这些作为numpy数组传递,但我仍然有同样的错误。

1 个答案:

答案 0 :(得分:2)

Keras希望numpy数组不是pandas,因此您需要将所有数据转换为Keras API ..而不仅仅是y_trainy_test

所以:

x_train = x_train.values
y_train = y_train.values
x_test = x_test.values
y_test = y_test.values

或者

x_train = numpy.asarray(x_train)
y_train = numpy.asarray(y_train)
x_test = numpy.asarray(x_test)
y_test = numpy.asarray(y_test)