Hi, I encountered some serious problems with multilayer perceptrons, I don't understand anything and everybody seems to talk about different things...Please edit my code and tell me what i did wrong...
Code:#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
float error[100];
int i;
float weight[100];
bool percept[100];
int input[100];
double sum[100];
int successive_right = 0;
int total_go_round = 1;
const float learning_rate = 0.1f;
const float learning_rate2 = 0.25f;
const float learning_rate3 = 0.1f;
double f(double x){ //NOLINEAR ACTIVATION FUNCTION
return sin(atan(x));
}
bool target(int y, int z) {
if ((y && !z) || (z && !y)) return 1;
return 0;
}
int educate() {
input[0] = 1; //BIAS
input[3] = 1; //BIAS
input[4] = 1; //BIAS
input[5] = 1; //BIAS
input[6] = 1; //BIAS
input[1] = rand() % 2; //INPUT1
input[2] = rand() % 2; //INPUT2
bool goal = target(input[1], input[2]);
sum[0] = (weight[0]*input[0]+weight[1]*input[1]+weight[2]*input[2]);
sum[1] = (weight[3]*input[3]+weight[4]*input[1]+weight[5]*input[2]);
sum[2] = (weight[6]*input[4]+weight[7]*percept[0]+weight[8]*percept[1]);
sum[3] = (weight[9]*input[5]+weight[10]*percept[0]+weight[11]*percept[1]);
sum[4] = (weight[12]*input[6]+weight[13]*percept[2]+weight[14]*percept[3]);
percept[0] = f(sum[0]); //ACTIVATION
percept[1] = f(sum[1]); //ACTIVATION
percept[2] = f(sum[2]); //ACTIVATION
percept[3] = f(sum[3]); //ACTIVATION
percept[4] = f(sum[4]); //ACTIVATION
if (percept[4] == goal) {
successive_right++;
}
else {
successive_right = 0;
error[0] = goal - percept[4]; //ERRORS IN BACKPROPAGATION
error[1] = (error[0]*weight[14]);//ERRORS IN BACKPROPAGATION
error[2] = (error[0]*weight[13]);//ERRORS IN BACKPROPAGATION
error[3] = (error[1]*weight[1])+(error[2]*weight[2]);//ERRORS IN BACKPROPAGATION
error[4] = (error[1]*weight[4])+(error[2]*weight[5]);//ERRORS IN BACKPROPAGATION
weight[0] += learning_rate*error[4]*input[0];//input layer
weight[1] += learning_rate*error[4]*input[1];//input
weight[2] += learning_rate*error[4]*input[2];//input
weight[3] += learning_rate*error[4]*input[3];//input
weight[4] += learning_rate*error[3]*percept[1];//input
weight[5] += learning_rate*error[3]*percept[0];//input
weight[6] += learning_rate2*error[3]*input[4];//hiden layer
weight[7] += learning_rate2*error[2]*percept[0];//hiden
weight[8] += learning_rate2*error[2]*percept[1];//hiden
weight[9] += learning_rate2*error[2]*input[5];//hiden
weight[10] += learning_rate2*error[1]*percept[0];//hiden
weight[11] += learning_rate2*error[1]*percept[1];//hiden
weight[12] += learning_rate3*error[1]*input[6];//output layer
weight[13] += learning_rate3*error[0]*percept[4];//output
weight[14] += learning_rate3*error[0]*percept[3];//output
}
return;
}
int main(){
srand(time(NULL));
for(i=0;i<14;i++){
weight[i] = 0.5;
}
total_go_round=1;
while(total_go_round <= 5000){
educate();
total_go_round++;
}
printf("inputs:\n");
scanf("%d", &input[1]);
scanf("%d", &input[2]);
sum[0] = (weight[0]*input[0]+weight[1]*input[1]+weight[2]*input[2]);
sum[1] = (weight[3]*input[3]+weight[4]*input[1]+weight[5]*input[2]);
sum[2] = (weight[6]*input[4]+weight[7]*percept[0]+weight[8]*percept[1]);
sum[3] = (weight[9]*input[5]+weight[10]*percept[0]+weight[11]*percept[1]);
sum[4] = (weight[12]*input[6]+weight[13]*percept[2]+weight[14]*percept[3]);
percept[0] = f(sum[0]); //ACTIVATION
percept[1] = f(sum[1]); //ACTIVATION
percept[2] = f(sum[2]); //ACTIVATION
percept[3] = f(sum[3]); //ACTIVATION
percept[4] = f(sum[4]); //ACTIVATION
printf("%d\n", percept[4]);
return 0;
}