跳到目录

从 JSON 加载 XGBoost

介绍

本教程的目的是向您展示如何正确加载和使用已转储到 JSON 的 XGBoost 模型。 XGBoost 内部将所有数据转换为32 位浮点数,转储到 JSON 的值是这些值的十进制表示。使用从 JSON 文件解析的模型时,必须小心处理:

  • 输入数据,应将其转换为 32 位浮点数
  • 以十进制表示形式存储在 JSON 中的任何 32 位浮点数
  • 任何计算都必须使用 32 位数学运算符进行

设置

出于本教程的目的,我们将加载 xgboost、jsonlite 和 float 包。我们还将选项中的 digits 设置为 22,以防我们想检查结果的许多位数。

## Loading required package: xgboost
## Loading required package: jsonlite
## Loading required package: float
options(digits = 22)

我们将根据此处首次提供的示例创建一个玩具二进制逻辑模型,以便我们可以轻松理解转储的 JSON 模型对象的结构。这将帮助我们理解可能出现差异的地方以及如何处理它们。

dates <- c(20180130, 20180130, 20180130,
           20180130, 20180130, 20180130,
           20180131, 20180131, 20180131,
           20180131, 20180131, 20180131,
           20180131, 20180131, 20180131,
           20180134, 20180134, 20180134)

labels <- c(1, 1, 1,
            1, 1, 1,
            0, 0, 0,
            0, 0, 0,
            0, 0, 0,
            0, 0, 0)

data <- data.frame(dates = dates, labels = labels)

bst <- xgb.train(
  data = xgb.DMatrix(as.matrix(data$dates), label = labels, missing = NA, nthread = 1),
  nrounds = 1,
  params = xgb.params(
    objective = "binary:logistic",
    nthread = 2,
    max_depth = 1
  )
)

比较结果

现在我们将模型转储到 JSON,并尝试说明可能出现的各种问题以及如何正确处理它们。

首先将模型转储到 JSON

bst_json <- xgb.dump(bst, with_stats = FALSE, dump_format = 'json')
bst_from_json <- fromJSON(bst_json, simplifyDataFrame = FALSE)
node <- bst_from_json[[1]]
cat(bst_json)
## [
##   { "nodeid": 0, "depth": 0, "split": "f0", "split_condition": 20180132, "yes": 1, "no": 2, "missing": 2 , "children": [
##     { "nodeid": 1, "leaf": 0.514285684 }, 
##     { "nodeid": 2, "leaf": -0.327272743 }
##   ]}
## ]

上面代码块显示的树 JSON 告诉我们,如果数据小于 20180132,树将输出第一个叶子的值。否则,它将输出第二个叶子的值。让我们尝试使用现有数据手动重现这一点,并确认它与我们已经计算出的模型预测相匹配。

bst_preds_logodds <- predict(bst, as.matrix(data$dates), outputmargin = TRUE)

# calculate the logodds values using the JSON representation
bst_from_json_logodds <- ifelse(data$dates < node$split_condition,
                                node$children[[1]]$leaf,
                                node$children[[2]]$leaf)

bst_preds_logodds
##  [1] -0.1788614988327026367188 -0.1788614988327026367188
##  [3] -0.1788614988327026367188 -0.1788614988327026367188
##  [5] -0.1788614988327026367188 -0.1788614988327026367188
##  [7] -1.0204199552536010742188 -1.0204199552536010742188
##  [9] -1.0204199552536010742188 -1.0204199552536010742188
## [11] -1.0204199552536010742188 -1.0204199552536010742188
## [13] -1.0204199552536010742188 -1.0204199552536010742188
## [15] -1.0204199552536010742188 -1.0204199552536010742188
## [17] -1.0204199552536010742188 -1.0204199552536010742188
bst_from_json_logodds
##  [1]  0.5142856839999999651880  0.5142856839999999651880
##  [3]  0.5142856839999999651880  0.5142856839999999651880
##  [5]  0.5142856839999999651880  0.5142856839999999651880
##  [7]  0.5142856839999999651880  0.5142856839999999651880
##  [9]  0.5142856839999999651880  0.5142856839999999651880
## [11]  0.5142856839999999651880  0.5142856839999999651880
## [13]  0.5142856839999999651880  0.5142856839999999651880
## [15]  0.5142856839999999651880 -0.3272727429999999770871
## [17] -0.3272727429999999770871 -0.3272727429999999770871
# test that values are equal
bst_preds_logodds == bst_from_json_logodds
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE

