61 fLogger( new
MsgLogger(
"SVWorkingSet", kINFO ) )
70 : fdoRegression(doreg),
71 fInputData(inputVectors),
73 fKFunction(kernelFunction),
79 fLogger( new
MsgLogger(
"SVWorkingSet", kINFO ) )
122 if (fKMatrix != 0) {
delete fKMatrix; fKMatrix = 0;}
136 std::vector<TMVA::SVEvent*>::iterator idIter;
139 for(idIter = fInputData->begin(); idIter != fInputData->end(); ++idIter){
140 if((*idIter)->GetAlpha()>0)
141 fErrorC_J += (*idIter)->GetAlpha()*(*idIter)->GetTypeFlag()*fKVals[k];
149 if((jevt->
GetIdx() == 1) && (fErrorC_J < fB_up )){
153 else if ((jevt->
GetIdx() == -1)&&(fErrorC_J > fB_low)) {
160 if((jevt->
GetIdx()>=0) && (fB_low - fErrorC_J > 2*fTolerance)) {
165 if((jevt->
GetIdx()<=0) && (fErrorC_J - fB_up > 2*fTolerance)) {
170 if (converged)
return kFALSE;
173 if(fB_low - fErrorC_J > fErrorC_J - fB_up) ievt = fTEventLow;
174 else ievt = fTEventUp;
177 if (TakeStep(ievt, jevt))
return kTRUE;
186 if (ievt == jevt)
return kFALSE;
187 std::vector<TMVA::SVEvent*>::iterator idIter;
194 Float_t newAlpha_I, newAlpha_J;
208 s =
Int_t( type_I * type_J );
216 if (type_I == type_J) {
217 Float_t gamma = alpha_I + alpha_J;
247 Float_t gamma = alpha_I - alpha_J;
250 if ( gamma >= (c_i - c_j) )
257 if ( (c_i - c_j) >= gamma)
265 Float_t kernel_II, kernel_IJ, kernel_JJ;
267 kernel_II = fKMatrix->GetElement(ievt->
GetNs(),ievt->
GetNs());
268 kernel_IJ = fKMatrix->GetElement(ievt->
GetNs(), jevt->
GetNs());
269 kernel_JJ = fKMatrix->GetElement(jevt->
GetNs(),jevt->
GetNs());
271 eta = 2*kernel_IJ - kernel_II - kernel_JJ;
273 newAlpha_J = alpha_J + (type_J*( errorC_J - errorC_I ))/eta;
274 if (newAlpha_J <
l) newAlpha_J =
l;
275 else if (newAlpha_J >
h) newAlpha_J =
h;
282 Float_t c_J = type_J*( errorC_I - errorC_J ) - eta * alpha_J;
283 lobj = c_I *
l *
l + c_J *
l;
284 hobj = c_I *
h *
h + c_J *
h;
286 if (lobj > hobj +
epsilon) newAlpha_J =
l;
287 else if (lobj < hobj -
epsilon) newAlpha_J =
h;
288 else newAlpha_J = alpha_J;
295 newAlpha_I = alpha_I - s*( newAlpha_J - alpha_J );
297 if (newAlpha_I < 0) {
298 newAlpha_J += s* newAlpha_I;
301 else if (newAlpha_I > c_i) {
302 Float_t temp = newAlpha_I - c_i;
303 newAlpha_J += s * temp;
307 Float_t dL_I = type_I * ( newAlpha_I - alpha_I );
308 Float_t dL_J = type_J * ( newAlpha_J - alpha_J );
311 for(idIter = fInputData->begin(); idIter != fInputData->end(); ++idIter){
313 if((*idIter)->GetIdx()==0){
314 Float_t ii = fKMatrix->GetElement(ievt->
GetNs(), (*idIter)->GetNs());
315 Float_t jj = fKMatrix->GetElement(jevt->
GetNs(), (*idIter)->GetNs());
317 (*idIter)->UpdateErrorCache(dL_I * ii + dL_J * jj);
327 ievt->
SetErrorCache(errorC_I + dL_I*kernel_II + dL_J*kernel_IJ);
328 jevt->
SetErrorCache(errorC_J + dL_I*kernel_IJ + dL_J*kernel_JJ);
335 for(idIter = fInputData->begin(); idIter != fInputData->end(); ++idIter){
336 if((*idIter)->GetIdx()==0){
337 if((*idIter)->GetErrorCache()> fB_low){
338 fB_low = (*idIter)->GetErrorCache();
339 fTEventLow = (*idIter);
341 if( (*idIter)->GetErrorCache()< fB_up){
342 fB_up =(*idIter)->GetErrorCache();
343 fTEventUp = (*idIter);
377 if((fB_up > fB_low - 2*fTolerance))
return kTRUE;
387 Int_t numChanged = 0;
388 Int_t examineAll = 1;
391 Int_t deltaChanges = 0;
394 std::vector<TMVA::SVEvent*>::iterator idIter;
396 while ((numChanged > 0) || (examineAll > 0)) {
397 if (fIPyCurrentIter) *fIPyCurrentIter = numit;
398 if (fExitFromTraining && *fExitFromTraining)
break;
401 for (idIter = fInputData->begin(); idIter!=fInputData->end(); ++idIter){
402 if(!fdoRegression) numChanged += (
UInt_t)ExamineExample(*idIter);
403 else numChanged += (
UInt_t)ExamineExampleReg(*idIter);
407 for (idIter = fInputData->begin(); idIter!=fInputData->end(); ++idIter) {
408 if ((*idIter)->IsInI0()) {
409 if(!fdoRegression) numChanged += (
UInt_t)ExamineExample(*idIter);
410 else numChanged += (
UInt_t)ExamineExampleReg(*idIter);
419 if (examineAll == 1) examineAll = 0;
420 else if (numChanged == 0 || numChanged < 10 || deltaChanges > 3 ) examineAll = 1;
422 if (numChanged == numChangedOld) deltaChanges++;
423 else deltaChanges = 0;
424 numChangedOld = numChanged;
427 if (numit >= nMaxIter) {
429 <<
"Max number of iterations exceeded. "
430 <<
"Training may not be completed. Try use less Cost parameter" <<
Endl;
461 std::vector<TMVA::SVEvent*>::iterator idIter;
463 for( idIter = fInputData->begin(); idIter != fInputData->end(); ++idIter)
464 if((*idIter)->GetAlpha() !=0) counter++;
471 std::vector<TMVA::SVEvent*>::iterator idIter;
472 if( fSupVec != 0) {
delete fSupVec; fSupVec = 0; }
473 fSupVec =
new std::vector<TMVA::SVEvent*>(0);
475 for( idIter = fInputData->begin(); idIter != fInputData->end(); ++idIter){
476 if((*idIter)->GetDeltaAlpha() !=0){
477 fSupVec->push_back((*idIter));
487 if (ievt == jevt)
return kFALSE;
488 std::vector<TMVA::SVEvent*>::iterator idIter;
496 const Float_t eta = -2*kernel_IJ + kernel_II + kernel_JJ;
502 Bool_t caseA, caseB, caseC, caseD, terminated;
503 caseA = caseB = caseC = caseD = terminated =
kFALSE;
504 Float_t b_alpha_i, b_alpha_j, b_alpha_i_p, b_alpha_j_p;
520 Float_t tmp_alpha_i, tmp_alpha_j;
521 tmp_alpha_i = tmp_alpha_j = 0.;
524 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)))
531 tmp_alpha_j = b_alpha_j - (deltafi/eta);
534 tmp_alpha_i = b_alpha_i - (tmp_alpha_j - b_alpha_j);
537 if( IsDiffSignificant(b_alpha_j,tmp_alpha_j,
epsilon) || IsDiffSignificant(b_alpha_i,tmp_alpha_i,
epsilon)){
538 b_alpha_j = tmp_alpha_j;
539 b_alpha_i = tmp_alpha_i;
548 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)))
552 high =
TMath::Min( b_cost_i , b_cost_j + gamma);
556 tmp_alpha_j = b_alpha_j_p - ((deltafi-2*
epsilon)/eta);
559 tmp_alpha_i = b_alpha_i - (tmp_alpha_j - b_alpha_j_p);
562 if( IsDiffSignificant(b_alpha_j_p,tmp_alpha_j,
epsilon) || IsDiffSignificant(b_alpha_i,tmp_alpha_i,
epsilon)){
563 b_alpha_j_p = tmp_alpha_j;
564 b_alpha_i = tmp_alpha_i;
572 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)))
579 tmp_alpha_j = b_alpha_j - ((deltafi+2*
epsilon)/eta);
582 tmp_alpha_i = b_alpha_i_p - (tmp_alpha_j - b_alpha_j);
585 if( IsDiffSignificant(b_alpha_j,tmp_alpha_j,
epsilon) || IsDiffSignificant(b_alpha_i_p,tmp_alpha_i,
epsilon)){
586 b_alpha_j = tmp_alpha_j;
587 b_alpha_i_p = tmp_alpha_i;
595 else if((caseD ==
kFALSE) &&
596 (b_alpha_i_p>0 || (b_alpha_i==0 && deltafi <0 )) &&
597 (b_alpha_j_p>0 || (b_alpha_j==0 && deltafi >0 )))
604 tmp_alpha_j = b_alpha_j_p + (deltafi/eta);
607 tmp_alpha_i = b_alpha_i_p - (tmp_alpha_j - b_alpha_j_p);
609 if( IsDiffSignificant(b_alpha_j_p,tmp_alpha_j,
epsilon) || IsDiffSignificant(b_alpha_i_p,tmp_alpha_i,
epsilon)){
610 b_alpha_j_p = tmp_alpha_j;
611 b_alpha_i_p = tmp_alpha_i;
637 for(idIter = fInputData->begin(); idIter != fInputData->end(); ++idIter){
640 if((*idIter)->GetIdx()==0){
641 Float_t k_ii = fKMatrix->GetElement(ievt->
GetNs(), (*idIter)->GetNs());
642 Float_t k_jj = fKMatrix->GetElement(jevt->
GetNs(), (*idIter)->GetNs());
644 (*idIter)->UpdateErrorCache(diff_alpha_i * k_ii + diff_alpha_j * k_jj);
661 for(idIter = fInputData->begin(); idIter != fInputData->end(); ++idIter){
662 if((!(*idIter)->IsInI3()) && ((*idIter)->GetErrorCache()> fB_low)){
663 fB_low = (*idIter)->GetErrorCache();
664 fTEventLow = (*idIter);
667 if((!(*idIter)->IsInI2()) && ((*idIter)->GetErrorCache()< fB_up)){
668 fB_up =(*idIter)->GetErrorCache();
669 fTEventUp = (*idIter);
690 std::vector<TMVA::SVEvent*>::iterator idIter;
693 for(idIter = fInputData->begin(); idIter != fInputData->end(); ++idIter){
694 fErrorC_J -= (*idIter)->GetDeltaAlpha()*fKVals[k];
702 if(fErrorC_J + feps < fB_up ){
703 fB_up = fErrorC_J + feps;
706 else if(fErrorC_J -feps > fB_low) {
707 fB_low = fErrorC_J - feps;
710 }
else if((jevt->
IsInI2()) && (fErrorC_J + feps > fB_low)){
711 fB_low = fErrorC_J + feps;
713 }
else if((jevt->
IsInI3()) && (fErrorC_J - feps < fB_up)){
714 fB_up = fErrorC_J - feps;
722 if( fB_low -fErrorC_J + feps > 2*fTolerance){
725 if(fErrorC_J-feps-fB_up > fB_low-fErrorC_J+feps){
728 }
else if(fErrorC_J -feps - fB_up > 2*fTolerance){
731 if(fB_low - fErrorC_J+feps > fErrorC_J-feps -fB_up){
739 if( fB_low -fErrorC_J - feps > 2*fTolerance){
742 if(fErrorC_J+feps-fB_up > fB_low-fErrorC_J-feps){
745 }
else if(fErrorC_J + feps - fB_up > 2*fTolerance){
748 if(fB_low - fErrorC_J-feps > fErrorC_J+feps -fB_up){
756 if( fB_low -fErrorC_J - feps > 2*fTolerance){
759 if(fErrorC_J+feps-fB_up > fB_low-fErrorC_J-feps){
762 }
else if(fErrorC_J - feps - fB_up > 2*fTolerance){
765 if(fB_low - fErrorC_J+feps > fErrorC_J-feps -fB_up){
773 if( fErrorC_J + feps -fB_up > 2*fTolerance){
781 if(fB_low -fErrorC_J +feps > 2*fTolerance){
787 if(converged)
return kFALSE;
788 if (TakeStepReg(ievt, jevt))
return kTRUE;
ostringstream derivative to redirect and format output
Event class for Support Vector Machine.
Float_t GetTarget() const
Float_t GetErrorCache() const
Float_t GetCweight() const
Float_t * GetLine() const
void SetAlpha_p(Float_t alpha)
Float_t GetAlpha_p() const
void SetAlpha(Float_t alpha)
Int_t GetTypeFlag() const
void SetErrorCache(Float_t err_cache)
Float_t GetDeltaAlpha() const
Kernel for Support Vector Machine.
Kernel matrix for Support Vector Machine.
Float_t * GetLine(UInt_t)
returns a row of the kernel matrix
Bool_t TakeStep(SVEvent *, SVEvent *)
void Train(UInt_t nIter=1000)
train the SVM
Bool_t IsDiffSignificant(Float_t, Float_t, Float_t)
Bool_t ExamineExample(SVEvent *)
Bool_t ExamineExampleReg(SVEvent *)
Bool_t TakeStepReg(SVEvent *, SVEvent *)
void SetIndex(TMVA::SVEvent *)
~SVWorkingSet()
destructor
SVWorkingSet()
constructor
std::vector< TMVA::SVEvent * > * fInputData
SVKernelMatrix * fKMatrix
std::vector< TMVA::SVEvent * > * GetSupportVectors()
Random number generator class based on M.
virtual UInt_t Integer(UInt_t imax)
Returns a random integer uniformly distributed on the interval [ 0, imax-1 ].
MsgLogger & Endl(MsgLogger &ml)
Short_t Max(Short_t a, Short_t b)
Short_t Min(Short_t a, Short_t b)