Last active
June 13, 2020 18:06
-
-
Save TanmayChakrabarty/1ea82a9cbd78f5035cf085f2f1541e73 to your computer and use it in GitHub Desktop.
Single Layer Perceptron Learning Algorithm in C++
This file contains hidden or 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
/* | |
Perceptron Leanrning Algorithm | |
By Tanmay Chakrabarty | |
Compiled and Ran succesfully with C++ compiler @https://www.onlinegdb.com/ | |
*/ | |
#include<iostream> | |
using namespace std; | |
int | |
main() { | |
double weights[4], Threshold = 0.5, sum = 0.0, output = 0.0; | |
int values[16][4], given_value[4], i, j, k; | |
char | |
const * 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 | |
*/ | |
//(calculating and storing the values for the values) | |
i = 0; | |
j = 0; | |
while (i < 16) | |
{ | |
j = 3; | |
k = i; | |
while (j >= 0) | |
{ | |
values[i][j] = k % 2; | |
k = k / 2; | |
j--; | |
} | |
i++; | |
} | |
//starting the main loop | |
option = 's'; | |
while (option == 's' || option == 'S') | |
{ | |
cout << | |
"No kind of input error has been handled, enter accuret values\n"; | |
cout << "Please input the weights:\n"; | |
j = 0; | |
while (j < 4) | |
{ | |
cout << "\tWeights 0" << (j+1) << ": "; | |
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) | |
{ | |
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) | |
{ | |
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; | |
} | |
} | |
} | |
cout << "\nWeights are " << weights[0] << " , " << weights[1] << | |
" , " << | |
weights[2] << " , " << weights[3] << " while threshold is " << | |
Threshold << "\n"; | |
cout << | |
"\nWeights are set, ready to percept\n"; | |
option = 'e'; | |
while (option == 'e' | |
or option == 'E') | |
{ | |
cout << | |
"\nPlease enter an element to find which class is it of : \t"; | |
j = 0; | |
while (j < 4) | |
{ | |
cin >> given_value[j]; //taking the value to test from user | |
cout << "\t\t\t\t\t\t\t"; | |
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: "; | |
cin >> option; | |
if (option != 'e' || option != 'E' || option != 's' || | |
option != 'S') | |
return 0; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment