-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaline.cpp
More file actions
74 lines (58 loc) · 1.12 KB
/
adaline.cpp
File metadata and controls
74 lines (58 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<math.h>
using namespace std;
int main(int argc, char const *argv[])
{
//hardcoded
int n=4;//no.of training sets
int m=3;//no.of features
float w[]={0.1,0.1,0.1};
float lr=0.1;
int x[n][m]={{1,1,1}, {1,1,-1}, {1,-1,1},{1,-1,-1}};
int target[]={1,1,1,-1};
// for(int i=0;i<n;i++)
// {for(int j=0;j<m;j++)
// cout<<x[i][j]<<" ";
// cout<<endl;
// }
float se[n];//squared error
for(int i=0;i<n;i++)
{
cout<<"\n######### for training set i= "<<i;
float y_in=0;
for(int j=0;j<m;j++)
{
y_in+=w[j]*x[i][j];
}
cout<<"\nY_in = "<<y_in;
float error= target[i]-y_in;
cout<<"\nerror = "<<error;
se[i]=error*error;
cout<<"\nsquared error= "<<se[i];
if(se[i]<lr)
{
cout<<"weights adjusted..training stopped";
break;
}
else
{
for(int j=0;j<m;j++)
{
w[j]+=(lr*error*x[i][j]);
cout<<"\nfor j= "<<j<< "weight is"<<w[j];
}
}
}
float mse=0;
for(int k=0;k<n;k++)
{
mse+=se[k];
}
mse/=n;
cout<<"\n\n************OUTPUT**************rmse = "<<sqrt(mse)<<"\nweights:\n";
for(int j=0;j<m;j++)
{
cout<<w[j]<<" ";
}
return 0;
}