Definition at line 1541 of file svm.cpp. References info(), Malloc, and Impala::Application::DemoCamera2d::sigma. 01544 { 01545 double prior1=0, prior0 = 0; 01546 int i; 01547 01548 for (i=0;i<l;i++) 01549 if (labels[i] > 0) prior1+=1; 01550 else prior0+=1; 01551 01552 int max_iter=100; // Maximal number of iterations 01553 double min_step=1e-10; // Minimal step taken in line search 01554 double sigma=1e-3; // For numerically strict PD of Hessian 01555 double eps=1e-5; 01556 double hiTarget=(prior1+1.0)/(prior1+2.0); 01557 double loTarget=1/(prior0+2.0); 01558 double *t=Malloc(double,l); 01559 double fApB,p,q,h11,h22,h21,g1,g2,det,dA,dB,gd,stepsize; 01560 double newA,newB,newf,d1,d2; 01561 int iter; 01562 01563 // Initial Point and Initial Fun Value 01564 A=0.0; B=log((prior0+1.0)/(prior1+1.0)); 01565 double fval = 0.0; 01566 01567 for (i=0;i<l;i++) 01568 { 01569 if (labels[i]>0) t[i]=hiTarget; 01570 else t[i]=loTarget; 01571 fApB = dec_values[i]*A+B; 01572 if (fApB>=0) 01573 fval += t[i]*fApB + log(1+exp(-fApB)); 01574 else 01575 fval += (t[i] - 1)*fApB +log(1+exp(fApB)); 01576 } 01577 for (iter=0;iter<max_iter;iter++) 01578 { 01579 // Update Gradient and Hessian (use H' = H + sigma I) 01580 h11=sigma; // numerically ensures strict PD 01581 h22=sigma; 01582 h21=0.0;g1=0.0;g2=0.0; 01583 for (i=0;i<l;i++) 01584 { 01585 fApB = dec_values[i]*A+B; 01586 if (fApB >= 0) 01587 { 01588 p=exp(-fApB)/(1.0+exp(-fApB)); 01589 q=1.0/(1.0+exp(-fApB)); 01590 } 01591 else 01592 { 01593 p=1.0/(1.0+exp(fApB)); 01594 q=exp(fApB)/(1.0+exp(fApB)); 01595 } 01596 d2=p*q; 01597 h11+=dec_values[i]*dec_values[i]*d2; 01598 h22+=d2; 01599 h21+=dec_values[i]*d2; 01600 d1=t[i]-p; 01601 g1+=dec_values[i]*d1; 01602 g2+=d1; 01603 } 01604 01605 // Stopping Criteria 01606 if (fabs(g1)<eps && fabs(g2)<eps) 01607 break; 01608 01609 // Finding Newton direction: -inv(H') * g 01610 det=h11*h22-h21*h21; 01611 dA=-(h22*g1 - h21 * g2) / det; 01612 dB=-(-h21*g1+ h11 * g2) / det; 01613 gd=g1*dA+g2*dB; 01614 01615 01616 stepsize = 1; // Line Search 01617 while (stepsize >= min_step) 01618 { 01619 newA = A + stepsize * dA; 01620 newB = B + stepsize * dB; 01621 01622 // New function value 01623 newf = 0.0; 01624 for (i=0;i<l;i++) 01625 { 01626 fApB = dec_values[i]*newA+newB; 01627 if (fApB >= 0) 01628 newf += t[i]*fApB + log(1+exp(-fApB)); 01629 else 01630 newf += (t[i] - 1)*fApB +log(1+exp(fApB)); 01631 } 01632 // Check sufficient decrease 01633 if (newf<fval+0.0001*stepsize*gd) 01634 { 01635 A=newA;B=newB;fval=newf; 01636 break; 01637 } 01638 else 01639 stepsize = stepsize / 2.0; 01640 } 01641 01642 if (stepsize < min_step) 01643 { 01644 info("Line search fails in two-class probability estimates\n"); 01645 break; 01646 } 01647 } 01648 01649 if (iter>=max_iter) 01650 info("Reaching maximal iterations in two-class probability estimates\n"); 01651 free(t); 01652 }
Here is the call graph for this function:
|