Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
How to perform 10 fold cross validation with LibSVM in R?
Next article icon

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

Last Updated : 12 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.
Python
# Import necessary libraries from sklearn.model_selection import KFold from sklearn.metrics import accuracy_score import numpy as np from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.optimizers import Adam from tensorflow.keras.datasets import mnist  # Example dataset  # Load dataset (X_train, y_train), (X_test, y_test) = mnist.load_data()  # Preprocess data: Flatten and normalize X_train = X_train.reshape((X_train.shape[0], -1)) / 255.0 X_test = X_test.reshape((X_test.shape[0], -1)) / 255.0  # Set up K-Fold Cross-Validation k = 5  # Number of folds kf = KFold(n_splits=k, shuffle=True, random_state=42)  # Function to create and compile the model def create_model():     model = Sequential([         Dense(128, activation='relu', input_shape=(X_train.shape[1],)),         Dense(64, activation='relu'),         Dense(10, activation='softmax')  # Output layer for classification     ])     model.compile(optimizer=Adam(), loss='sparse_categorical_crossentropy', metrics=['accuracy'])     return model  # List to store accuracy for each fold accuracy_per_fold = []  # K-Fold Cross-Validation for fold, (train_index, val_index) in enumerate(kf.split(X_train)):     print(f'Fold {fold + 1}')          # Split the data into training and validation sets for this fold     X_train_fold, X_val_fold = X_train[train_index], X_train[val_index]     y_train_fold, y_val_fold = y_train[train_index], y_train[val_index]          # Create a new instance of the model for each fold     model = create_model()          # Train the model on the training fold     model.fit(X_train_fold, y_train_fold, epochs=5, batch_size=32, verbose=0)          # Evaluate the model on the validation fold     val_predictions = np.argmax(model.predict(X_val_fold), axis=1)     accuracy = accuracy_score(y_val_fold, val_predictions)          # Store the accuracy for this fold     accuracy_per_fold.append(accuracy)     print(f'Accuracy for fold {fold + 1}: {accuracy * 100:.2f}%')  # Calculate the average accuracy across all folds average_accuracy = np.mean(accuracy_per_fold) print(f'\nAverage Accuracy Across {k} Folds: {average_accuracy * 100:.2f}%') 

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%



Next Article
How to perform 10 fold cross validation with LibSVM in R?
author
vaibhav_tyagi
Improve
Article Tags :
  • Deep Learning
  • AI-ML-DS
  • Data Science Questions
  • AI-ML-DS With Python

Similar Reads

  • How to perform 10 fold cross validation with LibSVM in R?
    Support Vector Machines (SVM) are a powerful tool for classification and regression tasks. LibSVM is a widely used library that implements SVM, and it can be accessed in R with the e1071 package. Cross-validation, particularly 10-fold cross-validation, is an essential technique for assessing the per
    5 min read
  • K- Fold Cross Validation in Machine Learning
    K-Fold Cross Validation is a statistical technique to measure the performance of a machine learning model by dividing the dataset into K subsets of equal size (folds). The model is trained on K − 1 folds and tested on the last fold. This process is repeated K times, with each fold being used as the
    4 min read
  • Stratified K Fold Cross Validation
    Stratified K-Fold Cross Validation is a technique used for evaluating a model. It is particularly useful for classification problems in which the class labels are not evenly distributed i.e data is imbalanced. It is a enhanced version of K-Fold Cross Validation. Key difference is that it uses strati
    4 min read
  • Cross-Validation Using K-Fold With Scikit-Learn
    Cross-validation involves repeatedly splitting data into training and testing sets to evaluate the performance of a machine-learning model. One of the most commonly used cross-validation techniques is K-Fold Cross-Validation. In this article, we will explore the implementation of K-Fold Cross-Valida
    12 min read
  • Choose Optimal Number of Epochs to Train a Neural Network in Keras
    One of the critical issues while training a neural network on the sample data is Overfitting. When the number of epochs used to train a neural network model is more than necessary, the training model learns patterns that are specific to sample data to a great extent. This makes the model incapable t
    6 min read
  • How to interpret cross validation output from cv.kknn (kknn package)
    Cross-validation is a resampling procedure used to evaluate machine learning models on a limited data sample. The most common type is k-fold cross-validation, where the dataset is divided into k subsets (folds). The model is trained on k-1 of these folds and validated on the remaining one. This proc
    3 min read
  • How Neural Networks are used for Regression in R Programming?
    Neural networks consist of simple input/output units called neurons (inspired by neurons of the human brain). These input/output units are interconnected and each connection has a weight associated with it. Neural networks are flexible and can be used for both classification and regression. In this
    4 min read
  • How to Deal with Factors with Rare Levels in Cross-Validation in R
    Cross-validation is a vital technique for evaluating model performance in machine learning. However, traditional cross-validation approaches may lead to biased or unreliable results when dealing with factors (categorical variables) that contain rare levels. In this guide, we'll explore strategies fo
    4 min read
  • What is Forward Propagation in Neural Networks?
    Forward propagation is the fundamental process in a neural network where input data passes through multiple layers to generate an output. It is the process by which input data passes through each layer of neural network to generate output. In this article, we’ll more about forward propagation and se
    4 min read
  • How to Calculate Optimal Batch Size for Training Neural Networks
    Choosing the right batch size is a crucial hyperparameter in training neural networks. It affects not only the performance and convergence speed of the model but also its ability to generalize to unseen data. In this article, we will explore how to calculate the optimal batch size, considering vario
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences