

Hyperparameter Optimization and Tuning
source link: https://blog.knoldus.com/hyperparameter-optimization-and-tuning/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Hyperparameter Optimization and Tuning
Reading Time: 3 minutes
A Machine Learning model is defined as a mathematical model with a number of parameters that need to be learned from the data. By training a model with existing data, we are able to fit the model parameters.
However, there is another kind of parameters, known as Hyperparameters. Hyperparameters contain the data that govern the training process itself These parameters express important properties of the model such as its complexity or how fast it should learn. Parameters which define the model architecture are referred to as hyperparameters and thus this process of searching for the ideal model architecture is referred to as hyperparameter optimization and tuning.
The purpose of this article is to consider various strategies for optimizing hyperparameters.
These hyperparameters Tuning & optimization might address model design questions such as:
Degree of polynomial features should I use for my linear model?
What is maximum depth of the decision tree I’m using?
Amount of minimum number of samples required at a leaf node in my decision tree?
Number of trees should I include in my random forest?
How many neurons should I have in my neural network layer?
How many layers should I have in my neural network?
What should I set my learning rate to for gradient descent?
Optimal hyperparameter tuning could accomplished using variety of methods. We’ll focus on two strategies in particular.
Grid search
The grid search is an exhaustive search through a set of manually specified set of values of hyperparameters. It means you have a set of models (which differ from each other in their parameter values, which lie on a grid). What you do is you then train each of the models and evaluate it using cross-validation. You then select the one that performed best.
from sklearn.datasets import load_iris
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
iris = load_iris()
svc = SVC()
# grid search on kernel and C hyperparameters
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
clf = GridSearchCV(svc, param_grid=parameters)
clf.fit(iris.data, iris.target)
>>> print('Grid best parameters (max accuracy): ', clf.best_params_)
Grid best parameters (max accuracy): {'C': 1, 'kernel': 'linear'}
>>> print('Grid best score (accuracy): ', clf.best_score_)
Grid best score (accuracy): 0.98
Drawback : GridSearchCV will go through all the intermediate combinations of hyperparameters which makes grid search computationally very expensive.
Random search
Random search differ from grid search in that random search does not provide a separate set of values that can be searched for each hyperparameter, but instead provides a statistical distribution of each hyperparameter that allows random selection of values. For each hyperparameter, we’ll define a sampling distribution. Often some of the hyperparameters matter much more than others. Performing random search rather than grid search allows much more precise discovery of good values for the important ones. This approach reduces unnecessary computation.
# Necessary imports
from scipy.stats import randint
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import RandomizedSearchCV
# Creating the hyperparameter grid
param_dist = {"max_depth": [3, None],
"max_features": randint(1, 9),
"min_samples_leaf": randint(1, 9),
"criterion": ["gini", "entropy"]}
# Instantiating Decision Tree classifier
tree = DecisionTreeClassifier()
# Instantiating RandomizedSearchCV object
tree_cv = RandomizedSearchCV(tree, param_dist, cv = 5)
tree_cv.fit(X, y)
# Print the tuned parameters and score
print("Tuned Decision Tree Parameters: {}".format(tree_cv.best_params_))
print("Best score is {}".format(tree_cv.best_score_))
Output
Tuned Decision Tree Parameters: {‘min_samples_leaf’: 5, ‘max_depth’: 3, ‘max_features’: 5, ‘criterion’: ‘gini’}
Best score is 0.7265625
References:
Recommend
-
65
Hyperparameter tuning using TPUs in Cloud ML Engine 2018-08-18adminGoogleCloud...
-
63
Keras Tuner An hyperparameter tuner for Keras , specifically for tf.keras with TensorFlow 2.0. Basic example Here's how to perform hyperparameter...
-
43
README.md Keras Tuner An hyperparameter tuner for Keras, specifically for tf.keras with TensorFlow 2.0....
-
30
Experiments on Hyperparameter tuning in deep learning — Rules to follow
-
20
Using Ray’s Tune to Optimize your Models Christian...
-
20
Hyperparameter Tuning with Keras Tuner Getting the most out of your models Photo by
-
19
This article will give you an overview of how to tune the deep learning model hyperparameters. ...
-
9
What is Hyperparameter tuning?What is Hyperparameter tuning? | Machine Learning20 Views19/07/2022In this video, we will be learnin...
-
6
Use hyperparameter tuning bookmark_border...
-
6
Hyperparameter tuning for Random Forests Motivated to write this post based on a few different examples at work. One, we have periodically tried different auto machine learning (automl) libraries at work (with quite mediocre success)...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK