Search code examples
rr-caretcatboost

Save and Load a catboost model with Caret in R


I am able to train a Catboost model with caret (in Rstudio) and it works great.

my_catboost <- caret::train(x, y, 

              method=catboost.caret, 
              trControl=fitControl, 
              tuneGrid = param,
              metric = "ROC")

If I use the model to predict on new data in the same session, no issue, it works:

output <- caret::predict.train(my_catboost, newdata=x_testing, type="prob")

However, If I save the model and load it later (or save it, delete "my_catboost" and load), the function predict will crash R and Rstudio without error message and can't find anything in Rstudio log. After the load, I can see the model being created in the global Environment and it seems fine.

I tried the R function save and load, saveRDS and readRDS and both crashed

Thanks !


Solution

  • You have misunderstood my comment. Here is an answer using the inbuilt data set Sonar:

    library(caret)
    library(catboost)
    library(mlbench)
    data(Sonar)
    

    create train and test data sets:

    set.seed(1)
    
    tr <- createDataPartition(Sonar$Class, p = 0.7, list = FALSE)
    
    trainer <- Sonar[tr,]
    tester <- Sonar[-tr,]
    

    train models:

    fitControl <- trainControl(method = "cv",
                               number = 3,
                               savePredictions = TRUE,
                               summaryFunction = twoClassSummary,
                               classProbs = TRUE)
    
    model <- train(x = trainer[,1:60],
                   y = trainer$Class,
                   method = catboost.caret, 
                   trControl = fitControl, 
                   tuneLength = 5,
                   metric = "ROC")
    

    predict using caret:

    preds1 <- predict(model, tester, type = "prob")
    

    save the final model:

    catboost::catboost.save_model(model$finalModel, "model")
    

    load the saved model:

    model2 <- catboost::catboost.load_model("model")
    

    predict using the saved model:

    preds2 <- catboost.predict(model2,
                               catboost.load_pool(tester),
                               prediction_type = "Probability")
    

    check equality of predictions

    all.equal(preds1[,2], preds2)
    

    EDIT: while:

    saveRDS(model, "caret.model.rds")
    model3 <- readRDS("caret.model.rds")
    preds3 <- predict(model3, tester, type = "prob")
    

    results in R session crash

    R version 3.5.0 (2018-04-23)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows >= 8 x64 (build 9200)
    
    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] mlbench_2.1-1        catboost_0.10.3      caret_6.0-80         ggplot2_2.2.1        lattice_0.20-35      RevoUtils_11.0.0    
    [7] RevoUtilsMath_11.0.0
    
    loaded via a namespace (and not attached):
     [1] httr_1.3.1         magic_1.5-8        ddalpha_1.3.3      tidyr_0.8.1        sfsmisc_1.1-2      jsonlite_1.5      
     [7] viridisLite_0.3.0  splines_3.5.0      foreach_1.5.0      prodlim_2018.04.18 assertthat_0.2.0   stats4_3.5.0      
    [13] DRR_0.0.3          yaml_2.1.19        robustbase_0.93-0  ipred_0.9-6        pillar_1.2.3       glue_1.2.0        
    [19] digest_0.6.15      colorspace_1.3-2   recipes_0.1.2      htmltools_0.3.6    Matrix_1.2-14      plyr_1.8.4        
    [25] psych_1.8.4        timeDate_3043.102  pkgconfig_2.0.1    CVST_0.2-2         broom_0.4.4        purrr_0.2.4       
    [31] scales_0.5.0       gower_0.1.2        lava_1.6.1         tibble_1.4.2       withr_2.1.2        nnet_7.3-12       
    [37] lazyeval_0.2.1     mnormt_1.5-5       survival_2.41-3    magrittr_1.5       nlme_3.1-137       MASS_7.3-49       
    [43] dimRed_0.1.0       foreign_0.8-70     class_7.3-14       tools_3.5.0        data.table_1.11.4  stringr_1.3.1     
    [49] plotly_4.7.1       kernlab_0.9-26     munsell_0.4.3      bindrcpp_0.2.2     compiler_3.5.0     RcppRoll_0.2.2    
    [55] rlang_0.2.0        grid_3.5.0         iterators_1.0.10   htmlwidgets_1.2    geometry_0.3-6     gtable_0.2.0      
    [61] ModelMetrics_1.1.0 codetools_0.2-15   abind_1.4-5        reshape2_1.4.3     R6_2.2.2           lubridate_1.7.4   
    [67] dplyr_0.7.5        bindr_0.1.1        stringi_1.1.7      parallel_3.5.0     Rcpp_0.12.17       rpart_4.1-13      
    [73] DEoptimR_1.0-8     tidyselect_0.2.4