Error when unserializing an xgboost model

Hey all,

I'm trying to save and deploy an xgboost model to sql server using the

unserialize(as.raw(paste(serialize(model_xgb, NULL), collapse = "")))

methodology found here: https://longhowlam.wordpress.com/2016/12/23/did-you-say-sql-server-yes-i-did/

Before moving to SQL, I'm trying the process entirely in R first, just to make sure it works and I'm getting the follow error and warning messages:

Error in unserialize(as.raw(xgb_str)) : read error
In addition: Warning messages:
1: In unserialize(as.raw(xgb_str)) : NAs introduced by coercion
2: In unserialize(as.raw(xgb_str)) :
  out-of-range values treated as 0 in coercion to raw

Here's a reprex:

require(data.table)
require(mltools)
require(xgboost)
require(dplyr)


mat_cars <- mtcars %>% 
  select(., mpg, disp, hp, wt, cyl) %>% 
  mutate_at(., vars(cyl), as.factor) %>% 
  as.data.table() %>% 
  one_hot() %>% 
  data.matrix()

model_xgb <- xgboost(data = mat_cars[, -1], 
                     label = mat_cars[, 1],
                     nrounds = 10,
                     verbose = 0)

xgb_str <- paste(serialize(model_xgb, NULL), collapse = "")
model_xgb2 <- unserialize(as.raw(xgb_str))

Also, here's my session info:

R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 2012 R2 x64 (build 9600)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] bindrcpp_0.2.2       dplyr_0.7.8          xgboost_0.71.2       mltools_0.3.5        data.table_1.12.0    RevoUtilsMath_11.0.0
 [7] RevoUtils_11.0.2     RevoMods_11.0.1      MicrosoftML_9.4.7    mrsdeploy_1.1.3      RevoScaleR_9.4.7     lattice_0.20-38     
[13] rpart_4.1-13        

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0             rstudioapi_0.9.0       CompatibilityAPI_1.1.0 bindr_0.1.1            magrittr_1.5           tidyselect_0.2.5      
 [7] R6_2.3.0               rlang_0.3.1            foreach_1.4.4          tools_3.5.2            grid_3.5.2             iterators_1.0.11      
[13] assertthat_0.2.0       tibble_2.0.1           crayon_1.3.4           Matrix_1.2-15          purrr_0.3.0            codetools_0.2-15      
[19] curl_3.3               glue_1.3.0             stringi_1.2.4          pillar_1.3.1           compiler_3.5.2         jsonlite_1.6          
[25] pkgconfig_2.0.2 

Note that I'm also getting the same error when I try on my local machine that has R 3.6.1 and xgboost 0.9. Also I've tried replacing serialize with xgb.save.raw and gotten the exact same error.

Any help would be appreciated!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.