#include<CONIO.H>
#include<IOSTREAM.H>
#include<STDLIB.H>
#include<STDIO.H>
#include<math.h>
#include<complex.h>
#include<dos.h>
//This
program implements bagging on backprop algorithm. It is good for 4 dim-vectors
and two classes only.
// It
is designed to work on 2 by 2 matrices (each matrix is represented as
// a
column vector). But, it can be generalized easily to work on higher
//
dimensional matrices and more classes. Notice also it works on 4-dim. 2
class-vectors
// and
can be generalized to higher dim. and more classes.
// It
implements the on-line mode algorithm and it finishes the # of iterations.
struct
my_struct {
float x1;float x2;float x3;float x4;
};
typedef
my_struct input;
class
classify
{
public:
int
round(float a); //This function rounds numbers in the interval [0,1] only.
void
backprop();
};
int
classify::round(float a)
{
if
(a<0.5) return 0;else return 1;
}
void
classify::backprop()
{
//
Notice that the counted nodes of each layer start from 1. This means
// that
the node zero is just for initilization (like including 1 in perc.).
//
Notice the (train) input here for each class is a 4 dim vec;
[trcix1(j),trcix2(j),trcix3(j),trcix4(j)].
//
Becuase here each 2 by 2 matrix is represented by a 4 dim vec.
// The
test vector is represented similarly, but instaed of trcixk(j), we have
tscixk(j).
// The
output is assumed to be 0 or 1; i.e., we have two classes.
// Thus
the number of nodes for layer 0 is 4 and for layer L is 1.
// Ni
is the number of training vectors for class i and
// the
number of testing is: N for class 1, NN for class 2.
int
count1,count2,flag,ii,jj,iter,kk,i,j,r,L,max,ff,N1,N2,N,NN,NoOfIter,NoOfClasses;
float
alpha,temp,error1,error2,error,Perror1,Perror2,Perror,a,eighta,dat1,dat2,dat3,dat4;
int
count,M,t,T,indd[50];
//float
dat3,dat4;
double
val1,val2,delta1,delta2;
input
*trc1,*trc2,*tsc1,*tsc2,*ORIGtrc1,*ORIGtrc2;
struct
time tt;
char
char1,char2;
float
dat,sum1,Bagg1[50][50],Bagg2[50][50];
div_t
QuotRem;
FILE
*p,*c1tr,*c2tr,*c1ts,*c2ts;
//cout<<"Notice
that all the arrays here start at 0\n";
trc1=(input
*) malloc((204) * sizeof(input));
trc2=(input
*) malloc((204) * sizeof(input));
tsc1=(input
*) malloc((204) * sizeof(input));
tsc2=(input
*) malloc((204) * sizeof(input));
cout<<"Here
"<<"\n";
c1tr =
fopen("C:\M1.TXT","r");
i=1;
while ( !feof(c1tr) )
{
fscanf(c1tr,"%f %f %f %f",
&dat1, &dat2, &dat3, &dat4);
// READING DATA FROM THE FILE.
//cout<<dat1<<"
"<<dat2<<" "<<dat3<<"
"<<dat4<<"\n";delay(2000);
trc1[i].x1=dat1;trc1[i].x2=dat2;
trc1[i].x3=dat3;trc1[i].x4=dat4;
if (trc1[i].x1 < -300)
cout<<"ERROR\n";
if (trc1[i].x2 < -300)
cout<<"ERROR\n";
if (trc1[i].x3 < -300)
cout<<"ERROR\n";
if (trc1[i].x4 < -300)
cout<<"ERROR\n";
if (trc1[i].x1 > 300)
cout<<"ERROR\n";
if (trc1[i].x2 > 300)
cout<<"ERROR\n";
if (trc1[i].x3 > 300)
cout<<"ERROR\n";
if (trc1[i].x4 > 300)
cout<<"ERROR\n";
i++;
}
fclose(c1tr); // CLOSING THE FILE.
N1=i-2;
cout<<"N1= "<<N1<<"\n";
delay(0);
c2tr =
fopen("C:\M2.TXT","r");
i=1;
while ( !feof(c2tr) )
{
fscanf(c2tr,"%f %f %f %f",
&dat1, &dat2, &dat3, &dat4);
// READING DATA FROM THE FILE.
//cout<<dat1<<"
"<<dat2<<" "<<dat3<<"
"<<dat4<<"\n";delay(2000);
trc2[i].x1=dat1;trc2[i].x2=dat2;
trc2[i].x3=dat3;trc2[i].x4=dat4;
if (trc2[i].x1 < -300)
cout<<"ERROR\n";
if (trc2[i].x2 < -300)
cout<<"ERROR\n";
if (trc2[i].x3 < -300)
cout<<"ERROR\n";
if (trc2[i].x4 < -300)
cout<<"ERROR\n";
if (trc2[i].x1 > 300)
cout<<"ERROR\n";
if (trc2[i].x2 > 300)
cout<<"ERROR\n";
if (trc2[i].x3 > 300)
cout<<"ERROR\n";
if (trc2[i].x4 > 300)
cout<<"ERROR\n";
i++;
}
fclose(c2tr); // CLOSING THE FILE.
N2=i-2;
cout<<"N2= "<<N2<<"\n";
delay(0);
cout<<"To test on training
vectors, press 1. On testing vectors, press 2\n";
char2=getche();cout<<"\n";
if (char2=='1')
c1ts =
fopen("C:\M1.TXT","r");
if (char2=='2')
c1ts =
fopen("C:\M.TXT","r");
i=1;
while (!feof(c1ts))
{
fscanf(c1ts,"%f %f %f %f",
&dat1, &dat2, &dat3, &dat4);
// READING DATA FROM THE FILE.
//cout<<dat1<<"
"<<dat2<<" "<<dat3<<"
"<<dat4<<"\n";delay(2000);
tsc1[i].x1=dat1;tsc1[i].x2=dat2;
tsc1[i].x3=dat3;tsc1[i].x4=dat4;
//cout<<tsc1[i].x1<<"
"<<tsc1[i].x2<<" ";
//cout<<tsc1[i].x3<<"
"<<tsc1[i].x4<<"\n";delay(2000);
if (tsc1[i].x1 < -300)
cout<<"ERROR\n";
if (tsc1[i].x2 < -300)
cout<<"ERROR\n";
if (tsc1[i].x3 < -300)
cout<<"ERROR\n";
if (tsc1[i].x4 < -300)
cout<<"ERROR\n";
if (tsc1[i].x1 > 300)
cout<<"ERROR\n";
if (tsc1[i].x2 > 300)
cout<<"ERROR\n";
if (tsc1[i].x3 > 300)
cout<<"ERROR\n";
if (tsc1[i].x4 > 300)
cout<<"ERROR\n";
i++;
}
fclose(c1ts); //
CLOSING THE FILE.
N=i-2;
cout<<"N= "<<N<<"\n";
delay(0);
if (char2=='1')
c2ts =
fopen("C:\M2.TXT","r");
if (char2=='2')
c2ts =
fopen("C:\MM.TXT","r");
i=1;
while (!feof(c2ts))
{
fscanf(c2ts,"%f %f %f %f",
&dat1, &dat2, &dat3, &dat4);
// READING DATA FROM THE FILE.
//cout<<dat1<<"
"<<dat2<<" "<<dat3<<"
"<<dat4<<"\n";delay(2000);
tsc2[i].x1=dat1;tsc2[i].x2=dat2;
tsc2[i].x3=dat3;tsc2[i].x4=dat4;
//cout<<tsc2[i].x1<<"
"<<tsc2[i].x2<<" ";
//cout<<tsc2[i].x3<<"
"<<tsc2[i].x4<<"\n";delay(2000);
if (tsc2[i].x1 < -300)
cout<<"ERROR\n";
if (tsc2[i].x2 < -300)
cout<<"ERROR\n";
if (tsc2[i].x3 < -300)
cout<<"ERROR\n";
if (tsc2[i].x4 < -300)
cout<<"ERROR\n";
if (tsc2[i].x1 > 300)
cout<<"ERROR\n";
if (tsc2[i].x2 > 300)
cout<<"ERROR\n";
if (tsc2[i].x3 > 300)
cout<<"ERROR\n";
if (tsc2[i].x4 > 300)
cout<<"ERROR\n";
i++;
}
fclose(c2ts); // CLOSING THE FILE.
NN=i-2;
cout<<"NN= "<<NN<<"\n";
delay(0);
cout<<"Here 2\n";
cout<<"Enter N1
";cin>>N1;cout<<"\n";
cout<<"Enter N2
";cin>>N2;cout<<"\n";
cout<<"Enter N
";cin>>N;cout<<"\n";
cout<<"Enter NN
";cin>>NN;cout<<"\n";
cout<<"Enter
the number of iterations: ";
cin>>NoOfIter;
cout<<"\n";
cout<<"Enter
the learning rate 'eighta': ";
cin>>eighta;
cout<<"\n";
cout<<"Enter
the speeding constant 'alpha': ";
cin>>alpha;
cout<<"\n";
cout<<"Enter
the Bagging constant 'T'. Notice T must be < 51 here " ;
cin>>T;
cout<<"\n";
cout<<"Enter
the number of layers 'L'. Notice that the layers: ";
cout<<"are
from 0 to L\n";
cin>>L;
cout<<"\n";
cout<<"Notice
the number of layers and nodes/layer should be less than 8\n";
int
k[10];
max=1;
for
(r=0;r<=L;r++)
{
cout<<"Enter the number of
nodes in layer "<<r<<": ";
cin>>k[r];
if (k[r]>max) (max=k[r]);
cout<<"\n";
}
float
DELTAw[8][8][8],w[8][8][8],v[8][8],y[8][8],righty[8][8];
//notice
that if class i is represented by (1,0,1), then
// we
have righty[i][1]=1,righty[i][2]=0,righty[i][3]=1.
float
predy[8][8],delta[8][8];
flag=-1;
cout<<"Do
you want to use the old weights? If yes, press y, elso n\n";
char1=getche();
if
(char1=='n')
{
for (r=1;r<=L;r++)
for (i=1;i<=k[r];i++)
for (j=0;j<=k[r-1];j++)
{
w[r][i][j]=-1.0*flag*random(max)/max;
//w[r][i][j]=0.5;
}
}
if
(char1=='y')
{
p =
fopen("c:\WEIGHTMX.DAT","r"); // TO OPEN THE FILE FOR READING.
for (r=1;r<=L;r++)
for (i=1;i<=k[r];i++)
for (j=0;j<=k[r-1];j++)
{
fscanf(p,"%f",&dat); // READING DATA FROM THE FILE.
w[r][i][j]=dat;
}
fclose(p);
}
cout<<"\n";
cout<<"Enter
your 'a' value for the activation function\n";
cin>>a;
/*
for
(r=0;r<=L;r++)
cout<<"k("<<r<<")="<<k[r]<<" ";
cout<<"\n";
*/
delay(0);
cout<<"Enter
the number of classes. Notice that the maximum number of\n";
cout<<"allowed
classes is "<<pow(L,2)<<" : ";
cin>>NoOfClasses;
for
(ii=1;ii<=NoOfClasses;ii++)
{
cout<<"Enter representation for
class "<<ii<<". Notice this must ";
cout<<" be "<<k[L]<<"
dimensional vector\n";
for (jj=1;jj<=k[L];jj++)
cin>>righty[ii][jj];
}
//Beginning
of training:
cout<<"Time
at the beginning of training is: ";
gettime(&tt);
cprintf("%2d:%02d:%02d.%02d",tt.ti_hour,
tt.ti_min, tt.ti_sec, tt.ti_hund);
cout<<"\n";
cout<<"Training
is: ";
for
(i=1;i<=N1;i++)
{
ORIGtrc1[i].x1=trc1[i].x1;
ORIGtrc1[i].x2=trc1[i].x2;
ORIGtrc1[i].x3=trc1[i].x3;
ORIGtrc1[i].x4=trc1[i].x4;
}
for
(i=1;i<=N2;i++)
{
ORIGtrc2[i].x1=trc2[i].x1;
ORIGtrc2[i].x2=trc2[i].x2;
ORIGtrc2[i].x3=trc2[i].x3;
ORIGtrc2[i].x4=trc2[i].x4;
}
for
(t=1;t<=T;t++)
{
QuotRem=div(t,2);//QuotRem.quot,
QuotRem.rem
if
(QuotRem.rem==0) {gotoxy(20,25);cout<<(t*1.0/T)*100<<"%
";}
for
(i=1;i<=N1;i++)
indd[i]=random(N1)+1;
for (i=1;i<=N1;i++)
{
trc1[i].x1=ORIGtrc1[indd[i]].x1;
trc1[i].x2=ORIGtrc1[indd[i]].x2;
trc1[i].x3=ORIGtrc1[indd[i]].x3;
trc1[i].x4=ORIGtrc1[indd[i]].x4;
};
for
(i=1;i<=N2;i++)
indd[i]=random(N2)+1;
for
(i=1;i<=N2;i++)
{
trc2[i].x1=ORIGtrc2[indd[i]].x1;
trc2[i].x2=ORIGtrc2[indd[i]].x2;
trc2[i].x3=ORIGtrc2[indd[i]].x3;
trc2[i].x4=ORIGtrc2[indd[i]].x4;
}
for
(iter=1;iter<=NoOfIter;iter++)
{
//QuotRem=div(iter,10);//QuotRem.quot,
QuotRem.rem
//if (QuotRem.rem==0) {gotoxy(20,25);cout<<(iter*1.0/NoOfIter)*100<<"%
";}
count1=1;count2=1;count=1;
//while ((count1 <= N1) &&
(count2 <=N2))
if (N1>N2) M=N1; else M=N2;
while (count <= M)
{
if (count1<=N1)
{
//righty[0]=1;
v[0][1]=trc1[count1].x1;
v[0][2]=trc1[count1].x2;
v[0][3]=trc1[count1].x3;
v[0][4]=trc1[count1].x4;
//cout<<v[0][1]<<"
";
//The following are added:
if (v[0][1] < -300)
{cout<<"ERROR\n";delay(3000);};
if (v[0][2] < -300)
{cout<<"ERROR\n";delay(3000);};
if (v[0][3] < -300)
{cout<<"ERROR\n";delay(3000);};
if (v[0][4] < -300)
{cout<<"ERROR\n";delay(3000);};
if (v[0][1] > 300)
{cout<<"ERROR\n";delay(3000);};
if (v[0][2] > 300)
{cout<<"ERROR\n";delay(3000);};
if (v[0][3] > 300)
{cout<<"ERROR\n";delay(3000);};
if (v[0][4] > 300)
{cout<<"ERROR\n";delay(3000);};
for (i=1;i<=k[0];i++)
y[0][i]=v[0][i];
for (r=0;r<=L;r++)
y[r][0]=1;
for (r=1;r<=L;r++)
for (j=1;j<=k[r];j++)
{
v[r][j]=0;
for
(kk=1;kk<=k[r-1];kk++)
{
//cout<<"y=
"<<y[r-1][kk]; //added
//cout<<"w= "<<w[r][j][kk]; //added
//cout<<"\n";delay(2000);
v[r][j]=v[r][j]+(w[r][j][kk])*(y[r-1][kk]);
//cout<<y[r-1][kk];
//added
}
v[r][j]=v[r][j]+w[r][j][0];
y[r][j]=1.0/(1+exp(-1.0*a*v[r][j]));
delay(0);
} //the j loop
//cout<<"HERE HERE HERE
....\n";//new
for (j=1;j<=k[L];j++)
delta[L][j]=a*(y[L][j])*(righty[1][j]-y[L][j])*(1-y[L][j]);
for (r=L;r>=2;r--)
{
//for (j=1;j<=k[r+1];j++)
//changed to:
for (j=1;j<=k[r];j++) //new
{
delta[r-1][j]=0;
//for
(kk=1;kk<=k[r+1];kk++) //changed to:
for
(kk=1;kk<=k[r];kk++) //new
//delta[r][j]=delta[r][j]+(delta[r+1][kk])*(w[r+1][kk][j]);
//changed to:
delta[r-1][j]=delta[r-1][j]+(delta[r][kk])*(w[r][kk][j]);
//new
delta[r-1][j]=(delta[r-1][j])*a*y[r-1][j]*(1-y[r-1][j]);
//cout<<"delta
"<<delta[r][j]<<"\n";
delay(0);
} // the j loop
} // the r loop
for (r=1;r<=L;r++)
for (j=1;j<=k[r];j++)
for
(kk=0;kk<=k[r-1];kk++)
{
if ((count==1)
&& (iter==1))
w[r][j][kk]=w[r][j][kk]+eighta*(y[r-1][kk])*(delta[r][j]);
else
{
w[r][j][kk]=w[r][j][kk]+eighta*(y[r-1][kk])*(delta[r][j])+alpha*DELTAw[r][j][kk];
DELTAw[r][j][kk]=eighta*(y[r-1][kk])*(delta[r][j]);
}
//cout<<"w("<<r<<","<<j<<","<<kk<<")="<<w[r][j][kk];//new
//cout<<"\n";//new
}
delay(0);
count1=count1+1;
} //if loop for count1
if (count2 <= N2)
{
//99999
v[0][1]=trc2[count2].x1;
v[0][2]=trc2[count2].x2;
v[0][3]=trc2[count2].x3;
v[0][4]=trc2[count2].x4;
//cout<<v[0][1]<<"
";
for (i=1;i<=k[0];i++)
y[0][i]=v[0][i];
for (r=0;r<=L;r++)
y[r][0]=1;
for (r=1;r<=L;r++)
for (j=1;j<=k[r];j++)
{
v[r][j]=0;
for
(kk=1;kk<=k[r-1];kk++)
v[r][j]=v[r][j]+(w[r][j][kk])*(y[r-1][kk]);
v[r][j]=v[r][j]+w[r][j][0];
y[r][j]=1.0/(1+exp(-a*v[r][j]));
delay(0);
}
for (j=1;j<=k[L];j++)
delta[L][j]=a*(y[L][j])*(righty[2][j]-y[L][j])*(1-y[L][j]);
for (r=L;r>=2;r--)
{
//for (j=1;j<=k[r+1];j++)
//changed to:
for (j=1;j<=k[r];j++) //new
{
delta[r-1][j]=0;
//for
(kk=1;kk<=k[r+1];kk++) //changed to:
for (kk=1;kk<=k[r];kk++)
//delta[r][j]=delta[r][j]+(delta[r+1][kk])*(w[r+1][kk][j]);//changed
to
delta[r-1][j]=delta[r-1][j]+(delta[r][kk])*(w[r][kk][j]);//new
delta[r-1][j]=(delta[r-1][j])*a*y[r-1][j]*(1-y[r-1][j]);
delay(0);
} // the j loop
} // the r loop
for (r=1;r<=L;r++)
for (j=1;j<=k[r];j++)
for
(kk=0;kk<=k[r-1];kk++)
{
w[r][j][kk]=w[r][j][kk]+eighta*(y[r-1][kk])*(delta[r][j])+alpha*DELTAw[r][j][kk];
DELTAw[r][j][kk]=eighta*(y[r-1][kk])*(delta[r][j]);
}
//99999
count2=count2+1;
};
// the if loop for count2
count=count+1;
}; // the while loop for count
} //
the iter loop
/*
for
(ii=1;ii<=k[L];ii++)
for (jj=0;jj<=k[L-1];jj++)
cout<<w[L][ii][jj]<<"
";
cout<<"\n";
*/
//Now
classifying.
//First
class 1.
//Do
not forget the training error.
//error1=N;
for
(ff=1;ff<=N;ff++)
{
v[0][1]=tsc1[ff].x1;
v[0][2]=tsc1[ff].x2;
v[0][3]=tsc1[ff].x3;
v[0][4]=tsc1[ff].x4;
for (i=1;i<=k[0];i++)
y[0][i]=v[0][i];
for (r=0;r<=L;r++)
y[r][0]=1;
for (r=1;r<=L;r++)
for (j=1;j<=k[r];j++)
{
v[r][j]=0;
for
(kk=1;kk<=k[r-1];kk++)
v[r][j]=v[r][j]+(w[r][j][kk])*(y[r-1][kk]);
v[r][j]=v[r][j]+w[r][j][0];
y[r][j]=1.0/(1+exp(-a*v[r][j]));
}
flag=1;
for (ii=1;ii<=k[L];ii++)
if (round(y[L][ii]) !=
righty[1][ii]) flag=0;
//if (flag == 1) (error1=error1-1);
if (flag==0) Bagg1[ff][t]=0; else
Bagg1[ff][t]=1;
};
// Now
classifying on class 2.
//cout<<"\n";
//error2=NN;
for
(ff=1;ff<=NN;ff++)
{
v[0][1]=tsc2[ff].x1;
v[0][2]=tsc2[ff].x2;
v[0][3]=tsc2[ff].x3;
v[0][4]=tsc2[ff].x4;
for (i=1;i<=k[0];i++)
y[0][i]=v[0][i];
for (r=0;r<=L;r++)
y[r][0]=1;
for (r=1;r<=L;r++)
for (j=1;j<=k[r];j++)
{
v[r][j]=0;
for
(kk=1;kk<=k[r-1];kk++)
v[r][j]=v[r][j]+(w[r][j][kk])*(y[r-1][kk]);
v[r][j]=v[r][j]+w[r][j][0];
y[r][j]=1.0/(1+exp(-a*v[r][j]));
}
flag=1;
for (ii=1;ii<=k[L];ii++)
if (round(y[L][ii]) !=
righty[2][ii]) flag=0;
//if (flag == 1) (error2=error2-1);
if (flag==0) Bagg2[ff][t]=1; else
Bagg2[ff][t]=0;
};
} //
the t loop
error1=0;error2=0;
for
(i=1;i<=N;i++)
{
sum1=0;
for (t=1;t<=T;t++)
sum1=sum1+Bagg1[i][t];
if (sum1<N1*1.0/2) (error1=error1+1);
}
for
(i=1;i<=NN;i++)
{
sum1=0;
for (t=1;t<=T;t++)
sum1=sum1+Bagg2[i][t];
if (sum1>N1*1.0/2) (error2=error2+1);
}
cout<<"\n";
cout<<"Time
at the end of training and classifying is: ";
gettime(&tt);
cprintf("%2d:%02d:%02d.%02d",tt.ti_hour,
tt.ti_min, tt.ti_sec, tt.ti_hund);
cout<<"\n";
cout<<"\n";delay(3000);
error=error1+error2;
Perror1=(1.0*error1)/(1.0*N);
Perror2=(1.0*error2)/(1.0*NN);
Perror=(1.0*(error1+error2))/(1.0*(N+NN));
cout<<"Number
of errors from class 1 is: "<<error1<<"\n";
cout<<"Number
of errors from class 2 is: "<<error2<<"\n";
cout<<"Number
of errors from both classes is: "<<error<<"\n";
cout<<"Percentage
of error from class 1 is: "<<Perror1<<"\n";
cout<<"Percentage
of error from class 2 is: "<<Perror2<<"\n";
cout<<"Percentage
of error from both classes is: "<<Perror<<"\n";
cout<<"Save
the weights? If yes, press y. If no press n. \n";
char1=getche();
if
(char1=='y')
{
p =
fopen("C:\WEIGHTMX.DAT","w"); // TO OPEN THE FILE FO WRITING.
for (r=1;r<=L;r++)
for (i=1;i<=k[r];i++)
for (j=0;j<=k[r-1];j++)
{
//dat= w[r][i][j]; // TO ASSIGN DATA IN THE FILE.
//cout<<dat<<"
";
fprintf(p,"%f
",w[r][i][j]);
} // CLOSING THE
FILE.
fclose(p);
}
cout<<"\n";
delete
trc1;delete trc2;delete tsc1;delete tsc2;
}
//
//================
main()
{
//input
trc1[3002],trc2[3002],tsc1[3002],tsc2[3002];
int
ind,N1,N2,N,NN,ii,k,i,K,e1,e2;
double
tim;
char
ch,chr,char1;
classify
*classes;
do
{
clrscr();
for(i=1;i<=3;i++) cout<<"\n";
cout<<" To
use all the data : press
1\n\n";
cout<<" To
use part of the data : press
2\n\n";
cin>>char1;
if
(char1=='1')
{
again:
cout<<" BackProp : press 1\n\n";
cout<<"
To change the dimensions :
press 4\n\n";
cout<<"
To exit : press
3\n\n";
cin>>chr;
if(chr=='1')
{
void start();
classes->backprop();
void stop();
double time();
goto again;
}
if
(chr=='3')
exit(0);
if
(chr=='4')
{
cout<<"Enter N1
";cin>>N1;cout<<"\n";
cout<<"Enter N2
";cin>>N2;cout<<"\n";
cout<<"Enter N
";cin>>N;cout<<"\n";
cout<<"Enter NN ";cin>>NN;cout<<"\n";
goto again;
}
}
if (char1=='2')
{
}
ch=getche();
}
while ((ch !='n') && (ch !='N'));
return
0;
}