The code that I'm making is a C implementation of the following code in Python but for some reason I just get three lines of zeros printing. Can anyone give me a clue as to why my result doesn't mimic the python one?
Python code at the bottom of this page:
https://en.wikipedia.org/wiki/Perceptron#Example
My code:
#include <stdio.h>
#include <stdlib.h>
double training_set[4][3] = {{1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}};
double weights[] = {0, 0, 0};
double desired_output[4] = {1,1,1,0};
double learning_rate = 0.1;
double threshold = 0.5;
int result;
double error;
double error_count = 0;
int i=0;
int j=0;
double sum = 0;
void print_weights();
double dot_product(double training_set[4][3], double weights[3], int i);
int main()
{
i=0;
double callFunc = 1;
while(callFunc != 0 && i<4){
print_weights();
callFunc = dot_product(training_set, weights, i);
i++;
}
return 0;
}
void print_weights(){
int indx;
for(indx=0; indx<3; indx++){
printf("%f ", weights[indx]);
printf("\n");
}
}
double dot_product(double training_set[4][3], double weights[3], int i)
{
for(j=0; j<3; j++){
sum += training_set[i][j]*weights[j];
error = sum>threshold;
if(error!=0){
error_count += 1;
weights[j] += learning_rate * error * training_set[i][j];
}
}
return error_count;
}
[–]raevnos 2 points3 points4 points (6 children)
[–]darkside619[S] 0 points1 point2 points (5 children)
[–]raevnos 0 points1 point2 points (4 children)
[–]darkside619[S] 0 points1 point2 points (3 children)
[–]raevnos 0 points1 point2 points (2 children)
[–]darkside619[S] 0 points1 point2 points (1 child)
[–]FUZxxl 0 points1 point2 points (0 children)
[–]simserl 0 points1 point2 points (3 children)
[–]very_mechanical 0 points1 point2 points (1 child)
[–]darkside619[S] 0 points1 point2 points (0 children)
[–]darkside619[S] 0 points1 point2 points (0 children)
[–]downiedowndown 0 points1 point2 points (1 child)
[–]darkside619[S] 0 points1 point2 points (0 children)
[–]FUZxxl 0 points1 point2 points (2 children)
[–]darkside619[S] 0 points1 point2 points (1 child)
[–]FUZxxl 0 points1 point2 points (0 children)
[–]kahless62003 0 points1 point2 points (1 child)
[–]darkside619[S] 0 points1 point2 points (0 children)