機(jī)器學(xué)習(xí)面試題集 - 超參數(shù)調(diào)優(yōu)

超參數(shù)搜索算法一般包括哪幾個(gè)要素

目標(biāo)函數(shù)

搜索范圍

算法的其他參數(shù)


超參數(shù)有哪些調(diào)優(yōu)方法?

網(wǎng)格搜索

給出一個(gè)搜索范圍后,遍歷所有點(diǎn),找出最優(yōu)值

缺點(diǎn):耗時(shí)

對(duì)策:將搜索范圍和步長(zhǎng)先設(shè)置的大一些,鎖定最優(yōu)值的范圍。

    再逐漸縮小范圍和步長(zhǎng),更精確的確定最優(yōu)值

缺點(diǎn):可能會(huì)錯(cuò)過(guò)全局最優(yōu)值

隨機(jī)搜索

給定一個(gè)搜索范圍后,從中隨機(jī)的選擇樣本點(diǎn)。

缺點(diǎn):可能會(huì)錯(cuò)過(guò)全局最優(yōu)值

貝葉斯優(yōu)化算法

通過(guò)學(xué)習(xí)目標(biāo)函數(shù)的形狀,找到影響最優(yōu)值的參數(shù)。

 算法:首先根據(jù)先驗(yàn)分布,假設(shè)一個(gè)搜集函數(shù)。再用每個(gè)新的樣本點(diǎn),更新目標(biāo)函數(shù)的先驗(yàn)分布。由后驗(yàn)分布得到全局最值可能的位置

 缺點(diǎn):容易陷入局部最優(yōu)值,因?yàn)檎业搅艘粋€(gè)局部最優(yōu)值,會(huì)在該區(qū)域不斷采樣

 對(duì)策:在還未取樣的區(qū)域進(jìn)行探索,在最可能出現(xiàn)全局最值的區(qū)域進(jìn)行采樣

下面來(lái)具體看看如何用 網(wǎng)格搜索(grid search) 對(duì) SVM 進(jìn)行調(diào)參。

網(wǎng)格搜索實(shí)際上就是暴力搜索:
首先為想要調(diào)參的參數(shù)設(shè)定一組候選值,然后網(wǎng)格搜索會(huì)窮舉各種參數(shù)組合,根據(jù)設(shè)定的評(píng)分機(jī)制找到最好的那一組設(shè)置。


以支持向量機(jī)分類器 SVC 為例,用 GridSearchCV 進(jìn)行調(diào)參:

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.svm import SVC

1. 導(dǎo)入數(shù)據(jù)集,分成 train 和 test 集:

digits = datasets.load_digits()

n_samples = len(digits.images)
X = digits.images.reshape((n_samples, -1))
y = digits.target

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.5, random_state=0)

2. 備選的參數(shù)搭配有下面兩組,并分別設(shè)定一定的候選值:
例如我們用下面兩個(gè) grids:
kernel='rbf', gamma, 'C'
kernel='linear', 'C'

tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
                     'C': [1, 10, 100, 1000]},
                    {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]

3. 定義評(píng)分方法為:

scores = ['precision', 'recall']

4. 調(diào)用 GridSearchCV

SVC(), tuned_parameters, cv=5, 還有 scoring 傳遞進(jìn)去,
用訓(xùn)練集訓(xùn)練這個(gè)學(xué)習(xí)器 clf,
再調(diào)用 clf.best_params_ 就能直接得到最好的參數(shù)搭配結(jié)果,

例如,在 precision 下,
返回最好的參數(shù)設(shè)置是:{'C': 10, 'gamma': 0.001, 'kernel': 'rbf'}

還可以通過(guò) clf.cv_results_ 的 'params','mean_test_score',看一下具體的參數(shù)間不同數(shù)值的組合后得到的分?jǐn)?shù)是多少:
結(jié)果中可以看到最佳的組合的分?jǐn)?shù)為:0.988 (+/-0.017)

還可以通過(guò) classification_report 打印在測(cè)試集上的預(yù)測(cè)結(jié)果 clf.predict(X_test) 與真實(shí)值 y_test 的分?jǐn)?shù):

for score in scores:
    print("# Tuning hyper-parameters for %s" % score)
    print()

     # 調(diào)用 GridSearchCV,將 SVC(), tuned_parameters, cv=5, 還有 scoring 傳遞進(jìn)去,
    clf = GridSearchCV(SVC(), tuned_parameters, cv=5,
                       scoring='%s_macro' % score)
    # 用訓(xùn)練集訓(xùn)練這個(gè)學(xué)習(xí)器 clf
    clf.fit(X_train, y_train)

    print("Best parameters set found on development set:")
    print()
    
    # 再調(diào)用 clf.best_params_ 就能直接得到最好的參數(shù)搭配結(jié)果
    print(clf.best_params_)
    
    print()
    print("Grid scores on development set:")
    print()
    means = clf.cv_results_['mean_test_score']
    stds = clf.cv_results_['std_test_score']
    
    # 看一下具體的參數(shù)間不同數(shù)值的組合后得到的分?jǐn)?shù)是多少
    for mean, std, params in zip(means, stds, clf.cv_results_['params']):
        print("%0.3f (+/-%0.03f) for %r"
              % (mean, std * 2, params))
              
    print()

    print("Detailed classification report:")
    print()
    print("The model is trained on the full development set.")
    print("The scores are computed on the full evaluation set.")
    print()
    y_true, y_pred = y_test, clf.predict(X_test)
    
    # 打印在測(cè)試集上的預(yù)測(cè)結(jié)果與真實(shí)值的分?jǐn)?shù)
    print(classification_report(y_true, y_pred))
    
    print()

相關(guān)文章:

用驗(yàn)證曲線 validation curve 選擇超參數(shù)
為什么要用交叉驗(yàn)證
用學(xué)習(xí)曲線 learning curve 來(lái)判別過(guò)擬合問(wèn)題


大家好!我是 Alice,歡迎進(jìn)入 機(jī)器學(xué)習(xí)面試題集 系列!

這個(gè)系列會(huì)以《百面機(jī)器學(xué)習(xí)》的學(xué)習(xí)筆記為主線,除了用導(dǎo)圖的形式提煉出精華,還會(huì)對(duì)涉及到的重要概念進(jìn)行更深度的解釋,順便也梳理一下機(jī)器學(xué)習(xí)的知識(shí)體系。

歡迎關(guān)注我,一起交流學(xué)習(xí)!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容