diff --git a/include/NeuralNetwork/Learning/BackPropagation.h b/include/NeuralNetwork/Learning/BackPropagation.h index c880519..1df9b42 100644 --- a/include/NeuralNetwork/Learning/BackPropagation.h +++ b/include/NeuralNetwork/Learning/BackPropagation.h @@ -37,6 +37,7 @@ namespace Learning { void setMomentumWeight(const float& m) { momentumWeight=m; + resize(); } float getWeightDecay() const { @@ -47,6 +48,14 @@ namespace Learning { weightDecay=wd; } + std::size_t getBatchSize() const { + return batchSize; + } + + void setBatchSize(std::size_t size) { + batchSize = size; + } + protected: virtual inline void resize() { @@ -58,25 +67,40 @@ namespace Learning { slopes[i].resize(network[i].size()); } - if(lastDeltas.size()!=network.size()) - lastDeltas.resize(network.size()); + if(deltas.size() != network.size()) + deltas.resize(network.size()); - for(std::size_t i=0; i < network.size(); i++) { - if(lastDeltas[i].size()!=network[i].size()) { - lastDeltas[i].resize(network[i].size()); + bool resized = false; - for(std::size_t j = 0; j < lastDeltas[i].size(); j++) { - lastDeltas[i][j] = 0.0; + for(std::size_t i = 0; i < network.size(); i++) { + if(deltas[i].size() != network[i].size()) { + deltas[i].resize(network[i].size()); + resized = true; + + if(i > 0) { + for(std::size_t j = 0; j < deltas[i].size(); j++) { + deltas[i][j].resize(network[i - 1].size()); + std::fill(deltas[i][j].begin(),deltas[i][j].end(),0.0); + } } } } - deltas= lastDeltas; + + if(momentumWeight > 0.0 && (resized || lastDeltas.size() != deltas.size())) { + lastDeltas = deltas; + } } - virtual void updateWeights(const std::vector &input); + virtual void computeDeltas(const std::vector &input); + + void updateWeights(); virtual void computeSlopes(const std::vector &expectation); + virtual void endBatch() { + + } + FeedForward::Network &network; CorrectionFunction::CorrectionFunction *correctionFunction; @@ -87,9 +111,13 @@ namespace Learning { float weightDecay = 0.0; + std::size_t batchSize = 1; + std::size_t currentBatchSize = 0; + std::vector> slopes; - std::vector> deltas; - std::vector> lastDeltas; + std::vector>> deltas = {}; + std::vector>> lastDeltas = {}; + }; } } \ No newline at end of file diff --git a/src/NeuralNetwork/Learning/BackPropagation.cpp b/src/NeuralNetwork/Learning/BackPropagation.cpp index 9801169..b562393 100644 --- a/src/NeuralNetwork/Learning/BackPropagation.cpp +++ b/src/NeuralNetwork/Learning/BackPropagation.cpp @@ -4,47 +4,15 @@ #include void NeuralNetwork::Learning::BackPropagation::teach(const std::vector &input, const std::vector &expectation) { - network.computeOutput(input); - resize(); - computeSlopes(expectation); - updateWeights(input); - - std::swap(deltas,lastDeltas); -} - - -void NeuralNetwork::Learning::BackPropagation::updateWeights(const std::vector &input) { - - for(std::size_t layerIndex=1;layerIndex= batchSize) { + updateWeights(); + endBatch(); + currentBatchSize=0; } } @@ -71,4 +39,55 @@ void NeuralNetwork::Learning::BackPropagation::computeSlopes(const std::vector &input) { + for(std::size_t layerIndex=1;layerIndex 0.0; + + for(std::size_t layerIndex=1;layerIndex