超参数调优方法整理大全

 

机器学习中总是会碰见调参这种枯燥无味且消耗时间的事情,所幸,有很多可以帮助你自动调参的库以及相应的方法,在这里统一总结一下吧。

一、随机森林超参数优化- RandomSearch和GridSearch

(1)RandomSearch

原理:

超参数优化也就是常说的调参,python-sklearn里常用的有GridSearchCV和RandomizedSearchCV可以用。其中GridSearchCV的原理很简明,就是程序去挨个尝试每一组超参数,然后选取最好的那一组。可以想象,这个是比较费时间的,面临着维度灾难。因此James Bergstra和Yoshua Bengio在2012年提出了超参数优化的RandomSearch方法。

RandomizedSearchCV是在论文的基础上加入了cross-validation

RandomSearchCV是如何"随机搜索"的:

考察其源代码,其搜索策略如下:

(a)对于搜索范围是distribution的超参数,根据给定的distribution随机采样;

(b)对于搜索范围是lit的超参数,在给定的list中等概率采样;

(c)对a、b两步中得到的n_iter组采样结果,进行遍历。

(补充)如果给定的搜索范围均为list,则不放回抽样n_iter次。

更详细的可以参考sklearn-RandomizedSearchCV的ParameterSampler类的代码。

为什么RandomSearchCV会有效?

在这里插入图片描述

(a)目标函数为 f(x,y)=g(x)+h(y),其中绿色为g(x),黄色为h(y),目的是求f的最大值。

(b)其中由于g(x)数值上要明显大于h(y),因此有f(x,y)=g(x)+h(y)≈g(x),也就是说在整体求解f(x,y)最大值的过程中,g(x)的影响明显大于h(y)。

(c)两个图都进行9次实验(搜索),可以看到左图实际探索了各三个点(在横轴和纵轴上的投影均为3个),而右图探索了9个不同的点(横轴纵轴均是,不过实际上横轴影响更大)。

(d)右图更可能找到目标函数的最大值。

因此引入随机因素在某些情况下可以提高寻优效率。 

下面是具体代码


 
  1. from sklearn.ensemble import RandomForestRegressor
  2. from sklearn.model_selection import RandomizedSearchCV
  3. import numpy as np
  4. from pprint import pprint
  5. from sklearn.datasets import load_iris
  6. if __name__== '__main__':
  7. # Number of trees in random forest
  8. n_estimators = [int(x) for x in np.linspace(start = 200, stop = 2000, num = 10)]
  9. # Number of features to consider at every split
  10. max_features = [ 'auto', 'sqrt']
  11. # Maximum number of levels in tree
  12. max_depth = [int(x) for x in np.linspace( 10, 110, num = 11)]
  13. max_depth.append( None)
  14. # Minimum number of samples required to split a node
  15. min_samples_split = [ 2, 5, 10]
  16. # Minimum number of samples required at each leaf node
  17. min_samples_leaf = [ 1, 2, 4]
  18. # Method of selecting samples for training each tree
  19. bootstrap = [ True, False]
  20. # Create the random grid
  21. random_grid = { 'n_estimators': n_estimators,
  22. 'max_features': max_features,
  23. 'max_depth': max_depth,
  24. 'min_samples_split': min_samples_split,
  25. 'min_samples_leaf': min_samples_leaf,
  26. 'bootstrap': bootstrap}
  27. pprint(random_grid)
  28. #导入数据
  29. data=load_iris()
  30. # 使用随机网格搜索最佳超参数
  31. # 首先创建要调优的基本模型
  32. rf = RandomForestRegressor()
  33. # 随机搜索参数,使用3倍交叉验证
  34. # 采用100种不同的组合进行搜索,并使用所有可用的核心
  35. rf_random = RandomizedSearchCV(estimator = rf, param_distributions = random_grid, n_iter = 100, cv = 3, verbose= 2, random_state= 42, n_jobs = -1)
  36. # Fit模型
  37. rf_random.fit(data.data, data.target)
  38. print(rf_random.best_params_)

(2)GridSearch网格搜索


 
  1. #下面是网格搜索:
  2. from sklearn.model_selection import GridSearchCV
  3. data = load_iris()
  4. # Create the parameter grid based on the results of random search
  5. param_grid = {
  6. 'bootstrap': [ True, False],
  7. 'max_depth': [int(x) for x in np.linspace( 10, 110, num = 11)],
  8. 'max_features': [ 'auto', 'sqrt'],
  9. 'min_samples_leaf': [ 1, 2, 4],
  10. 'min_samples_split': [ 2, 5, 10],
  11. 'n_estimators': [int(x) for x in np.linspace(start = 200, stop = 2000, num = 10)]
  12. }
  13. # Create a based model
  14. rf = RandomForestRegressor()
  15. # Instantiate the grid search model
  16. grid_search = GridSearchCV(estimator=rf, param_grid=param_grid, cv= 3, n_jobs= -1, verbose= 2)
  17. grid_search.fit(data.data, data.target)
  18. pprint(grid_search.best_params_)
  19. best_grid = grid_search.best_estimator_
  20. grid_accuracy = evaluate(best_grid, data.data, data.target)
  21. pprint(best_grid)
  22. pprint(grid_accuracy)

二、 Hyperopt自动化超参数调优- 贝叶斯优化

网格搜索和随机搜索则对ml模型超参数的优化能取得不错的效果,但是需要大量运行时间去评估搜索空间中并不太可能找到最优点的区域。因此越来越多的的超参数调优过程都是通过自动化的方法完成的,它们旨在使用带有策略的启发式搜索(informed search)在更短的时间内找到最优超参数。

 

贝叶斯优化是一种基于模型的用于寻找函数最小值的方法。近段时间以来,贝叶斯优化开始被用于机器学习超参数调优,结果表明,该方法在测试集上的表现更加优异,并且需要的迭代次数小于随机搜索。

Python 环境下有一些贝叶斯优化程序库,它们目标函数的代理算法有所区别。本部分主要介绍「Hyperopt」库,它使用树形 Parzen 评估器(TPE,https://papers.nips.cc/paper/4443-algorithms-for-hyper-parameter-optimization.pdf)作为搜索算法,其他的 Python 库还包含「Spearmint」(高斯过程代理)和「SMAC」(随即森林回归)。

 

贝叶斯优化问题有四个组成部分:

1)目标函数:我们想要最小化的对象,这里指带超参数的机器学习模型的验证误差

2)域空间:待搜索的超参数值

3)优化算法:构造代理模型和选择接下来要评估的超参数值的方法

4)结果的历史数据:存储下来的目标函数评估结果,包含超参数和验证损失

通过以上四个步骤,我们可以对任意实值函数进行优化(找到最小值)。

详解:

1)目标函数

模型训练目的是最小化目标函数,所以输出为需要最小化的实值——交叉验证损失。Hyperopt 将目标函数作为黑盒处理,因为这个库只关心输入和输出是什么。为了找到使损失最小的输入值。

cross_val_score
对衡量的estimator,它默认返回的是一个array,包含K folder情况下的各次的评分,一般采用mean()。 需要确定这个estimator默认的 scoring 是什么,它的值是越大越匹配还是越小越匹配。如果自己指定了scoring,一定要确定这个scoring值的意义,切记切记! 而如果用户不指定,一般对于Classification类的estimator,使用accuracy,它是越大越好,那么,hyperopt里面的loss的值就应该是对这个值取负数,因为hyperopt通过loss最小取找最佳匹配。 可以把feature的normalize或者scale作为一个choice,然后看看是否更合适。如果更合适,best里面就会显示 normalize 为1。


 
  1. from sklearn.datasets import load_iris
  2. from sklearn import datasets
  3. from sklearn.preprocessing import normalize, scale
  4. from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
  5. iris = load_iris()
  6. X = iris.data
  7. y = iris.target
  8. def hyperopt_train_test(params):
  9. X_ = X[:]
  10. # 因为下面的两个参数都不属于KNeighborsClassifier支持的参数,故使用后直接删除
  11. if 'normalize' in params:
  12. if params[ 'normalize'] == 1:
  13. X_ = normalize(X_)
  14. del params[ 'normalize']
  15. if 'scale' in params:
  16. if params[ 'scale'] == 1:
  17. X_ = scale(X_)
  18. del params[ 'scale']
  19. clf = KNeighborsClassifier(**params)
  20. return cross_val_score(clf, X_, y).mean()
  21. space4knn = {
  22. 'n_neighbors': hp.choice( 'n_neighbors', range( 1, 50)),
  23. 'scale': hp.choice( 'scale', [ 0, 1]), # 必须是choice,不要用quniform
  24. 'normalize': hp.choice( 'normalize', [ 0, 1])
  25. }
  26. def f(params):
  27. acc = hyperopt_train_test(params)
  28. return { 'loss': -acc, 'status': STATUS_OK} #注意这里的负号
  29. trials = Trials()
  30. best = fmin(f, space4knn, algo=tpe.suggest, max_evals= 100, trials=trials)
  31. print best

例二,也是取负 


 
  1. def objective(trial):
  2. C=trial.suggest_loguniform( 'C', 10e-10, 10)
  3. model=LogisticRegression(C=C, class_weight= 'balanced',max_iter= 10000, solver= 'lbfgs', n_jobs= -1)
  4. score=-cross_val_score(model, Xtrain, Ytrain, cv=kf, scoring= 'roc_auc').mean()
  5. return score

实际GBM完整的目标函数


 
  1. import lightgbm as lgb
  2. from hyperopt import STATUS_OK
  3. N_FOLDS = 10
  4. # Create the dataset
  5. train_set = lgb.Dataset(train_features, train_labels)
  6. def objective(params, n_folds = N_FOLDS):
  7. """Objective function for Gradient Boosting Machine Hyperparameter Tuning"""
  8. # Perform n_fold cross validation with hyperparameters
  9. # Use early stopping and evalute based on ROC AUC
  10. cv_results = lgb.cv(params, train_set, nfold = n_folds, num_boost_round = 10000, early_stopping_rounds = 100, metrics = 'auc', seed = 50)
  11. #此部分为核心代码,
  12. # Extract the best score
  13. best_score = max(cv_results[ 'auc-mean'])
  14. # Loss must be minimized
  15. loss = 1 - best_score
  16. # Dictionary with information for evaluation
  17. return { 'loss': loss, 'params': params, 'status': STATUS_OK}

2)域空间

贝叶斯优化中,域空间对每个超参数来说是一个概率分布而不是离散的值。因为很难确定不同数据集之间的最佳模型设定区间,此处主要采用贝叶斯算法进行推理。

此外,模型中有些参数是不需要调优的。以GBM为例,除了n_estimator之外,还有10个左右的参数需要调整。因此我们采用不同的分布来定义每个参数的域空间


 
  1. from hyperopt import hp
  2. # Define the search space
  3. space = {
  4. 'class_weight': hp.choice( 'class_weight', [ None, 'balanced']),
  5. 'boosting_type': hp.choice( 'boosting_type',
  6. [{ 'boosting_type': 'gbdt',
  7. 'subsample': hp.uniform( 'gdbt_subsample', 0.5, 1)},
  8. { 'boosting_type': 'dart',
  9. 'subsample': hp.uniform( 'dart_subsample', 0.5, 1)},
  10. { 'boosting_type': 'goss'}]),
  11. 'num_leaves': hp.quniform( 'num_leaves', 30, 150, 1),
  12. 'learning_rate': hp.loguniform( 'learning_rate', np.log( 0.01), np.log( 0.2)),
  13. 'subsample_for_bin': hp.quniform( 'subsample_for_bin', 20000, 300000, 20000),
  14. 'min_child_samples': hp.quniform( 'min_child_samples', 20, 500, 5),
  15. 'reg_alpha': hp.uniform( 'reg_alpha', 0.0, 1.0),
  16. 'reg_lambda': hp.uniform( 'reg_lambda', 0.0, 1.0),
  17. 'colsample_bytree': hp.uniform( 'colsample_by_tree', 0.6, 1.0)
  18. }

