From ce88f690cf9ff40e5f17f54ba94059058084c4e6 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 15 Oct 2015 22:16:34 +0200 Subject: [PATCH] More back-propagation code, calculation of output-neuron gradients --- Net.cpp | 26 ++++++++++++++++++++++---- Neuron.cpp | 8 ++++++++ Neuron.h | 4 ++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Net.cpp b/Net.cpp index 7daa4eb..3e9f200 100644 --- a/Net.cpp +++ b/Net.cpp @@ -59,7 +59,7 @@ std::vector Net::getOutput() void Net::backProp(const std::vector &targetValues) { - const Layer &outputLayer = back(); + Layer &outputLayer = back(); if (targetValues.size() != outputLayer.size()) { @@ -67,12 +67,30 @@ void Net::backProp(const std::vector &targetValues) } std::vector resultValues = getOutput(); - + unsigned int numResultValues = resultValues.size(); double rmsError = 0.0; - for (unsigned int i = 0; i < resultValues.size(); ++i) + + for (unsigned int i = 0; i < numResultValues; ++i) { double delta = resultValues[i] - targetValues[i]; rmsError += delta * delta; } - rmsError = sqrt(rmsError / resultValues.size()); + + rmsError = sqrt(rmsError / numResultValues); + + for (unsigned int i = 0; i < numResultValues; ++i) + { + outputLayer[i].calcOutputGradients(targetValues[i]); + } + + for (auto it = end() - 1; it != begin(); --it) + { + Layer &hiddenLayer = *it; + Layer &prevLayer = *(it - 1); + + for (auto neuron : hiddenLayer) + { + //neuron.calcHiddenGradients(prevLayer); + } + } } diff --git a/Neuron.cpp b/Neuron.cpp index 3983237..21cb67d 100644 --- a/Neuron.cpp +++ b/Neuron.cpp @@ -4,6 +4,7 @@ Neuron::Neuron(double value) : outputValue(value) + , gradient(0) { } @@ -52,3 +53,10 @@ double Neuron::getOutputValue() const { return outputValue; } + +void Neuron::calcOutputGradients(double targetValue) +{ + double delta = targetValue - outputValue; + gradient = delta * transferFunctionDerivative(outputValue); +} + diff --git a/Neuron.h b/Neuron.h index 3aecfd3..42d5ad1 100644 --- a/Neuron.h +++ b/Neuron.h @@ -7,6 +7,7 @@ class Neuron private: double outputValue; std::vector outputWeights; + double gradient; public: Neuron(double value = 1.0); @@ -18,4 +19,7 @@ public: double getWeightedOutputValue(unsigned int outputNeuron) const; void createRandomOutputWeights(unsigned int numberOfWeights); double getOutputValue() const; + + void calcOutputGradients(double targetValue); + //void calcHiddenGradients(const Layer &prevLayer); };