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

📄 adaboost_train_main.cpp

📁 it is an adaboost weak learner
💻 CPP
字号:
#include "stdafx.h"
#include "AdaBoost.h"
#include "matrix.h"
#include "Adaboost_train_main.h"
#include "Adaboost_results_file.h"
#include "Adaboost_common.h"
//#include "engine.h"
#define  BUFSIZE 256

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

static char temstr[200];
int Adaboost_train_main()
{  	
	//long *train, *train_label, *test, *test_label;
	long train_max_docs,train_max_words_doc,test_max_docs,test_max_words_doc,train_ll,test_ll;
	char trainfile[200];
	char testfile[200];
	char resultfile[200];
	RESULTS result;
	com_pro.show_action=TRUE;
	
	/* print this message to the windows */
	if (com_pro.show_action)
		printm("Begin to running Adaboost algorithm");
	if (com_pro.show_action)
		printm("Start to Scan train & test file...");
	
	/* Copy the train & test files to the right name */
	strcpy(trainfile,com_param.trainfile);
	strcpy(testfile,com_param.testfile);
	strcpy(resultfile,com_param.resultfile);
	
	/* nol_ll function return the size of files, as follow: max_docs => length, max_words_doc => width */
	nol_ll(trainfile,&train_max_docs,&train_max_words_doc,&train_ll); /* scan size of input file */
	train_max_words_doc+=1;
	train_ll+=2;
	train_max_docs+=-1;
	
	/* nol_ll function return the size of files, as follow: max_docs => length, max_words_doc => width */
	nol_ll(testfile,&test_max_docs,&test_max_words_doc,&test_ll); /* scan size of input file */
	test_max_words_doc+=1;
	test_ll+=2;
	test_max_docs+=-1;
	if (com_pro.show_action)
		printm("Finish to Scan train & test file...");

	// Run Adaboost
	int r_train=train_max_docs+1; 
	int c_train=train_max_words_doc;
	int r_test=test_max_docs+1; 
	int c_test=test_max_words_doc;
	int one=1;
	int two=2;
	int value_one=1;
	int three=3;
	Matrix *train_matrix = new Matrix(r_train,c_train);
	Matrix *train_label_matrix = new Matrix(r_train,one);
	Matrix *test_matrix = new Matrix(r_test,c_test);
	Matrix *test_label_matrix = new Matrix(r_test,one);
	read_train_file(trainfile,train_max_words_doc,train_max_docs,train_ll, train_matrix, train_label_matrix);
	read_test_file(testfile,test_max_words_doc,test_max_docs,test_ll,test_matrix, test_label_matrix);
	int step=100;
	int cycles=100;
	//disp('run adaboost with cycles=100');
	if (com_pro.show_action)
		printm("run adaboost with cycles == 100");
	
	Matrix* boost = new Matrix(step,one);
	Matrix* iii = new Matrix(step,one);
	Matrix* ttt = new Matrix(step,one);
	Matrix* errorTrain_acc = new Matrix(one,step/10);
	Matrix* errorTest_acc = new Matrix(one,step/10);
	/* ********************************************************************************************************/
	/* step=100;                                                                                              */
    /* cycles=100;                                                                                            */
    /* for m=step:step:1000                                                                                   */
    /*   disp(m);                                                                                             */
    /*   boost=adaBoost(train(1:m,:),train_label(1:m),cycles);                                                */
    /*   [errorTrain(m/step),errorTest(m/step)]=getError(boost,train(1:m,:),train_label(1:m),test,test_label);*/
    /* end                                                                                                    */
	/**********************************************************************************************************/
    
	int tt=0;
	while (step < 101)
	{
		//disp(m);		
		if (com_pro.show_compute_1)
		{
        sprintf(temstr,"step numbers: %.d\n",step);
        printm(temstr);
		}
		// take only part of matrix train(1:m,:), train_label(1:m)
		Matrix *tmp = new Matrix(one,one);
		Matrix *train = new Matrix(step,c_train);
		Matrix *train_first = new Matrix(cycles,c_train);
		Matrix *train_next = new Matrix(cycles,c_train);
		Matrix *train_label = new Matrix(step,value_one+1);
		Matrix* errorTrain = new Matrix(one,step/10);
	    Matrix* errorTest = new Matrix(one,step/10);
		Matrix *Ava_data_matrix_first = new Matrix(100,c_train);
		Matrix *Ava_data_matrix_next= new Matrix(cycles,c_train);

		train->partOfMatrix(step,c_train,*train_matrix);
		train_label->partOfMatrix(step,one,*train_label_matrix);
		double ave_delta = 0;
		
		train_first->partOfMatrix(100,c_train,*train_matrix);
		//train_next->partOfMatrixFromEnd(100,
		train_next->partOfMatrixNext(cycles,c_train,step,*train_matrix);
		
		// Comapre between the average of first martix (1..100) and the next matrix (400..500)
		double Sum_ava_data_first=0;
		double Sum_ava_data_val_first=0;
		Ava_data_matrix_first->matrixSumCol(&Sum_ava_data_first, *train_first); 
		Sum_ava_data_val_first = Sum_ava_data_first / (cycles * (c_train-1));	
		
		double Sum_ava_data_next=0;
		double Sum_ava_data_val_next=0;
		Ava_data_matrix_next->matrixSumCol(&Sum_ava_data_next, *train_next); 
		Sum_ava_data_val_next = Sum_ava_data_next / (cycles * (c_train-1));	
		ave_delta=Sum_ava_data_val_next - Sum_ava_data_val_first;
		ave_delta=fabs(ave_delta);
		ave_delta = 3 * ceil(ave_delta);
		
		
		/********************************************************/
		/* boost=adaBoost(train(1:m,:),train_label(1:m),cycles);*/
		/********************************************************/	
//		adaBoost(train,train_label,train_max_words_doc,train_max_docs,step,cycles,boost,iii,ttt,ave_delta);
	
		/*********************************************************************************************************/      
		/* [errorTrain(m/step),errorTest(m/step)]=getError(boost,train(1:m,:),train_label(1:m),test,test_label); */
		/*********************************************************************************************************/	
//		getError(boost,iii,ttt,train_matrix,train_label_matrix,test_matrix,test_label_matrix,errorTrain,errorTest,train_max_words_doc,train_max_docs,step,cycles);
	
		double errorTrain_value=0;
		double errorTest_value=0;
		tmp->getValueSpecific(0,0,&errorTrain_value,*errorTrain);
		tmp->getValueSpecific(0,0,&errorTest_value,*errorTest);
		if (com_pro.show_compute_1)
		{
          sprintf(temstr,"Train error cycle result: %f\n",errorTrain_value);
          printm(temstr);
		  sprintf(temstr,"Test error cycle result: %f\n",errorTest_value);
          printm(temstr);
		}

		errorTrain_acc->copy(tt,*errorTrain);
		errorTest_acc->copy(tt,*errorTest);
		tt=tt+1;
		step = step + 100;

		//delete Matrix
		delete tmp;
		delete train;
		delete train_label;
		delete errorTrain;
		delete errorTest;
		delete train_first;
		delete train_next;
		delete Ava_data_matrix_first;
		delete Ava_data_matrix_next;
	}
	delete boost;
    delete iii;
	delete ttt;
	/********************************************************************************************************/
	/* step=20;                                                                                             */
	/* maxCycles=100;                                                                                       */
	/* for cycles=step:step:maxCycles                                                                       */
	/*  disp(cycles);                                                                                       */
	/*  boost=adaBoost(train,train_label,cycles);                                                           */
	/*  [errorTrain(cycles/step),errorTest(cycles/step)]=getError(boost,train,train_label,test,test_label); */
	/*  clear boost;                                                                                        */
	/* end                                                                                                  */
    /********************************************************************************************************/
	if (com_pro.show_action)
		printm("run adaboost algorothm with different boosting");
	int step_1 = 20;
	cycles=20;
	int maxCycles=201;
	Matrix* errorTrain_acc_1 = new Matrix(one,maxCycles/step_1);
	Matrix* errorTest_acc_1 = new Matrix(one,maxCycles/step_1);
	while (step_1 < maxCycles)
	{
		Matrix* boost = new Matrix(step_1,one);
	    Matrix* iii = new Matrix(step_1,one);
    	Matrix* ttt = new Matrix(step_1,one);
		if (com_pro.show_compute_1)
		{
        sprintf(temstr,"step numbers: %.d\n",step_1);
        printm(temstr);
		}
		Matrix *tmp = new Matrix(one,one);
		Matrix *train = new Matrix(r_train,c_train);
		Matrix *train_label = new Matrix(r_train,two);
		Matrix* errorTrain_1 = new Matrix(one,maxCycles/step_1);
    	Matrix* errorTest_1 = new Matrix(one,maxCycles/step_1);

		train->partOfMatrix(r_train,c_train,*train_matrix);
		train_label->partOfMatrix(r_train,one,*train_label_matrix);

		double ave_delta = 0;

		/*********************************************/
        /* boost=adaBoost(train,train_label,cycles); */
		/*********************************************/
		adaBoost(train,train_label,train_max_words_doc,train_max_docs,step_1,cycles,boost,iii,ttt,ave_delta);
        
		/*********************************************************************************************************/      
		/* [errorTrain(m/step),errorTest(m/step)]=getError(boost,train(1:m,:),train_label(1:m),test,test_label); */
		/*********************************************************************************************************/		
		getError(boost,iii,ttt,train_matrix,train_label_matrix,test_matrix,test_label_matrix,errorTrain_1,errorTest_1,train_max_words_doc,train_max_docs,step_1,cycles);
		
		double errorTrain_value_1=0;
		double errorTest_value_1=0;
		tmp->getValueSpecific(0,0,&errorTrain_value_1,*errorTrain_1);
		tmp->getValueSpecific(0,0,&errorTest_value_1,*errorTest_1);
		if (com_pro.show_compute_1)
		{
          sprintf(temstr,"Train error cycle result: %f\n",errorTrain_value_1);
          printm(temstr);
		  sprintf(temstr,"Test error cycle result: %f\n",errorTest_value_1);
          printm(temstr);
		}

		step_1 = step_1 + 20;
		cycles= cycles + 20;
		tt=tt+1;
		errorTrain_acc_1->copy(tt,*errorTrain_1);
		errorTest_acc_1->copy(tt,*errorTest_1);

	    //delete Matrix
		delete train;
		delete train_label;
		delete errorTrain_1;
		delete errorTest_1;
		delete boost;
        delete iii;
	    delete ttt;
	}

	/***************************************************************/
	/* Get the results of Test & Train error into the results file */
	/***************************************************************/
	if (com_param.blAadaboostresults)
	  write_result(resultfile,&result, errorTrain_acc, errorTest_acc, errorTrain_acc_1, errorTest_acc_1);

	// delete THE MATRIX
	delete train_matrix;
	delete train_label_matrix;
	delete test_matrix;
	delete test_label_matrix;
	delete boost;
    delete iii;
	delete ttt;
    delete errorTrain_acc;
	delete errorTest_acc;
	delete errorTrain_acc_1;
	delete errorTest_acc_1;


	if (com_pro.show_action)
		printm("Cease to compute adaboost algorithm");	
	return(0);
}

/************************ tmp code **************************************/

/*
	double Array_iii[100];
	    double Array_ttt[100];
		double Array_boost[100];
		Matrix* getArrayiii = new Matrix(100,one);
        Matrix* getArrayttt = new Matrix(100,one);
		Matrix* getArrayboost = new Matrix(100,one);
        getArrayiii->matrixToArray(Array_iii,*iii);
        getArrayttt->matrixToArray(Array_ttt,*ttt);
	    getArrayboost->matrixToArray(Array_boost,*boost);
ngine *ep;
	if (!(ep = engOpen("\0"))) 
	{
		fprintf(stderr, "\nCan't start MATLAB engine\n");
		return EXIT_FAILURE;
	}
	mxArray *T = NULL;
	mxArray *D = NULL; 
	mxArray *R = NULL;

	
	double X_cycles[10]={100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0, 1000.0};

  	T = mxCreateDoubleMatrix(1, 10, mxREAL);
	memcpy((void *)mxGetPr(T), (void *)X_cycles, sizeof(X_cycles));
	engPutVariable(ep, "T", T);

	double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
	R = mxCreateDoubleMatrix(1, 10, mxREAL);
	memcpy((void *)mxGetPr(R), (void *)time, sizeof(time));
	engEvalString(ep, "D = R * 1;");

//    D = mxCreateDoubleMatrix(1, 10, mxREAL);
//	R = mxCreateDoubleMatrix(1, 10, mxREAL);

//	memcpy((void *)mxGetPr(R), (void *)X_cycles, sizeof(X_cycles));
//	memcpy((void *)mxGetPr(T), (void *)Array_train, sizeof(Array_train));
//	memcpy((void *)mxGetPr(D), (void *)Array_test, sizeof(Array_test));
//	engPutVariable(ep, "R", R);

//	engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
	engEvalString(ep, "plot(T,D);");

	// Determine the title
    engEvalString(ep, "title('adaboost vs. size of train file');");
    engEvalString(ep, "xlabel('num of samples (on train file)');");
    engEvalString(ep, "ylabel('Errrors rate');");
    mxDestroyArray(T);
	mxDestroyArray(D);
    engEvalString(ep, "close;");

  	Matrix *train = new Matrix(step_1,c_train);
		Matrix *train_label = new Matrix(step_1,two);
		Matrix* errorTrain_1 = new Matrix(one,step_1/10);
    	Matrix* errorTest_1 = new Matrix(one,step_1/10);
		train->partOfMatrix(step_1,c_train,*train_matrix);
		train_label->partOfMatrix(step_1,one,*train_label_matrix);


*/

⌨️ 快捷键说明

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