How to tune hyperparameters for keras model?

This recipe helps you tune hyperparameters for keras model

Recipe Objective

How to tune hyperparameters for keras model.

The process of selecting the right hyperparameters in a Deep Learning or Machine Learning Model is called hyperparameter tuning.

Hyperparameters are the variables that control the training of the dataset, they have a huge impact on the learning of the model, and tuning of these hyperparameters controls the accuracy.

Step 1- Importing Libraries.

import tensorflow as tf from tensorflow import keras import kerastuner as kt from sklearn.model_selection import train_test_split from keras.models import Sequential from keras.layers import Dense from keras.layers import Dropout from keras.layers import Layer #!pip install -U keras-tuner

Step 2- Creating a function.

Create a function for hypertuning, defining all the hyper parameters

def model_tune(new): model = keras.Sequential() model.add(keras.layers.Flatten(input_shape=(28, 28))) units = new.Int('units', min_value = 16, max_value = 128, step = 16) model.add(keras.layers.Dense(units = units, activation = 'sigmoid')) model.add(keras.layers.Dense(10)) learning_rate = new.Choice('learning_rate', values = [1e-1,1e-2, 1e-3, 1e-4]) model.compile(optimizer = keras.optimizers.Adam(learning_rate = learning_rate), loss = keras.losses.SparseCategoricalCrossentropy, metrics = ['mse']) return model

Step 3-Instantiating the hypertuning model.

We will instantiate the hypertuning model here.

tuning = kt.Hyperband(model_tune, objective = 'val_accuracy', max_epochs = 100, factor = 3, directory = 'my_dir', project_name = 'learning_hyperparameter') print(tuning)

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .