question

ImranDesai-4866 avatar image
0 Votes"
ImranDesai-4866 asked KalyanChanumolu-MSFT edited

is there any way I can serialize the R model which trained using caret or some other libraries instead of RevoScaleR?

I am using SQL Server Machine Learning services to Run a R model. I am able to serialize it using RevoScaleR and rxSerializeModel but I want to use caret instead of RevoScaleR here is my code which is giving an error when asks to serialiaze the trained model

Step1 train model using SP

DROP PROCEDURE IF EXISTS generate_PCL_R_native_model;
go
CREATE PROCEDURE generate_PCL_R_native_model (@model_type varchar(30), @trained_model varbinary(max) OUTPUT)
AS
BEGIN

 EXECUTE sp_execute_external_script
   @language = N'R'                                  -- Spesify langauge and R code 

 , @script = N'                                      

require("RevoScaleR")
require("caret")
require("ranger")
library(caret)
library(ranger)

fitControl <- trainControl(method = "cv",
number = 5,

                        savePredictions = TRUE
                       ,
                           
                        classProbs = T, 
                       verboseIter = F
                        )

                        rf_grid <- expand.grid(mtry = 2,
                    splitrule = c("gini", "extratrees"),
                    min.node.size = c(1, 3, 5));

if(model_type == "dtree") {
model_linmod <-
train(pclitemspct10r_new ~

 + d1
 + d2
 + d3
 + d4
 + d5
 + d6
 + d7
 + e1
 + e2
 + e3
 + e4
 + e5
 + e6
 + marriedd1
 + marriedd2

    
    
    
 , data = PCL_train_data, 
             method = "ranger",
             trControl = fitControl,
             #tuneGrid = rf_grid
             )
    


 #serialize the model
    
 trained_model <- as.raw(serialize((model_linmod, NULL));
 }

'

 , @input_data_1 = N'select  * from [dbo].[training_IOP_data_new]'
 , @input_data_1_name = N'PCL_train_data'; 

STEP 2 - Setup model table for storing the model

DROP TABLE IF EXISTS PCL_models;
GO
CREATE TABLE PCL_models (
model_name VARCHAR(30) NOT NULL DEFAULT('default model'),
lang VARCHAR(30),
model VARBINARY(MAX),
native_model VARBINARY(MAX),
PRIMARY KEY (model_name, lang)

);
GO
Step 3 Save the model in table format

DECLARE @model2 VARBINARY(MAX);
EXEC generate_PCL_R_native_model "dtree", @model2 OUTPUT;
INSERT INTO PCL_models (model_name, native_model, lang) VALUES('dtree_model', @model2, 'R');

is there any way I can serialize the R model which trained using caret or some other libraries instead of RevoScaleR.

sql-server-generalazure-machine-learning
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Great question - I am also looking for this information

0 Votes 0 ·

0 Answers