I am new into neural networks, I want to use K-fold cross-validation to train my neural network. I want to use 5 folds 50 epochs and a batch size of 64 I found a function in scikit for k-fold cross validation

model_selection.cross_val_score(model_kfold, x_train, y_train, cv=5)

and my code without cross validation is

history = alexNet_model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1,validation_data=(x_validation, y_validation))

I don't know how to implement this with batch size and epochs in python using keras and scikit. any idea?


Here's a good resource showing you the exact code for what you're trying to do.

Here's the particular snippet:

# evaluate a model using k-fold cross-validation
def evaluate_model(dataX, dataY, n_folds=5):
    scores, histories = list(), list()
    # prepare cross validation
    kfold = KFold(n_folds, shuffle=True, random_state=1)
    # enumerate splits
    for train_ix, test_ix in kfold.split(dataX):
        # define model
        model = define_model()
        # select rows for train and test
        trainX, trainY, testX, testY = dataX[train_ix], dataY[train_ix], dataX[test_ix], dataY[test_ix]
        # fit model
        history = model.fit(trainX, trainY, epochs=10, batch_size=32, validation_data=(testX, testY), verbose=0)
        # evaluate model
        _, acc = model.evaluate(testX, testY, verbose=0)
        print('> %.3f' % (acc * 100.0))
        # stores scores
    return scores, histories

So you're using .fit in a loop over the folds set up by sklearn.model_selection.KFold.

  • $\begingroup$ In this code, define_model is repeating for each fold, is it updating the last model weights or just randomize the start point and create a new model? $\endgroup$
    – SahaTib
    Feb 25 '20 at 20:25
  • $\begingroup$ New model from scratch each time. $\endgroup$ Feb 25 '20 at 20:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.