//dnn dense nero network sample for AND operation
//2017/06/27
#ifndef DNN_EXAMPLE
#define DNN_EXAMPLE
#include <stdio.h>
float input[4][2] = {{0,0},{0,1},{1,0},{1,1}};
float output[4][1] = {{0},{0},{0},{1}};
float* weight;
//one layer to construct net
void load_weight(){
weight = new float[2];
weight[0] = 0.1;
weight[1] = 0.2;
}
float threshhold(float x){
if(x>=0.9)return 1.0;
else return 0;
}//float
void show(){
printf("%f %f\n",weight[0],weight[1]);
for(int i = 0;i<4;i++){
float sum = 0.0;
sum = input[i][0]*weight[0]+input[i][1]*weight[1];
float value = threshhold(sum);
printf("%f %f\t %f\n",input[i][0],input[i][1],value);
}//for
}//void
void train(){
for(int i = 0;i<4;i++){
float error = 0.0;
float sum = 0.0;
sum = input[i][0]*weight[0]+input[i][1]*weight[1];
float value = threshhold(sum);
error = output[i][0] - value;
weight[0] = weight[0] + error*0.01;
weight[1] = weight[1] + error*0.01;
}
}
void main(int argc, char **argv){
load_weight();
int iterations = 40;
for(int i=0;i<iterations;i++)
train();
show();
getchar();
}
#endif