Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Add patience to --auto-stop #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ffm-train.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ string train_help() {
"-l <lambda>: set regularization parameter (default 0.00002)\n"
"-k <factor>: set number of latent factors (default 4)\n"
"-t <iteration>: set number of iterations (default 15)\n"
"-b <patience>: set number of iterations for patience (default 1)\n"
"-r <eta>: set learning rate (default 0.2)\n"
"-s <nr_threads>: set number of threads (default 1)\n"
"-p <path>: set path to the validation set\n"
Expand Down Expand Up @@ -70,6 +71,14 @@ Option parse_option(int argc, char **argv) {
opt.param.nr_iters = atoi(args[i].c_str());
if(opt.param.nr_iters <= 0)
throw invalid_argument("number of iterations should be greater than zero");
} else if(args[i].compare("-b") == 0)
{
if(i == argc-1)
throw invalid_argument("need to specify number of iterations after -b");
i++;
opt.param.nr_patience_iters = atoi(args[i].c_str());
if(opt.param.nr_patience_iters <= 0)
throw invalid_argument("number of iterations for patience should be greater than zero");
} else if(args[i].compare("-k") == 0) {
if(i == argc-1)
throw invalid_argument("need to specify number of factors after -k");
Expand Down
11 changes: 8 additions & 3 deletions ffm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ ffm_model ffm_train_on_disk(string tr_path, string va_path, ffm_parameter param)
ffm_model model = init_model(tr.meta.n, tr.meta.m, param);

bool auto_stop = param.auto_stop && !va_path.empty();
ffm_int patience = 0;

ffm_long w_size = get_w_size(model);
vector<ffm_float> prev_W(w_size, 0);
Expand Down Expand Up @@ -623,10 +624,14 @@ ffm_model ffm_train_on_disk(string tr_path, string va_path, ffm_parameter param)

if(auto_stop) {
if(va_loss > best_va_loss) {
memcpy(model.W, prev_W.data(), w_size*sizeof(ffm_float));
cout << endl << "Auto-stop. Use model at " << iter-1 << "th iteration." << endl;
break;
patience++;
if (patience == param.nr_patience_iters) {
memcpy(model.W, prev_W.data(), w_size*sizeof(ffm_float));
cout << endl << "Auto-stop. Use model at " << (iter - patience) << "th iteration." << endl;
break;
}
} else {
patience = 0;
memcpy(prev_W.data(), model.W, w_size*sizeof(ffm_float));
best_va_loss = va_loss;
}
Expand Down
1 change: 1 addition & 0 deletions ffm.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct ffm_parameter {
ffm_int k = 4; // number of latent factors
bool normalization = true;
bool auto_stop = false;
ffm_int nr_patience_iters = 1;
};

void ffm_read_problem_to_disk(string txt_path, string bin_path);
Expand Down