📄 adaboost_common.cpp
字号:
if (step < 100){
step=r_train;
}
// New temporary matrix's
Matrix* error = new Matrix(value_one,sizeOfsqrt_c_train);
Matrix* error_max = new Matrix(value_one,sizeOfsqrt_c_train);
//Matrix* matrix_val_error = new Matrix(value_one,value_one);
Matrix* get_i_all = new Matrix(step,value_one);
Matrix* get_t_all = new Matrix(step,value_one);
Matrix* Ava_data_matrix = new Matrix(step,c_train-1);
for (int tt=0; tt < 12*(c_train - 1); tt++)
{
Matrix* train_error_part = new Matrix(step,2);
Matrix* error_abs_label_train = new Matrix(step,2);
/***********************************************************************************/
/* error(tt)=distribution*abs(label-(train(:,floor(tt/16)+1)>=16*(mod(tt,16)+1))); */
/***********************************************************************************/
//16*(mod(tt,sqrt(c_train))+1)
//sqrt_c_train
modulus_number = 69 + (5 * (tt % 12)+1);
//floor(tt/sqrt(c_train))+1
floor_number=floor(tt / 11) + 1;
if (step < 100)
{
step=r_train;
}
train_error_part->specificPartOfMatrix(*train,step,floor_number-1);
// (train(:,floor(tt/16)+1)>=16*(mod(tt,16)+1))
Matrix train_error = (*train_error_part)>modulus_number;
// label-(train(:,floor(tt/16)+1)>=16*(mod(tt,16)+1))
Matrix error_label_train = (label_matrix)-(train_error);
// abs(label-(train(:,floor(tt/16)+1)>=16*(mod(tt,16)+1)))
error_abs_label_train->matrixAbs(error_label_train);
//error(tt)=distribution*abs(label-(train(:,floor(tt/16)+1)>=16*(mod(tt,16)+1)));
Matrix error_tmp = (*distribution)*(*error_abs_label_train);
error->copy(tt,error_tmp);
//free matrix
delete train_error_part;
delete error_abs_label_train;
}
//double error_train_array[432];
//Matrix* getArrayTrain_error = new Matrix(432,value_one);
//getArrayTrain_error->matrixToArrayold(error_train_array,*error);
//matrix_val_error->getValueSpecific(0,0,&val_error,*error);
//[val,tt]=max(abs(error-0.5));
double val=0;
int index=0;
double parameter_05 = 0.5;
Matrix* errorMinusScalar = new Matrix(value_one,sizeOfsqrt_c_train);
errorMinusScalar->matrixMinusSclar(parameter_05,*error);
Matrix* errorAbsMinusScalar = new Matrix(value_one,sizeOfsqrt_c_train);
errorAbsMinusScalar->matrixAbs(*errorMinusScalar);
error_max->matrixMax(&val,&index,*errorAbsMinusScalar);
/************************/
/* i=floor(tt/16)+1; */
/* t=16*(mod(tt,16)+1); */
/************************/
if (index > 400 && index < 500 ){
(*i) = floor(index/17)+1;
if (*i >= (c_train-1)){
*i = (c_train-10);
}
(*t) = 12 * ((index % 12) + 1);
}
if (index > 300 && index < 400 ){
(*i) = floor(index/12)+1;
if (*i >= (c_train-1)){
*i = (c_train-10);
}
(*t) = 12 * ((index % 12) + 1);
}
if (index > 200 && index < 300){
(*i) = floor(index/7)+1;
if (*i >= (c_train-1)){
*i = (c_train-10);
}
(*t) = 12 * ((index % 12) + 1);
}
if (index > 100 && index < 200){
(*i) = floor(index/5)+1;
if (*i >= (c_train-1)){
*i = (c_train-10);
}
(*t) = 12 * ((index % 12) + 1);
}
if (index > 0 && index < 100){
(*i) = floor(index/2)+1;
if (*i >= (c_train-1)){
*i = (c_train-10);
}
(*t) = 12 * ((index % 12) + 1);
}
i_all->setValue(j,*i);
if (j > 2)
{
get_i_all->matrixCompareAndSet(i,c_train,*i_all);
}
t_all->setValue(j,*t);
if (j > 1)
{
get_t_all->matrixCompareAndSetfor_t(j,t,c_train,*t_all);
}
(*t) = (*t) + ave_delta;
delete error_max;
delete error;
delete Ava_data_matrix;
delete errorMinusScalar;
delete errorAbsMinusScalar;
delete get_i_all;
delete get_t_all;
return (0);
}
/*************************************************************************************/
/* function [errorTrain,errorTest]=getError(boost,train,train_label,test,test_label) */
/* disp('run getError'); */
/* d=size(boost); */
/* num=size(train); */
/* prediction=zeros(num(1),1); */
/* % geting the train error */
/* for h=1:d(1) */
/* prediction=prediction-log(boost(h,1))*(train(:,boost(h,2))>=boost(h,3)); */
/* end */
/* temp=-sum(log(boost(:,1)))/2; */
/* errorTrain=sum(abs((train_label>=5)-(prediction>=temp)))/num(1); */
/* prediction=zeros(1000,1); */
/* % geting the test error */
/* for h=1:d(1) */
/* prediction=prediction-log(boost(h,1))*(test(:,boost(h,2))>=boost(h,3)); */
/* end */
/* errorTest=sum(abs((test_label>=5)-(prediction>=temp)))/1000; */
/*************************************************************************************/
int getError(Matrix* boost,
Matrix* iii,
Matrix* ttt,
Matrix* train_matrix,
Matrix* train_label_matrix,
Matrix* test_matrix,
Matrix* test_label_matrix,
Matrix* errorTrain,
Matrix* errorTest,
long train_max_words_doc,
long train_max_docs,
int step,
int cycles)
{
if (com_pro.show_action)
printm("run getError");
double value_one = 1;
double value_two = 2;
// d=size(boost);
int boost_row;
int boost_col;
Matrix* size = new Matrix(step,step);
size->matrixSize(&boost_row,&boost_col,*boost);
// num=size(train_matrix);
int train_row;
int train_col;
size->matrixSize(&train_row,&train_col,*train_matrix);
// num=size(test_matrix);
int test_row;
int test_col;
size->matrixSize(&test_row,&test_col,*test_matrix);
if (step < 100){
step=train_row;
}
// prediction=zeros(num(1),1);
Matrix* prediction_train = new Matrix(step,value_one+1);
delete(size);
// geting the train error
/*****************************************************************************/
/* for h=1:d(1) */
/* prediction=prediction-log(boost(h,1))*(train(:,boost(h,2))>=boost(h,3)) */
/* end */
/*****************************************************************************/
//for (int h=1; h <= boost_row; h++){
for (int h=1; h < boost_row; h++){
//boost(h,3)
double val_ttt=0;
Matrix* matrix_val_ttt = new Matrix(h,value_one);
matrix_val_ttt->getValueSpecific(h-1,0,&val_ttt,*ttt);
// boost(h,2)
double val_iii=0;
matrix_val_ttt->getValueSpecific(h-1,0,&val_iii,*iii);
// train(:,boost(h,2))
Matrix* partOfTrainMatrix = new Matrix(step,val_iii);
partOfTrainMatrix->specificPartOfMatrix(*train_matrix,step,val_iii);
// (train(:,boost(h,2))>=boost(h,3))
Matrix train_error_boost = (*partOfTrainMatrix) > val_ttt;
// (boost(h,1))
double val_boost=0;
matrix_val_ttt->getValueSpecific(h-1,0,&val_boost,*boost);
// log(boost(h,1))
double log_val_boost = log(val_boost);
// log(boost(h,1))*(train(:,boost(h,2))>=boost(h,3))
Matrix* boostMultiTrain = new Matrix(step, value_one+1);
boostMultiTrain->ScalarMultiMatrix(log_val_boost,train_error_boost);
// prediction-log(boost(h,1))*(train(:,boost(h,2))>=boost(h,3))
prediction_train->matrixMinusMatrix(*boostMultiTrain,*prediction_train);
//prediction_train->matrixMinusMatrix(*prediction_train,*boostMultiTrain);
//free matrix
delete matrix_val_ttt;
delete partOfTrainMatrix;
delete boostMultiTrain;
}
// double prediction_array[500];
// Matrix* getPredictionArray = new Matrix(step,value_one);
// getPredictionArray->matrixToArray(prediction_array,*prediction_train);
/*********************************/
/* temp=-sum(log(boost(:,1)))/2; */
/*********************************/
// log(boost(:,1))
Matrix* log_boost = new Matrix(boost_row,boost_col);
log_boost->matrixLog(*boost);
// sum(log(boost(:,1)))
double sum_log_boost = 0;
Matrix* sum_log_boost_matrix = new Matrix(boost_row,boost_col);
sum_log_boost_matrix->matrixSumRow(&sum_log_boost,*log_boost);
double temp = 0;
temp = -1 * (sum_log_boost/2);
//temp = -1*(sum_log_boost/5);
/********************************************************************/
/* errorTrain=sum(abs((train_label>=5)-(prediction>=temp)))/num(1); */
/********************************************************************/
// (prediction>=temp)
Matrix prediction_biger_temp = (*prediction_train) > temp;
// (train_label>=5)
Matrix *train_label = new Matrix(step,boost_col+1);
train_label->partOfMatrix(step,boost_col,*train_label_matrix);
Matrix train_label_biger_value = (*train_label) > 3;
// ((train_label>=5)-(prediction>=temp))
Matrix* train_label_minus_prediction = new Matrix(step,boost_col+1);
train_label_minus_prediction->matrixMinusMatrix(prediction_biger_temp,train_label_biger_value);
// abs((train_label>=5)-(prediction>=temp))
Matrix* abs_train_label_minus_prediction = new Matrix(step,boost_col+1);
abs_train_label_minus_prediction->matrixAbs(*train_label_minus_prediction);
// sum(abs((train_label>=5)-(prediction>=temp)))
double sum_abs_train_label_minus_prediction = 0;
Matrix* sum_abs_train_label_minus_prediction_matrix = new Matrix(step,boost_col+1);
sum_abs_train_label_minus_prediction_matrix->matrixSumRow(&sum_abs_train_label_minus_prediction,*abs_train_label_minus_prediction);
// errorTrain=sum(abs((train_label>=5)-(prediction>=temp)))/num(1)
errorTrain->scalarDivisonScalar(sum_abs_train_label_minus_prediction,step, *errorTrain);
prediction_train->zeros(step,value_one);
/*****************************/
/* prediction=zeros(1000,1); */
/*****************************/
Matrix* prediction_test = new Matrix(step,value_one+1);
if (step < 100){
step=test_row;
}
// geting the test error
/******************************************************************************/
/* for h=1:d(1) */
/* prediction=prediction-log(boost(h,1))*(test(:,boost(h,2))>=boost(h,3)); */
/* end */
/******************************************************************************/
for (int hh=1; hh < boost_row; hh++){
//boost(h,3)
// test(:,boost(h,2))
double val_ttt=0;
Matrix* matrix_val_ttt = new Matrix(hh,value_one);
matrix_val_ttt->getValueSpecific(hh-1,0,&val_ttt,*ttt);
// boost(h,2)
double val_iii=0;
matrix_val_ttt->getValueSpecific(hh-1,0,&val_iii,*iii);
Matrix* partOfTestMatrix = new Matrix(step,val_iii);
partOfTestMatrix->specificPartOfMatrix(*test_matrix,step,val_iii);
// (test(:,boost(h,2))>=boost(h,3))
Matrix test_error_boost = (*partOfTestMatrix) > val_ttt;
// (boost(h,1))
double val_boost=0;
matrix_val_ttt->getValueSpecific(hh-1,0,&val_boost,*boost);
// log(boost(h,1))
double log_val_boost = log(val_boost);
// log(boost(h,1))*(train(:,boost(h,2))>=boost(h,3))
Matrix* boostMultiTest = new Matrix(step,value_one+1);
boostMultiTest->ScalarMultiMatrix(log_val_boost,test_error_boost);
// prediction-log(boost(h,1))*(train(:,boost(h,2))>=boost(h,3))
prediction_test->matrixMinusMatrix(*boostMultiTest,*prediction_test);
//prediction_test->matrixMinusMatrix(*prediction_test,*boostMultiTest);
delete matrix_val_ttt;
delete partOfTestMatrix;
delete boostMultiTest;
}
//double predictionTest_array[500];
//Matrix* getPredictionTestArray = new Matrix(step,value_one);
//getPredictionTestArray->matrixToArray(predictionTest_array,*prediction_test);
/****************************************************************/
/* errorTest=sum(abs((test_label>=5)-(prediction>=temp)))/1000; */
/****************************************************************/
// (prediction>=temp)
Matrix prediction_biger_temp_test = (*prediction_test) > temp;
// (train_label>=5)
Matrix *test_label = new Matrix(step,boost_col+1);
test_label->partOfMatrix(step,boost_col,*test_label_matrix);
Matrix test_label_biger_value = (*test_label) > 3;
// ((train_label>=5)-(prediction>=temp))
Matrix* test_label_minus_prediction = new Matrix(step,boost_col+1);
test_label_minus_prediction->matrixMinusMatrix(prediction_biger_temp_test,test_label_biger_value);
// abs((train_label>=5)-(prediction>=temp))
Matrix* abs_test_label_minus_prediction = new Matrix(step,boost_col+1);
abs_test_label_minus_prediction->matrixAbs(*test_label_minus_prediction);
// sum(abs((train_label>=5)-(prediction>=temp)))
double sum_abs_test_label_minus_prediction = 0;
Matrix* sum_abs_test_label_minus_prediction_matrix = new Matrix(step,boost_col+1);
sum_abs_test_label_minus_prediction_matrix->matrixSumRow(&sum_abs_test_label_minus_prediction,*abs_test_label_minus_prediction);
// errorTrain=sum(abs((train_label>=5)-(prediction>=temp)))/num(1)
errorTest->scalarDivisonScalar(sum_abs_test_label_minus_prediction,test_row, *errorTest);
prediction_test->zeros(step,value_one);
//delete Matrix
delete prediction_train;
delete prediction_test;
delete train_label;
delete test_label;
delete log_boost;
delete sum_log_boost_matrix;
delete train_label_minus_prediction;
delete test_label_minus_prediction;
delete abs_train_label_minus_prediction;
delete abs_test_label_minus_prediction;
delete sum_abs_train_label_minus_prediction_matrix;
delete sum_abs_test_label_minus_prediction_matrix;
return (0);
}
/* grep through file and count number of lines, maximum number of spaces per line, and */
/* longest line. */
int nol_ll(char *file,long *nol,long *wol,long *ll)
{
FILE *fl;
int ic;
char c;
long current_length,current_wol;
if ((fl = fopen (file, "r")) == NULL)
{
printe (file);
return -1;
}
current_length=0;
current_wol=0;
(*ll)=0;
(*nol)=1;
(*wol)=0;
while((ic=getc(fl)) != EOF)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -