我有一个数据集,我正在为 Keras 网络塑造如下:
scaler.fit(X)
X_Scaled = pd.DataFrame(scaler.transform(X.values), columns=X.columns, index=X.index)
X_Scaled.info()
X_data = X_Scaled.values
X_data = X_data.reshape((X_data.shape[0],X_data.shape[1],1))
y = to_categorical(y)
整形的结果是这样y.shape = (13609, 5)
的X_data.shape = (13609, 61, 1)
然后,我尝试在数据集上使用 KFold Validation,并将 Keras 神经网络包装到以下循环中:
from sklearn.model_selection import KFold
num_folds = 10
kfold = KFold(n_splits=num_folds, shuffle=True)
acc_per_fold = []
loss_per_fold = []
fold_no = 1
for train, test in kfold.split(X_data, y):
model = models.Sequential()
model.add(Dense(128, activation='tanh' ,input_dim = 61))
model.add(layers.Dropout(0.3))
model.add(Dense(128, activation='relu', kernel_regularizer = keras.regularizers.l1(0.001)))
model.add(Dense(62, activation='relu', kernel_regularizer = keras.regularizers.l2(0.001)))
model.add(layers.Dropout(0.35))
model.add(Dense(32, activation='relu', kernel_regularizer = keras.regularizers.l1(0.001) ))
model.add(Dense(5, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
history = model.fit(X_data[train], y[train], epochs=175, batch_size=64, validation_split = 0.15, shuffle = True, verbose=2)
#Generate generalization metrics
scores = model.evaluate(X_data[test], y[test], verbose=0)
该网络在没有 KFold 验证的情况下在循环之外作为独立的工作正常,但在此循环内不能正常工作。
我得到的错误是这个
ValueError: Error when checking input: expected dense_66_input to have 2 dimensions, but got array with shape (12248, 61, 1)
但我不明白二维发生在哪里。
我怀疑我错误地使用了 Kfold for 循环,但我找不到原因。任何人都可以对此提出一些建议吗?谢谢