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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.