都不相等。发生了什么?

在这个阶段发生了两件事

  • 输入数据未转换为 32 位浮点数
  • JSON 变量未转换为 32 位浮点数

第一课:所有数据都是 32 位浮点数

处理导入的 JSON 时,所有数据都必须转换为 32 位浮点数

为了解释这一点,让我们重复比较并将结果四舍五入到两位小数

round(bst_preds_logodds, 2) == round(bst_from_json_logodds, 2)
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE

如果我们四舍五入到两位小数,我们会看到只有与数据值 20180131 相关的元素不一致。如果我们将数据转换为浮点数,它们就一致了

# now convert the dates to floats first
bst_from_json_logodds <- ifelse(fl(data$dates) < node$split_condition,
                                node$children[[1]]$leaf,
                                node$children[[2]]$leaf)

# test that values are equal
round(bst_preds_logodds, 2) == round(bst_from_json_logodds, 2)
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE

吸取什么教训?如果我们要使用导入的 JSON 模型,任何数据都必须首先转换为浮点数。在这种情况下,由于“20180131”无法表示为 32 位浮点数,它会四舍五入到 20180132,如下所示

fl(20180131)
## # A float32 vector: 1
## [1] 20180132

第二课:JSON 参数是 32 位浮点数

所有存储为浮点数的 JSON 参数都必须转换为浮点数。

现在假设我们关心小数点后两位之后的数字。

# test that values are equal
bst_preds_logodds == bst_from_json_logodds
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE

都不完全相等。发生了什么?尽管我们已经将数据转换为 32 位浮点数,但我们还需要将 JSON 参数转换为 32 位浮点数。让我们这样做

# now convert the dates to floats first
bst_from_json_logodds <- ifelse(fl(data$dates) < fl(node$split_condition),
                                as.numeric(fl(node$children[[1]]$leaf)),
                                as.numeric(fl(node$children[[2]]$leaf)))

# test that values are equal
bst_preds_logodds == bst_from_json_logodds
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE

都相等了。吸取什么教训?如果我们要使用导入的 JSON 模型,任何存储为浮点数的 JSON 参数也必须首先转换为浮点数。

第三课:使用 32 位数学运算

始终使用 32 位数字和运算符

我们能够使对数几率(log-odds)一致,所以现在我们手动计算对数几率的 Sigmoid 函数。这应该与 xgboost 的预测一致。

bst_preds <- predict(bst, as.matrix(data$dates))

# calculate the predictions casting doubles to floats
bst_from_json_preds <- ifelse(
  fl(data$dates) < fl(node$split_condition)
  , as.numeric(1 / (1 + exp(-1 * fl(node$children[[1]]$leaf))))
  , as.numeric(1 / (1 + exp(-1 * fl(node$children[[2]]$leaf))))
)

# test that values are equal
bst_preds == bst_from_json_preds
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE

又都不完全相等了。这是怎么回事?嗯,由于我们在计算中使用了值 1,我们在计算中引入了一个双精度数。因此,所有浮点值都被提升为 64 位双精度数,并且也使用了 64 位版本的指数运算符 exp。另一方面,xgboost 在其Sigmoid 函数中使用了 32 位版本的指数运算符。

如何解决这个问题?我们必须确保在所有地方使用正确的数据类型和正确的运算符。如果只使用浮点数,我们加载的浮点库将确保应用 32 位浮点指数运算符。

# calculate the predictions casting doubles to floats
bst_from_json_preds <- ifelse(
  fl(data$dates) < fl(node$split_condition)
  , as.numeric(fl(1) / (fl(1) + exp(fl(-1) * fl(node$children[[1]]$leaf))))
  , as.numeric(fl(1) / (fl(1) + exp(fl(-1) * fl(node$children[[2]]$leaf))))
)

# test that values are equal
bst_preds == bst_from_json_preds
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE

都相等了。吸取什么教训?如果我们想重现 xgboost 的结果,必须确保所有计算都使用 32 位浮点运算符进行。