Python Keras不接受目标标签,因为其形状?

Python Keras不接受目标标签,因为其形状?,python,keras,scikit-learn,classification,cross-validation,Python,Keras,Scikit Learn,Classification,Cross Validation,我试图让Keras处理一个分类问题,这个问题有五个分类目标标签(1、2、3、4、5)。由于某些原因,在使用StratifiedKFold时,我无法使其工作。X和y分别是形状为(500,20)和(500,)的NumPy阵列 错误消息是“ValueError:error when check target:expected densite_35 have shape(1,)但get array have shape(5,)”,这让我认为错误肯定在于目标变量的格式。同样值得注意的是,“dense_35

我试图让Keras处理一个分类问题,这个问题有五个分类目标标签(1、2、3、4、5)。由于某些原因,在使用StratifiedKFold时,我无法使其工作。X和y分别是形状为(500,20)和(500,)的NumPy阵列

错误消息是“ValueError:error when check target:expected densite_35 have shape(1,)但get array have shape(5,)”,这让我认为错误肯定在于目标变量的格式。同样值得注意的是,“dense_35”中的数字似乎因每次尝试运行代码而有所不同

random_state = 123
n_splits = 10
cv = StratifiedKFold(n_splits=n_splits, 
random_state=random_state, shuffle=False)

def baseline_model():
    nn_model = Sequential()
    nn_model.add(Dense(units=50, input_dim=X.shape[1], init='normal',
                       activation= 'relu' ))
    nn_model.add(Dense(30, init='normal', activation='relu'))
    nn_model.add(Dense(10, init='normal', activation='relu'))
    nn_model.add(Dense(1, init='normal', activation='softmax'))

    nn_model.compile(optimizer='adam', loss='categorical_crossentropy',
                     metrics = ['accuracy'])
    return nn_model

for train, test in cv.split(X, y):   
    X_train, X_test = X[train], X[test]
    y_train, y_test = y[train], y[test]

    np_utils.to_categorical(y_train)
    np_utils.to_categorical(y_test)

    estimator = KerasClassifier(build_fn=baseline_model,
                                epochs=200, batch_size=5,
                                verbose=0)

    estimator.fit(X_train, y_train)
    y_pred = estimator.predict(X_test)

The numpy array (y), that I am trying to split:
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5
 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5]
  • 通过在图层中指定名称参数,可以命名图层。通过这样做,您将获得层的确切名称,以防每次出现错误
  • model.summary()是另一个有用的函数,您可以使用它检查每个层的输出形状

由于这是一个分类问题,有五个分类目标标签,最后一个密集层(输出层)必须有5个单位:

def baseline_model():
    nn_model = Sequential()
    nn_model.add(Dense(units=50, input_dim=X.shape[1], init='normal',
                       activation= 'relu' ))
    nn_model.add(Dense(30, init='normal', activation='relu'))
    nn_model.add(Dense(10, init='normal', activation='relu'))

    #Output layer
    nn_model.add(Dense(5, init='normal', activation='softmax'))
    nn_model.compile(optimizer='adam', loss='categorical_crossentropy',
                     metrics = ['accuracy'])
    return nn_model

没错-网络只需要期待一个热编码矢量哇,真不敢相信我错过了。现在看来很明显。谢谢!
def baseline_model():
    nn_model = Sequential()
    nn_model.add(Dense(units=50, input_dim=X.shape[1], init='normal',
                       activation= 'relu' ))
    nn_model.add(Dense(30, init='normal', activation='relu'))
    nn_model.add(Dense(10, init='normal', activation='relu'))

    #Output layer
    nn_model.add(Dense(5, init='normal', activation='softmax'))
    nn_model.compile(optimizer='adam', loss='categorical_crossentropy',
                     metrics = ['accuracy'])
    return nn_model