Skip to content

Instantly share code, notes, and snippets.

@spceaza
Created September 20, 2015 13:34
Show Gist options
  • Save spceaza/13bb077f65ee196977c2 to your computer and use it in GitHub Desktop.
Save spceaza/13bb077f65ee196977c2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <random>
#include <cmath>
void print_output(int impulse[2], double** weight, double ** threshold);
double evaluate(int impulse[2], double** weight, double ** threshold)
{
double output1 = impulse[0]*weight[0][0] + impulse[1]*weight[0][2];
output1 = output1 >= threshold[0][0] ? output1 : 0;
double output2 = impulse[0]*weight[0][1] + impulse[1]*weight[0][3];
output2 = output2 >= threshold[0][1] ? output2 : 0;
double output3 = output1*weight[1][0] + output2*weight[1][1];
output3 = output3 >= threshold[1][0] ? output3 : 0;
return output3;
}
int main()
{
std::cauchy_distribution<> distribution(0.0,1.0);
std::normal_distribution<> dist(0, 10);
std::random_device rd;
std::mt19937 e2(rd()), e2_2(rd());
int layers = 2;
int input = 2;
int hidden = 2;
int output = 1;
double ** weight, ** threshold;
weight = new double*[layers];
weight[0] = new double[input*hidden];
weight[1] = new double[hidden*output];
threshold = new double*[layers];
threshold[0] = new double[hidden];
threshold[1] = new double[output];
int impulse[4][2];
impulse[0][0] = 0;
impulse[0][1] = 0;
impulse[1][0] = 0;
impulse[1][1] = 1;
impulse[2][0] = 1;
impulse[2][1] = 0;
impulse[3][0] = 1;
impulse[3][1] = 1;
int iterations = 0;
while(true)
{
int counter = 0;
for(int i = 0; i < input*hidden; i++)
{
weight[0][i] = distribution(e2_2);
}
for(int i = 0; i < hidden*output; i++)
{
weight[1][i] = distribution(e2_2);
}
for(int i = 0; i < hidden; i++)
{
threshold[0][i] = dist(e2);
// threshold[0][i] = 1;
}
for(int i = 0; i < output; i++)
{
threshold[1][i] = dist(e2);
// threshold[1][i] = 1;
}
double tol = 0.01;
double result = evaluate(impulse[0], weight, threshold);
counter += std::abs(result) <= tol ? 1 : 0;
// if(std::abs(result) <= 0.001) std::cout << "0-" << result << "=" << std::abs(result) << std::endl;
result = evaluate(impulse[1], weight, threshold);
counter += std::abs(result-1) <= tol ? 1 : 0;
// if(std::abs(result-1) <= 0.001) std::cout << "1-" << result << "=" << std::abs(result-1.0) << std::endl;
result = evaluate(impulse[2], weight, threshold);
counter += std::abs(result-1) <= tol ? 1 : 0;
// if(std::abs(result-1) <= 0.001) std::cout << "1-" << result << "=" << std::abs(result-1.0) << std::endl;
result = evaluate(impulse[3], weight, threshold);
counter += std::abs(result) <= tol ? 1 : 0;
// if(std::abs(result) <= 0.001) std::cout << "0-" << result << "=" << std::abs(result) << std::endl;
iterations++;
if(counter == 4) break;
}
std::cout << "Iterations = " << iterations << std::endl;
print_output(impulse[0], weight, threshold);
print_output(impulse[1], weight, threshold);
print_output(impulse[2], weight, threshold);
print_output(impulse[3], weight, threshold);
return 0;
}
void print_output(int impulse[2], double** weight, double ** threshold)
{
double result = evaluate(impulse, weight, threshold);
std::cout << " " << weight[0][0] << std::endl
<< " ↓" << std::endl
<< " ((" << impulse[0] << "))-----((" << threshold[0][0] << "))" << std::endl
<< " \\ / \\" << std::endl
<< " \\ / \\" << std::endl
<< " " << weight[0][1] << " / " << weight[1][0] << std::endl
<< " \\ / \\" << std::endl
<< " X" << " ((" << threshold[1][0] << "))" << "---" << result << std::endl
<< " / \\ /" << std::endl
<< " " << weight[0][2] << " \\ "<< weight[1][1] << std::endl
<< " / \\ /" << std::endl
<< " / \\ /" << std::endl
<< " ((" << impulse[1] << "))-----((" << threshold[0][1] << "))" << std::endl
<< " ↑" << std::endl
<< " " << weight[0][3] << std::endl
<< "#####################################################" << std::endl;
}
@spceaza
Copy link
Author

spceaza commented Sep 20, 2015

g++ -std=c++11 -o result.bin ann_cauchy.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment