VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/hyperparameter-tuning-using-gridsearchcv-and-kerasclassifier/

⇱ Hyperparameter tuning using GridSearchCV and KerasClassifier - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Hyperparameter tuning using GridSearchCV and KerasClassifier

Last Updated : 15 Jul, 2025

Hyperparameter tuning is done to increase the efficiency of a model by tuning the parameters of the neural network. Some scikit-learn APIs like GridSearchCV and RandomizedSearchCV are used to perform hyper parameter tuning. 

In this article, you'll learn how to use GridSearchCV to tune Keras Neural Networks hyper parameters. 

Approach: 

  1. We will wrap Keras models for use in scikit-learn using KerasClassifier which is a wrapper.
  2. We will use cross validation using KerasClassifier and GridSearchCV
  3. Tune hyperparameters like number of epochs, number of neurons and batch size.

Implementation of the scikit-learn classifier API for Keras:

tf.keras.wrappers.scikit_learn.KerasClassifier(

   build_fn=None, **sk_params

)

Code:


Import the dataset using which we'll predict if a customer stays or leave. 
 

Code: Code: Preprocess the data

To use the KerasClassifier wrapper, we will need to build our model in a function which needs to be passed to the build_fn argument in the KerasClassifier constructor. 

Code: Code: create the object of KerasClassifier class

Now we will create the dictionary of the parameters we want to tune and pass as an argument in GridSearchCV. 

Code:

The best_score_ member gives the best score observed during the optimization procedure and the best_params_ describes the combination of parameters that achieved the best results.

Code:

Output:

Accuracy:  0.80325

Best Params:  {'batch_size': 20, 'nb_epoch': 200, 'unit': 15}

Comment