From f22e4221a1c2f2fb381c6df7ceafe0e5195ef428 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 22 Oct 2015 16:02:27 +0200 Subject: [PATCH] Fixed feed-forward algo --- Layer.cpp | 9 ++++----- Neuron.cpp | 20 ++++++++++++++++++++ Neuron.h | 5 +++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Layer.cpp b/Layer.cpp index a38ba24..76d524a 100644 --- a/Layer.cpp +++ b/Layer.cpp @@ -23,11 +23,10 @@ void Layer::setOutputValues(const std::vector & outputValues) } void Layer::feedForward(const Layer &inputLayer) -{ - int neuronNumber = 0; - for (auto neuronIt = begin(); neuronIt != end(); ++neuronIt) +{ + for (int neuronNumber = 0; neuronNumber < sizeWithoutBiasNeuron(); ++neuronNumber) { - neuronIt->feedForward(inputLayer.getWeightedSum(neuronNumber++)); + at(neuronNumber).feedForward(inputLayer.getWeightedSum(neuronNumber)); } } @@ -47,7 +46,7 @@ void Layer::connectTo(const Layer & nextLayer) { for (Neuron &neuron : *this) { - neuron.createRandomOutputWeights(nextLayer.size()); + neuron.createOutputWeights(nextLayer.sizeWithoutBiasNeuron(), 0.5); } } diff --git a/Neuron.cpp b/Neuron.cpp index 7083f99..2e2cff8 100644 --- a/Neuron.cpp +++ b/Neuron.cpp @@ -50,6 +50,26 @@ void Neuron::createRandomOutputWeights(size_t numberOfWeights) } } +void Neuron::createOutputWeights(std::list weights) +{ + outputWeights.clear(); + + for (const double &weight : weights) + { + outputWeights.push_back(weight); + } +} + +void Neuron::createOutputWeights(size_t numberOfWeights, double weight) +{ + outputWeights.clear(); + + for (unsigned int i = 0; i < numberOfWeights; ++i) + { + outputWeights.push_back(weight); + } +} + double Neuron::getOutputValue() const { return outputValue; diff --git a/Neuron.h b/Neuron.h index c0356d3..b5e13c2 100644 --- a/Neuron.h +++ b/Neuron.h @@ -1,6 +1,7 @@ #pragma once #include +#include class Layer; @@ -17,7 +18,11 @@ public: void setOutputValue(double value); void feedForward(double inputValue); double getWeightedOutputValue(unsigned int outputNeuron) const; + void createRandomOutputWeights(size_t numberOfWeights); + void createOutputWeights(std::list weights); + void createOutputWeights(size_t numberOfWeights, double weight); + double getOutputValue() const; void calcOutputGradients(double targetValue);