⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 variations.cpp

📁 在线支持向量机C++程序,程序中包含了应用的例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				(*MinIndex) = 0;
				 break;
			case 3:
				(*MinIndex) = MinLsIndex;
				break;
			case 4:
				(*MinIndex) = MinLeIndex;
				break;
			case 5:
				(*MinIndex) = MinLrIndex;
				break;
		}

		// Clear
		delete Ls;
		delete Le;
		delete Lr;
		delete Variations;


		// PROVE
		static int Counts = 0;		
		if (*MinVariation == 0)
			Counts++;
		else
			Counts=0;
		if (Counts>=50){
			cout << "ERROR! Cycle found! (TRAINING)" << endl;
			this->SetVerbosity(OnlineSVR::VERBOSITY_DEBUG);
			system("pause");
		}
	}

	void OnlineSVR::FindUnlearningMinVariation (Vector<double>* H, Vector<double>* Beta, Vector<double>* Gamma, int SampleIndex, double* MinVariation, int* MinIndex, int* Flag)
	{

		// Find Samples Variations
		int Direction = SIGN(-this->Weights->Values[SampleIndex]);
		double Lc = this->FindVariationLc (SampleIndex);
		Vector<double>* Ls = this->FindVariationLs (H, Beta, Direction);
		Vector<double>* Le = this->FindVariationLe (H, Gamma, Direction);
		Vector<double>* Lr = this->FindVariationLr (H, Gamma, Direction);
		if (this->Verbosity>2) {
			this->ShowVariations(H, Beta, Gamma, SampleIndex, Lc, 0, Ls, Le, Lr, this->OPERATION_UNLEARNING);			
		}

		// Find Min Variation
		double MinLsValue, MinLeValue, MinLrValue;
		int MinLsIndex, MinLeIndex, MinLrIndex;
		if (this->GetSupportSetElementsNumber()>0)
			Ls->MinAbs (&MinLsValue, &MinLsIndex);
		else
			MinLsValue = Direction * INF;
		if (this->GetErrorSetElementsNumber()>0)
			Le->MinAbs (&MinLeValue, &MinLeIndex);
		else
			MinLeValue = Direction * INF;
		if (this->GetRemainingSetElementsNumber()>0)
			Lr->MinAbs (&MinLrValue, &MinLrIndex);
		else
			MinLrValue = Direction * INF;
		Vector<double>* Variations = new Vector<double>(5);
		Variations->Add(Lc);
		Variations->Add(Direction*INF);
		Variations->Add(MinLsValue);
		Variations->Add(MinLeValue);
		Variations->Add(MinLrValue);
		Variations->MinAbs(MinVariation, Flag);

		// Find Sample Index Variation
		(*MinVariation) *= Direction;
		(*Flag) ++;
		switch (*Flag) {
			case 1:
			case 2:
				(*MinIndex) = 0;
				 break;
			case 3:
				(*MinIndex) = MinLsIndex;
				break;
			case 4:
				(*MinIndex) = MinLeIndex;
				break;
			case 5:
				(*MinIndex) = MinLrIndex;
				break;
		}

		// Clear
		delete Ls;
		delete Le;
		delete Lr;
		delete Variations;

				// PROVE
		static int Counts = 0;		
		if (*MinVariation == 0)
			Counts++;
		else
			Counts=0;
		if (Counts>=10){
			//cout << "ERROR! Cycle found! (FORGET)" << endl;
			//system("pause");
		}

		if (*MinVariation == 0) {
			//cout << "ATTENTION" << endl;
			//system("pause");
		}

	}

		
	// Matrix R Operations
	void OnlineSVR::UpdateWeightsAndBias (Vector<double>** H, Vector<double>* Beta, Vector<double>* Gamma, int SampleIndex, double MinVariation)
	{

		// Update Weights and Bias
		if (this->GetSupportSetElementsNumber()>0) {

			// Update Weights New Sample
			this->Weights->Values[SampleIndex] += MinVariation;

			// Update Bias
			Vector<double>* DeltaWeights = Beta->Clone();
			DeltaWeights->ProductScalar(MinVariation);
			this->Bias += DeltaWeights->Values[0];
			
			// Update Weights Support Set
			int i;
			for (i=0; i<this->GetSupportSetElementsNumber(); i++) {
				this->Weights->Values[this->SupportSetIndexes->Values[i]] += DeltaWeights->Values[i+1];
			}
			delete DeltaWeights;
			
			// Update H
			Vector<double>* DeltaH = Gamma->Clone();
			DeltaH->ProductScalar(MinVariation);
 			for (i=0; i<this->GetSamplesTrainedNumber(); i++) {
				//if (this->SupportSetIndexes->Contains(i))
				//	cout << "S(" << i << ") before=" << (*H)->Values[i] << "   variation=" << DeltaH->Values[i] << "   Gamma = " << Gamma->Values[i] << endl;
				(*H)->Values[i] += DeltaH->Values[i];
			}
			delete DeltaH;
		}
		else {
			
			// Update Bias
			this->Bias += MinVariation;
			
			// Update H
			(*H)->SumScalar(MinVariation);
		}
	}

	void OnlineSVR::AddSampleToRemainingSet (int SampleIndex)
	{
		this->ShowMessage("> Case 0 : the sample has been classified correctly",2);
		this->RemainingSetIndexes->Add(SampleIndex);  
	}

		
	// Set Operations
	void OnlineSVR::AddSampleToSupportSet (Vector<double>** H, Vector<double>* Beta, Vector<double>* Gamma, int SampleIndex, double MinVariation)
	{
		// Message
		char Line[100];	
		sprintf(Line, "> Case 1 : sample %d is a support sample", SampleIndex);	
		for (int i=strlen(Line); i<62; i++)		
			Line[i] = ' ';
		Line[62] = 0;
		sprintf(Line, "%s(Var= %f)", Line, MinVariation);
		this->ShowMessage(Line,2);
		// Update H and Sets
		(*H)->Values[SampleIndex] = SIGN((*H)->Values[SampleIndex]) * this->Epsilon;
		this->SupportSetIndexes->Add (SampleIndex);
		this->AddSampleToR (SampleIndex, this->SUPPORT_SET, Beta, Gamma);
	}

	void OnlineSVR::AddSampleToErrorSet (int SampleIndex, double MinVariation)
	{
		// Message
		char Line[100];
		sprintf(Line, "> Case 2 : sample %d is an error sample", SampleIndex);
		for (int i=strlen(Line); i<62; i++)		
			Line[i] = ' ';
		Line[62] = 0;
		sprintf(Line, "%s(Var= %f)", Line, MinVariation);
		this->ShowMessage(Line,2);
		// Update H and Sets
		this->Weights->Values[SampleIndex] = SIGN(this->Weights->Values[SampleIndex]) * this->C;
		this->ErrorSetIndexes->Add(SampleIndex);
	}

	void OnlineSVR::MoveSampleFromSupportSetToErrorRemainingSet (int MinIndex, double MinVariation)
	{
		int Index = this->SupportSetIndexes->Values[MinIndex];
		double Weightsi = this->Weights->Values[Index];

		if (ABS(Weightsi)<ABS(this->C-ABS(Weightsi))) {
			this->Weights->Values[Index] = 0;
		}
		else {
			this->Weights->Values[Index] = SIGN(Weightsi) * this->C;
		}

		if (this->Weights->Values[Index] == 0) {
        
			// CASE 3a: Move Sample from SupportSet to RemainingSet                   	
			// Message
			char Line[100];
			sprintf(Line, "> Case 3a: move sample %d from support to remaining set", Index);
			for (int i=strlen(Line); i<62; i++)		
				Line[i] = ' ';
			Line[62] = 0;
			sprintf(Line, "%s(Var= %f)", Line, MinVariation);
			this->ShowMessage(Line,2);
			// Update H and Sets
			this->RemainingSetIndexes->Add(Index);
			this->SupportSetIndexes->RemoveAt(MinIndex);
			this->RemoveSampleFromR(MinIndex);
		}	
		else {
			// CASE 3b: Move Sample from SupportSet to ErrorSet            
			// Message
			char Line[100];
			sprintf(Line, "> Case 3b: move sample %d from support to error set", Index);
			for (int i=strlen(Line); i<62; i++)		
				Line[i] = ' ';
			Line[62] = 0;
			sprintf(Line, "%s(Var= %f)", Line, MinVariation);
			this->ShowMessage(Line,2);
			// Update H and Sets
			this->ErrorSetIndexes->Add(Index);
			this->SupportSetIndexes->RemoveAt(MinIndex);
			this->RemoveSampleFromR(MinIndex);
		}
	}

	void OnlineSVR::MoveSampleFromErrorSetToSupportSet (Vector<double>** H, Vector<double>* Beta, Vector<double>* Gamma, int MinIndex, double MinVariation)
	{
		int Index = this->ErrorSetIndexes->Values[MinIndex];
		// Message
		char Line[100];
		sprintf(Line, "> Case 4 : move sample %d from error to support set", Index);
		for (int i=strlen(Line); i<62; i++)		
			Line[i] = ' ';
		Line[62] = 0;
		sprintf(Line, "%s(Var= %f)", Line, MinVariation);
		this->ShowMessage(Line,2);
		// Update H and Sets	
		(*H)->Values[Index] = SIGN((*H)->Values[Index]) * this->Epsilon;
		this->SupportSetIndexes->Add(Index);
		this->ErrorSetIndexes->RemoveAt(MinIndex);
		this->AddSampleToR (Index, this->ERROR_SET, Beta, Gamma);
	}

	void OnlineSVR::MoveSampleFromRemainingSetToSupportSet (Vector<double>** H, Vector<double>* Beta, Vector<double>* Gamma, int MinIndex, double MinVariation)
	{
		int Index = this->RemainingSetIndexes->Values[MinIndex];
		// Message
		char Line[100];
		sprintf(Line, "> Case 5 : move sample %d from remaining to support set", Index);
		for (int i=strlen(Line); i<62; i++)		
			Line[i] = ' ';
		Line[62] = 0;
		sprintf(Line, "%s(Var= %f)", Line, MinVariation);
		this->ShowMessage(Line,2);
		// Update H and Sets	
		(*H)->Values[Index] = SIGN((*H)->Values[Index]) * this->Epsilon;
		this->SupportSetIndexes->Add(Index);
		this->RemainingSetIndexes->RemoveAt(MinIndex);
		this->AddSampleToR (Index, this->REMAINING_SET, Beta, Gamma);
	}

	void OnlineSVR::RemoveSampleFromSupportSet (int SampleSetIndex)
	{
		this->SupportSetIndexes->RemoveAt(SampleSetIndex);
		this->RemoveSampleFromR(SampleSetIndex);
	}

	void OnlineSVR::RemoveSampleFromErrorSet (int SampleSetIndex)
	{
		this->ErrorSetIndexes->RemoveAt(SampleSetIndex);
	}

	void OnlineSVR::RemoveSampleFromRemainingSet (int SampleSetIndex)
	{
		int SampleIndex = this->RemainingSetIndexes->Values[SampleSetIndex];
		this->ShowMessage("> Case 0 : the sample was removed from the remaining set",2);
		this->RemainingSetIndexes->RemoveAt(SampleSetIndex);
		this->X->RemoveRow(SampleIndex);
		this->Y->RemoveAt(SampleIndex);
		this->Weights->RemoveAt(SampleIndex);
		if (this->SaveKernelMatrix) {
			this->RemoveSampleFromKernelMatrix(SampleIndex);
		}
		int i;
		for (i=0; i<this->GetSupportSetElementsNumber(); i++) {
			if (this->SupportSetIndexes->Values[i]>SampleIndex) {
				this->SupportSetIndexes->Values[i] --;
			}
		}
		for (i=0; i<this->GetErrorSetElementsNumber(); i++) {
			if (this->ErrorSetIndexes->Values[i]>SampleIndex) {
				this->ErrorSetIndexes->Values[i] --;
			}
		}
		for (i=0; i<this->GetRemainingSetElementsNumber(); i++) {
			if (this->RemainingSetIndexes->Values[i]>SampleIndex) {
				this->RemainingSetIndexes->Values[i] --;
			}
		}
		this->SamplesTrainedNumber --;
		if (this->SamplesTrainedNumber==0) {
			this->Bias = 0;
		}
	}

	void OnlineSVR::RemoveSample (int SampleIndex)
	{
		this->ShowMessage("> Case 1 : the sample Weights becomes 0",2);	
		this->X->RemoveRow(SampleIndex);
		this->Y->RemoveAt(SampleIndex);
		this->Weights->RemoveAt(SampleIndex);
		if (this->SaveKernelMatrix) {
			this->RemoveSampleFromKernelMatrix(SampleIndex);
		}

		int i;
		for (i=0; i<this->GetSupportSetElementsNumber(); i++) {
			if (this->SupportSetIndexes->Values[i]>SampleIndex) {
				this->SupportSetIndexes->Values[i] --;
			}
		}
		for (i=0; i<this->GetErrorSetElementsNumber(); i++) {
			if (this->ErrorSetIndexes->Values[i]>SampleIndex) {
				this->ErrorSetIndexes->Values[i] --;
			}
		}
		for (i=0; i<this->GetRemainingSetElementsNumber(); i++) {
			if (this->RemainingSetIndexes->Values[i]>SampleIndex) {
				this->RemainingSetIndexes->Values[i] --;
			}
		}	

		this->SamplesTrainedNumber --;
		if (this->SamplesTrainedNumber==1 && this->GetErrorSetElementsNumber()>0) {
			this->ErrorSetIndexes->RemoveAt(0);
			this->RemainingSetIndexes->Add(0);
			this->Weights->Values[0] = 0;
			this->Bias = this->Margin(this->X->Values->Values[0],this->Y->Values[0]);
		}
		if (this->SamplesTrainedNumber==0) {
			this->Bias = 0;
		}
	}

}
	
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -