| 1 |
|
| #include <algorithm> |
| 2 |
|
| #include <cerrno> |
| 3 |
|
| #include <cstdlib> |
| 4 |
|
| #include <cstring> |
| 5 |
|
| #include "linear.h" |
| 6 |
|
| |
| 7 |
|
| #define NO_XSLOCKS |
| 8 |
|
| #include "EXTERN.h" |
| 9 |
|
| #include "XSUB.h" |
| 10 |
|
| #include "perl.h" |
| 11 |
|
| #include "ppport.h" |
| 12 |
|
| |
| 13 |
|
| namespace { |
| 14 |
|
| |
| 15 |
|
| struct parameter * |
| 16 |
9 |
| alloc_parameter(pTHX_ int num_weights) { |
| 17 |
|
| struct parameter *parameter_; |
| 18 |
9 |
| Newx(parameter_, 1, struct parameter); |
| 19 |
9 |
▶ | if (num_weights == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_weights == 0) { |
5 |
4 |
9 |
|
| 20 |
5 |
| parameter_->weight_label = NULL; |
| 21 |
5 |
| parameter_->weight = NULL; |
| 22 |
|
| } else { |
| 23 |
4 |
| Newx(parameter_->weight_label, num_weights, int); |
| 24 |
4 |
| Newx(parameter_->weight, num_weights, double); |
| 25 |
|
| } |
| 26 |
9 |
| parameter_->init_sol = NULL; |
| 27 |
9 |
| parameter_->nr_weight = num_weights; |
| 28 |
9 |
| return parameter_; |
| 29 |
|
| } |
| 30 |
|
| |
| 31 |
|
| struct problem * |
| 32 |
13 |
| alloc_problem(pTHX_ int num_training_data) { |
| 33 |
|
| struct problem *problem_; |
| 34 |
13 |
| Newx(problem_, 1, struct problem); |
| 35 |
13 |
| Newx(problem_->y, num_training_data, double); |
| 36 |
|
| // Assuming that internal representation of null pointer is zero. |
| 37 |
13 |
| Newxz(problem_->x, num_training_data, struct feature_node *); |
| 38 |
13 |
| problem_->l = num_training_data; |
| 39 |
13 |
| return problem_; |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
|
| void |
| 43 |
66 |
| dummy_puts(const char *) {} |
| 44 |
|
| |
| 45 |
|
| int |
| 46 |
13 |
| find_max_feature_index(pTHX_ AV *features) { |
| 47 |
13 |
| int num_features = av_len(features) + 1; |
| 48 |
13 |
| int max_feature_index = 0; |
| 49 |
1644 |
▶ | for (int i = 0; i < num_features; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < num_features; ++i) { |
1631 |
13 |
1644 |
|
| 50 |
1631 |
| SV *feature = *av_fetch(features, i, 0); |
| 51 |
1631 |
▶ | if (!(SvROK(feature) && SvTYPE(SvRV(feature)) == SVt_PVHV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(SvROK(feature) && SvTYPE(SvRV(feature)) == SVt_PVHV)) { |
1631 |
0 |
1631 |
| if (!(SvROK(feature) && SvTYPE(SvRV(feature)) == SVt_PVHV)) { |
0 |
1631 |
1631 |
|
| 52 |
0 |
| Perl_croak(aTHX_ "Not a HASH reference."); |
| 53 |
|
| } |
| 54 |
1631 |
| HV *feature_hash = (HV *)SvRV(feature); |
| 55 |
1631 |
| hv_iterinit(feature_hash); |
| 56 |
|
| HE *nonzero_element; |
| 57 |
21913 |
▶ | while ((nonzero_element = hv_iternext(feature_hash))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((nonzero_element = hv_iternext(feature_hash))) { |
20282 |
1631 |
21913 |
|
| 58 |
|
| I32 index_length; |
| 59 |
20282 |
▶ | int index = atoi(hv_iterkey(nonzero_element, &index_length)); |
Branch: 1 branch
| Branch | True | False | Total |
| int index = atoi(hv_iterkey(nonzero_element, &index_length)); |
20282 |
0 |
20282 |
|
| 60 |
20282 |
▶ | if (max_feature_index < index) { max_feature_index = index; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (max_feature_index < index) { max_feature_index = index; } |
15 |
20267 |
20282 |
|
| 61 |
|
| } |
| 62 |
|
| } |
| 63 |
13 |
| return max_feature_index; |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
|
| void |
| 67 |
9 |
| free_parameter(pTHX_ struct parameter *parameter_) { |
| 68 |
9 |
| Safefree(parameter_->weight_label); |
| 69 |
9 |
| Safefree(parameter_->weight); |
| 70 |
9 |
| Safefree(parameter_); |
| 71 |
9 |
| } |
| 72 |
|
| |
| 73 |
|
| void |
| 74 |
13 |
| free_problem(pTHX_ struct problem *problem_) { |
| 75 |
1644 |
▶ | for (int i = 0; i < problem_->l; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < problem_->l; ++i) { |
1631 |
13 |
1644 |
|
| 76 |
1631 |
| struct feature_node *feature_vector = problem_->x[i]; |
| 77 |
1631 |
▶ | if (feature_vector) { Safefree(feature_vector); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (feature_vector) { Safefree(feature_vector); } |
1631 |
0 |
1631 |
|
| 78 |
|
| } |
| 79 |
13 |
| Safefree(problem_->x); |
| 80 |
13 |
| Safefree(problem_->y); |
| 81 |
13 |
| Safefree(problem_); |
| 82 |
13 |
| } |
| 83 |
|
| |
| 84 |
|
| bool |
| 85 |
80456 |
| has_less_index(const struct feature_node& a, const struct feature_node& b) { |
| 86 |
80456 |
| return a.index < b.index; |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
|
| struct feature_node * |
| 90 |
1632 |
| hv2feature( |
| 91 |
|
| pTHX_ HV *feature_hash, int bias_index = 0, double bias = -1.0) { |
| 92 |
1632 |
| bool has_bias = bias >= 0; |
| 93 |
|
| int feature_vector_size = |
| 94 |
1632 |
▶ | hv_iterinit(feature_hash) + (has_bias ? 1 : 0) + 1; |
Branch: 2 branches
| Branch | True | False | Total |
| hv_iterinit(feature_hash) + (has_bias ? 1 : 0) + 1; |
1632 |
0 |
1632 |
| hv_iterinit(feature_hash) + (has_bias ? 1 : 0) + 1; |
5 |
1627 |
1632 |
|
| 95 |
|
| struct feature_node *feature_vector; |
| 96 |
1632 |
▶ | Newx(feature_vector, feature_vector_size, struct feature_node); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(feature_vector, feature_vector_size, struct feature_node); |
1632 |
0 |
1632 |
|
| 97 |
|
| char *index; |
| 98 |
|
| I32 index_length; |
| 99 |
|
| SV *value; |
| 100 |
1632 |
| struct feature_node *curr = feature_vector; |
| 101 |
21926 |
▶ | while ((value = hv_iternextsv(feature_hash, &index, &index_length))) { |
Branch: 2 branches
| Branch | True | False | Total |
| while ((value = hv_iternextsv(feature_hash, &index, &index_length))) { |
21926 |
0 |
21926 |
| while ((value = hv_iternextsv(feature_hash, &index, &index_length))) { |
20294 |
1632 |
21926 |
|
| 102 |
20294 |
| curr->index = atoi(index); |
| 103 |
20294 |
▶ | curr->value = SvNV(value); |
Branch: 1 branch
| Branch | True | False | Total |
| curr->value = SvNV(value); |
20294 |
0 |
20294 |
|
| 104 |
20294 |
| ++curr; |
| 105 |
|
| } |
| 106 |
1632 |
▶ | if (has_bias) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (has_bias) { |
5 |
1627 |
1632 |
|
| 107 |
5 |
| curr->index = bias_index; |
| 108 |
5 |
| curr->value = bias; |
| 109 |
5 |
| ++curr; |
| 110 |
|
| } |
| 111 |
|
| // Sentinel. LIBLINEAR doesn't care about its value. |
| 112 |
1632 |
| curr->index = -1; |
| 113 |
|
| // Since LIBLINEAR 2.40, |sparse_operator::sparse_dot|, used in one-class |
| 114 |
|
| // SVM solver (|solve_oneclass_svm|), started to assume that the |
| 115 |
|
| // |feature_node| vector is sorted by |index|. |
| 116 |
1632 |
| std::sort( |
| 117 |
|
| feature_vector, |
| 118 |
|
| // |- 1| for removing sentinel node from the range of sorting. |
| 119 |
1632 |
▶ | feature_vector + feature_vector_size - 1, |
Branch: 1 branch
| Branch | True | False | Total |
| feature_vector + feature_vector_size - 1, |
1632 |
0 |
1632 |
|
| 120 |
|
| has_less_index); |
| 121 |
1632 |
| return feature_vector; |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
|
| inline bool |
| 125 |
4 |
| is_regression_solver(const struct parameter *parameter_) { |
| 126 |
4 |
▶ | switch (parameter_->solver_type) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (parameter_->solver_type) { |
1 |
3 |
4 |
|
| 127 |
1 |
| case L2R_L2LOSS_SVR: |
| 128 |
|
| case L2R_L2LOSS_SVR_DUAL: |
| 129 |
|
| case L2R_L1LOSS_SVR_DUAL: |
| 130 |
1 |
| return true; |
| 131 |
3 |
| default: |
| 132 |
3 |
| return false; |
| 133 |
|
| } |
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
|
| void |
| 137 |
10 |
| validate_parameter( |
| 138 |
|
| pTHX_ |
| 139 |
|
| struct problem *problem_, |
| 140 |
|
| struct parameter *parameter_) { |
| 141 |
10 |
| const char *message = check_parameter(problem_, parameter_); |
| 142 |
10 |
▶ | if (message) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (message) { |
6 |
4 |
10 |
|
| 143 |
6 |
| Perl_croak(aTHX_ "Invalid training parameter: %s", message); |
| 144 |
|
| } |
| 145 |
4 |
| } |
| 146 |
|
| |
| 147 |
|
| } // namespace |
| 148 |
|
| |
| 149 |
|
| MODULE = Algorithm::LibLinear PACKAGE = Algorithm::LibLinear::Model::Raw PREFIX = ll_ |
| 150 |
|
| |
| 151 |
|
| TYPEMAP: <<'EOT' |
| 152 |
|
| TYPEMAP |
| 153 |
|
| AV * T_AVREF_REFCOUNT_FIXED |
| 154 |
|
| |
| 155 |
|
| struct model * T_LIBLINEAR_MODEL |
| 156 |
|
| |
| 157 |
|
| struct parameter * T_LIBLINEAR_TRAINING_PARAMETER |
| 158 |
|
| |
| 159 |
|
| struct problem * T_LIBLINEAR_PROBLEM |
| 160 |
|
| |
| 161 |
|
| INPUT |
| 162 |
|
| T_LIBLINEAR_MODEL |
| 163 |
|
| if (SvROK($arg) && |
| 164 |
|
| sv_derived_from($arg, \"Algorithm::LibLinear::Model::Raw\")) { |
| 165 |
|
| IV tmp = SvIV((SV*)SvRV($arg)); |
| 166 |
|
| $var = INT2PTR($type,tmp); |
| 167 |
|
| } |
| 168 |
|
| else { |
| 169 |
|
| Perl_croak(aTHX_ \"%s: %s is not of type %s\", |
| 170 |
|
| ${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]}, |
| 171 |
|
| \"$var\", \"$ntype\"); |
| 172 |
|
| } |
| 173 |
|
| |
| 174 |
|
| T_LIBLINEAR_TRAINING_PARAMETER |
| 175 |
|
| if (SvROK($arg) && |
| 176 |
|
| sv_derived_from($arg, \"Algorithm::LibLinear::TrainingParameter\")) { |
| 177 |
|
| IV tmp = SvIV((SV*)SvRV($arg)); |
| 178 |
|
| $var = INT2PTR($type,tmp); |
| 179 |
|
| } |
| 180 |
|
| else { |
| 181 |
|
| Perl_croak(aTHX_ \"%s: %s is not of type %s\", |
| 182 |
|
| ${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]}, |
| 183 |
|
| \"$var\", \"$ntype\"); |
| 184 |
|
| } |
| 185 |
|
| |
| 186 |
|
| T_LIBLINEAR_PROBLEM |
| 187 |
|
| if (SvROK($arg) && |
| 188 |
|
| sv_derived_from($arg, \"Algorithm::LibLinear::Problem\")) { |
| 189 |
|
| IV tmp = SvIV((SV*)SvRV($arg)); |
| 190 |
|
| $var = INT2PTR($type,tmp); |
| 191 |
|
| } |
| 192 |
|
| else { |
| 193 |
|
| Perl_croak(aTHX_ \"%s: %s is not of type %s\", |
| 194 |
|
| ${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]}, |
| 195 |
|
| \"$var\", \"$ntype\"); |
| 196 |
|
| } |
| 197 |
|
| |
| 198 |
|
| OUTPUT |
| 199 |
|
| T_LIBLINEAR_MODEL |
| 200 |
|
| sv_setref_pv($arg, \"Algorithm::LibLinear::Model::Raw\", (void*)$var); |
| 201 |
|
| |
| 202 |
|
| T_LIBLINEAR_TRAINING_PARAMETER |
| 203 |
|
| sv_setref_pv( |
| 204 |
|
| $arg, \"Algorithm::LibLinear::TrainingParameter\", (void*)$var); |
| 205 |
|
| |
| 206 |
|
| T_LIBLINEAR_PROBLEM |
| 207 |
|
| sv_setref_pv($arg, \"Algorithm::LibLinear::Problem\", (void*)$var); |
| 208 |
|
| EOT |
| 209 |
|
| |
| 210 |
|
| BOOT: |
| 211 |
4 |
| set_print_string_function(dummy_puts); |
| 212 |
|
| |
| 213 |
|
| PROTOTYPES: DISABLE |
| 214 |
|
| |
| 215 |
|
| struct model * |
| 216 |
|
| ll_train(klass, problem_, parameter_) |
| 217 |
|
| struct problem *problem_; |
| 218 |
|
| struct parameter *parameter_; |
| 219 |
|
| CODE: |
| 220 |
6 |
| validate_parameter(aTHX_ problem_, parameter_); |
| 221 |
3 |
| RETVAL = train(problem_, parameter_); |
| 222 |
|
| OUTPUT: |
| 223 |
|
| RETVAL |
| 224 |
|
| |
| 225 |
|
| struct model * |
| 226 |
|
| ll_load(klass, filename) |
| 227 |
|
| const char *filename; |
| 228 |
|
| CODE: |
| 229 |
3 |
| RETVAL = load_model(filename); |
| 230 |
3 |
▶ | if (!RETVAL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!RETVAL) { |
1 |
2 |
3 |
|
| 231 |
1 |
| Perl_croak(aTHX_ "Failed to load a model from file: %s.", filename); |
| 232 |
|
| } |
| 233 |
|
| OUTPUT: |
| 234 |
|
| RETVAL |
| 235 |
|
| |
| 236 |
|
| double |
| 237 |
|
| ll_bias(self, label) |
| 238 |
|
| struct model *self; |
| 239 |
|
| int label; |
| 240 |
|
| CODE: |
| 241 |
3 |
▶ | RETVAL = get_decfun_bias(self, label); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = get_decfun_bias(self, label); |
2 |
1 |
3 |
|
| 242 |
|
| OUTPUT: |
| 243 |
|
| RETVAL |
| 244 |
|
| |
| 245 |
|
| AV * |
| 246 |
|
| ll_class_labels(self) |
| 247 |
|
| struct model *self; |
| 248 |
|
| CODE: |
| 249 |
4 |
| RETVAL = newAV(); |
| 250 |
4 |
| av_extend(RETVAL, self->nr_class - 1); |
| 251 |
13 |
▶ | for (int i = 0; i < self->nr_class; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < self->nr_class; ++i) { |
9 |
4 |
13 |
|
| 252 |
9 |
| av_push(RETVAL, newSViv(self->label[i])); |
| 253 |
|
| } |
| 254 |
|
| OUTPUT: |
| 255 |
|
| RETVAL |
| 256 |
|
| |
| 257 |
|
| double |
| 258 |
|
| ll_coefficient(self, feature, label) |
| 259 |
|
| struct model *self; |
| 260 |
|
| int feature; |
| 261 |
|
| int label; |
| 262 |
|
| CODE: |
| 263 |
12 |
▶ | RETVAL = get_decfun_coef(self, feature, label); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = get_decfun_coef(self, feature, label); |
11 |
1 |
12 |
|
| 264 |
|
| OUTPUT: |
| 265 |
|
| RETVAL |
| 266 |
|
| |
| 267 |
|
| bool |
| 268 |
|
| ll_is_oneclass_model(self) |
| 269 |
|
| struct model* self; |
| 270 |
|
| CODE: |
| 271 |
4 |
▶ | RETVAL = check_oneclass_model(self); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = check_oneclass_model(self); |
0 |
4 |
4 |
|
| 272 |
|
| OUTPUT: |
| 273 |
|
| RETVAL |
| 274 |
|
| |
| 275 |
|
| bool |
| 276 |
|
| ll_is_probability_model(self) |
| 277 |
|
| struct model *self; |
| 278 |
|
| CODE: |
| 279 |
2 |
▶ | RETVAL = check_probability_model(self); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = check_probability_model(self); |
0 |
2 |
2 |
|
| 280 |
|
| OUTPUT: |
| 281 |
|
| RETVAL |
| 282 |
|
| |
| 283 |
|
| bool |
| 284 |
|
| ll_is_regression_model(self) |
| 285 |
|
| struct model *self; |
| 286 |
|
| CODE: |
| 287 |
1 |
▶ | RETVAL = check_regression_model(self); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = check_regression_model(self); |
0 |
1 |
1 |
|
| 288 |
|
| OUTPUT: |
| 289 |
|
| RETVAL |
| 290 |
|
| |
| 291 |
|
| int |
| 292 |
|
| ll_num_classes(self) |
| 293 |
|
| struct model *self; |
| 294 |
|
| CODE: |
| 295 |
7 |
▶ | RETVAL = get_nr_class(self); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = get_nr_class(self); |
5 |
2 |
7 |
|
| 296 |
|
| OUTPUT: |
| 297 |
|
| RETVAL |
| 298 |
|
| |
| 299 |
|
| int |
| 300 |
|
| ll_num_features(self) |
| 301 |
|
| struct model *self; |
| 302 |
|
| CODE: |
| 303 |
3 |
▶ | RETVAL = get_nr_feature(self); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = get_nr_feature(self); |
1 |
2 |
3 |
|
| 304 |
|
| OUTPUT: |
| 305 |
|
| RETVAL |
| 306 |
|
| |
| 307 |
|
| SV * |
| 308 |
|
| ll_predict(self, feature_hash) |
| 309 |
|
| struct model *self; |
| 310 |
|
| HV *feature_hash; |
| 311 |
|
| CODE: |
| 312 |
1 |
| struct feature_node *feature_vector = hv2feature(aTHX_ feature_hash); |
| 313 |
1 |
| double prediction = predict(self, feature_vector); |
| 314 |
1 |
| Safefree(feature_vector); |
| 315 |
2 |
▶ | RETVAL = is_regression_solver(&self->param) ? |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = is_regression_solver(&self->param) ? |
0 |
1 |
1 |
|
| 316 |
1 |
| newSVnv(prediction) : newSViv((int)prediction); |
| 317 |
|
| OUTPUT: |
| 318 |
|
| RETVAL |
| 319 |
|
| |
| 320 |
|
| AV * |
| 321 |
|
| ll_predict_probability(self, feature_hash) |
| 322 |
|
| struct model *self; |
| 323 |
|
| HV *feature_hash; |
| 324 |
|
| CODE: |
| 325 |
0 |
| RETVAL = newAV(); |
| 326 |
0 |
▶ | if (check_probability_model(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (check_probability_model(self)) { |
0 |
0 |
0 |
|
| 327 |
0 |
| struct feature_node *feature_vector = hv2feature(aTHX_ feature_hash); |
| 328 |
|
| double *estimated_probabilities; |
| 329 |
0 |
| int num_classes = get_nr_class(self); |
| 330 |
0 |
| Newx(estimated_probabilities, num_classes, double); |
| 331 |
0 |
| predict_probability(self, feature_vector, estimated_probabilities); |
| 332 |
0 |
| av_extend(RETVAL, num_classes - 1); |
| 333 |
0 |
▶ | for (int i = 0; i < num_classes; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < num_classes; ++i) { |
0 |
0 |
0 |
|
| 334 |
0 |
| av_push(RETVAL, newSVnv(estimated_probabilities[i])); |
| 335 |
|
| } |
| 336 |
0 |
| Safefree(feature_vector); |
| 337 |
0 |
| Safefree(estimated_probabilities); |
| 338 |
|
| } |
| 339 |
|
| OUTPUT: |
| 340 |
|
| RETVAL |
| 341 |
|
| |
| 342 |
|
| AV * |
| 343 |
|
| ll_predict_values(self, feature_hash) |
| 344 |
|
| struct model *self; |
| 345 |
|
| HV *feature_hash; |
| 346 |
|
| CODE: |
| 347 |
0 |
| struct feature_node *feature_vector = hv2feature(aTHX_ feature_hash); |
| 348 |
0 |
| int num_classes = get_nr_class(self); |
| 349 |
0 |
| int num_decision_values = |
| 350 |
0 |
▶ | (num_classes == 2 && self->param.solver_type != MCSVM_CS) ? |
Branch: 2 branches
| Branch | True | False | Total |
| (num_classes == 2 && self->param.solver_type != MCSVM_CS) ? |
0 |
0 |
0 |
| (num_classes == 2 && self->param.solver_type != MCSVM_CS) ? |
0 |
0 |
0 |
|
| 351 |
|
| 1 : num_classes; |
| 352 |
|
| double *decision_values; |
| 353 |
0 |
| Newx(decision_values, num_decision_values, double); |
| 354 |
0 |
| predict_values(self, feature_vector, decision_values); |
| 355 |
0 |
| RETVAL = newAV(); |
| 356 |
0 |
| av_extend(RETVAL, num_decision_values - 1); |
| 357 |
0 |
▶ | for (int i = 0; i < num_decision_values; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < num_decision_values; ++i) { |
0 |
0 |
0 |
|
| 358 |
0 |
| av_push(RETVAL, newSVnv(decision_values[i])); |
| 359 |
|
| } |
| 360 |
0 |
| Safefree(decision_values); |
| 361 |
0 |
| Safefree(feature_vector); |
| 362 |
|
| OUTPUT: |
| 363 |
|
| RETVAL |
| 364 |
|
| |
| 365 |
|
| double |
| 366 |
|
| ll_rho(self) |
| 367 |
|
| struct model *self; |
| 368 |
|
| CODE: |
| 369 |
0 |
▶ | RETVAL = get_decfun_rho(self); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = get_decfun_rho(self); |
0 |
0 |
0 |
|
| 370 |
|
| OUTPUT: |
| 371 |
|
| RETVAL |
| 372 |
|
| |
| 373 |
|
| void |
| 374 |
|
| ll_save(self, filename) |
| 375 |
|
| struct model *self; |
| 376 |
|
| const char *filename; |
| 377 |
|
| CODE: |
| 378 |
1 |
▶ | if (save_model(filename, self) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (save_model(filename, self) != 0) { |
0 |
1 |
1 |
|
| 379 |
0 |
| Perl_croak( |
| 380 |
|
| aTHX_ |
| 381 |
|
| "Error occured during save process: %s", |
| 382 |
0 |
▶ | errno == 0 ? "unknown error" : strerror(errno) |
Branch: 1 branch
| Branch | True | False | Total |
| errno == 0 ? "unknown error" : strerror(errno) |
0 |
0 |
0 |
|
| 383 |
|
| ); |
| 384 |
|
| } |
| 385 |
|
| |
| 386 |
|
| void |
| 387 |
|
| ll_DESTROY(self) |
| 388 |
|
| struct model *self; |
| 389 |
|
| CODE: |
| 390 |
5 |
▶ | free_and_destroy_model(&self); |
Branch: 1 branch
| Branch | True | False | Total |
| free_and_destroy_model(&self); |
5 |
0 |
5 |
|
| 391 |
|
| |
| 392 |
|
| MODULE = Algorithm::LibLinear PACKAGE = Algorithm::LibLinear::TrainingParameter PREFIX = ll_ |
| 393 |
|
| |
| 394 |
|
| PROTOTYPES: DISABLE |
| 395 |
|
| |
| 396 |
|
| struct parameter * |
| 397 |
|
| ll_new(klass, solver_type, epsilon, cost, weight_labels, weights, loss_sensitivity, nu, regularize_bias) |
| 398 |
|
| int solver_type; |
| 399 |
|
| double epsilon; |
| 400 |
|
| double cost; |
| 401 |
|
| AV *weight_labels; |
| 402 |
|
| AV *weights; |
| 403 |
|
| double loss_sensitivity; |
| 404 |
|
| double nu; |
| 405 |
|
| bool regularize_bias; |
| 406 |
|
| CODE: |
| 407 |
9 |
▶ | int num_weights = av_len(weight_labels) + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| int num_weights = av_len(weight_labels) + 1; |
9 |
0 |
9 |
|
| 408 |
9 |
▶ | if (av_len(weights) + 1 != num_weights) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (av_len(weights) + 1 != num_weights) { |
9 |
0 |
9 |
| if (av_len(weights) + 1 != num_weights) { |
0 |
9 |
9 |
|
| 409 |
0 |
| Perl_croak( |
| 410 |
|
| aTHX_ |
| 411 |
|
| "The number of weight labels is not equal to the number of" |
| 412 |
|
| " weights."); |
| 413 |
|
| } |
| 414 |
|
| // |init_sol| is initialized within |alloc_parameter|. |
| 415 |
9 |
▶ | RETVAL = alloc_parameter(aTHX_ num_weights); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = alloc_parameter(aTHX_ num_weights); |
9 |
0 |
9 |
|
| 416 |
9 |
| RETVAL->solver_type = solver_type; |
| 417 |
9 |
| RETVAL->eps = epsilon; |
| 418 |
9 |
| RETVAL->C = cost; |
| 419 |
9 |
| RETVAL->p = loss_sensitivity; |
| 420 |
9 |
| RETVAL->nu = nu; |
| 421 |
9 |
▶ | RETVAL->regularize_bias = regularize_bias ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL->regularize_bias = regularize_bias ? 1 : 0; |
9 |
0 |
9 |
|
| 422 |
9 |
| dXCPT; |
| 423 |
9 |
▶ | XCPT_TRY_START { |
Branch: 1 branch
| Branch | True | False | Total |
| XCPT_TRY_START { |
9 |
0 |
9 |
|
| 424 |
9 |
| int *weight_labels_ = RETVAL->weight_label; |
| 425 |
9 |
| double *weights_ = RETVAL->weight; |
| 426 |
17 |
▶ | for (int i = 0; i < num_weights; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < num_weights; ++i) { |
8 |
9 |
17 |
|
| 427 |
8 |
▶ | weight_labels_[i] = SvIV(*av_fetch(weight_labels, i, 0)); |
Branch: 2 branches
| Branch | True | False | Total |
| weight_labels_[i] = SvIV(*av_fetch(weight_labels, i, 0)); |
8 |
0 |
8 |
| weight_labels_[i] = SvIV(*av_fetch(weight_labels, i, 0)); |
8 |
0 |
8 |
|
| 428 |
8 |
▶ | weights_[i] = SvNV(*av_fetch(weights, i, 0)); |
Branch: 2 branches
| Branch | True | False | Total |
| weights_[i] = SvNV(*av_fetch(weights, i, 0)); |
8 |
0 |
8 |
| weights_[i] = SvNV(*av_fetch(weights, i, 0)); |
8 |
0 |
8 |
|
| 429 |
|
| } |
| 430 |
9 |
| } XCPT_TRY_END |
| 431 |
9 |
▶ | XCPT_CATCH { |
Branch: 1 branch
| Branch | True | False | Total |
| XCPT_CATCH { |
0 |
9 |
9 |
|
| 432 |
0 |
▶ | free_parameter(aTHX_ RETVAL); |
Branch: 1 branch
| Branch | True | False | Total |
| free_parameter(aTHX_ RETVAL); |
0 |
0 |
0 |
|
| 433 |
0 |
▶ | XCPT_RETHROW; |
Branch: 4 branches
| Branch | True | False | Total |
| XCPT_RETHROW; |
0 |
0 |
0 |
| XCPT_RETHROW; |
0 |
0 |
0 |
| XCPT_RETHROW; |
0 |
0 |
0 |
| XCPT_RETHROW; |
0 |
0 |
0 |
|
| 434 |
|
| } |
| 435 |
|
| OUTPUT: |
| 436 |
|
| RETVAL |
| 437 |
|
| |
| 438 |
|
| AV * |
| 439 |
|
| ll_cross_validation(self, problem_, num_folds) |
| 440 |
|
| struct parameter *self; |
| 441 |
|
| struct problem *problem_; |
| 442 |
|
| int num_folds; |
| 443 |
|
| CODE: |
| 444 |
4 |
| validate_parameter(aTHX_ problem_, self); |
| 445 |
|
| double *targets; |
| 446 |
1 |
| Newx(targets, problem_->l, double); |
| 447 |
1 |
| cross_validation(problem_, self, num_folds, targets); |
| 448 |
1 |
| RETVAL = newAV(); |
| 449 |
1 |
| av_extend(RETVAL, problem_->l - 1); |
| 450 |
271 |
▶ | for (int i = 0; i < problem_->l; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < problem_->l; ++i) { |
270 |
1 |
271 |
|
| 451 |
270 |
| av_push(RETVAL, newSVnv(targets[i])); |
| 452 |
|
| } |
| 453 |
1 |
| Safefree(targets); |
| 454 |
|
| OUTPUT: |
| 455 |
|
| RETVAL |
| 456 |
|
| |
| 457 |
|
| AV * |
| 458 |
|
| ll_find_parameters(self, problem_, num_folds, initial_C, initial_p, update) |
| 459 |
|
| struct parameter *self; |
| 460 |
|
| struct problem *problem_; |
| 461 |
|
| int num_folds; |
| 462 |
|
| double initial_C; |
| 463 |
|
| double initial_p; |
| 464 |
|
| bool update; |
| 465 |
|
| CODE: |
| 466 |
|
| double best_C, best_p, accuracy; |
| 467 |
2 |
▶ | find_parameters( |
Branch: 1 branch
| Branch | True | False | Total |
| find_parameters( |
2 |
0 |
2 |
|
| 468 |
|
| problem_, self, num_folds, initial_C, initial_p, &best_C, &best_p, |
| 469 |
|
| &accuracy); |
| 470 |
|
| // LIBLINEAR 2.0 resets default printer function during call of |
| 471 |
|
| // find_parameter_C(). So disable it again. |
| 472 |
2 |
▶ | set_print_string_function(dummy_puts); |
Branch: 1 branch
| Branch | True | False | Total |
| set_print_string_function(dummy_puts); |
2 |
0 |
2 |
|
| 473 |
2 |
| bool is_regression_model = self->solver_type == L2R_L2LOSS_SVR; |
| 474 |
2 |
▶ | if (update) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (update) { |
0 |
2 |
2 |
|
| 475 |
0 |
| self->C = best_C; |
| 476 |
0 |
▶ | if (is_regression_model) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_regression_model) { |
0 |
0 |
0 |
|
| 477 |
0 |
| self->p = best_p; |
| 478 |
|
| } |
| 479 |
|
| } |
| 480 |
2 |
▶ | RETVAL = newAV(); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = newAV(); |
2 |
0 |
2 |
|
| 481 |
2 |
▶ | av_push(RETVAL, newSVnv(best_C)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(RETVAL, newSVnv(best_C)); |
2 |
0 |
2 |
| av_push(RETVAL, newSVnv(best_C)); |
2 |
0 |
2 |
|
| 482 |
2 |
▶ | av_push( |
Branch: 4 branches
| Branch | True | False | Total |
| av_push( |
0 |
2 |
2 |
| av_push( |
0 |
0 |
0 |
| av_push( |
2 |
0 |
2 |
| av_push( |
2 |
0 |
2 |
|
| 483 |
|
| RETVAL, |
| 484 |
|
| is_regression_model ? newSVnv(best_p) : newSVsv(&PL_sv_undef)); |
| 485 |
2 |
▶ | av_push(RETVAL, newSVnv(accuracy)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(RETVAL, newSVnv(accuracy)); |
2 |
0 |
2 |
| av_push(RETVAL, newSVnv(accuracy)); |
2 |
0 |
2 |
|
| 486 |
|
| OUTPUT: |
| 487 |
|
| RETVAL |
| 488 |
|
| |
| 489 |
|
| bool |
| 490 |
|
| ll_is_regression_solver(self) |
| 491 |
|
| struct parameter *self; |
| 492 |
|
| CODE: |
| 493 |
3 |
| RETVAL = is_regression_solver(self); |
| 494 |
|
| OUTPUT: |
| 495 |
|
| RETVAL |
| 496 |
|
| |
| 497 |
|
| double |
| 498 |
|
| ll_cost(self) |
| 499 |
|
| struct parameter *self; |
| 500 |
|
| CODE: |
| 501 |
2 |
▶ | RETVAL = self->C; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->C; |
1 |
1 |
2 |
|
| 502 |
|
| OUTPUT: |
| 503 |
|
| RETVAL |
| 504 |
|
| |
| 505 |
|
| double |
| 506 |
|
| ll_epsilon(self) |
| 507 |
|
| struct parameter *self; |
| 508 |
|
| CODE: |
| 509 |
2 |
▶ | RETVAL = self->eps; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->eps; |
1 |
1 |
2 |
|
| 510 |
|
| OUTPUT: |
| 511 |
|
| RETVAL |
| 512 |
|
| |
| 513 |
|
| double |
| 514 |
|
| ll_loss_sensitivity(self) |
| 515 |
|
| struct parameter *self; |
| 516 |
|
| CODE: |
| 517 |
1 |
▶ | RETVAL = self->p; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->p; |
0 |
1 |
1 |
|
| 518 |
|
| OUTPUT: |
| 519 |
|
| RETVAL |
| 520 |
|
| |
| 521 |
|
| int |
| 522 |
|
| ll_solver_type(self) |
| 523 |
|
| struct parameter *self; |
| 524 |
|
| CODE: |
| 525 |
3 |
▶ | RETVAL = self->solver_type; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->solver_type; |
2 |
1 |
3 |
|
| 526 |
|
| OUTPUT: |
| 527 |
|
| RETVAL |
| 528 |
|
| |
| 529 |
|
| AV * |
| 530 |
|
| ll_weights(self) |
| 531 |
|
| struct parameter *self; |
| 532 |
|
| CODE: |
| 533 |
2 |
| RETVAL = newAV(); |
| 534 |
2 |
| av_extend(RETVAL, self->nr_weight - 1); |
| 535 |
4 |
▶ | for (int i = 0; i < self->nr_weight; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < self->nr_weight; ++i) { |
2 |
2 |
4 |
|
| 536 |
2 |
| av_push(RETVAL, newSVnv(self->weight[i])); |
| 537 |
|
| } |
| 538 |
|
| OUTPUT: |
| 539 |
|
| RETVAL |
| 540 |
|
| |
| 541 |
|
| AV * |
| 542 |
|
| ll_weight_labels(self) |
| 543 |
|
| struct parameter *self; |
| 544 |
|
| CODE: |
| 545 |
2 |
| RETVAL = newAV(); |
| 546 |
2 |
| av_extend(RETVAL, self->nr_weight - 1); |
| 547 |
4 |
▶ | for (int i = 0; i < self->nr_weight; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < self->nr_weight; ++i) { |
2 |
2 |
4 |
|
| 548 |
2 |
| av_push(RETVAL, newSViv(self->weight_label[i])); |
| 549 |
|
| } |
| 550 |
|
| OUTPUT: |
| 551 |
|
| RETVAL |
| 552 |
|
| |
| 553 |
|
| void |
| 554 |
|
| ll_DESTROY(self) |
| 555 |
|
| struct parameter *self; |
| 556 |
|
| CODE: |
| 557 |
9 |
| free_parameter(aTHX_ self); |
| 558 |
|
| |
| 559 |
|
| MODULE = Algorithm::LibLinear PACKAGE = Algorithm::LibLinear::Problem PREFIX = ll_ |
| 560 |
|
| |
| 561 |
|
| PROTOTYPES: DISABLE |
| 562 |
|
| |
| 563 |
|
| struct problem * |
| 564 |
|
| ll_new(klass, labels, features, bias) |
| 565 |
|
| AV *labels; |
| 566 |
|
| AV *features; |
| 567 |
|
| double bias; |
| 568 |
|
| CODE: |
| 569 |
13 |
▶ | int num_training_data = av_len(labels) + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| int num_training_data = av_len(labels) + 1; |
13 |
0 |
13 |
|
| 570 |
13 |
▶ | if (num_training_data == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_training_data == 0) { |
0 |
13 |
13 |
|
| 571 |
0 |
| Perl_croak(aTHX_ "No training set is given."); |
| 572 |
|
| } |
| 573 |
13 |
▶ | if (av_len(features) + 1 != num_training_data) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (av_len(features) + 1 != num_training_data) { |
13 |
0 |
13 |
| if (av_len(features) + 1 != num_training_data) { |
0 |
13 |
13 |
|
| 574 |
0 |
| Perl_croak( |
| 575 |
|
| aTHX_ |
| 576 |
|
| "The number of labels is not equal to the number of features."); |
| 577 |
|
| } |
| 578 |
|
| |
| 579 |
13 |
▶ | RETVAL = alloc_problem(aTHX_ num_training_data); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = alloc_problem(aTHX_ num_training_data); |
13 |
0 |
13 |
|
| 580 |
13 |
| bool has_bias = bias >= 0; |
| 581 |
13 |
| dXCPT; |
| 582 |
13 |
▶ | XCPT_TRY_START { |
Branch: 1 branch
| Branch | True | False | Total |
| XCPT_TRY_START { |
13 |
0 |
13 |
|
| 583 |
13 |
| double *labels_ = RETVAL->y; |
| 584 |
1644 |
▶ | for (int i = 0; i < num_training_data; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < num_training_data; ++i) { |
1631 |
13 |
1644 |
|
| 585 |
1631 |
▶ | SV *label = *av_fetch(labels, i, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *label = *av_fetch(labels, i, 0); |
1631 |
0 |
1631 |
|
| 586 |
1631 |
▶ | labels_[i] = SvIV(label); |
Branch: 1 branch
| Branch | True | False | Total |
| labels_[i] = SvIV(label); |
1631 |
0 |
1631 |
|
| 587 |
|
| } |
| 588 |
|
| |
| 589 |
13 |
| struct feature_node **features_ = RETVAL->x; |
| 590 |
|
| int max_feature_index = |
| 591 |
13 |
▶ | find_max_feature_index(aTHX_ features) + (has_bias ? 1 : 0); |
Branch: 2 branches
| Branch | True | False | Total |
| find_max_feature_index(aTHX_ features) + (has_bias ? 1 : 0); |
13 |
0 |
13 |
| find_max_feature_index(aTHX_ features) + (has_bias ? 1 : 0); |
1 |
12 |
13 |
|
| 592 |
1644 |
▶ | for (int i = 0; i < num_training_data; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < num_training_data; ++i) { |
1631 |
13 |
1644 |
|
| 593 |
1631 |
▶ | SV *feature = *av_fetch(features, i, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *feature = *av_fetch(features, i, 0); |
1631 |
0 |
1631 |
|
| 594 |
1631 |
▶ | if (!(SvROK(feature) && SvTYPE(SvRV(feature)) == SVt_PVHV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(SvROK(feature) && SvTYPE(SvRV(feature)) == SVt_PVHV)) { |
1631 |
0 |
1631 |
| if (!(SvROK(feature) && SvTYPE(SvRV(feature)) == SVt_PVHV)) { |
0 |
1631 |
1631 |
|
| 595 |
0 |
| Perl_croak(aTHX_ "Not a HASH reference."); |
| 596 |
|
| } |
| 597 |
1631 |
| HV *feature_hash = (HV *)SvRV(feature); |
| 598 |
3262 |
| features_[i] = |
| 599 |
1631 |
▶ | hv2feature(aTHX_ feature_hash, max_feature_index, bias); |
Branch: 1 branch
| Branch | True | False | Total |
| hv2feature(aTHX_ feature_hash, max_feature_index, bias); |
1631 |
0 |
1631 |
|
| 600 |
|
| } |
| 601 |
13 |
| RETVAL->bias = bias; |
| 602 |
13 |
| RETVAL->n = max_feature_index; |
| 603 |
13 |
| } XCPT_TRY_END |
| 604 |
13 |
▶ | XCPT_CATCH { |
Branch: 1 branch
| Branch | True | False | Total |
| XCPT_CATCH { |
0 |
13 |
13 |
|
| 605 |
0 |
▶ | free_problem(aTHX_ RETVAL); |
Branch: 1 branch
| Branch | True | False | Total |
| free_problem(aTHX_ RETVAL); |
0 |
0 |
0 |
|
| 606 |
0 |
▶ | XCPT_RETHROW; |
Branch: 4 branches
| Branch | True | False | Total |
| XCPT_RETHROW; |
0 |
0 |
0 |
| XCPT_RETHROW; |
0 |
0 |
0 |
| XCPT_RETHROW; |
0 |
0 |
0 |
| XCPT_RETHROW; |
0 |
0 |
0 |
|
| 607 |
|
| } |
| 608 |
|
| OUTPUT: |
| 609 |
|
| RETVAL |
| 610 |
|
| |
| 611 |
|
| double |
| 612 |
|
| ll_bias(self) |
| 613 |
|
| struct problem *self; |
| 614 |
|
| CODE: |
| 615 |
1 |
▶ | RETVAL = self->bias; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->bias; |
0 |
1 |
1 |
|
| 616 |
|
| OUTPUT: |
| 617 |
|
| RETVAL |
| 618 |
|
| |
| 619 |
|
| int |
| 620 |
|
| ll_data_set_size(self) |
| 621 |
|
| struct problem *self; |
| 622 |
|
| CODE: |
| 623 |
1 |
▶ | RETVAL = self->l; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->l; |
0 |
1 |
1 |
|
| 624 |
|
| OUTPUT: |
| 625 |
|
| RETVAL |
| 626 |
|
| |
| 627 |
|
| int |
| 628 |
|
| ll_num_features(self) |
| 629 |
|
| struct problem *self; |
| 630 |
|
| CODE: |
| 631 |
1 |
▶ | RETVAL = self->n; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->n; |
0 |
1 |
1 |
|
| 632 |
|
| OUTPUT: |
| 633 |
|
| RETVAL |
| 634 |
|
| |
| 635 |
|
| void |
| 636 |
|
| ll_DESTROY(self) |
| 637 |
|
| struct problem *self; |
| 638 |
|
| CODE: |
| 639 |
13 |
| free_problem(aTHX_ self); |