如何在 xgboost 中关闭 silent = 1?

How to turn off silent = 1 in xgboost?

我正在尝试训练 xgboost 模型,训练似乎有效,但我无法将参数 silent 设置为 0,即打印训练迭代。我使用下面的代码:

param <- list(max_depth = 2, eta = 0.005, nthread = 2, objective = "multi:softprob", eval_metric = "auc", num_class = 3, verbose = 2, silent = 0)

xgb.train(param, data = test_matrix_1, nrounds = 10, print_every_n = 1)

并在 return 中得到这个:

##### xgb.Booster
raw: 12.2 Kb 
call:
 xgb.train(params = param, data = test_matrix_1, nrounds = 10, 
  print_every_n = 1)
params (as set within xgb.train):
 max_depth = "2", eta = "0.005", nthread = "2", objective = "multi:softprob", eval_metric = "auc", num_class = "3", verbose = "2", silent = "0", silent = "1"
xgb.attributes:
 niter
callbacks:
 cb.print.evaluation(period = print_every_n)
niter: 10

尝试options(warn=-1, echo=FALSE, verbose=FALSE),您还可以在.Options

中查看其他选项

你没有在 R 中使用 silent 参数。 您使用 verbose 参数。

这里是verbose = 0, 1 or 2

的例子
# verbose = 0, no message
bst <- xgboost(data = dtrain, max.depth = 2, eta = 1, nthread = 2, nround = 2, objective = "binary:logistic", verbose = 0)

# verbose = 1, print evaluation metric
bst <- xgboost(data = dtrain, max.depth = 2, eta = 1, nthread = 2, nround = 2, objective = "binary:logistic", verbose = 1)

## [0]  train-error:0.046522
## [1]  train-error:0.022263

# verbose = 2, also print information about tree
bst <- xgboost(data = dtrain, max.depth = 2, eta = 1, nthread = 2, nround = 2, objective = "binary:logistic", verbose = 2)

## [11:41:01] amalgamation/../src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 6 extra nodes, 0 pruned nodes, max_depth=2
## [0]  train-error:0.046522
## [11:41:01] amalgamation/../src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 4 extra nodes, 0 pruned nodes, max_depth=2
## [1]  train-error:0.022263

首先如果你想关闭silent = 1(参考?xgboost).

其次你需要watchlist参数,因为你关心在学习时观察eval_metric。它能够在第一个数据集上学习并在第二个数据集上测试其模型(有关更多信息,请参阅 ?xgboost)。例如

watchlist <- list(train=dtrain, test=dtest)

现在可以通过以下方式完成示例实现 -

library(xgboost)

#sample data
data(agaricus.train, package='xgboost')
data(agaricus.test, package='xgboost')

dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
dtest  <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
watchlist <- list(train=dtrain, test=dtest)

#training XGBoost model 
param <- list(max_depth = 2, eta = 1, nthread = 2, 
              objective = "binary:logistic", eval_metric = "auc", eval_metric="error")
fit <- xgb.train(param, data=dtrain, nrounds=10, watchlist=watchlist, verbose = 2)

这个解决方法对我有用:

将数据转换为包含标签和数据的列表,作为来自 Matrix 包的 CsparseMatrix。

test_matrix_3 <- list(data = as(training_data, "CsparseMatrix"), label = label)

之后这个功能就可以正常工作了。

xgboost(data = test_matrix_3$data, label = test_matrix_3$label, 
                 max.depth = 4, eta = 1, nthread = 2, nround = 500, objective = "multi:softmax", num_class = 3, eval.error = "auc")