2015-03-23 20:58:30 +00:00
|
|
|
#include "Net.h"
|
|
|
|
|
|
|
|
Net::Net(std::initializer_list<unsigned int> layerSizes)
|
|
|
|
{
|
|
|
|
if (layerSizes.size() < 3)
|
|
|
|
{
|
|
|
|
throw std::exception("A net needs at least 3 layers");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int numNeurons : layerSizes)
|
|
|
|
{
|
|
|
|
push_back(Layer(numNeurons));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto layerIt = begin(); layerIt != end() - 1; ++layerIt)
|
|
|
|
{
|
|
|
|
Layer ¤tLayer = *layerIt;
|
|
|
|
const Layer &nextLayer = *(layerIt + 1);
|
|
|
|
|
2015-10-15 17:18:26 +00:00
|
|
|
Neuron biasNeuron(1.0);
|
|
|
|
currentLayer.push_back(biasNeuron);
|
2015-03-24 12:45:38 +00:00
|
|
|
|
2015-03-23 20:58:30 +00:00
|
|
|
currentLayer.connectTo(nextLayer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Net::feedForward(const std::vector<double> &inputValues)
|
|
|
|
{
|
|
|
|
Layer &inputLayer = front();
|
|
|
|
|
2015-03-24 12:45:38 +00:00
|
|
|
if (inputLayer.size() - 1 != inputValues.size())
|
2015-03-23 20:58:30 +00:00
|
|
|
{
|
|
|
|
throw std::exception("The number of input values has to match the input layer size");
|
|
|
|
}
|
|
|
|
|
|
|
|
inputLayer.setOutputValues(inputValues);
|
|
|
|
|
|
|
|
for (auto layerIt = begin(); layerIt != end() - 1; ++layerIt)
|
|
|
|
{
|
|
|
|
const Layer ¤tLayer = *layerIt;
|
|
|
|
Layer &nextLayer = *(layerIt + 1);
|
|
|
|
|
|
|
|
nextLayer.feedForward(currentLayer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 17:18:26 +00:00
|
|
|
std::vector<double> Net::getOutput()
|
2015-03-23 20:58:30 +00:00
|
|
|
{
|
|
|
|
std::vector<double> result;
|
|
|
|
|
|
|
|
const Layer &outputLayer = back();
|
|
|
|
for (const Neuron &neuron : outputLayer)
|
|
|
|
{
|
|
|
|
result.push_back(neuron.getOutputValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Net::backProp(const std::vector<double> &targetValues)
|
|
|
|
{
|
2015-10-15 20:16:34 +00:00
|
|
|
Layer &outputLayer = back();
|
2015-03-23 20:58:30 +00:00
|
|
|
|
|
|
|
if (targetValues.size() != outputLayer.size())
|
|
|
|
{
|
|
|
|
throw std::exception("The number of target values has to match the output layer size");
|
|
|
|
}
|
|
|
|
|
2015-10-15 17:18:26 +00:00
|
|
|
std::vector<double> resultValues = getOutput();
|
2015-10-15 20:16:34 +00:00
|
|
|
unsigned int numResultValues = resultValues.size();
|
2015-03-23 20:58:30 +00:00
|
|
|
double rmsError = 0.0;
|
2015-10-15 20:16:34 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < numResultValues; ++i)
|
2015-03-23 20:58:30 +00:00
|
|
|
{
|
|
|
|
double delta = resultValues[i] - targetValues[i];
|
|
|
|
rmsError += delta * delta;
|
|
|
|
}
|
2015-10-15 20:16:34 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2015-03-23 20:58:30 +00:00
|
|
|
}
|