認識 Grid Search

Python 超參數調校

Alex Scriven

Data Scientist

自動化 2 個超參數

你先前的作業:

neighbors_list = [3,5,10,20,50,75]
accuracy_list = []
for test_number in neighbors_list:
    model = KNeighborsClassifier(n_neighbors=test_number)
    predictions = model.fit(X_train, y_train).predict(X_test)
    accuracy = accuracy_score(y_test, predictions)
    accuracy_list.append(accuracy)

接著我們彙整到一個 dataframe 來分析。

Python 超參數調校

自動化 2 個超參數

 

那如果要測試 2 個超參數的取值呢?

以 GBM 演算法為例:

  • learn_rate [0.001, 0.01, 0.05]
  • max_depth [4,6,8,10]

我們可以用(巢狀)for 迴圈!

Python 超參數調校

自動化 2 個超參數

先寫一個建立模型的函式:

def gbm_grid_search(learn_rate, max_depth):
    model = GradientBoostingClassifier(
            learning_rate=learn_rate,
            max_depth=max_depth)

predictions = model.fit(X_train, y_train).predict(X_test)
return([learn_rate, max_depth, accuracy_score(y_test, predictions)])
Python 超參數調校

自動化 2 個超參數

 

現在可以走訪超參數清單並呼叫函式:

results_list = []

for learn_rate in learn_rate_list:
    for max_depth in max_depth_list:
        results_list.append(gbm_grid_search(learn_rate,max_depth))

Python 超參數調校

自動化 2 個超參數

 

我們也能把結果放進 DataFrame 並印出:

results_df = pd.DataFrame(results_list, columns=['learning_rate', 'max_depth', 'accuracy'])
print(results_df)

results table

Python 超參數調校

要建多少模型?

 

如果再加入更多超參數與取值,會產生更多模型。

  • 關係不是線性,而是指數成長
  • 多一個超參數的取值,並不只多一個模型
  • 超參數 1 有 5 個、超參數 2 有 10 個,共 50 個模型!

那交叉驗證呢?

  • 10 折交叉驗證會變成 50x10 = 500 個模型!
Python 超參數調校

從 2 個到 N 個超參數

 

若再加更多超參數呢?

我們可以把迴圈再巢狀下去!

# 調整要測試的取值清單
learn_rate_list = [0.001, 0.01, 0.1, 0.2, 0.3, 0.4, 0.5]
max_depth_list = [4,6,8, 10, 12, 15, 20, 25, 30]
subsample_list = [0.4,0.6, 0.7, 0.8, 0.9]
max_features_list = ['auto', 'sqrt']
Python 超參數調校

從 2 個到 N 個超參數

調整我們的函式:

def gbm_grid_search(learn_rate, max_depth,subsample,max_features):
    model = GradientBoostingClassifier(
        learning_rate=learn_rate, 
        max_depth=max_depth,
        subsample=subsample,
        max_features=max_features)
    predictions = model.fit(X_train, y_train).predict(X_test)
    return([learn_rate, max_depth, accuracy_score(y_test, predictions)])
Python 超參數調校

從 2 個到 N 個超參數

調整 for 迴圈(巢狀):

for learn_rate in learn_rate_list:
    for max_depth in max_depth_list:
        for subsample in subsample_list:
            for max_features in max_features_list:
                results_list.append(gbm_grid_search(learn_rate,max_depth,
                                     subsample,max_features))
results_df = pd.DataFrame(results_list, columns=['learning_rate',
                         'max_depth', 'subsample', 'max_features','accuracy'])
print(results_df)
Python 超參數調校

從 2 個到 N 個超參數

 

現在有多少模型?

  • 7x9x5x2 = 630(若做交叉驗證則是 6,300)

我們不可能一直巢狀下去!

而且,如果我們想要:

  • 訓練時間與分數的細節
  • 交叉驗證分數的細節
Python 超參數調校

認識 Grid Search

來建立一個網格:

  • 左側列出所有 max_depth 的取值
  • 上方列出所有 learning_rate 的取值

超參數組合表

Python 超參數調校

認識 Grid Search

逐一走訪網格中的每個儲存格:

超參數組合表

(4,0.001) 等同於建立這樣的估計器:

GradientBoostingClassifier(max_depth=4, learning_rate=0.001)
Python 超參數調校

Grid Search 的優缺點

 

此方法的幾個優點:

優點:

  • 你不必寫上千行程式碼
  • 能在網格內找到最佳模型(此處有特別說明)
  • 容易解釋
Python 超參數調校

Grid Search 的優缺點

 

此方法的幾個缺點:

  • 計算成本高!還記得我們多快就做出 6,000+ 個模型嗎?
  • 它是「不具引導性」的。一個模型的結果不會幫助下一個模型。

 

之後我們會介紹「具引導性」的方法!

Python 超參數調校

一起來練習吧!

Python 超參數調校

Preparing Video For Download...