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

📄 copy of 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_common.h"
#include "engine.h"
#define  BUFSIZE 256

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

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 trainfile_matrix[200];
	com_pro.show_action=TRUE;
	//  DOC *docs;  /* training examples */
	//  long totwords,totdoc; //,i;
	
	/* print this message to the windows */
	if (com_pro.show_action)
		printm("begin to compute");
	if (com_pro.show_action)
		printm("Scanning examples...");
	
	/* Copy the train & test files to the right name */
	strcpy(trainfile,com_param.trainfile);
	strcpy(testfile,com_param.testfile);
	
	/* 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;
	
	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;
	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;
	//  int iii = 0;
	//  int ttt = 0;
	//disp('run adaboost with cycles=100');
	printf("run adaboost with cycles=100\n");
	//for m=step:step:1000
	
	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);

	int tt=0;   
	//while (step < (train_max_docs+1))
	while (step < 1001)
	{
		//disp(m);
		
		printf("step numbers:%d\n", step);
		// take only part of matrix train(1:m,:), train_label(1:m)
		Matrix *train = new Matrix(step,c_train);
		Matrix *train_label = new Matrix(step,two);
		Matrix* errorTrain = new Matrix(one,step/10);
	    Matrix* errorTest = new Matrix(one,step/10);
		train->partOfMatrix(step,c_train,*train_matrix);
		train_label->partOfMatrix(step,one,*train_label_matrix);
			
		/********************************************************/
		/* boost=adaBoost(train(1:m,:),train_label(1:m),cycles);*/
		/********************************************************/
		int three=3;
		
		adaBoost(train,train_label,train_max_words_doc,train_max_docs,step,cycles,boost,iii,ttt);
		
		/*********************************************************************************************************/      
		/* [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);
	    errorTrain_acc->copy(tt,*errorTrain);
		errorTest_acc->copy(tt,*errorTest);
		tt=tt+1;
		step = step + 100;
		free(train);
		free(train_label);
		free(errorTrain);
		free(errorTest);
	}
	/*
	% image figure No. 1
	f1h = figure;   
	% set various properties and display images
	set(f1h,'units','normalized');  
	set(f1h,'position',[0 0 0.5 1]); 
	set(f1h,'numbertitle','off');
	titleName='adaboost first work';
	set(f1h,'name',titleName);
	X = step : step : 1000 ;
	plot(X,errorTrain,'r+:',X,errorTest,'bd-');
	title('adaboost vs. size of train file','Fontsize',16);
	xlabel('num of samples (on train file)');
	ylabel('Errrors rate');
	legend('error on train file','error on test file');
	grid on;
	clear errorTrain;
	clear errorTest;
	disp('run adaboost algorothm with different boosting');
	*/
    Engine *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 Array_train[10];
	double Array_test[10];
	double X_cycles[10]={100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0, 1000.0};
	Matrix* getArrayTrain = new Matrix(10,one);
    Matrix* getArrayTest = new Matrix(10,one);

    getArrayTrain->matrixToArray(Array_train,*errorTrain_acc);
    getArrayTest->matrixToArray(Array_test,*errorTest_acc);

	
	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;");
	
	/*
	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
	*/
	int step_1 = 20;
	int maxCycles=100;
	while (step_1 < maxCycles)
	{
		printf("step numbers:%d\n", step);
		
		/*********************************************/
        /* boost=adaBoost(train,train_label,cycles); */
		/*********************************************/
		adaBoost(train_matrix,train_label_matrix,train_max_words_doc,train_max_docs,step,cycles,boost,iii,ttt);
		
		/*********************************************************************************************************/      
		/* [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);
		step_1 = step_1 + 20;
	}
	/*
	% image figure No. 2
	f2h = figure;      
	% set various properties and display images
	set(f2h,'units','normalized');  
	set(f2h,'position',[0 0 0.5 1]); 
	set(f2h,'numbertitle','off');
	set(f2h,'name',titleName);
	X = step : step : maxCycles ;
	plot(X,errorTrain,'r+:',X,errorTest,'bd-');
	title('adaboost vs. boosting cycles','Fontsize',16);
	xlabel('num of samples (on train file)');
	ylabel('Errrors rate');
	legend('error on train file','error on test file');
	grid on;   
	*/
	
	//  free(train);
	//  free(train_label);
	free(train_matrix);
	free(train_label_matrix);
	free(test_matrix);
	free(test_label_matrix);
	
	if (com_pro.show_action)
		printm("Cease to compute");	
	return(0);
}

⌨️ 快捷键说明

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