📄 gentleboost_model.c
字号:
randini();
gentelboost_perceptron(X , y , T , options , labels , m,
model.featureIdx , model.a , model.b,
d , N);
}
}
/*----------------------------------------------------------------------------------------------------------------------------------------- */
void gentelboost_decision_stump(double *X , double *y , int T , struct opts options , double *labels , int m,
double *featuresIdx, double *th , double *a, double *b,
int d , int N)
{
double cteN =1.0/(double)N;
int i , j , t , c , cT;
int indN , Nd = N*d , ind, N1 = N - 1 , featuresIdx_opt , currentlabel , templabel;
double *w, *Xt, *Xtsorted , *xtemp , *ytemp , *wtemp ;
int *Ytsorted , *index, *idX;
double atemp , btemp , sumSw , Eyw , fm , sumwyy , error , errormin, th_opt , a_opt , b_opt , label ;
double temp , Sw , Syw;
Xt = (double *)mxMalloc(Nd*sizeof(double));
Xtsorted = (double *)mxMalloc(Nd*sizeof(double));
ytemp = (double *)mxMalloc(N*sizeof(double));
wtemp = (double *)mxMalloc(N*sizeof(double));
Ytsorted = (int *)mxMalloc(Nd*sizeof(int));
idX = (int *)mxMalloc(Nd*sizeof(int));
w = (double *)mxMalloc(N*sizeof(double));
index = (int *)mxMalloc(N*sizeof(int));
xtemp = (double *)mxMalloc(N*sizeof(double));
/* Transpose data to speed up computation */
transpose(X , Xt , d , N);
/* Sorting data to speed up computation */
indN = 0;
for(j = 0 ; j < d ; j++)
{
for(i = 0 ; i < N ; i++)
{
index[i] = i;
xtemp[i] = Xt[i + indN];
}
qsindex(xtemp , index , 0 , N1);
for(i = 0 ; i < N ; i++)
{
ind = index[i];
Xtsorted[i + indN] = xtemp[i];
Ytsorted[i + indN] = (int)y[ind];
idX[i + indN] = ind;
}
indN += N;
}
cT = 0;
for (c = 0 ; c < m ; c++)
{
label = labels[c];
currentlabel = (int)label;
for(i = 0 ; i < N ; i++)
{
w[i] = cteN;
}
for(t = 0 + cT ; t < T + cT; t++)
{
errormin = huge;
indN = 0;
for(j = 0 ; j < d ; j++)
{
Eyw = 0.0;
sumwyy = 0.0;
for(i = 0 ; i < N ; i++)
{
ind = i + indN;
xtemp[i] = Xtsorted[ind];
templabel = Ytsorted[ind];
if(templabel == currentlabel)
{
ytemp[i] = 1.0;
}
else
{
ytemp[i] = -1.0;
}
wtemp[i] = w[idX[ind]];
temp = ytemp[i]*wtemp[i];
Eyw += temp;
sumwyy += ytemp[i]*temp;
}
Sw = 0.0;
Syw = 0.0;
for(i = 0 ; i < N ; i++)
{
ind = i + indN;
Sw += wtemp[i];
Syw += ytemp[i]*wtemp[i];
btemp = Syw/Sw;
if(Sw != 1.0)
{
atemp = (Eyw - Syw)/(1.0 - Sw) - btemp;
}
else
{
atemp = (Eyw - Syw) - btemp;
}
error = sumwyy - 2.0*atemp*(Eyw - Syw) - 2.0*btemp*Eyw + (atemp*atemp + 2.0*atemp*btemp)*(1.0 - Sw) + btemp*btemp;
if(error < errormin)
{
errormin = error;
featuresIdx_opt = j;
if(i < N1)
{
th_opt = (xtemp[i] + xtemp[i + 1])/2;
}
else
{
th_opt = xtemp[i];
}
a_opt = atemp;
b_opt = btemp;
}
}
indN += N;
}
/* Best parameters for weak-learner t */
featuresIdx[t] = (double) (featuresIdx_opt + 1);
th[t] = th_opt;
a[t] = a_opt;
b[t] = b_opt;
/* Weigth's update */
ind = featuresIdx_opt*N;
sumSw = 0.0;
for (i = 0 ; i < N ; i++)
{
fm = a_opt*(Xt[i + ind] > th_opt) + b_opt;
if(y[i] == label)
{
w[i] *= exp(-fm);
}
else
{
w[i] *= exp(fm);
}
sumSw += w[i];
}
sumSw = 1.0/sumSw;
for (i = 0 ; i < N ; i++)
{
w[i] *= sumSw;
}
}
cT += T;
}
mxFree(w);
mxFree(Xt);
mxFree(Xtsorted);
mxFree(Ytsorted);
mxFree(index);
mxFree(xtemp);
mxFree(idX);
mxFree(ytemp);
mxFree(wtemp);
}
/*----------------------------------------------------------------------------------------------------------------------------------------- */
void gentelboost_perceptron(double *X , double *y , int T , struct opts options , double *labels , int m ,
double *featuresIdx, double *a , double *b,
int d , int N)
{
double epsi = options.epsi , lambda = options.lambda , error , errormin , cteN =1.0/(double)N;
int t , j , i , k , c , cT ;
int featuresIdx_opt;
int max_ite = options.max_ite, Nd = N*d , indN , index;
double *Xt , *w;
double *ytemp;
double atemp , btemp , xi , temp , fx , tempyifx , sum , fm , currentlabel;
double a_opt, b_opt;
Xt = (double *)mxMalloc(Nd*sizeof(double));
w = (double *)mxMalloc(N*sizeof(double));
ytemp = (double *)mxMalloc(N*sizeof(double));
transpose(X , Xt , d , N);
cT = 0;
for (c = 0 ; c < m ; c++)
{
currentlabel = labels[c];
for(i = 0 ; i < N ; i++)
{
w[i] = cteN;
if(y[i] == currentlabel)
{
ytemp[i] = 1.0;
}
else
{
ytemp[i] = -1.0;
}
}
for(t = 0 + cT ; t < T + cT ; t++)
{
errormin = huge;
indN = 0;
for(j = 0 ; j < d ; j++)
{
/* Random initialisation of weights */
index = floor(N*rand());
atemp = Xt[index + indN];
index = floor(N*rand());
btemp = Xt[index + indN];
/* Weight's optimization */
for(k = 0 ; k < max_ite ; k++)
{
for(i = 0 ; i < N ; i++)
{
xi = Xt[i + indN];
fx = (2.0/( 1.0 + exp(-2.0*epsi*(atemp*xi + btemp)) )) - 1.0; /* sigmoid in [-1 , 1] */
temp = lambda*(ytemp[i] - fx)*epsi*(1.0 - fx*fx); /* d(sig(x;epsi))/dx = epsi*(1 - fx
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -