Algorithm-LibLinear
view release on metacpan or search on metacpan
src/liblinear/linear.cpp view on Meta::CPAN
int end = fold_start[i+1];
param_tmp->init_sol = prev_w[i];
struct model *submodel = train(&subprob[i],param_tmp);
int total_w_size;
if(submodel->nr_class == 2)
total_w_size = subprob[i].n;
else
total_w_size = subprob[i].n * submodel->nr_class;
if(prev_w[i] == NULL)
{
prev_w[i] = Malloc(double, total_w_size);
for(j=0; j<total_w_size; j++)
prev_w[i][j] = submodel->w[j];
}
else if(num_unchanged_w >= 0)
{
double norm_w_diff = 0;
for(j=0; j<total_w_size; j++)
{
norm_w_diff += (submodel->w[j] - prev_w[i][j])*(submodel->w[j] - prev_w[i][j]);
prev_w[i][j] = submodel->w[j];
}
norm_w_diff = sqrt(norm_w_diff);
if(norm_w_diff > 1e-15)
num_unchanged_w = -1;
}
else
{
for(j=0; j<total_w_size; j++)
prev_w[i][j] = submodel->w[j];
}
for(j=begin; j<end; j++)
target[perm[j]] = predict(submodel,prob->x[perm[j]]);
free_and_destroy_model(&submodel);
}
set_print_string_function(default_print_string);
if(param_tmp->solver_type == L2R_LR || param_tmp->solver_type == L2R_L2LOSS_SVC)
{
int total_correct = 0;
for(i=0; i<prob->l; i++)
if(target[i] == prob->y[i])
++total_correct;
double current_rate = (double)total_correct/prob->l;
if(current_rate > *best_score)
{
*best_C = param_tmp->C;
*best_score = current_rate;
}
info("log2c=%7.2f\trate=%g\n",log(param_tmp->C)/log(2.0),100.0*current_rate);
}
else if(param_tmp->solver_type == L2R_L2LOSS_SVR)
{
double total_error = 0.0;
for(i=0; i<prob->l; i++)
{
double y = prob->y[i];
double v = target[i];
total_error += (v-y)*(v-y);
}
double current_error = total_error/prob->l;
if(current_error < *best_score)
{
*best_C = param_tmp->C;
*best_score = current_error;
}
info("log2c=%7.2f\tp=%7.2f\tMean squared error=%g\n",log(param_tmp->C)/log(2.0),param_tmp->p,current_error);
}
num_unchanged_w++;
if(num_unchanged_w == 5)
break;
param_tmp->C = param_tmp->C*ratio;
}
if(param_tmp->C > max_C)
info("WARNING: maximum C reached.\n");
free(target);
for(i=0; i<nr_fold; i++)
free(prev_w[i]);
free(prev_w);
}
//
// Interface functions
//
model* train(const problem *prob, const parameter *param)
{
int i,j;
int l = prob->l;
int n = prob->n;
int w_size = prob->n;
model *model_ = Malloc(model,1);
if(prob->bias>=0)
model_->nr_feature=n-1;
else
model_->nr_feature=n;
model_->param = *param;
model_->bias = prob->bias;
if(check_regression_model(model_))
{
model_->w = Malloc(double, w_size);
if(param->init_sol != NULL)
for(i=0;i<w_size;i++)
model_->w[i] = param->init_sol[i];
else
for(i=0;i<w_size;i++)
model_->w[i] = 0;
model_->nr_class = 2;
model_->label = NULL;
train_one(prob, param, model_->w, 0, 0);
}
else if(check_oneclass_model(model_))
{
model_->w = Malloc(double, w_size);
model_->nr_class = 2;
model_->label = NULL;
solve_oneclass_svm(prob, param, model_->w, &(model_->rho));
}
else
{
int nr_class;
src/liblinear/linear.cpp view on Meta::CPAN
};
int save_model(const char *model_file_name, const struct model *model_)
{
int i;
int nr_feature=model_->nr_feature;
int n;
const parameter& param = model_->param;
if(model_->bias>=0)
n=nr_feature+1;
else
n=nr_feature;
int w_size = n;
FILE *fp = fopen(model_file_name,"w");
if(fp==NULL) return -1;
char *old_locale = setlocale(LC_ALL, NULL);
if (old_locale)
{
old_locale = strdup(old_locale);
}
setlocale(LC_ALL, "C");
int nr_w;
if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS)
nr_w=1;
else
nr_w=model_->nr_class;
fprintf(fp, "solver_type %s\n", solver_type_table[param.solver_type]);
fprintf(fp, "nr_class %d\n", model_->nr_class);
if(model_->label)
{
fprintf(fp, "label");
for(i=0; i<model_->nr_class; i++)
fprintf(fp, " %d", model_->label[i]);
fprintf(fp, "\n");
}
fprintf(fp, "nr_feature %d\n", nr_feature);
fprintf(fp, "bias %.17g\n", model_->bias);
if(check_oneclass_model(model_))
fprintf(fp, "rho %.17g\n", model_->rho);
fprintf(fp, "w\n");
for(i=0; i<w_size; i++)
{
int j;
for(j=0; j<nr_w; j++)
fprintf(fp, "%.17g ", model_->w[i*nr_w+j]);
fprintf(fp, "\n");
}
setlocale(LC_ALL, old_locale);
free(old_locale);
if (ferror(fp) != 0 || fclose(fp) != 0) return -1;
else return 0;
}
//
// FSCANF helps to handle fscanf failures.
// Its do-while block avoids the ambiguity when
// if (...)
// FSCANF();
// is used
//
#define FSCANF(_stream, _format, _var)do\
{\
if (fscanf(_stream, _format, _var) != 1)\
{\
fprintf(stderr, "ERROR: fscanf failed to read the model\n");\
EXIT_LOAD_MODEL()\
}\
}while(0)
// EXIT_LOAD_MODEL should NOT end with a semicolon.
#define EXIT_LOAD_MODEL()\
{\
setlocale(LC_ALL, old_locale);\
free(model_->label);\
free(model_);\
free(old_locale);\
return NULL;\
}
struct model *load_model(const char *model_file_name)
{
FILE *fp = fopen(model_file_name,"r");
if(fp==NULL) return NULL;
int i;
int nr_feature;
int n;
int nr_class;
double bias;
double rho;
model *model_ = Malloc(model,1);
parameter& param = model_->param;
// parameters for training only won't be assigned, but arrays are assigned as NULL for safety
param.nr_weight = 0;
param.weight_label = NULL;
param.weight = NULL;
param.init_sol = NULL;
model_->label = NULL;
char *old_locale = setlocale(LC_ALL, NULL);
if (old_locale)
{
old_locale = strdup(old_locale);
}
setlocale(LC_ALL, "C");
char cmd[81];
while(1)
{
FSCANF(fp,"%80s",cmd);
if(strcmp(cmd,"solver_type")==0)
src/liblinear/linear.cpp view on Meta::CPAN
{
FSCANF(fp,"%d",&nr_class);
model_->nr_class=nr_class;
}
else if(strcmp(cmd,"nr_feature")==0)
{
FSCANF(fp,"%d",&nr_feature);
model_->nr_feature=nr_feature;
}
else if(strcmp(cmd,"bias")==0)
{
FSCANF(fp,"%lf",&bias);
model_->bias=bias;
}
else if(strcmp(cmd,"rho")==0)
{
FSCANF(fp,"%lf",&rho);
model_->rho=rho;
}
else if(strcmp(cmd,"w")==0)
{
break;
}
else if(strcmp(cmd,"label")==0)
{
int nr_class = model_->nr_class;
model_->label = Malloc(int,nr_class);
for(int i=0;i<nr_class;i++)
FSCANF(fp,"%d",&model_->label[i]);
}
else
{
fprintf(stderr,"unknown text in model file: [%s]\n",cmd);
EXIT_LOAD_MODEL()
}
}
nr_feature=model_->nr_feature;
if(model_->bias>=0)
n=nr_feature+1;
else
n=nr_feature;
int w_size = n;
int nr_w;
if(nr_class==2 && param.solver_type != MCSVM_CS)
nr_w = 1;
else
nr_w = nr_class;
model_->w=Malloc(double, w_size*nr_w);
for(i=0; i<w_size; i++)
{
int j;
for(j=0; j<nr_w; j++)
FSCANF(fp, "%lf ", &model_->w[i*nr_w+j]);
}
setlocale(LC_ALL, old_locale);
free(old_locale);
if (ferror(fp) != 0 || fclose(fp) != 0) return NULL;
return model_;
}
int get_nr_feature(const model *model_)
{
return model_->nr_feature;
}
int get_nr_class(const model *model_)
{
return model_->nr_class;
}
void get_labels(const model *model_, int* label)
{
if (model_->label != NULL)
for(int i=0;i<model_->nr_class;i++)
label[i] = model_->label[i];
}
// use inline here for better performance (around 20% faster than the non-inline one)
static inline double get_w_value(const struct model *model_, int idx, int label_idx)
{
int nr_class = model_->nr_class;
int solver_type = model_->param.solver_type;
const double *w = model_->w;
if(idx < 0 || idx > model_->nr_feature)
return 0;
if(check_regression_model(model_) || check_oneclass_model(model_))
return w[idx];
else
{
if(label_idx < 0 || label_idx >= nr_class)
return 0;
if(nr_class == 2 && solver_type != MCSVM_CS)
{
if(label_idx == 0)
return w[idx];
else
return -w[idx];
}
else
return w[idx*nr_class+label_idx];
}
}
// feat_idx: starting from 1 to nr_feature
// label_idx: starting from 0 to nr_class-1 for classification models;
// for regression and one-class SVM models, label_idx is
// ignored.
double get_decfun_coef(const struct model *model_, int feat_idx, int label_idx)
{
if(feat_idx > model_->nr_feature)
return 0;
return get_w_value(model_, feat_idx-1, label_idx);
}
double get_decfun_bias(const struct model *model_, int label_idx)
( run in 0.832 second using v1.01-cache-2.11-cpan-796a6f069b2 )