Single Layer Perceptron Learning Algorithm and Flowchart of The Program and The Code of The Program in C
Single Layer Perceptron Learning Algorithm and Flowchart of The Program and The Code of The Program in C
Single Layer Perceptron Learning Algorithm and Flowchart of The Program and The Code of The Program in C
If the desired output mismatches with the output Decrease those weights which have corresponding 1 in the input Else Take the next input and go to Step 2 Else If the desired output mismatches with the output Increase those weights which have corresponding 1 in the input Else Take the next input and go to Step 2 Step 5: While all weights has been set, take input from the user. Step 6: Get the weighted sum for this input Step 7: Compare the weighted sum with the threshold if weighted sum is less then the threshold input is from Class A Else input is from class B
Part 02 of the Flowchart of the Program on Single Layer Perceptron Learning Algorithm
8)
*/
9) #include<iostream.h> 10) #include<conio.h> 11) 12) 13) 14) 15) 16) 17) 18) 19) 20) 21) int main() { ouble weights[4], Threshold = 0.5, sum = 0.0, output=0.0; int values[16][4], given_value[4], i, j, k; char* new_start = "Not Required"; char option; /* weights for storing the weights | Threshold to store the threshold sum to store the weighted sum | output to store the 0 or 1 after comparing the sum with the threshold values to store the values | given_value is the value that will entered by the user to test i, j, k are loop controller | new_start is an indicator whether to start from the beginning or not while weights have been found ok for one of the 16 values option has been used to take input from the user for program flow control */
22) 23)
24) //---(calculating and storing the values for the values) 25) i=0; 26) j=0; 27) while(i<16) 28) { 29) j=3; 30) k=i; 31) while(j>=0) 32) { 33) values[i][j]=k%2; 34) k=k/2; 35) j--; 36) } 37) i++; 38) } 39) //------------------------------------------------------
40) 41) 42) 43) 44) 45) 46) 47) 48) 49) 50) 51) 52) 53) 54)
//starting the main loop option='s'; while(option=='s' || option=='S') { cout<<"\n---No kind of input error has been handled, enter accuret values)-----"; cout<<"\nPlease input the weights:\n"; j=0; while(j<4) { cout<<"\tWeights 01: "; cin>>weights[j]; //taking the weights from the user j++; } cout<<"Please input the Threshold : "; cin>>Threshold; //taking threshold from the user i=0; while(i<16) //to run the loop 16 times (0-15) { sum=0; j=0; while(j<4) { sum += (values[i][j]*weights[j]); //calculating the weighted sum j++; } if(sum < Threshold) //comparing weighted sum with threshold output = 0; //and setting the value for the output else output = 1; if(i < 8) //to find which class the value is from, it indicates that this is from class A (0-7) {
55) 56) 57) 58) 59) 60) 61) 62) 63) 64) 65) 66) 67) 68) 69)
70)
80)
81)
94) 95)
if(output != 0) //The desired output of class A is 0, but if not then we will decrease { //that corresponding weight of the input j=0; while(j<4) { //multiplying with corresponding values beacuse we need to weights[j] -= ((0.1) * (values[i][j])); //that weight which correspondingly having 1 in its input j++; //if value is 0, "((0.1) * (values[i][j]))" will result 0 } //thus the weight will not decrease, otherwise, it will new_start = "Required"; //as wrong weights has been found, thus after we have decreased the weigths i=i; //we will test these weights for these inputs again and after ok, this value of } //new_start will indicate to start from first value with these new corrected weights else { if(new_start=="Required") //when weights has been found ok and new_start = "Required", it means we will start from first { new_start="Not Required"; //Untill we found these weight faulty for any value we will just go on i=0; } else i++; //weights are ok, so go on } } else //this is class B part and same as previous, only that we will increase weights here { if(output == 1)
96) 97) 98) 99) 100) 101) 102) 103) 104) 105) 106) 107) 108) 109) 110) 111) 112) 113) 114) 115) 116) 117)
{ if(new_start=="Required") { new_start="Not Required"; i=0; } else i++; } else { j=0; while(j<4) { weights[j] += ((0.1) * (values[i][j])); j++; } new_start = "Required"; i=i; } } }
118) cout<<"\n\nWeights are "<<weights[0]<<" , "<<weights[1]<<" , "<<weights[2]<<" , "<<weights[3]<<" while threshold is "<<Threshold; 119) cout<<"\n\n---------------------(Weights are set, ready to percept)---------------------"; 120) option='e'; 121) while(option=='e' or option=='E') 122) { 123) cout<<"\n\nPlease enter an element to find which class is it of : \t"; j=0; while(j<4) { cin>>given_value[j]; value to test from user 128) cout<<"\t\t\t\t\t\t\t"; 124) 125) 126) 127)
//taking the
129) 130) 131) 132) 133) 134) 135) 136) 137) 138) 139)
140) 141)
j++; } sum=0; j=0; while(j<4) { sum += (given_value[j]*weights[j]); //getting the weight j++; } if(sum < Threshold) //comparing the weight with the threshold and getting the result cout<<"\nThe percepted element "<<given_value[0]<<given_value[1]<<given_value[2]<<given_value [3]<<" is from Class A\n"; else cout<<"\nThe percepted element "<<given_value[0]<<given_value[1]<<given_value[2]<<given_value [3]<<" is from Class B\n"; cout<<"\nPress """"e"""" to enter an element again or """"s"""" to start from the beginning, any (else key+enter) to exit: "; option=getch(); if(option!='e' || option!='E' || option!='s' || option!='S' ) system("EXIT"); } }
142)
[It was an assignment when I was having the course Theory of Computation. I don't think this blog post has simplified the matter at all. However if it helps you anyway, I will be glad to know that.]