Tutorial: Hyper-parameter Optimisation for Machine Learning Models using MiP-EGO
When you start with using machine learning, the choices that you have to make can be overwhelming. There are many machine learning algorithms / models that you can use that are already available in packages like scikit-learn. Which of these algorithms should you use? Which will perform best for your specific data-set or the problem you want to solve?
And once you have chosen one of these models, each algorithm has usually many hyper-parameters. Hyper-parameters are options of the algorithm that are most of the time essential for it's performance.
For example, if we want to perform a classification task, we might want to use Support Vector Machines due to its outstanding performance. However, SVC has a staggering 15 hyper-parameters, not all of these are essential to tune, but the C, kernel, degree and gamma parameters are crucial for its performance.
How to best tune these hyper-parameters?
pip install mipegofrom sklearn.datasets import load_iris from sklearn.svm import SVC from sklearn.model_selection import cross_val_score, KFold import numpy as np #import our package, the surrogate model and the search space classes from mipego import ParallelBO from mipego.Surrogate import RandomForest from mipego.SearchSpace import ContinuousSpace, NominalSpace, OrdinalSpace
# Load the dataset iris = load_iris() X_iris = iris.data y_iris = iris.target
# First we need to define the Search Space # the search space consists of one continues variable # one ordinal (integer) variable # and two categorical (nominal) variables. Cvar = ContinuousSpace([1.0, 20.0],'C') # one integer variable with label C degree = OrdinalSpace([2,6], 'degree') gamma = NominalSpace(['scale', 'auto'], 'gamma') kernel = NominalSpace(['linear', 'poly', 'rbf', 'sigmoid'], 'kernel') #the complete search space is just the sum of the parameter spaces search_space = Cvar + gamma + degree + kernel
#now we define the objective function (the model optimization) def train_model(c): #define the model # We will use a Support Vector Classifier svm = SVC(kernel=c['kernel'], gamma=c['gamma'], C=c['C'], degree=c['degree']) cv = KFold(n_splits=4, shuffle=True, random_state=42) # Nested CV with parameter optimization cv_score = cross_val_score(svm, X=X_iris, y=y_iris, cv=cv) #by default mip-ego minimises, so we reverse the accuracy return -1 * np.mean(cv_score)
model = RandomForest(levels=search_space.levels) opt = ParallelBO( search_space=search_space, obj_fun=train_model, model=model, max_FEs=40, DoE_size=5, # the initial DoE size eval_type='dict', # has to be dict for parallel evaluations acquisition_fun='MGFI', acquisition_par={'t' : 2}, n_job=3, # number of processes n_point=3, # number of the candidate solution proposed in each iteration verbose=True # turn this off, if you prefer no output ) xopt, fopt, stop_dict = opt.run()
xopt: {'C': 6.099174228041696, 'gamma': 'scale', 'degree': 2, 'kernel': 'rbf'}
fopt: -0.9797297297297298stop criteria: {'max_FEs': 41}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #import packages from sklearn.datasets import load_iris from sklearn.svm import SVC from sklearn.model_selection import cross_val_score, KFold import numpy as np #import our package, the surrogate model and the search space classes from mipego import ParallelBO from mipego.Surrogate import RandomForest from mipego.SearchSpace import ContinuousSpace, NominalSpace, OrdinalSpace # Load the dataset iris = load_iris() X_iris = iris.data y_iris = iris.target # First we need to define the Search Space # the search space consists of one continues variable # one ordinal (integer) variable # and two categorical (nominal) variables. Cvar = ContinuousSpace([1.0, 20.0],'C') # one integer variable with label C degree = OrdinalSpace([2,6], 'degree') gamma = NominalSpace(['scale', 'auto'], 'gamma') kernel = NominalSpace(['linear', 'poly', 'rbf', 'sigmoid'], 'kernel') #the complete search space is just the sum of the parameter spaces search_space = Cvar + gamma + degree + kernel #now we define the objective function (the model optimization) def train_model(c): #define the model # We will use a Support Vector Classifier svm = SVC(kernel=c['kernel'], gamma=c['gamma'], C=c['C'], degree=c['degree']) cv = KFold(n_splits=4, shuffle=True, random_state=42) # Nested CV with parameter optimization cv_score = cross_val_score(svm, X=X_iris, y=y_iris, cv=cv) #by default mip-ego minimises, so we reverse the accuracy return -1 * np.mean(cv_score) model = RandomForest(levels=search_space.levels) opt = ParallelBO( search_space=search_space, obj_fun=train_model, model=model, max_FEs=6, DoE_size=5, # the initial DoE size eval_type='dict', acquisition_fun='MGFI', acquisition_par={'t' : 2}, n_job=3, # number of processes n_point=3, # number of the candidate solution proposed in each iteration verbose=True # turn this off, if you prefer no output ) xopt, fopt, stop_dict = opt.run() print('xopt: {}'.format(xopt)) print('fopt: {}'.format(fopt)) print('stop criteria: {}'.format(stop_dict)) |
Comments
Post a Comment