Ticket #598: ann.cpp

File ann.cpp, 13.8 KB (added by Lucky, 13 years ago)

This is a simple implementation of a multilayer feed forward back propagation neural network that classifies Iris data samples into 3 classes.

Line 
1//Author-Snigdha Chandan Khilar
2#include<stdio.h>
3#include<conio.h>
4#include<math.h>
5#include<fstream>
6#include<vector>
7#define numinput 4
8#define numoutput 3
9using namespace std;
10double getRand(void){return ((double)rand())/(double)RAND_MAX;}
11double activation_function(double val)
12{
13 double v=val;
14 v=(1+exp(-v));
15 return 1/v;
16}
17int main()
18{
19 int i,j=0,k=0,l,var,numhidden,bias=1,epoch;
20 int train_class[150],test_class[30],data_class[150];
21 double attr,sum,IH,HO;
22 double sepal_length[150],sepal_width[150],petal_length[150],petal_width[150];
23 double sepal_length_testing[30],sepal_width_testing[30],petal_length_testing[30],petal_width_testing[30];
24 double sepal_length_training[150],sepal_width_training[150],petal_length_training[150],petal_width_training[150];
25 ifstream infile;
26 infile.open("data.txt");
27 ofstream outfile1("training.txt");
28 ofstream outfile2("testing.txt");
29 if(infile.is_open())
30 {
31 for(i=0;!infile.eof();i++)
32 {
33 infile>>attr;
34 sepal_length[i]=attr;
35 infile>>attr;
36 sepal_width[i]=attr;
37 infile>>attr;
38 petal_length[i]=attr;
39 infile>>attr;
40 petal_width[i]=attr;
41 infile>>var;
42 data_class[i]=var;
43 }
44 }
45 infile.close();
46 for(i=1;i<=150;i++)
47 {
48 if(i%5==0)
49 {
50
51 sepal_length_testing [j]=sepal_length[i];
52 sepal_width_testing[j]=sepal_width[i];
53 petal_length_testing[j]=petal_length[i];
54 petal_width_testing [j]=petal_width[i];
55 test_class[j]=data_class[i];
56 outfile2<<sepal_length[i]<<" "<<sepal_width[i]<<" "<<petal_length[i]<<" "<<petal_width[i]<<" => "<<data_class[i]<<endl;
57 j++;
58 }
59 else
60 {
61 sepal_length_training[k]=sepal_length[i];
62 sepal_width_training[k]=sepal_width[i];
63 petal_length_training[k]=petal_length[i];
64 petal_width_training[k]=petal_width[i];
65 train_class[k]=data_class[i];
66 outfile1<<sepal_length[i]<<" "<<sepal_width[i]<<" "<<petal_length[i]<<" "<<petal_width[i]<<" "<<data_class[i]<<endl<<endl;
67 k++;
68 }
69
70 }
71 outfile1.close();
72 outfile2.close();
73 printf("\n\nEnter the number of neurons in the input layer is 4.\n");
74 printf("\nEnter the number of neurons in the output layer is 3.\n");
75 printf("\nEnter the number of neurons in the hidden layer :(1-4) \n");
76 scanf("%d",&numhidden);
77 printf("\nEnter the number of epochs :");
78 scanf("%d",&epoch);
79 printf("\nEnter learning rate for Input Layer-Hidden Layer (0.07-0.7):");
80 scanf("%lf",&IH);
81 printf("\nEnter learning rate for Hidden Layer-Output Layer (0.07-0.7):");
82 scanf("%lf",&HO);
83 double wgtIH[numhidden][numinput],wgtHO[numoutput][numhidden];
84 double hidden[numhidden+1],input[numinput+1],output[numoutput];
85 double err_o[numoutput],err_h[numhidden];
86 for(i=0;i<numhidden;i++)
87 {
88 for(j=0;j<numinput;j++)
89 wgtIH[i][j]=0;
90 }
91 for(i=0;i<numoutput;i++)
92 {
93 for(j=0;j<numhidden;j++)
94 wgtHO[i][j]=0;
95 }
96 //Initialize Random Weights From Input Layer to Hidden Layer
97 for(i=0;i<numhidden+1;i++)
98 {
99 for(j=0;j<numinput+1;j++)
100 wgtIH[i][j]=(getRand())/5;
101 }
102 //Initialize Random Weights From Hidden Layer to Output Layer
103 for(i=0;i<numoutput;i++)
104 {
105 for(j=0;j<numhidden+1;j++)
106 wgtHO[i][j]=(getRand())/5;
107 }
108 while(epoch--)
109 {
110 for(l=0;l<120;l++)
111 {
112
113 input[0]=sepal_length_training[l];
114 input[1]=sepal_width_training[l];
115 input[2]=petal_length_training[l];
116 input[3]=petal_width_training[l];
117 input[4]=bias;
118 for(i=0;i<numhidden+1;i++)
119 {
120 sum=0.0;
121 for(j=0;j<numinput+1;j++)
122 sum=sum+wgtIH[i][j]*input[j];
123 sum=activation_function(sum);
124 hidden[i]=sum;
125 }
126 for(i=0;i<numoutput;i++)
127 {
128 sum=0.0;
129 for(j=0;j<numhidden+1;j++)
130 sum=sum+wgtHO[i][j]*hidden[j];
131 sum=activation_function(sum);
132 output[i]=sum;
133 }
134 // error calcalutaion @output layer
135 if(train_class[l]==1)
136 {
137 if(output[0]<0.8)
138 err_o[0]=output[0]*(1-output[0])*(1-output[0]);
139 else
140 err_o[0]=0;
141 if(output[1]>0.2)
142 err_o[1]=output[1]*(1-output[1])*(-output[1]);
143 else
144 err_o[1]=0;
145 if(output[2]>0.2)
146 err_o[2]=output[2]*(1-output[2])*(-output[2]);
147 else
148 err_o[2]=0;
149 }
150 else if(train_class[l]==2)
151 {
152 if(output[0]>0.2)
153 err_o[0]=output[0]*(1-output[0])*(-output[0]);
154 else
155 err_o[0]=0;
156 if(output[1]<0.8)
157 err_o[1]=output[1]*(1-output[1])*(1-output[1]);
158 else
159 err_o[1]=0;
160 if(output[2]>0.2)
161 err_o[2]=output[2]*(1-output[2])*(-output[2]);
162 else
163 err_o[2]=0;
164
165 }
166 else if(train_class[l]==3)
167 {
168
169 if(output[0]>0.2)
170 err_o[0]=output[0]*(1-output[0])*(-output[0]);
171 else
172 err_o[0]=0;
173 if(output[1]>0.2)
174 err_o[1]=output[1]*(1-output[1])*(-output[1]);
175 else
176 err_o[1]=0;
177 if(output[2]<0.8)
178 err_o[2]=output[2]*(1-output[2])*(1-output[2]);
179 else
180 err_o[2]=0;
181 }
182
183 for(i=0;i<numhidden+1;i++)
184 {
185 sum=0.0;
186 for(j=0;j<numoutput;j++)
187 sum+=wgtHO[i][j]*err_o[j];
188 err_h[i]=hidden[i]*(1-hidden[i])*sum;
189 }
190
191 // error calcalutaion @ hiddenlayer layer
192 // hidden to output layerweight updation.
193 for(i=0;i<numoutput;i++)
194 {
195 for(j=0;j<numhidden+1;j++)
196 wgtHO[i][j]+=HO*err_o[i]*hidden[j];
197 }
198 // input to hidden layerweight updation..
199 for(i=0;i<numhidden+1;i++)
200 {
201 for(j=0;j<numinput+1;j++)
202 wgtIH[i][j]+=IH*err_h[i]*input[j];
203 }
204 }
205}
206
207for(l=0;l<29;l++)
208{
209
210 input[0]=sepal_length_testing[l];
211 input[1]=sepal_width_testing[l];
212 input[2]=petal_length_testing[l];
213 input[3]=petal_width_testing[l];
214 input[4]=bias;
215 for(i=0;i<numhidden+1;i++)
216 {
217 sum=0.0;
218 for(j=0;j<numinput+1;j++)
219 {
220 sum=sum+wgtIH[i][j]*input[j];
221 }
222 sum=activation_function(sum);
223 hidden[i]=sum;
224 }
225 for(i=0;i<numoutput;i++)
226 {
227 sum=0.0;
228 for(j=0;j<numhidden+1;j++)
229 sum=sum+wgtHO[i][j]*hidden[j];
230 sum=activation_function(sum);
231 output[i]=sum;
232 }
233 printf("\n\n");
234 for(i=0;i<numoutput;i++)
235 printf("%lf\t",output[i]);
236 if((output[0]>0.8)&&(output[1]<0.2)&&(output[2]<0.2))
237 printf("setosa %d\n\n",test_class[l]);
238 else if((output[0]<0.2)&&(output[1]>0.8)&&(output[2]<0.2))
239 printf("versicolor %d\n\n",test_class[l]);
240 else if((output[0]<0.2)&&(output[1]<0.2)&&(output[2]>0.8))
241 printf("virginea %d\n\n",test_class[l]);
242 else
243 printf("Cannot classify\n\n");
244}
245 getch();
246 return 0;
247}