Calculation of hidden neuron gradients (partial)
This commit is contained in:
parent
ea454b58c6
commit
370451c2e6
@ -50,3 +50,7 @@ void Layer::connectTo(const Layer & nextLayer)
|
|||||||
neuron.createRandomOutputWeights(nextLayer.size());
|
neuron.createRandomOutputWeights(nextLayer.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Layer::updateInputWeights(const Layer & prevLayer)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
2
Layer.h
2
Layer.h
@ -13,4 +13,6 @@ public:
|
|||||||
void feedForward(const Layer &inputLayer);
|
void feedForward(const Layer &inputLayer);
|
||||||
double getWeightedSum(int outputNeuron) const;
|
double getWeightedSum(int outputNeuron) const;
|
||||||
void connectTo(const Layer & nextLayer);
|
void connectTo(const Layer & nextLayer);
|
||||||
|
|
||||||
|
void updateInputWeights(const Layer &prevLayer);
|
||||||
};
|
};
|
||||||
|
19
Net.cpp
19
Net.cpp
@ -68,6 +68,8 @@ void Net::backProp(const std::vector<double> &targetValues)
|
|||||||
|
|
||||||
std::vector<double> resultValues = getOutput();
|
std::vector<double> resultValues = getOutput();
|
||||||
size_t numResultValues = resultValues.size();
|
size_t numResultValues = resultValues.size();
|
||||||
|
|
||||||
|
// calculate rms error
|
||||||
double rmsError = 0.0;
|
double rmsError = 0.0;
|
||||||
|
|
||||||
for (unsigned int i = 0; i < numResultValues; ++i)
|
for (unsigned int i = 0; i < numResultValues; ++i)
|
||||||
@ -78,19 +80,30 @@ void Net::backProp(const std::vector<double> &targetValues)
|
|||||||
|
|
||||||
rmsError = sqrt(rmsError / numResultValues);
|
rmsError = sqrt(rmsError / numResultValues);
|
||||||
|
|
||||||
|
// calculate output neuron gradients
|
||||||
for (unsigned int i = 0; i < numResultValues; ++i)
|
for (unsigned int i = 0; i < numResultValues; ++i)
|
||||||
{
|
{
|
||||||
outputLayer[i].calcOutputGradients(targetValues[i]);
|
outputLayer[i].calcOutputGradients(targetValues[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// calculate hidden neuron gradients
|
||||||
for (auto it = end() - 1; it != begin(); --it)
|
for (auto it = end() - 1; it != begin(); --it)
|
||||||
{
|
{
|
||||||
Layer &hiddenLayer = *it;
|
Layer &hiddenLayer = *(it - 1);
|
||||||
Layer &prevLayer = *(it - 1);
|
Layer &nextLayer = *it;
|
||||||
|
|
||||||
for (auto neuron : hiddenLayer)
|
for (auto neuron : hiddenLayer)
|
||||||
{
|
{
|
||||||
//neuron.calcHiddenGradients(prevLayer);
|
neuron.calcHiddenGradients(nextLayer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update the input weights
|
||||||
|
for (auto it = end() - 1; it != begin(); --it)
|
||||||
|
{
|
||||||
|
Layer ¤tLayer = *it;
|
||||||
|
Layer &prevLayer = *(it - 1);
|
||||||
|
|
||||||
|
currentLayer.updateInputWeights(prevLayer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
15
Neuron.cpp
15
Neuron.cpp
@ -60,3 +60,18 @@ void Neuron::calcOutputGradients(double targetValue)
|
|||||||
gradient = delta * transferFunctionDerivative(outputValue);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
12
Neuron.h
12
Neuron.h
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
class Layer;
|
||||||
|
|
||||||
class Neuron
|
class Neuron
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
@ -13,13 +15,17 @@ public:
|
|||||||
Neuron(double value = 1.0);
|
Neuron(double value = 1.0);
|
||||||
|
|
||||||
void setOutputValue(double value);
|
void setOutputValue(double value);
|
||||||
static double transferFunction(double inputValue);
|
|
||||||
static double transferFunctionDerivative(double inputValue);
|
|
||||||
void feedForward(double inputValue);
|
void feedForward(double inputValue);
|
||||||
double getWeightedOutputValue(unsigned int outputNeuron) const;
|
double getWeightedOutputValue(unsigned int outputNeuron) const;
|
||||||
void createRandomOutputWeights(size_t numberOfWeights);
|
void createRandomOutputWeights(size_t numberOfWeights);
|
||||||
double getOutputValue() const;
|
double getOutputValue() const;
|
||||||
|
|
||||||
void calcOutputGradients(double targetValue);
|
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;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user