VOOZH about

URL: https://www.geeksforgeeks.org/deep-learning/how-to-use-k-fold-cross-validation-in-a-neural-network/

⇱ How to Use K-Fold Cross-Validation in a Neural Network - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Use K-Fold Cross-Validation in a Neural Network

Last Updated : 23 Jul, 2025

To use K-Fold Cross-Validation in a neural network, you need to perform K-Fold Cross-Validation splits the dataset into K subsets or "folds," where each fold is used as a validation set while the remaining folds are used as training sets. This helps in understanding how the model performs across different subsets of the data and avoids overfitting.

Here’s how you can implement K-Fold Cross-Validation in Python with a neural network using Keras and Scikit-Learn.

  1. Data Preparation: Load, flatten, and normalize the MNIST dataset.
  2. K-Fold Cross-Validation Setup: Define KFold with the specified number of splits (n_splits=k), shuffle=True to randomize the dataset, and a fixed random_state for reproducibility.
  3. Model Definition: Use the create_model() function to define a neural network with two hidden layers. Compile it using the Adam optimizer and sparse categorical cross-entropy as the loss function.
  4. Training and Evaluation: For each fold:
    • Train the model on the training data.
    • Evaluate on the validation data and store the accuracy.
  5. Results Summary: Calculate and print the average accuracy across all folds.

Output:

Fold 1
375/375 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step
Accuracy for fold 1: 97.22%

Fold 2
375/375 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step
Accuracy for fold 2: 97.54%

Fold 3
375/375 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step
Accuracy for fold 3: 97.32%

Fold 4
375/375 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step
Accuracy for fold 4: 97.04%

Fold 5
375/375 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step
Accuracy for fold 5: 97.09%

Average Accuracy Across 5 Folds: 97.24%


Comment