2

I have a dataset which which I am shaping for a Keras network as follows:

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)

The result of the shaping is this y.shape = (13609, 5) and X_data.shape = (13609, 61, 1)

I am then attempting to use KFold Validation on the dataset and I am wrapping a Keras Neural Network in to the following loop:

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)

This network works fine outside of the loop as a standalone without the KFold Validation, but not within this loop.

The error I am getting is this

ValueError: Error when checking input: expected dense_66_input to have 2 dimensions, but got array with shape (12248, 61, 1)

but I do not understand where the 2 dimensions are occurring.

I suspect I am using the Kfold for loop incorrectly, but I cannot find why. Can anyone give some advice on this? Thanks

SDROB
  • 125
  • 2
  • 14

2 Answers2

0

As you mention: X_data.shape = (13609, 61, 1) which means X_data has 3 dimensions, but the dense layer is expecting only 2 dimensions.

Franco Piccolo
  • 6,845
  • 8
  • 34
  • 52
0

Your training x data have 3 dimensions but your network expects 2 (input_dim = 61). Remove third dimensios by:

X_data=np.squeeze(X_data)
Ioannis Nasios
  • 8,292
  • 4
  • 33
  • 55