如何将 R 模型转换为原始二进制或字符串缓冲区?

How do you convert R models to raw binary or string buffer?

我在第三方环境 (IBM Watson Studio) 中使用 R 3.6 工作,需要能够将模型对象转换为原始二进制或字符串缓冲区,以便按指示保存模型 here. Briefly, I have to use a function that makes an API request that sends over the raw binary/string buffer data in order to save files. Similar to ,我选择使用 jsonlite::serializeJSON() 将模型转换为 JSON(然后转换为原始模型)。它似乎有效,但与 post 中的人相似,我 运行 陷入了 serializeJSON() 无法适当地转换模型对象中的复杂列表的问题。当我尝试预测时,它遇到了“找不到函数“列表””的错误。由于我有一个 GLM,即使在实施了建议的修复之后,我现在也会遇到错误“找不到对象 'C_logit_linkinv'”。显然,这种方法不能很好地推广到不同的模型。

我 post 提出一个新问题,因为 none 的解决方案适用于我的情况,而且我想看看是否有更可靠的模型转换方式对象到原始字节或字符串缓冲区。我没有 post 参加 IBM 董事会,因为我觉得这是一个关于模型对象和转换的更普遍的问题,但如果社区认为这更合适,我会这样做。这是一个可重现的例子:

require(jsonlite)

# Generate some data
data <- data.frame(x1=rnorm(100),
                   y=rbinom(100,1,.6))

# Fit and convert model->JSON->Raw
fitted_model <- glm(y ~ 0 + ., 
                   data = data, 
                   family = binomial)
model_as_json <- jsonlite::serializeJSON(fitted_model)
model_as_raw <- charToRaw(model_as_json)

# Convert back to model
back_to_json <- rawToChar(model_as_raw)
back_to_model <- jsonlite::unserializeJSON(back_to_json)

# Score
scoring_data <- data.frame(x1=rnorm(5))
predict(object=back_to_model,
        newdata = scoring_data,
        type='response')

规格:

您可以使用 serialize() 并将 connection 参数设置为 NULL,其中 returns 一个原始向量,unserialize() 用于恢复。

重要的是,根据文档:

Sharing of reference objects is preserved within the object

set.seed(9)
data <- data.frame(x1=rnorm(100),
                   y=rbinom(100,1,.6))

# Fit and convert model -> Raw
fitted_model <- glm(y ~ 0 + ., 
                    data = data, 
                    family = binomial)

model_as_raw <- serialize(fitted_model, connection = NULL)

# Convert back to model
back_to_model <- unserialize(model_as_raw)

# Check that predict works
scoring_data <- data.frame(x1=rnorm(5))

predict(object=back_to_model,
        newdata = scoring_data,
        type='response')

        1         2         3         4         5 
0.4908404 0.4871576 0.4955416 0.4978725 0.5065067