Last active
September 30, 2018 16:23
-
-
Save MelulekiDube/57d4284fec4e303c1c6819271bfc7940 to your computer and use it in GitHub Desktop.
c++ assignment
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
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
/* | |
* File: main.cpp | |
* Author: dube_ | |
* | |
* Created on 30 August 2018, 10:34 PM | |
*/ | |
#include <cstdlib> | |
#include <iostream> | |
#include "neuron.h" | |
#include <cmath> | |
using namespace std; | |
float LR = 0.1; | |
float get_error(float output, float target) { | |
return output * (1 - output) * (target - output); | |
} | |
vector<float> get_hidden_node_error(vector<neuron>& hidden_nodes, | |
vector<neuron>& out, vector<float>& inputs, vector<float>& o_error) { | |
vector<float> errors(hidden_nodes.size()); | |
for (int i = 0; i < hidden_nodes.size(); ++i) { | |
float oh = hidden_nodes[i].guess(inputs); | |
float summation = 0; | |
/**performing the summation with the output weights*/ | |
summation += (out[0].get_weights()[i] * o_error[0]); | |
errors[i] = oh * (1 - oh) * summation; | |
} | |
return errors; | |
} | |
void update_weightings(vector<neuron>& output, vector<float>& error, | |
vector<float>& input) { | |
// cout << "here" << endl; | |
float delta; | |
for (int i = 0; i < error.size(); i++) { | |
for (int j = 0; j < input.size(); j++) { | |
delta = LR * error[i] * input[j]; | |
output[i].weights[j] += delta; | |
} | |
} | |
} | |
vector<vector<float>> generate_input() { | |
vector<vector<float>> inputs = { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 1, 0 }, { 0, | |
1, 1 }, { 1, 0, 0 }, { 1, 0, 1 }, { 1, 1, 0 }, { 1, 1, 1 } }; | |
return inputs; | |
} | |
float RandomFloat(float a, float b) { | |
float random = ((float) rand()) / (float) RAND_MAX; | |
float diff = b - a; | |
float r = random * diff; | |
return a + r; | |
} | |
/**code from stacks overflow*/ | |
vector<float> generate_weights() { | |
vector<float> weights(3); | |
for (int i = 0; i < 3; ++i) { | |
weights[i] = RandomFloat(-3, 3); | |
} | |
return weights; | |
} | |
float calcMeanSquaredError(vector<float>& outputs, vector<float>& targets) { | |
float sumation = 0; | |
for (int i = 0; i < outputs.size(); ++i) { | |
sumation += powf((outputs[i] - targets[i]), 2); | |
} | |
return sumation * 0.5; | |
} | |
float calcMeanSquaredError(float outputs, float targets) { | |
float sumation = 0; | |
sumation += powf((outputs - targets), 2); | |
return sumation * 0.5; | |
} | |
void run() { | |
neuron hidden1(generate_weights()); | |
neuron hidden2(generate_weights()); | |
neuron hidden3(generate_weights()); | |
vector<neuron> hidden_nodes = { hidden1, hidden2, hidden3 }; | |
// vector<neuron> hidden_nodes = { hidden1}; | |
cout << "Hidden layer weights" << endl; | |
for (neuron n : hidden_nodes) { | |
for (float f : n.get_weights()) { | |
cout << f << endl; | |
} | |
cout << endl; | |
} | |
neuron y1(generate_weights()); | |
cout << "\nOutput Layer weights" << endl; | |
for (float f : y1.get_weights()) { | |
cout << f << endl; | |
} | |
vector<neuron> outputs = { y1 }; | |
vector<float> targets = { 0, 1, 1, 0, 1, 0, 0, 1 }; | |
vector<float> outputs_vector; | |
vector<vector<float>> inputs = generate_input(); //get the vector of input vectors | |
//ado{ | |
for (int i = 0; i < inputs.size(); ++i) { | |
float meanSQ=0; | |
for (int j = 0; j < 2000; j++) { | |
// vector<float> a = { hidden1.guess(inputs[i])}; | |
vector<float> a = { hidden1.guess(inputs[i]), hidden2.guess( | |
inputs[i]), hidden3.guess(inputs[i])}; //vector of outputs of the hidden layer | |
vector<float> output = { outputs[0].guess(a) }; | |
vector<float> o_error = { get_error(output[0], targets[i]) }; | |
cout<<"Output : "<<output[0]<< " targets is: " <<targets[i] <<endl; | |
vector<float> hidden_node_erros( | |
get_hidden_node_error(hidden_nodes, outputs, inputs[i], | |
o_error)); | |
update_weightings(outputs, o_error, a); | |
update_weightings(hidden_nodes, hidden_node_erros, inputs[i]); | |
meanSQ = calcMeanSquaredError(output[0], targets[i]); | |
} //while(); | |
cout<<"Mean Squared error: "<<meanSQ<<endl; | |
// cout << "\n" << calcMeanSquaredError(outputs_vector, targets) << endl; | |
} | |
cout << "\nupdated output weights" << endl; | |
for (float f : outputs[0].get_weights()) { | |
cout << f << endl; | |
} | |
cout << "\nUpdated Hidden layer weights" << endl; | |
for (neuron n : hidden_nodes) { | |
for (float f : n.get_weights()) { | |
cout << f << endl; | |
} | |
cout << endl; | |
} | |
} | |
/* | |
* | |
*/ | |
int main(int argc, char** argv) { | |
run(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment