Calculation of hidden neuron gradients (partial)

This commit is contained in:
mandlm 2015-10-16 22:59:04 +02:00
parent ea454b58c6
commit 370451c2e6
5 changed files with 46 additions and 6 deletions

View File

@ -50,3 +50,7 @@ void Layer::connectTo(const Layer & nextLayer)
neuron.createRandomOutputWeights(nextLayer.size());
}
}
void Layer::updateInputWeights(const Layer & prevLayer)
{
}

View File

@ -13,4 +13,6 @@ public:
void feedForward(const Layer &inputLayer);
double getWeightedSum(int outputNeuron) const;
void connectTo(const Layer & nextLayer);
void updateInputWeights(const Layer &prevLayer);
};

19
Net.cpp
View File

@ -68,6 +68,8 @@ void Net::backProp(const std::vector<double> &targetValues)
std::vector<double> resultValues = getOutput();
size_t numResultValues = resultValues.size();
// calculate rms error
double rmsError = 0.0;
for (unsigned int i = 0; i < numResultValues; ++i)
@ -78,19 +80,30 @@ void Net::backProp(const std::vector<double> &targetValues)
rmsError = sqrt(rmsError / numResultValues);
// calculate output neuron gradients
for (unsigned int i = 0; i < numResultValues; ++i)
{
outputLayer[i].calcOutputGradients(targetValues[i]);
}
// calculate hidden neuron gradients
for (auto it = end() - 1; it != begin(); --it)
{
Layer &hiddenLayer = *it;
Layer &prevLayer = *(it - 1);
Layer &hiddenLayer = *(it - 1);
Layer &nextLayer = *it;
for (auto neuron : hiddenLayer)
{
//neuron.calcHiddenGradients(prevLayer);
neuron.calcHiddenGradients(nextLayer);
}
}
// update the input weights
for (auto it = end() - 1; it != begin(); --it)
{
Layer &currentLayer = *it;
Layer &prevLayer = *(it - 1);
currentLayer.updateInputWeights(prevLayer);
}
}

View File

@ -60,3 +60,18 @@ void Neuron::calcOutputGradients(double targetValue)
gradient = delta * transferFunctionDerivative(outputValue);
}
double Neuron::sumDOW(const Layer & nextLayer) const
{
double sum = 0;
// sum it up
return sum;
}
void Neuron::calcHiddenGradients(const Layer &nextLayer)
{
double dow = sumDOW(nextLayer);
gradient = dow * transferFunctionDerivative(outputValue);
}

View File

@ -2,6 +2,8 @@
#include <vector>
class Layer;
class Neuron
{
private:
@ -13,13 +15,17 @@ public:
Neuron(double value = 1.0);
void setOutputValue(double value);
static double transferFunction(double inputValue);
static double transferFunctionDerivative(double inputValue);
void feedForward(double inputValue);
double getWeightedOutputValue(unsigned int outputNeuron) const;
void createRandomOutputWeights(size_t numberOfWeights);
double getOutputValue() const;
void calcOutputGradients(double targetValue);
//void calcHiddenGradients(const Layer &prevLayer);
void calcHiddenGradients(const Layer &nextLayer);
private:
static double transferFunction(double inputValue);
static double transferFunctionDerivative(double inputValue);
double sumDOW(const Layer &nextLayer) const;
};