I trained a model earlier in Flux.jl and saved it by doing:
#save "mymodel.bson" model
Now I want to load that model back and use it again. How can I achieve this in Flux?
Similar to the #save macro used above, there is also a built in #load macro which comes from the BSON package. You can access it by doing using BSON: #load and then quite simply do something like:
julia> using Flux
julia> using BSON: #load
julia> #load "mymodel.bson" model
julia> model
Chain(Dense(10, 5, NNlib.relu), Dense(5, 2), NNlib.softmax)
You can find out more about saving and loading models in the Flux.jl docs.
Related
I have a model which I have trained and I would like to save it for future use and distributing to others. What is the best way to save a trained model with Flux.jl?
If your model does not have things like dynamically created/sized layers, you should be able to save just the weights instead of serializing the whole model. This can be much more robust than using BSON.jl or the Serialization stdlib to serialize the whole model (both of which can be very fragile).
The weights can be obtained from a model by weights=collect(params(cpu(model))) and loaded back into the model by Flux.loadparams!(model, weights). Thus, one just needs to save a Vector of numeric arrays to disk, instead of more complicated Julia-side objects in the model. So I would suggest a pattern like:
function make_model(config)
...define layers, put them in a chain, etc...
return model
end
# train model
...
# collect weights
weights=collect(params(cpu(model)))
# save them to disk somehow...
Then when it's time to reload the model,
weights = # load them from disk
fresh_model = make_model(config)
Flux.loadparams!(model, weights)
Note that this approach means you can't e.g. add a layer to make_model and reload old weights; they will no longer be the right size. So you need to version your code and your weights and ensure they match up.
Last week I helped make a new package LegolasFlux.jl to make this pattern easier (in particular, providing a way to use Arrow to save the weights to disk along with any other configuration parameters, losses, etc, you would like to save). It should be registered in two days.
According to the Flux.jl docs (https://fluxml.ai/Flux.jl/stable/saving/) the best way to save a trained model is using BSON.jl by doing the following:
julia> using Flux
julia> model = Chain(Dense(10,5,relu),Dense(5,2),softmax)
Chain(Dense(10, 5, NNlib.relu), Dense(5, 2), NNlib.softmax)
julia> using BSON: #save
julia> #save "mymodel.bson" model
and then you can load the saved model by doing:
julia> using Flux
julia> using BSON: #load
julia> #load "mymodel.bson" model
julia> model
Chain(Dense(10, 5, NNlib.relu), Dense(5, 2), NNlib.softmax)
I'm trying to run a boosted robust regression on Caret (with the Huber family), however I get an error when training the model:
library(caret)
X <- rnorm(300, 0, 100)
Y <- rnorm(300, 0, 100000)
data <- cbind(X,Y)
model <- train(Y~X, method="glmboost", data=data, family=Huber())
I get the error 'could not find function Huber()', however this is explicitly included in the mboost package (the one on which glmboost is based).
Any help would be really appreciated.
If you Just run library(caret) with method="glmboost" it will load the mboost package, but it will not attach the mboost package to your search path. Packages are discouraged from automatically attaching other packages since they may import functions that could conflict with other functions you have loaded. Thus most packages load dependencies privately. If you fully qualify the function name with the package name, then you can use it in your model
model <- train(Y~X, method="glmboost", data=data, family=mboost::Huber())
Or you could just also run library(mboost) to attach the package to your search path so you don't have to include the package name prefix.
I created a model based on a very large dataset and had the program save the results using
saveRDS(featVarLogReg.mod, file="featVarLogReg.mod.RDS")
Now I'm trying to load the model to evaluate, but readRDS runs out of memory.
featVarLR.mod <- readRDS(file = "featVarLogReg.mod.RDS")
Is there a way to load the file that takes less memory? Or at least the same amount of memory that was used to save it?
The RDS file ended up being 1.5GB in size for this logistic regression using caret. My other models using the same dataset and very similar caret models were 50MB in size so I can load them.
The caret linear model saves the training data in the model object. You could try to use returnData = FALSE in the trainControl argument to train. I don't recall if this fixed my issue in the past.
https://www.rdocumentation.org/packages/caret/versions/6.0-77/topics/trainControl
You could also try to just export the coefficients into a dataframe and use a manual formula to score new data.
Use coef(model_object)
While trying to export an R classifier to PMML, using the pmml package, I noticed that the class distribution for a node in the tree is not exported.
PMML supports this with the ScoreDistribution element: http://www.dmg.org/v1-1/treemodel.html
Is there anyway to have this information in the PMML? I want to read the PMML with another tool that depends on this information.
I'm doing something like:
library(randomForest)
library(pmml)
iris.rf <- randomForest(Species ~ ., data=iris, importance=TRUE,proximity=TRUE)
pmml(iris.rf)
Can you provide some more information..such as, which function you are trying to use.
For example, if you are using the randomForest package, I believe it doesn't provide information about the score distribution; so neither can the PMML representation. However, if you are using the default values, the parameter 'nodesize' for classification ceses, for example, equals 1 and that means the terminal node will have a ScoreDistribution such as:
ScoreDistribution value=predictedValue probability="1.0"/>
ScoreDistribution value=AnyOtherTargetCategoty probability="0.0"/>
If you are using the rpart tree model, the pmml function does output the score distribution information. Perhaps you can give us the exact commands you used?
I've been using randomForest.getTree to give me a text representation of each tree in my forest but have recently switched to the caret package to train the forests (method='rf'). How can I either get an object that randomForest.getTree understands (since caret is allegedly using the same underlying code) or print out the trees in some other analogous way?
just figured it out:
library(caret)
.... #load data
model <- train(x,y,method='rf')
getTree(model$finalModel)