从 JSON 加载 XGBoost

简介

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

  • 输入数据,应将其转换为 32 位浮点数

  • 以十进制表示形式存储在 JSON 中的任何 32 位浮点数

  • 任何计算都必须使用 32 位数学运算符完成

设置

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

require(xgboost)
## Loading required package: xgboost
require(jsonlite)
## Loading required package: jsonlite
require(float)
## 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 位浮点数

经验 1:所有数据都是 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

经验 2: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 参数也必须首先转换为浮点数。

经验 3:使用 32 位数学

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

我们成功地使 log-odds 一致了,所以现在让我们手动计算 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,我们在计算中引入了一个 double。因此,所有浮点值都被提升为 64 位双精度浮点数,并且还使用了 64 位版本的幂运算运算符 exp。另一方面,xgboost 在其 Sigmoid 函数中使用了 32 位版本的幂运算运算符。

我们如何解决这个问题?我们必须确保在所有地方都使用正确的数据类型和正确的运算符。如果我们只使用浮点数,我们已加载的 float 库将确保应用 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 位浮点运算符完成。