78 lines
1.6 KiB
C++
78 lines
1.6 KiB
C++
#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);
|
|
|
|
currentLayer.push_back(Neuron(1.0));
|
|
|
|
currentLayer.connectTo(nextLayer);
|
|
}
|
|
}
|
|
|
|
void Net::feedForward(const std::vector<double> &inputValues)
|
|
{
|
|
Layer &inputLayer = front();
|
|
|
|
if (inputLayer.size() - 1 != inputValues.size())
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
std::vector<double> Net::getResult()
|
|
{
|
|
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)
|
|
{
|
|
const Layer &outputLayer = back();
|
|
|
|
if (targetValues.size() != outputLayer.size())
|
|
{
|
|
throw std::exception("The number of target values has to match the output layer size");
|
|
}
|
|
|
|
std::vector<double> resultValues = getResult();
|
|
|
|
double rmsError = 0.0;
|
|
for (unsigned int i = 0; i < resultValues.size(); ++i)
|
|
{
|
|
double delta = resultValues[i] - targetValues[i];
|
|
rmsError += delta * delta;
|
|
}
|
|
rmsError = sqrt(rmsError / resultValues.size());
|
|
}
|