-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from sofia100/main
soft computing algos
- Loading branch information
Showing
2 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int m,n; | ||
cout<<"enter no.of features and no.of training datasets: \n"; | ||
cin>> m>>n; | ||
|
||
int wt1[m], wt2[m]; | ||
|
||
int input[n][m]; | ||
cout<<"enter the input matrix row wise "<<endl; | ||
|
||
for(int i=0;i<n;i++) | ||
{ | ||
for(int j=0;j<m;j++) | ||
{ | ||
cin>>input[i][j]; | ||
} | ||
} | ||
|
||
int target1[n], target2[n]; | ||
|
||
cout<<" Enter the target in binary : "<<endl; | ||
for(int i=0;i<n;i++) | ||
{ | ||
cin>>target1[i]; | ||
} | ||
|
||
cout<<"Enter the target in bipolar: "<<endl; | ||
for(int i=0;i<n;i++) | ||
{ | ||
cin>>target2[i]; | ||
} | ||
|
||
for(int i=0;i<m;i++) | ||
{ | ||
wt1[i]=0; //step 1: initialise all wts to 0 | ||
wt2[i]=0; | ||
} | ||
|
||
for(int j=0;j<n;j++) //step 2: for each training vector on target pair do steps 3 to 5 | ||
{//step 3: x_0 = 1 and xi = si | ||
cout<<"###########j="<<j<<endl; | ||
for(int i=0; i<m;i++) | ||
{ | ||
//step 4: y_out = T_i | ||
//step 5: adjust the weights as WT_new= WT_old + (x_i*y_out) | ||
wt1[i]+= (input[j][i]*target1[j]); | ||
cout<<"weight1 at i="<<i<<" is "<<wt1[i]<<endl; | ||
wt2[i]+= (input[j][i]*target2[j]); | ||
cout<<"wt2 at i="<<i<<" is "<<wt2[i]<<endl; | ||
|
||
} | ||
|
||
} | ||
cout<<"************OUTPUT**************\nafter 1 epoch: binary weights: "<<endl; | ||
|
||
for (int i = 0; i < m; ++i) | ||
{ | ||
/* code */ | ||
cout<<wt1[i]<<" "; | ||
} | ||
cout<<"\nafter 1 epoch: bipolar weights: "<<endl; | ||
|
||
for (int i = 0; i < m; ++i) | ||
{ | ||
/* code */ | ||
cout<<wt2[i]<<" "; | ||
} | ||
|
||
|
||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
//given by Rosenblat | ||
//inspiration from retina of eye | ||
//seems like single feed forward network | ||
#include<iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int m,n; | ||
float lr, t; | ||
cout<<"enter no.of features , no.of training datasets , threshold and learning rate : \n"; | ||
cin>> m>>n>>t>>lr; | ||
|
||
int wt2[m]; | ||
|
||
int input[n][m]; | ||
cout<<"enter the input matrix row wise "<<endl; | ||
|
||
for(int i=0;i<n;i++) | ||
{ | ||
for(int j=0;j<m;j++) | ||
{ | ||
cin>>input[i][j]; | ||
} | ||
} | ||
|
||
int target2[n]; | ||
|
||
|
||
|
||
cout<<"Enter the target in bipolar: "<<endl; | ||
for(int i=0;i<n;i++) | ||
{ | ||
cin>>target2[i]; | ||
} | ||
|
||
for(int i=0;i<m;i++) | ||
{ | ||
//step 1: initialise all wts to 0 | ||
wt2[i]=0; | ||
} | ||
int y_out; | ||
|
||
for(int j=0;j<n;j++) | ||
{ | ||
cout<<"\n########### training set j="<<j<<endl; | ||
int y_in=0; | ||
for(int i=0; i<m;i++) | ||
{ | ||
y_in+= wt2[i]*input[j][i]; | ||
} | ||
cout<< "y_in = "<<y_in<<endl; | ||
|
||
if(y_in > t) | ||
y_out=1; | ||
else if(y_in < -1*t) | ||
y_out=-1; | ||
else | ||
y_out=0; | ||
// cout<<"wt2 at i="<<i<<" is "<<wt2[i]<<endl; | ||
|
||
cout<< "y_out = "<<y_out<<endl; | ||
|
||
|
||
if(y_out != target2[j]) | ||
{ | ||
for(int i=0; i<m;i++) | ||
|
||
wt2[i]+= (lr *target2[j] *input[j][i]); | ||
cout<<"updated weights :\n"; | ||
for (int i = 0; i < m; ++i) | ||
{ | ||
/* code */ | ||
cout<<wt2[i]<<" "; | ||
} | ||
|
||
} | ||
|
||
else | ||
{cout<<"\ntarget and y_out are equal. stop the training"; | ||
break; | ||
} | ||
|
||
|
||
|
||
} | ||
cout<<"\n************OUTPUT**************\nafter 1 epoch: weights: "<<endl; | ||
|
||
|
||
for (int i = 0; i < m; ++i) | ||
{ | ||
/* code */ | ||
cout<<wt2[i]<<" "; | ||
} | ||
|
||
|
||
|
||
return 0; | ||
} |