不同分布名称含义:

 choice:类别变量

   quniform:离散均匀分布(在整数空间上均匀分布)

   uniform:连续均匀分布(在浮点数空间上均匀分布)

   loguniform:连续对数均匀分布(在浮点数空间中的对数尺度上均匀分布)

  • hp.pchoice(label,p_options)以一定的概率返回一个p_options的一个选项。这个选项使得函数在搜索过程中对每个选项的可能性不均匀。
  • hp.uniform(label,low,high)参数在low和high之间均匀分布。
  • hp.quniform(label,low,high,q),参数的取值round(uniform(low,high)/q)*q,适用于那些离散的取值。
  • hp.loguniform(label,low,high) 返回根据 exp(uniform(low,high)) 绘制的值,以便返回值的对数是均匀分布的。
    优化时,该变量被限制在[exp(low),exp(high)]区间内。
  • hp.randint(label,upper) 返回一个在[0,upper)前闭后开的区间内的随机整数。
  • hp.normal(label, mu, sigma) where mu and sigma are the mean and standard deviation σ , respectively. 正态分布,返回值范围没法限制。
  • hp.qnormal(label, mu, sigma, q)
  • hp.lognormal(label, mu, sigma)
  • hp.qlognormal(label, mu, sigma, q)

定义与空间后,可以选择一个样本来查看典型样本形式


 
  1. # Sample from the full space
  2. example = sample(space)
  3. # Dictionary get method with default
  4. subsample = example[ 'boosting_type'].get( 'subsample', 1.0)
  5. # Assign top-level keys
  6. example[ 'boosting_type'] = example[ 'boosting_type'][ 'boosting_type']
  7. example[ 'subsample'] = subsample
  8. example

3)搜索算法

algo指定搜索算法,目前支持以下算法:
①随机搜索(hyperopt.rand.suggest)
②模拟退火(hyperopt.anneal.suggest)
③TPE算法(hyperopt.tpe.suggest,算法全称为Tree-structured Parzen Estimator Approach)

尽管从概念上来说,这是贝叶斯优化最难的一部分,但在 Hyperopt 中创建优化算法只需一行代码。使用树形 Parzen 评估器(Tree Parzen Estimation,以下简称 TPE)的代码如下:


 
  1. from hyperopt import tpe
  2. # Algorithm
  3. tpe_algorithm = tpe.suggest

4)结果历史数据

想知道背后的发展进程,可以使用「Trials」对象,它将存储基本的训练信息,还可以使用目标函数返回的字典(包含损失「loss」和参数「params」)


 
  1. from hyperopt import Trials
  2. # Trials object to track progress
  3. bayes_trials = Trials()

Trials只是用来记录每次eval的时候,具体使用了什么参数以及相关的返回值。这时候,fn的返回值变为dict,除了loss,还有一个status。Trials对象将数据存储为一个BSON对象,可以利用MongoDB做分布式运算。


 
  1. from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
  2. fspace = {
  3. 'x': hp.uniform( 'x', -5, 5)
  4. }
  5. def f(params):
  6. x = params[ 'x']
  7. val = x** 2
  8. return { 'loss': val, 'status': STATUS_OK}
  9. trials = Trials()
  10. best = fmin(fn=f, space=fspace, algo=tpe.suggest, max_evals= 50, trials=trials)
  11. print( 'best:', best)
  12. print( 'trials:')
  13. for trial in trials.trials[: 2]:
  14. print(trial)

对于STATUS_OK的返回,会统计它的loss值,而对于STATUS_FAIL的返回,则会忽略。

可以通过这里面的值,把一些变量与loss的点绘图,来看匹配度。或者tid与变量绘图,看它搜索的位置收敛(非数学意义上的收敛)情况。
trials有这几种:

  • trials.trials - a list of dictionaries representing everything about the search
  • trials.results - a list of dictionaries returned by ‘objective’ during the search
  • trials.losses() - a list of losses (float for each ‘ok’ trial) trials.statuses() - a list of status strings

