簡體   English   中英

如何使用`catboost`選擇輪次?

[英]How to choose the nrounds using `catboost`?

如果我理解正確catboost ,我們需要像在xgboost一樣使用 CV 調整nrounds 我在官方教程中看到如下代碼在[8]

params_with_od <- list(iterations = 500,
                       loss_function = 'Logloss',
                       train_dir = 'train_dir',
                       od_type = 'Iter',
                       od_wait = 30)
model_with_od <- catboost.train(train_pool, test_pool, params_with_od)

這導致最佳iterations = 211。

我的問題是:

  • 是否正確:此命令使用test_pool來選擇最佳iterations而不是使用交叉驗證?
  • 如果是,catboost 是否提供了從 CV 中選擇最佳iterations的命令,還是我需要手動執行?

Catboost 正在進行交叉驗證以確定最佳迭代次數。 train_pool 和 test_pool 都是包含目標變量的數據集。 在本教程的早些時候,他們寫了

train_path = '../R-package/inst/extdata/adult_train.1000'
test_path = '../R-package/inst/extdata/adult_test.1000'

column_description_vector = rep('numeric', 15)
cat_features <- c(3, 5, 7, 8, 9, 10, 11, 15)
for (i in cat_features)
    column_description_vector[i] <- 'factor'

train <- read.table(train_path, head=F, sep="\t", colClasses=column_description_vector)
test <- read.table(test_path, head=F, sep="\t", colClasses=column_description_vector)
target <- c(1)
train_pool <- catboost.from_data_frame(data=train[,-target], target=train[,target])
test_pool <- catboost.from_data_frame(data=test[,-target], target=test[,target])

當您執行 catboost.train(train_pool, test_pool, params_with_od) train_pool 用於訓練時, test_pool 用於通過交叉驗證確定最佳迭代次數。

現在您感到困惑是正確的,因為在本教程的后面,他們再次使用 test_pool 和擬合模型進行預測(model_best 類似於 model_with_od,但使用不同的過擬合檢測器 IncToDec):

prediction_best <- catboost.predict(model_best, test_pool, type = 'Probability')

這可能是不好的做法。 現在他們可能會用他們的 IncToDec 過擬合檢測器逃脫它 - 我不熟悉它背后的數學 - 但是對於 Iter 類型的過擬合檢測器,您需要有單獨的訓練、驗證和測試數據集(如果你想成為在保存方面,對 IncToDec 過擬合檢測器執行相同的操作)。 然而,這只是一個展示功能的教程,所以我不會對他們已經如何使用哪些數據過於迂腐。

這里有一個關於過擬合檢測器的更多細節的鏈接: https : //tech.yandex.com/catboost/doc/dg/concepts/overfit-detector-docpage/

使用插入符交叉驗證。 注意本教程的 [12]

  1. 將迭代次數基於一個 test_pool 和 catboost.train() 的最佳迭代是一個非常糟糕的決定。 這樣做時,您正在將參數調整到一個特定的測試集,並且您的模型將無法很好地處理新數據。 因此,您假設像 XGBoost 一樣,您需要應用 CV 來找到最佳迭代次數是正確的。
  2. catboost中確實有CV功能。 您應該做的是指定大量迭代並在不改進的情況下通過使用參數 early_stopping_rounds 在一定輪數后停止訓練。 不幸的是,與 LightGBM 不同的是,catboost 似乎沒有選項在 CV 之后自動給出最佳的提升輪數以應用於 catboost.train()。 因此,它需要一些解決方法。 這是一個應該工作的例子:
    library(catboost)
    library(data.table)

    parameter = list(
      thread_count = n_cores,
      loss_function = "RMSE",
      eval_metric = c("RMSE","MAE","R2"),
      iterations = 10^5, # Train up to 10^5 rounds
      early_stopping_rounds = 100, # Stop after 100 rounds of no improvement
    )

    # Apply 6-fold CV
    model = catboost.cv(
        pool = train_pool,
        fold_count = 6,
        params = parameter
      )

      # Transform output to DT
      setDT(cbt_occupancy)
      model[, iterations := .I]
      # Order from lowest to highgest RMSE
      setorder(model, test.RMSE.mean)
      # Select iterations with lowest RMSE
      parameter$iterations = model[1, iterations]

      # Train model with optimal iterations
      model = catboost.train(
        learn_pool = train_pool,
        test_pool = test_pool,
        params = parameter
      )

我認為這是 xgboost 和 catboost 的一般問題。 nround的選擇與學習率的選擇有關。 因此,我推薦更高的回合(1000+)和低學習率。 在找到最佳炒作參數並重試較低的學習率以檢查您選擇的炒作參數是否穩定后。

我發現@nikitxskv 的回答具有誤導性。

  1. R 教程中在 [12] 中只選擇learning_rate = 0.1沒有多重選擇。 因此,沒有關於nround調整的提示。
  2. 實際上,在 [12] 中只是使用函數expand.grid來找到最好的炒作參數。 它對depthgamma等的選擇起作用。
  3. 在實踐中,我們不使用這種方式來找到合適的nround (太長)。

現在來回答兩個問題。

是否正確:此命令使用 test_pool 來選擇最佳迭代而不是使用交叉驗證?

是的,但您可以使用 CV。

如果是,catboost 是否提供了從 CV 中選擇最佳迭代的命令,還是我需要手動執行?

這取決於你自己。 如果您非常厭惡提升過度擬合,我建議您嘗試一下。 有很多包可以解決這個問題。 我推薦tidymodel包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM