31 #ifndef ROOT_TMVA_MsgLogger
65 : fdoRegression(doreg),
66 fInputData(inputVectors),
68 fKFunction(kernelFunction),
117 if (fKMatrix != 0) {
delete fKMatrix; fKMatrix = 0;}
131 std::vector<TMVA::SVEvent*>::iterator idIter;
134 for(idIter = fInputData->begin(); idIter != fInputData->end(); idIter++){
135 if((*idIter)->GetAlpha()>0)
136 fErrorC_J += (*idIter)->GetAlpha()*(*idIter)->GetTypeFlag()*fKVals[k];
144 if((jevt->
GetIdx() == 1) && (fErrorC_J < fB_up )){
148 else if ((jevt->
GetIdx() == -1)&&(fErrorC_J > fB_low)) {
155 if((jevt->
GetIdx()>=0) && (fB_low - fErrorC_J > 2*fTolerance)) {
160 if((jevt->
GetIdx()<=0) && (fErrorC_J - fB_up > 2*fTolerance)) {
165 if (converged)
return kFALSE;
168 if(fB_low - fErrorC_J > fErrorC_J - fB_up) ievt = fTEventLow;
169 else ievt = fTEventUp;
172 if (TakeStep(ievt, jevt))
return kTRUE;
181 if (ievt == jevt)
return kFALSE;
182 std::vector<TMVA::SVEvent*>::iterator idIter;
189 Float_t newAlpha_I, newAlpha_J;
203 s =
Int_t( type_I * type_J );
211 if (type_I == type_J) {
245 if ( gamma >= (c_i - c_j) )
252 if ( (c_i - c_j) >= gamma)
259 if (l == h)
return kFALSE;
260 Float_t kernel_II, kernel_IJ, kernel_JJ;
262 kernel_II = fKMatrix->GetElement(ievt->
GetNs(),ievt->
GetNs());
263 kernel_IJ = fKMatrix->GetElement(ievt->
GetNs(), jevt->
GetNs());
264 kernel_JJ = fKMatrix->GetElement(jevt->
GetNs(),jevt->
GetNs());
266 eta = 2*kernel_IJ - kernel_II - kernel_JJ;
268 newAlpha_J = alpha_J + (type_J*( errorC_J - errorC_I ))/eta;
269 if (newAlpha_J < l) newAlpha_J =
l;
270 else if (newAlpha_J > h) newAlpha_J =
h;
277 Float_t c_J = type_J*( errorC_I - errorC_J ) - eta * alpha_J;
278 lobj = c_I * l * l + c_J *
l;
279 hobj = c_I * h * h + c_J *
h;
281 if (lobj > hobj + epsilon) newAlpha_J =
l;
282 else if (lobj < hobj - epsilon) newAlpha_J =
h;
283 else newAlpha_J = alpha_J;
286 if (
TMath::Abs( newAlpha_J - alpha_J ) < ( epsilon * ( newAlpha_J + alpha_J+
epsilon ))){
290 newAlpha_I = alpha_I - s*( newAlpha_J - alpha_J );
292 if (newAlpha_I < 0) {
293 newAlpha_J += s* newAlpha_I;
296 else if (newAlpha_I > c_i) {
297 Float_t temp = newAlpha_I - c_i;
298 newAlpha_J += s * temp;
302 Float_t dL_I = type_I * ( newAlpha_I - alpha_I );
303 Float_t dL_J = type_J * ( newAlpha_J - alpha_J );
306 for(idIter = fInputData->begin(); idIter != fInputData->end(); idIter++){
308 if((*idIter)->GetIdx()==0){
309 Float_t ii = fKMatrix->GetElement(ievt->
GetNs(), (*idIter)->GetNs());
310 Float_t jj = fKMatrix->GetElement(jevt->
GetNs(), (*idIter)->GetNs());
312 (*idIter)->UpdateErrorCache(dL_I * ii + dL_J * jj);
322 ievt->
SetErrorCache(errorC_I + dL_I*kernel_II + dL_J*kernel_IJ);
323 jevt->
SetErrorCache(errorC_J + dL_I*kernel_IJ + dL_J*kernel_JJ);
330 for(idIter = fInputData->begin(); idIter != fInputData->end(); idIter++){
331 if((*idIter)->GetIdx()==0){
332 if((*idIter)->GetErrorCache()> fB_low){
333 fB_low = (*idIter)->GetErrorCache();
334 fTEventLow = (*idIter);
336 if( (*idIter)->GetErrorCache()< fB_up){
337 fB_up =(*idIter)->GetErrorCache();
338 fTEventUp = (*idIter);
372 if((fB_up > fB_low - 2*fTolerance))
return kTRUE;
382 Int_t numChanged = 0;
383 Int_t examineAll = 1;
386 Int_t deltaChanges = 0;
389 std::vector<TMVA::SVEvent*>::iterator idIter;
391 while ((numChanged > 0) || (examineAll > 0)) {
394 for (idIter = fInputData->begin(); idIter!=fInputData->end(); idIter++){
395 if(!fdoRegression) numChanged += (
UInt_t)ExamineExample(*idIter);
396 else numChanged += (
UInt_t)ExamineExampleReg(*idIter);
400 for (idIter = fInputData->begin(); idIter!=fInputData->end(); idIter++) {
401 if ((*idIter)->IsInI0()) {
402 if(!fdoRegression) numChanged += (
UInt_t)ExamineExample(*idIter);
403 else numChanged += (
UInt_t)ExamineExampleReg(*idIter);
412 if (examineAll == 1) examineAll = 0;
413 else if (numChanged == 0 || numChanged < 10 || deltaChanges > 3 ) examineAll = 1;
415 if (numChanged == numChangedOld) deltaChanges++;
416 else deltaChanges = 0;
417 numChangedOld = numChanged;
420 if (numit >= nMaxIter) {
422 <<
"Max number of iterations exceeded. "
423 <<
"Training may not be completed. Try use less Cost parameter" <<
Endl;
439 else if( event->
GetAlpha() ==
event->GetCweight() )
445 else if( event->
GetAlpha() ==
event->GetCweight() )
454 std::vector<TMVA::SVEvent*>::iterator idIter;
456 for( idIter = fInputData->begin(); idIter != fInputData->end(); idIter++)
457 if((*idIter)->GetAlpha() !=0) counter++;
464 std::vector<TMVA::SVEvent*>::iterator idIter;
465 if( fSupVec != 0) {
delete fSupVec; fSupVec = 0; }
466 fSupVec =
new std::vector<TMVA::SVEvent*>(0);
468 for( idIter = fInputData->begin(); idIter != fInputData->end(); idIter++){
469 if((*idIter)->GetDeltaAlpha() !=0){
470 fSupVec->push_back((*idIter));
480 if (ievt == jevt)
return kFALSE;
481 std::vector<TMVA::SVEvent*>::iterator idIter;
489 const Float_t eta = -2*kernel_IJ + kernel_II + kernel_JJ;
495 Bool_t caseA, caseB, caseC, caseD, terminated;
496 caseA = caseB = caseC = caseD = terminated =
kFALSE;
497 Float_t b_alpha_i, b_alpha_j, b_alpha_i_p, b_alpha_j_p;
513 Float_t tmp_alpha_i, tmp_alpha_j;
514 tmp_alpha_i = tmp_alpha_j = 0.;
517 if((caseA ==
kFALSE) && (b_alpha_i > 0 || (b_alpha_i_p == 0 && deltafi > 0)) && (b_alpha_j > 0 || (b_alpha_j_p == 0 && deltafi < 0)))
524 tmp_alpha_j = b_alpha_j - (deltafi/eta);
527 tmp_alpha_i = b_alpha_i - (tmp_alpha_j - b_alpha_j);
530 if( IsDiffSignificant(b_alpha_j,tmp_alpha_j, epsilon) || IsDiffSignificant(b_alpha_i,tmp_alpha_i, epsilon)){
531 b_alpha_j = tmp_alpha_j;
532 b_alpha_i = tmp_alpha_i;
541 else if((caseB==
kFALSE) && (b_alpha_i>0 || (b_alpha_i_p==0 && deltafi >2*
epsilon )) && (b_alpha_j_p>0 || (b_alpha_j==0 && deltafi>2*
epsilon)))
545 high =
TMath::Min( b_cost_i , b_cost_j + gamma);
549 tmp_alpha_j = b_alpha_j_p - ((deltafi-2*
epsilon)/eta);
552 tmp_alpha_i = b_alpha_i - (tmp_alpha_j - b_alpha_j_p);
555 if( IsDiffSignificant(b_alpha_j_p,tmp_alpha_j, epsilon) || IsDiffSignificant(b_alpha_i,tmp_alpha_i, epsilon)){
556 b_alpha_j_p = tmp_alpha_j;
557 b_alpha_i = tmp_alpha_i;
565 else if((caseC==
kFALSE) && (b_alpha_i_p>0 || (b_alpha_i==0 && deltafi < -2*
epsilon )) && (b_alpha_j>0 || (b_alpha_j_p==0 && deltafi< -2*
epsilon)))
572 tmp_alpha_j = b_alpha_j - ((deltafi+2*
epsilon)/eta);
575 tmp_alpha_i = b_alpha_i_p - (tmp_alpha_j - b_alpha_j);
578 if( IsDiffSignificant(b_alpha_j,tmp_alpha_j, epsilon) || IsDiffSignificant(b_alpha_i_p,tmp_alpha_i, epsilon)){
579 b_alpha_j = tmp_alpha_j;
580 b_alpha_i_p = tmp_alpha_i;
588 else if((caseD ==
kFALSE) &&
589 (b_alpha_i_p>0 || (b_alpha_i==0 && deltafi <0 )) &&
590 (b_alpha_j_p>0 || (b_alpha_j==0 && deltafi >0 )))
597 tmp_alpha_j = b_alpha_j_p + (deltafi/eta);
600 tmp_alpha_i = b_alpha_i_p - (tmp_alpha_j - b_alpha_j_p);
602 if( IsDiffSignificant(b_alpha_j_p,tmp_alpha_j, epsilon) || IsDiffSignificant(b_alpha_i_p,tmp_alpha_i, epsilon)){
603 b_alpha_j_p = tmp_alpha_j;
604 b_alpha_i_p = tmp_alpha_i;
630 for(idIter = fInputData->begin(); idIter != fInputData->end(); idIter++){
633 if((*idIter)->GetIdx()==0){
634 Float_t k_ii = fKMatrix->GetElement(ievt->
GetNs(), (*idIter)->GetNs());
635 Float_t k_jj = fKMatrix->GetElement(jevt->
GetNs(), (*idIter)->GetNs());
637 (*idIter)->UpdateErrorCache(diff_alpha_i * k_ii + diff_alpha_j * k_jj);
654 for(idIter = fInputData->begin(); idIter != fInputData->end(); idIter++){
655 if((!(*idIter)->IsInI3()) && ((*idIter)->GetErrorCache()> fB_low)){
656 fB_low = (*idIter)->GetErrorCache();
657 fTEventLow = (*idIter);
660 if((!(*idIter)->IsInI2()) && ((*idIter)->GetErrorCache()< fB_up)){
661 fB_up =(*idIter)->GetErrorCache();
662 fTEventUp = (*idIter);
683 std::vector<TMVA::SVEvent*>::iterator idIter;
686 for(idIter = fInputData->begin(); idIter != fInputData->end(); idIter++){
687 fErrorC_J -= (*idIter)->GetDeltaAlpha()*fKVals[k];
695 if(fErrorC_J + feps < fB_up ){
696 fB_up = fErrorC_J + feps;
699 else if(fErrorC_J -feps > fB_low) {
700 fB_low = fErrorC_J - feps;
703 }
else if((jevt->
IsInI2()) && (fErrorC_J + feps > fB_low)){
704 fB_low = fErrorC_J + feps;
706 }
else if((jevt->
IsInI3()) && (fErrorC_J - feps < fB_up)){
707 fB_up = fErrorC_J - feps;
715 if( fB_low -fErrorC_J + feps > 2*fTolerance){
718 if(fErrorC_J-feps-fB_up > fB_low-fErrorC_J+feps){
721 }
else if(fErrorC_J -feps - fB_up > 2*fTolerance){
724 if(fB_low - fErrorC_J+feps > fErrorC_J-feps -fB_up){
732 if( fB_low -fErrorC_J - feps > 2*fTolerance){
735 if(fErrorC_J+feps-fB_up > fB_low-fErrorC_J-feps){
738 }
else if(fErrorC_J + feps - fB_up > 2*fTolerance){
741 if(fB_low - fErrorC_J-feps > fErrorC_J+feps -fB_up){
749 if( fB_low -fErrorC_J - feps > 2*fTolerance){
752 if(fErrorC_J+feps-fB_up > fB_low-fErrorC_J-feps){
755 }
else if(fErrorC_J - feps - fB_up > 2*fTolerance){
758 if(fB_low - fErrorC_J+feps > fErrorC_J-feps -fB_up){
766 if( fErrorC_J + feps -fB_up > 2*fTolerance){
774 if(fB_low -fErrorC_J +feps > 2*fTolerance){
780 if(converged)
return kFALSE;
781 if (TakeStepReg(ievt, jevt))
return kTRUE;
Random number generator class based on M.
MsgLogger & Endl(MsgLogger &ml)
Float_t * GetLine(UInt_t)
returns a row of the kernel matrix
void SetAlpha(Float_t alpha)
~SVWorkingSet()
destructor
SVWorkingSet()
constructor
void SetIndex(TMVA::SVEvent *)
message logger
void Train(UInt_t nIter=1000)
train the SVM
Short_t Min(Short_t a, Short_t b)
Bool_t TakeStep(SVEvent *, SVEvent *)
Float_t GetAlpha_p() const
void SetErrorCache(Float_t err_cache)
Float_t GetTarget() const
virtual UInt_t Integer(UInt_t imax)
Returns a random integer on [ 0, imax-1 ].
Float_t GetCweight() const
Bool_t ExamineExampleReg(SVEvent *)
Int_t GetTypeFlag() const
Float_t * GetLine() const
Bool_t ExamineExample(SVEvent *)
Float_t GetErrorCache() const
Bool_t TakeStepReg(SVEvent *, SVEvent *)
std::vector< TMVA::SVEvent * > * GetSupportVectors()
Float_t GetDeltaAlpha() const
SVKernelMatrix * fKMatrix
Bool_t IsDiffSignificant(Float_t, Float_t, Float_t)
Short_t Max(Short_t a, Short_t b)
void SetAlpha_p(Float_t alpha)
std::vector< TMVA::SVEvent * > * fInputData