5)优化算法


 
  1. from hyperopt import fmin
  2. MAX_EVALS = 500
  3. # Optimize
  4. best = fmin(fn = objective, space = space, algo = tpe.suggest,
  5. max_evals = MAX_EVALS, trials = bayes_trials)

使用sklearn的数据进行一次测试


 
  1. #coding:utf-8
  2. from hyperopt import fmin, tpe, hp, rand
  3. import numpy as np
  4. from sklearn.metrics import accuracy_score
  5. from sklearn import svm
  6. from sklearn import datasets
  7. # SVM的三个超参数:C为惩罚因子,kernel为核函数类型,gamma为核函数的额外参数(对于不同类型的核函数有不同的含义)
  8. # 有别于传统的网格搜索(GridSearch),这里只需要给出最优参数的概率分布即可,而不需要按照步长把具体的值给一个个枚举出来
  9. parameter_space_svc ={
  10. # loguniform表示该参数取对数后符合均匀分布
  11. 'C':hp.loguniform( "C", np.log( 1), np.log( 100)),
  12. 'kernel':hp.choice( 'kernel',[ 'rbf', 'poly']),
  13. 'gamma': hp.loguniform( "gamma", np.log( 0.001), np.log( 0.1)),
  14. }
  15. # 鸢尾花卉数据集,是一类多重变量分析的数据集
  16. # 通过花萼长度,花萼宽度,花瓣长度,花瓣宽度4个属性预测鸢尾花卉属于(Setosa,Versicolour,Virginica)三个种类中的哪一类
  17. iris = datasets.load_digits()
  18. #--------------------划分训练集和测试集--------------------
  19. train_data = iris.data[ 0: 1300]
  20. train_target = iris.target[ 0: 1300]
  21. test_data = iris.data[ 1300: -1]
  22. test_target = iris.target[ 1300: -1]
  23. #-----------------------------------------------------------
  24. # 计数器,每一次参数组合的枚举都会使它加1
  25. count = 0
  26. def function(args):
  27. print(args)
  28. # **可以把dict转换为关键字参数,可以大大简化复杂的函数调用
  29. clf = svm.SVC(**args)
  30. # 训练模型
  31. clf.fit(train_data,train_target)
  32. # 预测测试集
  33. prediction = clf.predict(test_data)
  34. global count
  35. count = count + 1
  36. score = accuracy_score(test_target,prediction)
  37. print( "第%s次,测试集正确率为:" % str(count),score)
  38. # 由于hyperopt仅提供fmin接口,因此如果要求最大值,则需要取相反数
  39. return -score
  40. # algo指定搜索算法,目前支持以下算法:
  41. # ①随机搜索(hyperopt.rand.suggest)
  42. # ②模拟退火(hyperopt.anneal.suggest)
  43. # ③TPE算法(hyperopt.tpe.suggest,算法全称为Tree-structured Parzen Estimator Approach)
  44. # max_evals指定枚举次数上限,即使第max_evals次枚举仍未能确定全局最优解,也要结束搜索,返回目前搜索到的最优解
  45. best = fmin(function, parameter_space_svc, algo=tpe.suggest, max_evals= 100)
  46. # best["kernel"]返回的是数组下标,因此需要把它还原回来
  47. kernel_list = [ 'rbf', 'poly']
  48. best[ "kernel"] = kernel_list[best[ "kernel"]]
  49. print( "最佳参数为:",best)
  50. clf = svm.SVC(**best)
  51. print(clf)

输出结果如下:


 
  1. { 'gamma': 0.0010051585652497248, 'kernel': 'poly', 'C': 29.551164584073586}
  2. 1次,测试集正确率为: 0.959677419355
  3. { 'gamma': 0.006498482991283678, 'kernel': 'rbf', 'C': 6.626826808981864}
  4. 2次,测试集正确率为: 0.834677419355
  5. { 'gamma': 0.008192671915044216, 'kernel': 'poly', 'C': 34.48947180442318}
  6. 3次,测试集正确率为: 0.959677419355
  7. { 'gamma': 0.001359874432712413, 'kernel': 'rbf', 'C': 1.6402360233244775}
  8. 98次,测试集正确率为: 0.971774193548
  9. { 'gamma': 0.0029328466160223813, 'kernel': 'poly', 'C': 1.6328276445108112}
  10. 99次,测试集正确率为: 0.959677419355
  11. { 'gamma': 0.0015786919481979775, 'kernel': 'rbf', 'C': 4.669133703622153}
  12. 100次,测试集正确率为: 0.969758064516
  13. 最佳参数为: { 'gamma': 0.00101162002595069, 'kernel': 'rbf', 'C': 21.12514792460218}
  14. SVC(C= 21.12514792460218, cache_size= 200, class_weight= None, coef0= 0.0,
  15. decision_function_shape= None, degree= 3, gamma= 0.00101162002595069,
  16. kernel= 'rbf', max_iter= -1, probability= False, random_state= None,
  17. shrinking= True, tol= 0.001, verbose= False)

三、Optuna

有关这个库的文献好少,不过看代码的话,形式和Hyperopt差不太多


 
  1. from sklearn.model_selection import train_test_split, cross_val_score, StratifiedKFold
  2. from sklearn.linear_model import LogisticRegression
  3. from sklearn.metrics import roc_auc_score
  4. import matplotlib.pyplot as plt
  5. import seaborn as sns
  6. import pandas as pd
  7. import numpy as np
  8. import optuna
  9. import os
  10. from hyperopt import hp
  11. # print(hp.loguniform('sdf',1,5))
  12. # list_space = [
  13. # hp.uniform('a', 0, 1),
  14. # hp.loguniform('b', 0, 1)]
  15. # tuple_space = (
  16. # hp.uniform('a', 0, 1),
  17. # hp.loguniform('b', 0, 1))
  18. # dict_space = {
  19. # 'a': hp.uniform('a', 0, 1),
  20. # 'b': hp.loguniform('b', 0, 1)}
  21. # print(dict_space,tuple_space,dict_space)
  22. train=pd.read_csv( '../data/train.csv', index_col= 'id')
  23. test=pd.read_csv( '../data/test.csv', index_col= 'id')
  24. submission=pd.read_csv( '../data/sample_submission.csv', index_col= 'id')
  25. Ytrain=train[ 'target']
  26. train=train[list(test)]
  27. all_data=pd.concat((train, test))
  28. print(train.shape, test.shape, all_data.shape)
  29. encoded=pd.get_dummies(all_data, columns=all_data.columns, sparse= True)
  30. encoded=encoded.sparse.to_coo()
  31. encoded=encoded.tocsr()
  32. Xtrain=encoded[:len(train)]
  33. Xtest=encoded[len(train):]
  34. kf=StratifiedKFold(n_splits= 10)
  35. def objective(trial):
  36. C=trial.suggest_loguniform( 'C', 10e-10, 10)
  37. model=LogisticRegression(C=C, class_weight= 'balanced',max_iter= 10000, solver= 'lbfgs', n_jobs= -1)
  38. score=-cross_val_score(model, Xtrain, Ytrain, cv=kf, scoring= 'roc_auc').mean()
  39. return score
  40. study=optuna.create_study()
  41. study.optimize(objective, n_trials= 5)
  42. print(study.best_params)
  43. print(-study.best_value)
  44. print(study.best_params)
  45. model=LogisticRegression(C= 0.09536298444122952, class_weight= 'balanced',max_iter= 10000, solver= 'lbfgs', n_jobs= -1)
  46. model.fit(Xtrain, Ytrain)
  47. predictions=model.predict_proba(Xtest)[:, 1]
  48. submission[ 'target']=predictions
  49. submission.to_csv( r"E:\TensorFlow\大数据之路\kaggle\Categorical_Feature_Encoding_Challenge\result\res2.csv",
  50. index= False)

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值