diff --git a/Layer.cpp b/Layer.cpp index e917c8c..a3647d5 100644 --- a/Layer.cpp +++ b/Layer.cpp @@ -10,24 +10,25 @@ Layer::Layer(unsigned int numNeurons) void Layer::setOutputValues(const std::vector & outputValues) { - if (size() != outputValues.size()) + if (size() - 1 != outputValues.size()) { throw std::exception("The number of output values has to match the layer size"); } - auto valueIt = outputValues.begin(); - for (Neuron &neuron : *this) + auto neuronIt = begin(); + for (const double &value : outputValues) { - neuron.setOutputValue(*valueIt++); + neuronIt->setOutputValue(value); + neuronIt++; } } void Layer::feedForward(const Layer &inputLayer) { int neuronNumber = 0; - for (Neuron &neuron : *this) + for (auto neuronIt = begin(); neuronIt != end(); ++neuronIt) { - neuron.feedForward(inputLayer.getWeightedSum(neuronNumber)); + neuronIt->feedForward(inputLayer.getWeightedSum(neuronNumber++)); } } diff --git a/Net.cpp b/Net.cpp index 713828e..50dbced 100644 --- a/Net.cpp +++ b/Net.cpp @@ -17,6 +17,8 @@ Net::Net(std::initializer_list layerSizes) Layer ¤tLayer = *layerIt; const Layer &nextLayer = *(layerIt + 1); + currentLayer.push_back(Neuron(1.0)); + currentLayer.connectTo(nextLayer); } } @@ -25,7 +27,7 @@ void Net::feedForward(const std::vector &inputValues) { Layer &inputLayer = front(); - if (inputLayer.size() != inputValues.size()) + if (inputLayer.size() - 1 != inputValues.size()) { throw std::exception("The number of input values has to match the input layer size"); } diff --git a/Neuro.cpp b/Neuro.cpp index 193d020..b6205e3 100644 --- a/Neuro.cpp +++ b/Neuro.cpp @@ -9,9 +9,9 @@ int main() { std::cout << "Neuro running" << std::endl; - Net myNet({ 2, 3, 1 }); + Net myNet({ 3, 4, 2 }); - myNet.feedForward({ 1.0, 0.0 }); + myNet.feedForward({ 1.0, 2.0, 3.0 }); std::vector result = myNet.getResult(); diff --git a/Neuron.cpp b/Neuron.cpp index 405f51c..e8be5ca 100644 --- a/Neuron.cpp +++ b/Neuron.cpp @@ -2,6 +2,12 @@ #include "Neuron.h" +Neuron::Neuron(double value) + : outputValue(value) +{ + +} + void Neuron::setOutputValue(double value) { outputValue = value; @@ -22,9 +28,14 @@ void Neuron::feedForward(double inputValue) outputValue = Neuron::transferFunction(inputValue); } -double Neuron::getWeightedOutputValue(int outputNeuron) const +double Neuron::getWeightedOutputValue(unsigned int outputNeuron) const { - return outputValue * outputWeights[outputNeuron]; + if (outputNeuron < outputWeights.size()) + { + return outputValue * outputWeights[outputNeuron]; + } + + return 0.0; } void Neuron::createOutputWeights(unsigned int number) diff --git a/Neuron.h b/Neuron.h index 98ed7e3..ffd01cd 100644 --- a/Neuron.h +++ b/Neuron.h @@ -9,11 +9,13 @@ private: std::vector outputWeights; 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(int outputNeuron) const; + double getWeightedOutputValue(unsigned int outputNeuron) const; void createOutputWeights(unsigned int number); double getOutputValue() const; };