Created
May 30, 2016 01:39
-
-
Save Kcrong/c2e8d8f51260d75d25d8b81c82b8d098 to your computer and use it in GitHub Desktop.
neuron_ask_main
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
#include <iostream> | |
#include <ctime> | |
using namespace std; | |
void main() | |
{ | |
srand((unsigned)time(NULL)); // Set random seed | |
Neuron* neuron = new Neuron(2, 0.1); | |
// Sample Sets // | |
double sample_input[4][2] = {{0,0},{0,1},{1,0},{1,1}}; | |
double sample_output[4] = { 0, 0, 0, 1 }; | |
for(int i=0; i<5000; i++) | |
{ | |
for(int j=0; j<4; j++) | |
{ | |
neuron->learn( sample_input[j], sample_output[j]); | |
} | |
neuron->fix(); | |
// Print result // | |
if((i+1)%100==0) | |
{ | |
cout<<"------ Learn "<<i+1<<" times -----"<<endl; | |
for(int j=0; j<4; j++) | |
{ | |
cout<<sample_input[j][0]<<' '<<sample_input[j][1]<<" : " | |
<<neuron->work(sample_input[j])<<endl; | |
} | |
} | |
} | |
delete neuron; | |
} |
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
#include <cstdlib> | |
#include <cmath> | |
#define sigmoid(x) ( 1.0/(1.0+exp(-(x))) ) | |
class Neuron | |
{ | |
private: | |
int num_of_input; // Number of input synapse | |
double* input_weight; // Chemical signal weight | |
double* weight_error; // Cumulative weight error | |
double alpha; // Sensitivity | |
public: | |
Neuron(int num_of_input, double alpha) | |
{ | |
this->num_of_input = num_of_input; | |
this->alpha = alpha; | |
input_weight = new double[num_of_input+1]; // The last one is for constant input! | |
weight_error = new double[num_of_input+1]; | |
for(int i=0; i<num_of_input+1; i++) | |
{ | |
input_weight[i] = ((double)rand()/RAND_MAX)*2-1; // -1 ~ 1 Random | |
weight_error[i] = 0.0; | |
} | |
} | |
~Neuron() | |
{ | |
delete[] input_weight; | |
delete[] weight_error; | |
} | |
double work(double input[]) | |
{ | |
double sum = 0; | |
for(int i=0; i<num_of_input; i++) | |
{ | |
sum += input_weight[i] * input[i]; | |
} | |
sum += input_weight[num_of_input] * 1.0; // Constant input | |
return sigmoid(sum); | |
} | |
void learn(double input[], double target) | |
{ | |
double output = work(input); | |
double output_error = output - target; | |
for(int i=0; i<num_of_input; i++) | |
{ | |
weight_error[i] += output_error * input[i] * output * (1-output); | |
} | |
weight_error[num_of_input] += output_error * 1.0 * output * (1-output); | |
} | |
void fix() | |
{ | |
for(int i=0; i<num_of_input+1; i++) | |
{ | |
input_weight[i] -= alpha * weight_error[i]; | |
weight_error[i] = 0.0; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment