How to save a model when using MXnet - r

I am using MXnet for training a CNN (in R) and I can train the model without any error with the following code:
model <- mx.model.FeedForward.create(symbol=network,
X=train.iter,
ctx=mx.gpu(0),
num.round=20,
array.batch.size=batch.size,
learning.rate=0.1,
momentum=0.1,
eval.metric=mx.metric.accuracy,
wd=0.001,
batch.end.callback=mx.callback.log.speedometer(batch.size, frequency = 100)
)
But as this process is time-consuming, I run it on a server during the night and I want to save the model for the purpose of using it after finishing the training.
I used:
save(list = ls(), file="mymodel.RData")
and
mx.model.save("mymodel", 10)
But none of them can save the model! for example when I load the "mymodel.RData", I can not predict the labels for the test set!
Another example is when I load the "mymodel.RData" and try to plot it with the following code:
graph.viz(model$symbol$as.json())
I get the following error:
Error in model$symbol$as.json() : external pointer is not valid
Can anybody give me a solution for saving and then loading this model for future use?
Thanks

You can save the model by
model <- mx.model.FeedForward.create(symbol=network,
X=train.iter,
ctx=mx.gpu(0),
num.round=20,
array.batch.size=batch.size,
learning.rate=0.1,
momentum=0.1,
eval.metric=mx.metric.accuracy,
wd=0.001,
epoch.end.callback=mx.callback.save.checkpoint("model_prefix")
batch.end.callback=mx.callback.log.speedometer(batch.size, frequency = 100)
)

A mxnet model is an R list, but its first component is not an R object but a C++ pointer and can't be saved and reloaded as an R object. Therefore, the model needs to be serialized to behave as an actual R object. The serialized object is also a list, but its first object is a text string containing model information.
To save a model:
modelR <- mx.serialize(model)
save(modelR, file="~/model1.RData")
To retrieve it and use it again:
load("~/model1.RData", verbose=TRUE)
model <- mx.unserialize(modelR)

The best practice for saving a snapshot of your training progress is to use save_snapshot (http://mxnet.io/api/python/module.html#mxnet.module.Module.save_checkpoint) as part of the callback after every epoch training. In R the equivalent command is probably mx.callback.save.checkpoint, but I'm not using R and not sure about it usage.
Using these snapshots can also allow you to take advantage of the low cost option of using AWS Spot market (https://aws.amazon.com/ec2/spot/pricing/ ), which for example now offers and instance with 16 K80 GPUs for $3.8/hour compare to the on-demand price of $14.4. Such 80%-90% discount is common in the spot market and can optimize the speed and cost of your training, as long as you use these snapshots correctly.

Related

How to save a ranger model in mlr3 without data?

I have created a ranger model using mlr3 library. I saved this model to my machine using following command. The created file is huge in size. The saved file also has the data along with the model. Is there a way to only save the model without the data?
learner_ranger = lrn("classif.ranger", predict_type = "prob", predict_sets = c("train", "test"), importance = "impurity", num.threads = 8)
learner_ranger$train(train_task])
save(learner_ranger, file = "model.rda")
When I try to load this saved model it does not load the model correctly.
learner_ranger = load("model.rda")
str(learner_ranger)
Error in str(learner_ranger) : object 'learner_ranger' not found
To reduce the file size and save only the model, I have tried following but I am getting an error
save(learner_ranger$model, file = "model.rda")
The error I am getting is -
Error in save(learner_ranger$model, file = "model.rda") :
object ‘learner_randomF$model’ not found
After some research found that there are two ways to save and load the model in R:
using save(), load(): When we use save(), we will have to load it using the same name.
using saveRDS(), loadRDS(): saveRDS() does not save the model name and we have the flexibility to load the model in any other name. But saveRDS() can only save one object at a time as it is a lower-level function.
Most people prefer saveRDS() over save() as it serializes the object.
I am still looking for ways to save the model without data.

Saving and Loading an mlr Model

I've been able to successfully generate a model to assist in my multilabel assignment using the mlr library using the following setup,
scene.task = makeMultilabelTask(data = training.data, target = labels)
lrn.br = makeLearner("classif.rpart", predict.type = "prob")
lrn.br = makeMultilabelNestedStackingWrapper(lrn.br)
mdl.lrn <- train(lrn.br, scene.task)
I went about saving my model using the following line,
save(mdl.lrn, file = "mdl.rda")
The model that is saved is just over 1GB in size.
I have attempted to load that saved model using the following line,
load("mdl.rda")
However, I continually run out of memory space as I receive messaging that reads, Error: cannot allocate vector of size ### Kb. I believe that my current amount of memory would allow for the loading of the model as it was enough to generate the model.
Has anyone been able to save and load an mlr generated model for reuse? If so, would you be able to recommend or confirm the approach of saving and loading a model?

Save the Deep learning model by R language

I have established a deep learning model with the h2o package of the R software. I gained a model with good presence and I wanna to save it. However, I tried all kinds of methods but failed. The code "save()" and "save.image()" are provided in the base package of R software. I used the "save()" function to conserve my model. But when I want to use the built model to run new data, it is said that the "model" object is not found in the function. I am really confused about this problem for a few days. If you have any good ideas, just tell me. Thanks for your reading~
load("F:/R/Rstudy/myfile") ##download the saved file
library(h2o)
h2o.init()
Te <- read.csv("F:/Rdata/Test.csv") ## import testing data
Te <- as.h2o(Te)
Te[,2] <- as.factor(Te[,2])
perf <- h2o.performance(model, Te) ## test model
ERROR: Unexpected HTTP Status code: 404 Not Found (url = http://localhost:54321/3/ModelMetrics/models/DeepLearning_model_R_1533035975237_1/frames/RTMP_sid_8185_2)
ERROR MESSAGE:
Object 'DeepLearning_model_R_1533035975237_1' not found in function: predict for argument: model
You can use the below to save and retrieve the model.
build the model
model <- h2o.deeplearning(params)
save the model
model_path <- h2o.saveModel(object=model, path=getwd(), force=TRUE)
print(model_path)
/tmp/mymodel/DeepLearning_model_R_1441838096933
load the model
saved_model <- h2o.loadModel(model_path)
Reference - http://docs.h2o.ai/h2o/latest-stable/h2o-docs/save-and-load-model.html
Hope this helps,
ND

Where did the forecast.Holtwinters go in R 3.4.3?

I'm using R Studio based on R 3.4.3. However, when I tried to call the forecast.HoltWinters function, R told me that "could not find function "forecast.HoltWinters"". Inspect the installed package (v8.2) told me that it's true, there is no forecast.HoltWinters. But the manual in https://cran.r-project.org/web/packages/forecast/ clearly stated that forecast.HoltWinters is still available.
I have also tried stats::HoldWinters, but it's working wrong. The code run fine on another computer, but it couldn't run at all on mine. Is there any solution?
Here is the code. Book2.csv has enough data to last more than 3 periods.
dltt <- read.csv("book2.csv", header = TRUE)
dltt.ts <- ts(dltt$Total, frequency=12, start=c(2014,4))
dltt.ts.hw <- HoltWinters(dltt.ts)
library(forecast)
dltt.ts.hw.fc <- forecast.HoltWinters(dltt.ts.hw) //Error as soon as I run this line
Fit a HoltWinters model using the HoltWinters function and then use forecast. Its all in the help for HoltWinters and forecast, namely "The function invokes particular _methods_ which depend on the class of the first argument". I'll copy the guts of it here:
m <- HoltWinters(co2)
forecast(m)
Note this will call the non-exported forecast.HoltWinters function, which you should never call directly using triple-colon notation as some may suggest.

Dirichlet-Categorical conjugate prior model using OpenBUGS,R and the package R2OpenBUGS

At first, let's create some sample categorical data with 3 levels.
y<-sample(c("A","B","C"),50,replace=TRUE)
I'm trying to formulate a Bayesian statistical model in which the y variable follows categorical distribution with parameters theta1,theta2,theta3. These parameters describe the probability a single y[i] belongs to the corresponding category. In the bayesian perspective, these parameters are also random variables and we use to assign a dirichlet prior to them with hyper-parameters alpha1,alpha2,alpha3.
I'm having some problems with the syntax as it seems.
CODE
model<-function(){
#likelihood
for( i in 1:N){
y[i]~ dcat(theta[])
}
#prior
theta[1:3]~ ddirch(alpha[])
}
library(R2OpenBUGS)
model.file <- file.path(tempdir(),"model.txt")
write.model(model, model.file)
y<-sample(c("A","B","C"),50,replace=TRUE)
N<-50
alpha<-c(1,1,1)
data<-list('y','N','alpha')
params<-c('theta')
inits<-function(){theta=c(1/3,1/3,1/3)}
We call OpenBUGS through R, with the bugs function
out<-bugs(data,inits,params,model.file,n.chains = 2
,n.iter=6000,codaPkg = TRUE,n.burnin = 1000,DIC = TRUE)
I've tried different ways to syntactically formulate the above code, dribbling through the errors and getting familiar with the log.txt file (that is the file that keeps the OpenBUGS output) until this code gave me a log.txt with no errors while R still has problems.
R output
Error in bugs.run(n.burnin, OpenBUGS.pgm, debug = debug, WINE = WINE, :
Look at the log file in /tmp/Rtmpofdk0t and
try again with 'debug=TRUE' to figure out what went wrong within OpenBUGS.
In addition: Warning message:
In FUN(X[[i]], ...) : class of 'x' was discarded
log.txt
OpenBUGS version 3.2.3 rev 1012
model is syntactically correct
data loaded
model compiled
initial values generated, model initialized
1000 updates took 0 s
monitor set
monitor set
monitor set
monitor set
deviance set
Thanks in advance for your help
I think you should rename theta1, theta2, theta3 with alpha1, alpha2, alpha3, because you use the alpha1,... in the function ddirch, but you never declare them. Instead you declare theta1 and so on, but never use them.
If there are any other issues, you might have a look at the log file, like the compiler suggests.
After numerous experiments, i figured out that for some reason OpenBUGS cant accept factor variables given as usual. So i changed my data ( format "A","B","C") to numeric (format 1,2,3) with the as.numeric R function and everything ran smoothly!

Resources