Home / Blog / Data Science / Introduction to the Keras Tuner

Introduction to the Keras Tuner

  • September 27, 2023
  • 3012
  • 83
Author Images

Meet the Author : Mr. Bharani Kumar

Bharani Kumar Depuru is a well known IT personality from Hyderabad. He is the Founder and Director of Innodatatics Pvt Ltd and 360DigiTMG. Bharani Kumar is an IIT and ISB alumni with more than 17 years of experience, he held prominent positions in the IT elites like HSBC, ITC Infotech, Infosys, and Deloitte. He is a prevalent IT consultant specializing in Industrial Revolution 4.0 implementation, Data Analytics practice setup, Artificial Intelligence, Big Data Analytics, Industrial IoT, Business Intelligence and Business Management. Bharani Kumar is also the chief trainer at 360DigiTMG with more than Ten years of experience and has been making the IT transition journey easy for his students. 360DigiTMG is at the forefront of delivering quality education, thereby bridging the gap between academia and industry.

Read More >
Keras Tuner


Keras Tuner, an open-source library created by the TensorFlow team, is tailored for optimizing hyperparameters in Keras models. This library offers a sophisticated and user-friendly API to effectively specify and discover optimal hyperparameter configurations. Keras Tuner presents a range of search algorithms, including random search, Hyperband, and Bayesian optimization, empowering users to select the approach that aligns best with their optimization requirements.

Earn yourself a promising career in Data Science by enrolling in Data Science Course in Bangalore offered by 360DigiTMG.

To start using Keras Tuner, you must configure the search mode by specifying the hyperparameters to be tuned and their corresponding value ranges. These hyperparameters can include the number of hidden layers, type of activation function, learning rate, and more. Keras Tuner then explores this search space and evaluates different combinations of hyperparameters to find the best configuration.ana

The Objective of Keras Tuner:

l of Keras Tuner is to automatically search and find optimal hyperparameter values ​​that maximize a certain metric, such as accuracy or loss. Tuner uses various search algorithms, including random search and Bayesian optimization, to efficiently navigate the hyperparameter search space and identify the best configuration of the CNN model.

Keras Tuner Implementation: To illustrate the Keras Tuner implementation, we use a simple example of image classification using the Fashion MNIST dataset. First we need to install the Keras Tuner library via pip install keras tuner. After installation, we import the necessary libraries and load the dataset. We then define a model building function that takes the hyperparameters as input and builds the neural network architecture accordingly. This function can be customized to create different network configurations, including different layer types, activation functions, and optimization algorithms. Keras Tuner allows us to define conditional hyperparameters and dynamically adjust the network architecture based on selected values.

Initializing Keras Tuners: After defining the model builder, we initialize the tuner with the desired search algorithm and search mode. Then we run the search function of the tuner, which sets the number of attempts or the time budget for the search. Keras Tuner performs the search process by evaluating different combinations of hyperparameters during training and evaluation of the corresponding models.

Code Snippet:

Keras Tuner Keras Tuner

Screenshot of output:

Keras Tuner

Explanation of Code:

1. We retrieve the MNIST dataset, comprising images depicting handwritten digits along with their corresponding labels.

1.1 - The pixel values of the images are scaled to the range [0, 1] to facilitate training.

2. We define a function build_model(hp) that constructs a neural network model.

2.1 - The function takes a HyperParameters object hp as an argument, which allows us to define hyperparameters to tune.

2.2 - The number of hidden layers and units per layer are hyperparameters to be tuned.

Learn the core concepts of Data Science Course video on YouTube:

3. We use the Adam optimizer with a tunable learning rate and sparse categorical cross-entropy loss.

3.1 - We create a RandomSearch tuner, specifying

3.2 - The build_model function is the model-building function to use.

3.3 - The objective is 'val_accuracy', indicating that we want to maximize validation accuracy.

3.4 - The maximum number of trials (max_trials) to explore different hyperparameter combinations.

3.5 - A directory to store results, and a project name for organizing them.

3.6 - The tuner searches for the best hyperparameters by training and evaluating models with different hyperparameter combinations.

3.7 - It uses the training and validation data specified.

3.8 - Once the tuning process is complete, we retrieve the best-performing model based on validation accuracy.

4. We evaluate the best model on the test dataset and print the test accuracy.

Keras Tuner

The key takeaway from the result is that the hyperparameter tuning process successfully identified a set of hyperparameters that led to a high-performing model. In this example, the model achieved a 97.5% accuracy on the MNIST test data, which is quite good for this dataset.

Become a Data Science Course expert with a single program. Go through 360DigiTMG's Data Science Course Course in Hyderabad. Enroll today!

Keras Tuner Evaluation: Once the search is complete, we can get the best hyperparameters and use them to build the final optimized model. We can now train this model on the entire dataset and evaluate its performance. Keras Tuner also offers additional features such as early stop, which can save time by stopping poorly performing experiments early.

Keras Tuner Performance and Tutorial: When using Keras Tuner, it is important to consider some tips and best practices to ensure efficient optimization of hyperparameters.

a) Define an acceptable search space: Prior knowledge of the problem and dataset can help define a more refined search space. Narrowing the search space can save computing resources and time.

b) Start with raw search: To do raw search, start with higher learning and larger network architecture. This helps to identify promising regions in the hyperparameter space.

c) Refine with more experiments: Once promising Hyperparameter settings are identified, perform a more refined search with more experiments to explore the best-fitting settings in detail.

d) Use parallel execution: Keras Tuner supports parallel execution, which allows multiple experiments to run simultaneously. This can speed up the optimization process, especially when using powerful hardware or a cloud-based environment.

e) Adjust and Validate: Adjustment methods such as truncation and batch normalization can improve model generalization. In addition, cross-validation or evaluation of the validation set can help to more accurately assess the performance of the model.

Keras Tuner in CNN: Keras Tuner enables efficient hyperparameter optimization in Convolutional Neural Networks (CNN). Keras Tuner helps fine-tune the architecture to improve CNN performance by exploring different settings and we can adjust hyperparameters like the quantity of filters, kernel size, and the number of units in the dense layers to fine-tune the model

An example of using Keras Tuner: Suppose we want to optimize CNN hyperparameters for image classification using the CIFAR-10 dataset. We can define a model building function that takes hyperparameters as input and builds a CNN architecture accordingly. Using the Keras Tuner, we can search for different hyperparameter values, such as the number of convolution layers, filter sizes, and density layer units. The fitter then evaluates different configurations, trains and evaluates the models to find the best performing combination of hyperparameters. This process ultimately helps us find an optimized CNN model for the CIFAR-10 image classification task.

Data Science, AI and Data Engineering is a promising career option. Enroll in Data Science course in Chennai Program offered by 360DigiTMG to become a successful Career.


Keras Tuner provides an efficient and effective solution to automate hyperparameter optimization in neural networks. By leveraging Keras Tuner functionality, developers can significantly reduce the manual effort required to fine-tune hyperparameters and improve the overall performance of their models. In this blog, we introduced the concept of hyperparameter tuning, discussed the benefits of using the Keras Tuner, and provided a step-by-step guide to applying it to a simple image classification task. We also found some tips and best practices to make hyperparameter optimization more efficient. Automatic hyperparameter tuning not only saves time and resources, but also allows researchers and developers to explore a wider range of network architectures, ultimately leading to better model performance. As the field of machine learning advances, tools like Keras Tuner play a crucial role in speeding up the optimization process and unlocking the true potential of neural networks.

Data Science Placement Success Story

Data Science Training Institutes in Other Locations

Agra, Ahmedabad, Amritsar, Anand, Anantapur, Bangalore, Bhopal, Bhubaneswar, Chengalpattu, Chennai, Cochin, Dehradun, Malaysia, Dombivli, Durgapur, Ernakulam, Erode, Gandhinagar, Ghaziabad, Gorakhpur, Gwalior, Hebbal, Hyderabad, Jabalpur, Jalandhar, Jammu, Jamshedpur, Jodhpur, Khammam, Kolhapur, Kothrud, Ludhiana, Madurai, Meerut, Mohali, Moradabad, Noida, Pimpri, Pondicherry, Pune, Rajkot, Ranchi, Rohtak, Roorkee, Rourkela, Shimla, Shimoga, Siliguri, Srinagar, Thane, Thiruvananthapuram, Tiruchchirappalli, Trichur, Udaipur, Yelahanka, Andhra Pradesh, Anna Nagar, Bhilai, Borivali, Calicut, Chandigarh, Chromepet, Coimbatore, Dilsukhnagar, ECIL, Faridabad, Greater Warangal, Guduvanchery, Guntur, Gurgaon, Guwahati, Hoodi, Indore, Jaipur, Kalaburagi, Kanpur, Kharadi, Kochi, Kolkata, Kompally, Lucknow, Mangalore, Mumbai, Mysore, Nagpur, Nashik, Navi Mumbai, Patna, Porur, Raipur, Salem, Surat, Thoraipakkam, Trichy, Uppal, Vadodara, Varanasi, Vijayawada, Visakhapatnam, Tirunelveli, Aurangabad

Data Analyst Courses in Other Locations

ECIL, Jaipur, Pune, Gurgaon, Salem, Surat, Agra, Ahmedabad, Amritsar, Anand, Anantapur, Andhra Pradesh, Anna Nagar, Aurangabad, Bhilai, Bhopal, Bhubaneswar, Borivali, Calicut, Cochin, Chengalpattu , Dehradun, Dombivli, Durgapur, Ernakulam, Erode, Gandhinagar, Ghaziabad, Gorakhpur, Guduvanchery, Gwalior, Hebbal, Hoodi , Indore, Jabalpur, Jaipur, Jalandhar, Jammu, Jamshedpur, Jodhpur, Kanpur, Khammam, Kochi, Kolhapur, Kolkata, Kothrud, Ludhiana, Madurai, Mangalore, Meerut, Mohali, Moradabad, Pimpri, Pondicherry, Porur, Rajkot, Ranchi, Rohtak, Roorkee, Rourkela, Shimla, Shimoga, Siliguri, Srinagar, Thoraipakkam , Tiruchirappalli, Tirunelveli, Trichur, Trichy, Udaipur, Vijayawada, Vizag, Warangal, Chennai, Coimbatore, Delhi, Dilsukhnagar, Hyderabad, Kalyan, Nagpur, Noida, Thane, Thiruvananthapuram, Uppal, Kompally, Bangalore, Chandigarh, Chromepet, Faridabad, Guntur, Guwahati, Kharadi, Lucknow, Mumbai, Mysore, Nashik, Navi Mumbai, Patna, Pune, Raipur, Vadodara, Varanasi, Yelahanka

Make an Enquiry
Call Us