Split up to different source files, entry-point for back propagation
This commit is contained in:
parent
c8e08193f1
commit
e3a804242c
8 changed files with 230 additions and 149 deletions
52
Layer.cpp
Normal file
52
Layer.cpp
Normal file
|
@ -0,0 +1,52 @@
|
|||
#include "Layer.h"
|
||||
|
||||
Layer::Layer(unsigned int numNeurons)
|
||||
{
|
||||
for (unsigned int i = 0; i < numNeurons; ++i)
|
||||
{
|
||||
push_back(Neuron());
|
||||
}
|
||||
}
|
||||
|
||||
void Layer::setOutputValues(const std::vector<double> & outputValues)
|
||||
{
|
||||
if (size() != outputValues.size())
|
||||
{
|
||||
throw std::exception("The number of output values has to match the layer size");
|
||||
}
|
||||
|
||||
auto valueIt = outputValues.begin();
|
||||
for (Neuron &neuron : *this)
|
||||
{
|
||||
neuron.setOutputValue(*valueIt++);
|
||||
}
|
||||
}
|
||||
|
||||
void Layer::feedForward(const Layer &inputLayer)
|
||||
{
|
||||
int neuronNumber = 0;
|
||||
for (Neuron &neuron : *this)
|
||||
{
|
||||
neuron.feedForward(inputLayer.getWeightedSum(neuronNumber));
|
||||
}
|
||||
}
|
||||
|
||||
double Layer::getWeightedSum(int outputNeuron) const
|
||||
{
|
||||
double sum = 0.0;
|
||||
|
||||
for (const Neuron &neuron : *this)
|
||||
{
|
||||
sum += neuron.getWeightedOutputValue(outputNeuron);
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
void Layer::connectTo(const Layer & nextLayer)
|
||||
{
|
||||
for (Neuron &neuron : *this)
|
||||
{
|
||||
neuron.createOutputWeights(nextLayer.size());
|
||||
}
|
||||
}
|
16
Layer.h
Normal file
16
Layer.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "Neuron.h"
|
||||
|
||||
class Layer : public std::vector < Neuron >
|
||||
{
|
||||
public:
|
||||
Layer(unsigned int numNeurons);
|
||||
|
||||
void setOutputValues(const std::vector<double> & outputValues);
|
||||
void feedForward(const Layer &inputLayer);
|
||||
double getWeightedSum(int outputNeuron) const;
|
||||
void connectTo(const Layer & nextLayer);
|
||||
};
|
75
Net.cpp
Normal file
75
Net.cpp
Normal file
|
@ -0,0 +1,75 @@
|
|||
#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.connectTo(nextLayer);
|
||||
}
|
||||
}
|
||||
|
||||
void Net::feedForward(const std::vector<double> &inputValues)
|
||||
{
|
||||
Layer &inputLayer = front();
|
||||
|
||||
if (inputLayer.size() != 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());
|
||||
}
|
154
Net.h
154
Net.h
|
@ -2,158 +2,14 @@
|
|||
|
||||
#include <vector>
|
||||
|
||||
class Neuron
|
||||
{
|
||||
private:
|
||||
double outputValue;
|
||||
std::vector<double> outputWeights;
|
||||
|
||||
public:
|
||||
void setOutputValue(double value)
|
||||
{
|
||||
outputValue = value;
|
||||
}
|
||||
|
||||
static double transferFunction(double inputValue)
|
||||
{
|
||||
return std::tanh(inputValue);
|
||||
}
|
||||
|
||||
void feedForward(double inputValue)
|
||||
{
|
||||
outputValue = Neuron::transferFunction(inputValue);
|
||||
}
|
||||
|
||||
double getWeightedOutputValue(int outputNeuron) const
|
||||
{
|
||||
return outputValue * outputWeights[outputNeuron];
|
||||
}
|
||||
|
||||
void createOutputWeights(unsigned int number)
|
||||
{
|
||||
outputWeights.clear();
|
||||
|
||||
for (unsigned int i = 0; i < number; ++i)
|
||||
{
|
||||
outputWeights.push_back(std::rand() / (double)RAND_MAX);
|
||||
}
|
||||
}
|
||||
|
||||
double getOutputValue() const
|
||||
{
|
||||
return outputValue;
|
||||
}
|
||||
};
|
||||
|
||||
class Layer : public std::vector < Neuron >
|
||||
{
|
||||
public:
|
||||
Layer(unsigned int numNeurons)
|
||||
{
|
||||
for (unsigned int i = 0; i < numNeurons; ++i)
|
||||
{
|
||||
push_back(Neuron());
|
||||
}
|
||||
}
|
||||
|
||||
void setOutputValues(const std::vector<double> & outputValues)
|
||||
{
|
||||
if (size() != outputValues.size())
|
||||
{
|
||||
throw std::exception("The number of output values has to match the layer size");
|
||||
}
|
||||
|
||||
auto valueIt = outputValues.begin();
|
||||
for (Neuron &neuron : *this)
|
||||
{
|
||||
neuron.setOutputValue(*valueIt++);
|
||||
}
|
||||
}
|
||||
|
||||
void feedForward(const Layer &inputLayer)
|
||||
{
|
||||
int neuronNumber = 0;
|
||||
for (Neuron &neuron : *this)
|
||||
{
|
||||
neuron.feedForward(inputLayer.getWeightedSum(neuronNumber));
|
||||
}
|
||||
}
|
||||
|
||||
double getWeightedSum(int outputNeuron) const
|
||||
{
|
||||
double sum = 0.0;
|
||||
|
||||
for (const Neuron &neuron : *this)
|
||||
{
|
||||
sum += neuron.getWeightedOutputValue(outputNeuron);
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
void connectTo(const Layer & nextLayer)
|
||||
{
|
||||
for (Neuron &neuron : *this)
|
||||
{
|
||||
neuron.createOutputWeights(nextLayer.size());
|
||||
}
|
||||
}
|
||||
};
|
||||
#include "Layer.h"
|
||||
|
||||
class Net : public std::vector < Layer >
|
||||
{
|
||||
public:
|
||||
Net(std::initializer_list<unsigned int> layerSizes)
|
||||
{
|
||||
if (layerSizes.size() < 3)
|
||||
{
|
||||
throw std::exception("A net needs at least 3 layers");
|
||||
}
|
||||
Net(std::initializer_list<unsigned int> layerSizes);
|
||||
|
||||
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.connectTo(nextLayer);
|
||||
}
|
||||
}
|
||||
|
||||
void feedForward(const std::vector<double> &inputValues)
|
||||
{
|
||||
Layer &inputLayer = front();
|
||||
|
||||
if (inputLayer.size() != 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> getResult()
|
||||
{
|
||||
std::vector<double> result;
|
||||
|
||||
const Layer &outputLayer = back();
|
||||
for (const Neuron &neuron : outputLayer)
|
||||
{
|
||||
result.push_back(neuron.getOutputValue());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
void feedForward(const std::vector<double> &inputValues);
|
||||
std::vector<double> getResult();
|
||||
void backProp(const std::vector<double> &targetValues);
|
||||
};
|
|
@ -78,10 +78,15 @@
|
|||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Layer.cpp" />
|
||||
<ClCompile Include="Net.cpp" />
|
||||
<ClCompile Include="Neuro.cpp" />
|
||||
<ClCompile Include="Neuron.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Layer.h" />
|
||||
<ClInclude Include="Net.h" />
|
||||
<ClInclude Include="Neuron.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
|
|
@ -18,10 +18,25 @@
|
|||
<ClCompile Include="Neuro.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Net.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Layer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Neuron.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Net.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Layer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Neuron.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
43
Neuron.cpp
Normal file
43
Neuron.cpp
Normal file
|
@ -0,0 +1,43 @@
|
|||
#pragma once
|
||||
|
||||
#include "Neuron.h"
|
||||
|
||||
void Neuron::setOutputValue(double value)
|
||||
{
|
||||
outputValue = value;
|
||||
}
|
||||
|
||||
double Neuron::transferFunction(double inputValue)
|
||||
{
|
||||
return std::tanh(inputValue);
|
||||
}
|
||||
|
||||
double Neuron::transferFunctionDerivative(double inputValue)
|
||||
{
|
||||
return 1.0 - (inputValue * inputValue);
|
||||
}
|
||||
|
||||
void Neuron::feedForward(double inputValue)
|
||||
{
|
||||
outputValue = Neuron::transferFunction(inputValue);
|
||||
}
|
||||
|
||||
double Neuron::getWeightedOutputValue(int outputNeuron) const
|
||||
{
|
||||
return outputValue * outputWeights[outputNeuron];
|
||||
}
|
||||
|
||||
void Neuron::createOutputWeights(unsigned int number)
|
||||
{
|
||||
outputWeights.clear();
|
||||
|
||||
for (unsigned int i = 0; i < number; ++i)
|
||||
{
|
||||
outputWeights.push_back(std::rand() / (double)RAND_MAX);
|
||||
}
|
||||
}
|
||||
|
||||
double Neuron::getOutputValue() const
|
||||
{
|
||||
return outputValue;
|
||||
}
|
19
Neuron.h
Normal file
19
Neuron.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
class Neuron
|
||||
{
|
||||
private:
|
||||
double outputValue;
|
||||
std::vector<double> outputWeights;
|
||||
|
||||
public:
|
||||
void setOutputValue(double value);
|
||||
static double transferFunction(double inputValue);
|
||||
static double transferFunctionDerivative(double inputValue);
|
||||
void feedForward(double inputValue);
|
||||
double getWeightedOutputValue(int outputNeuron) const;
|
||||
void createOutputWeights(unsigned int number);
|
||||
double getOutputValue() const;
|
||||
};
|
Loading…
Reference in a new issue