4

我正在尝试使用sink函数保存提升的日志,如下代码:

require(xgboost)
require(R.utils)

data(iris)
train.model <- model.matrix(Sepal.Length~., iris)

dtrain <- xgb.DMatrix(data=train.model, label=iris$Sepal.Length)

xgb_grid = list(eta = 0.05, max_depth = 5, subsample = 0.7, gamma = 0.3,
  min_child_weight = 1)

sink("evaluationLog.txt")
fit_boost <-xgb.cv(data  = dtrain,
                  nrounds     = 1000,
                  objective   = "reg:linear",
                  eval_metric = "logloss", 
                  params = xgb_grid,
                  colsample_bytree = 0.7, 
                  early_stopping_rounds = 100,
                  nfold = 5,
                  prediction = TRUE,
                  maximize = FALSE
                  )

sink()

但是我看不到“发生了什么”,因为它没有打印函数的输出和/或消息。

我的问题是我怎样才能同时检索一个.txt文件sink并查看xgb.cv正在打印的函数(在这种情况下是什么)?

谢谢!

4

1 回答 1

7

Use argument split:

sink('test.txt', split = TRUE)
print(letters)
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
#[18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
sink()

As you can see above it will both print on the console and you will also find a test.txt file in your current directory.

于 2017-12-14T16:47:56.070 回答