OK, this is my code, I still need to "account for the derivative of the activation when calculating error"
and i have no idea of how to do that, so be free to change my code anyway you want.
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];
float tres[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){
return sin(atan(x));
}
/*double f(double x){
if(x > 0.0) return 1.0;
return 0.0;
}*/
/*double f(double x){
return 1.0 / (1.0 + pow(2.71828182846 , 0.0 - x));
}*/
bool target(int y, int z) {
if ((y && !z) || (z && !y)) return 1;
return 0;
}
int educate() {
input[0] = 1; //bias, uvijek 1
input[3] = 1;
input[4] = 1;
input[5] = 1;
input[6] = 1;
input[1] = rand() % 2;
input[2] = rand() % 2;
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]);
percept[1] = f(sum[1]);
percept[2] = f(sum[2]);
percept[3] = f(sum[3]);
percept[4] = f(sum[4]);
if (percept[4] == goal) {
successive_right++;
}
else {
successive_right = 0;
error[0] = goal - percept[4];
error[1] = (error[0]*weight[14]);
error[2] = (error[0]*weight[13]);
error[3] = (error[1]*weight[1])+(error[2]*weight[2]);
error[4] = (error[1]*weight[4])+(error[2]*weight[5]);
weight[0] += learning_rate*error[4]*input[0];//input
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
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
weight[13] += learning_rate3*error[0]*percept[4];//output
weight[14] += learning_rate3*error[0]*percept[3];//output
}
return;
}
int main(){
tres[0] = 0;
tres[1] = 0;
tres[2] = 0;
tres[3] = 0;
tres[4] = 0;
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]);
percept[0] = (weight[0]*input[0]+ weight[1]*input[1]+weight[2]*input[2] > tres[0]);
percept[1] = (weight[3]*input[3]+weight[4]*input[1]+weight[5]*input[2] > tres[1]);
percept[2] = (weight[6]*input[4]+weight[7]*percept[0]+weight[8]*percept[1] > tres[2]);
percept[3] = (weight[9]*input[5]+weight[10]*percept[0]+weight[11]*percept[1] > tres[3]);
percept[4] = (weight[12]*input[6]+weight[13]*percept[2]+weight[14]*percept[3] > tres[4]);
printf("%d\n", percept[4]);
return 0;
}