| 303 | } |
| 304 | |
| 305 | void sigmoidTrain(const float_type *decValues, const int l, const vector<int> &labels, float_type &A, |
| 306 | float_type &B) { |
| 307 | double prior1 = 0, prior0 = 0; |
| 308 | int i; |
| 309 | |
| 310 | for (i = 0; i < l; i++) |
| 311 | if (labels[i] > 0) |
| 312 | prior1 += 1; |
| 313 | else |
| 314 | prior0 += 1; |
| 315 | |
| 316 | int max_iter = 100; // Maximal number of iterations |
| 317 | double min_step = 1e-10; // Minimal step taken in line search |
| 318 | double sigma = 1e-12; // For numerically strict PD of Hessian |
| 319 | double eps = 1e-5; |
| 320 | double hiTarget = (prior1 + 1.0) / (prior1 + 2.0); |
| 321 | double loTarget = 1 / (prior0 + 2.0); |
| 322 | double *t = (double *) malloc(sizeof(double) * l); |
| 323 | double fApB, p, q, h11, h22, h21, g1, g2, det, dA, dB, gd, stepsize; |
| 324 | double newA, newB, newf, d1, d2; |
| 325 | int iter; |
| 326 | |
| 327 | // Initial Point and Initial Fun Value |
| 328 | A = 0.0; |
| 329 | B = log((prior0 + 1.0) / (prior1 + 1.0)); |
| 330 | double fval = 0.0; |
| 331 | |
| 332 | for (i = 0; i < l; i++) { |
| 333 | if (labels[i] > 0) |
| 334 | t[i] = hiTarget; |
| 335 | else |
| 336 | t[i] = loTarget; |
| 337 | fApB = decValues[i] * A + B; |
| 338 | if (fApB >= 0) |
| 339 | fval += t[i] * fApB + log(1 + exp(-fApB)); |
| 340 | else |
| 341 | fval += (t[i] - 1) * fApB + log(1 + exp(fApB)); |
| 342 | } |
| 343 | for (iter = 0; iter < max_iter; iter++) { |
| 344 | // Update Gradient and Hessian (use H' = H + sigma I) |
| 345 | h11 = sigma; // numerically ensures strict PD |
| 346 | h22 = sigma; |
| 347 | h21 = 0.0; |
| 348 | g1 = 0.0; |
| 349 | g2 = 0.0; |
| 350 | for (i = 0; i < l; i++) { |
| 351 | fApB = decValues[i] * A + B; |
| 352 | if (fApB >= 0) { |
| 353 | p = exp(-fApB) / (1.0 + exp(-fApB)); |
| 354 | q = 1.0 / (1.0 + exp(-fApB)); |
| 355 | } else { |
| 356 | p = 1.0 / (1.0 + exp(fApB)); |
| 357 | q = exp(fApB) / (1.0 + exp(fApB)); |
| 358 | } |
| 359 | d2 = p * q; |
| 360 | h11 += decValues[i] * decValues[i] * d2; |
| 361 | h22 += d2; |
| 362 | h21 += decValues[i] * d2; |