3

私は RStudio で作業しており、XGBoost のカスタム目的関数を開発しようとしています。プロセスがどのように機能するかを確実に理解するために、「バイナリ:ロジスティック」目的を再現する目的関数を作成しようとしました。ただし、私のカスタム目的関数は、大幅に異なる結果をもたらします (多くの場合、はるかに悪い)。

XGBoost github リポジトリの例に基づいて、カスタム目的関数は次のようになります。

# custom objective function
logloss <- function(preds, dtrain){

  # Get weights and labels
  labels<- getinfo(dtrain, "label")

  # Apply logistic transform to predictions
  preds <- 1/(1 + exp(-preds))

  # Find gradient and hessian
  grad <- (preds - labels)
  hess <- preds * (1-preds)

  return(list("grad" = grad, "hess" = hess))
}

この中程度のブログ投稿に基づくと、これは XGBoost バイナリ目標で実装されているものと一致するようです。

いくつかの簡単なテスト データを使用すると、組み込み目標の最終的なトレーニング rmse は ~0.468 で、カスタム目標を使用すると ~0.72 です。

以下のコードを使用して、テスト データを生成し、問題を再現できます。

私のコードが目的の「バイナリ:ロジスティック」の動作を再現しない理由を誰かが説明できますか? XGBoost R-Package v0.90.0.2 を使用しています。

library(data.table)
library(xgboost)

# Generate test data
generate_test_data <- function(n_rows = 1e5, feature_count = 5, train_fraction = 0.5){

  # Make targets
  test_data <- data.table(
    target = sign(runif(n = n_rows, min=-1, max=1))
  )

  # Add feature columns.These are normally distributed and shifted by the target
  # in order to create a noisy signal
  for(feature in 1:feature_count){

    # Randomly create features of the noise
    mu <- runif(1, min=-1, max=1)
    sdev <- runif(1, min=5, max=10)

    # Create noisy signal
    test_data[, paste0("feature_", feature) := rnorm(
      n=n_rows, mean = mu, sd = sdev)*target + target]
  }

  # Split data into test/train
  test_data[, index_fraction := .I/.N]
  split_data <- list(
    "train" = test_data[index_fraction < (train_fraction)],
    "test" = test_data[index_fraction >= (train_fraction)]
  )

  # Make vector of feature names
  feature_names <- paste0("feature_", 1:feature_count)

  # Make test/train matrix and labels
  split_data[["test_trix"]] <- as.matrix(split_data$test[, feature_names, with=FALSE])
  split_data[["train_trix"]] <- as.matrix(split_data$train[, feature_names, with=FALSE])
  split_data[["test_labels"]] <- as.logical(split_data$test$target + 1)    
  split_data[["train_labels"]] <- as.logical(split_data$train$target + 1)

  return(split_data)
}

# Build the tree
build_model <- function(split_data, objective){

  # Make evaluation matrix
  train_dtrix <-
    xgb.DMatrix(
      data = split_data$train_trix, label = split_data$train_labels)

  # Train the model
  model <- xgb.train(
    data = train_dtrix,
    watchlist = list(
      train = train_dtrix),
    nrounds = 5,
    objective =  objective,
    eval_metric = "rmse"
  )

  return(model)
}

split_data <- generate_test_data()
cat("\nUsing built-in binary:logistic objective.\n")
test_1 <- build_model(split_data, "binary:logistic")
cat("\n\nUsing custom objective")
test_2 <- build_model(split_data, logloss)
4

0 に答える 0