Saving/Loading libFM models

1,503 views
Skip to first unread message

kensuke....@gmail.com

unread,
Oct 28, 2015, 5:55:28 AM10/28/15
to libFM - Factorization Machines
Hi,

I've just noticed that libFM now offers a way to save/load models after an update that took place earlier this month.

Are there any documents/instructions on how to use these functions?
Or, could someone guide me through?

I've been waiting for this functionality, and I am very excited.

Thank you

Kensuke

Thierry Silbermann

unread,
Nov 2, 2015, 9:23:30 AM11/2/15
to libFM - Factorization Machines, kensuke....@gmail.com
HI,
Please pull from the last commit on github as they were some changes recently.

To use the save/load flag (available only for sgd and als), you can use it like you were training any model previously and now save it using: -save_model model_name
example:
./libFM -train data_train.libfm -test data_test.libfm -dim '1,1,10' -iter 10 -method 'sgd' -out pred_libfm2 -task r -regular '1,1,1' -learn_rate 0.1 -seed 100 -save_model model1

Then if you want to continue training you can either directly load the model and give the number of extra iteration you want:
./libFM -train data_train.libfm -test data_test.libfm -dim '1,1,10' -method 'sgd' -out pred_libfm2 -task r -regular '1,1,1' -learn_rate 0.1 -seed 100 -iter 20 -load_model model1

or if you just want some prediction, you can ask libfm to do 0 iteration:
./libFM -train data_train.libfm -test data_test.libfm -dim '1,1,10' -method 'sgd' -out pred_libfm2 -task r -regular '1,1,1' -learn_rate 0.1 -seed 100 -iter 0 -load_model model1

Hope it helps.

mensur...@gmail.com

unread,
Jan 19, 2016, 10:32:46 PM1/19/16
to libFM - Factorization Machines, kensuke....@gmail.com
Did anyone get this save/load feature to work? After I train a classification model and save it, everything looks good. I also save the prediction in the same run using the -out command. When I repeat the command exactly the same way other than -iter 0 and -load_model instead of -save_model, I get completely different predictions. First, predictions are now integers rather than real numbers. Second, they are nothing like predictions obtained from the original run in which -save_model was used. This is all with ALS method.

Another thing that might be a bug: the model is saved even with MCMC method, even though it seems from the code that it shouldn't be.

Load/save model feature would be enormously helpful for my work, as the training on my full dataset longer than a day. I can't afford to train again each time I have new data to classify, not to mention that it wouldn't be 100% reproducible. If someone could check why this doesn't work, I'd be very grateful. I am happy to provide the data and full commands used if that helps.

By the way, libFM is very good in classification, at least for my purposes. Not only do I get better accuracy than from boosting (and faster), but the 0-1 estimates are also closer to real values (better ROC curve).

tct...@gmail.com

unread,
Sep 20, 2016, 11:01:52 PM9/20/16
to libFM - Factorization Machines, kensuke....@gmail.com, mensur...@gmail.com

I met the same issue. I was wondering whether the issue is solved?

Thanks.

martij...@gmail.com

unread,
Nov 23, 2016, 9:07:12 AM11/23/16
to libFM - Factorization Machines, kensuke....@gmail.com
Will there be a way to load/save model with the mcmc optimization?

adarsa...@gmail.com

unread,
Dec 7, 2016, 6:05:12 AM12/7/16
to libFM - Factorization Machines, kensuke....@gmail.com, mensur...@gmail.com, tct...@gmail.com
You can do load model using "./LibFM -train......., -save_model 'fm.model'"
To use this model ahead use -load_model `fm.model' with iter 1 in regression/classification task.

raa...@gmail.com

unread,
Mar 20, 2017, 10:31:25 AM3/20/17
to libFM - Factorization Machines, kensuke....@gmail.com, mensur...@gmail.com, tct...@gmail.com, adarsa...@gmail.com
Did anyone managed to save a trained model and predict data with it?

ada...@ilimi.in

unread,
Mar 20, 2017, 1:48:55 PM3/20/17
to libFM - Factorization Machines, kensuke....@gmail.com, mensur...@gmail.com, tct...@gmail.com, adarsa...@gmail.com, raa...@gmail.com
yes, as I have replied earlier, you could use -save_model/-load_model. But as others have mentioned, when using als, if you train on a set then load it and try to predict for the training set, you get values different from .out those generated during training .

oldiro...@gmail.com

unread,
Aug 18, 2017, 4:25:51 AM8/18/17
to libFM - Factorization Machines, kensuke....@gmail.com, mensur...@gmail.com
在 2016年1月20日星期三 UTC+8上午11:32:46,mensur...@gmail.com写道:
Hello, I encountered similar problem with you. I used als method and -load_model at the same time but got a warning:

WARNING: load/save enabled only for SGD and ALS. Nothing will be loaded.

and the results in -out is all zero.

Then I checked the source code and found this in libfm.cpp:

line128-132:
if (! cmdline.getValue(param_method).compare("als")) { // als is an mcmc without sampling and hyperparameter inference
cmdline.setValue(param_method, "mcmc");
if (! cmdline.hasParameter(param_do_sampling)) { cmdline.setValue(param_do_sampling, "0"); }
if (! cmdline.hasParameter(param_do_multilevel)) { cmdline.setValue(param_do_multilevel, "0"); }
}

Now, the param_method turned to "mcmc",

line256-267:
if (cmdline.hasParameter(param_load_model)) {
std::cout << "Reading FM model... \t" << std::endl;
if (!cmdline.getValue(param_method).compare("sgd") || !cmdline.getValue(param_method).compare("als")){ //load/save enabled only for SGD and ALS
if(!fm.loadModel(cmdline.getValue(param_load_model))){
std::cout << "WARNING: malformed model file. Nothing will be loaded." << std::endl;
fm.init();
}
}
else{
std::cout << "WARNING: load/save enabled only for SGD and ALS. Nothing will be loaded." << std::endl;
}
}
Surely, it will print "WARNING: load/save enabled only for SGD and ALS. Nothing will be loaded." and not load model.

So, could you please tell me how did you solve your problem or how did you use als and load_model?
Thank you!

郭梦卓

unread,
Jul 25, 2019, 5:33:05 AM7/25/19
to libFM - Factorization Machines
Hi, I have tried to compile the codes in Github on my PC, however, it did not work. 
I am a new to FM and C++ programing. 
I can successfully use the windows verision but it cannot output the model parameters.
I appreciate it if you could send me the latest windows verision FM that can use '-save_model'.
Many thanks.

在 2015年11月2日星期一 UTC+8下午10:23:30,Thierry Silbermann写道:

laizef...@gmail.com

unread,
Oct 20, 2019, 10:12:23 AM10/20/19
to libFM - Factorization Machines
Hello, I came across the same issue, did you address it? 

在 2019年7月25日星期四 UTC+8下午5:33:05,郭梦卓写道:
Reply all
Reply to author
Forward
0 new messages