How to save the output from rxNeuralNet (an mlModel) for reuse?

257 Views Asked by At

I used rxNeuralNet and got a great result. I'd like to save it for future use.

nnOutput <- rxNeuralNet(formula = savedFormula, data = inputData, 
                        type = "regression", acceleration = "sse")

I can use the nnOutput with rxPredict, and validate my answers.

rxPredict(nnOutput, data = testSet, outData = tempXDF)

Now I want to save the output, so I can reuse it later. (Training time wasn't insignificant.) I can't seem to find any examples on doing so. I've tried:

rxDataStep(inData = nnOutput, outFile = tempXDF, overwrite = TRUE)
rxImport(inData = nnOutput, outFile = tempXDF, overwrite = TRUE)
write.csv(nnOutput, file = "c:\\temp\\temp.csv")

Any suggestions?

2

There are 2 best solutions below

1
On BEST ANSWER

If you have SQL Server, you can persist the mode as binary to SQL, by using rxSerializeModel and/or rxWriteObject.

You can also use the "bog-standard" R serialize function, or save, or saveRDS.

3
On

A rxNeuralNet model object is just a regular R object. You can use the usual R methods for handling it.

For example, assuming your data is a data frame:

glmMod <- glm(y ~ x1 + x2 + x3, data=dat)
nnMod <- rxNeuralNet(y ~ x1 + x2 + x3, data=dat,
                     type="regression", accel="sse")
save(glmMod, nnMod, file="models.rdata")

The next time you start R:

load("models.rdata")
glmPred <- predict(glmMod, newdat)
nnPred <- rxPredict(nnMod, newdat, outData=NULL)