refactored propagation

This commit is contained in:
2016-10-31 15:03:27 +01:00
parent 8749b3eb03
commit 77b38dec65
19 changed files with 285 additions and 548 deletions

View File

@@ -1,10 +1,7 @@
#pragma once
#include <vector>
#include <cmath>
#include <NeuralNetwork/FeedForward/Network.h>
#include "CorrectionFunction/Linear.h"
#include "BatchPropagation.h"
namespace NeuralNetwork {
namespace Learning {
@@ -12,122 +9,48 @@ namespace NeuralNetwork {
/** @class Resilient Propagation
* @brief
*/
class RProp {
class RProp : public BatchPropagation {
public:
RProp(FeedForward::Network &feedForwardNetwork, CorrectionFunction::CorrectionFunction *correction = new CorrectionFunction::Linear()):
network(feedForwardNetwork), correctionFunction(correction) {
resize();
}
virtual ~RProp() {
delete correctionFunction;
RProp(FeedForward::Network &feedForwardNetwork, std::shared_ptr<CorrectionFunction::CorrectionFunction> correction = std::make_shared<CorrectionFunction::Linear>()):
BatchPropagation(feedForwardNetwork, correction) {
}
RProp(const RProp&)=delete;
RProp& operator=(const NeuralNetwork::Learning::RProp&) = delete;
void teach(const std::vector<float> &input, const std::vector<float> &output);
std::size_t getBatchSize() const {
return batchSize;
void setInitialWeightChange(float initVal) {
initialWeightChange=initVal;
}
void setLearningCoefficient(float) {
void setBatchSize(std::size_t size) {
batchSize = size;
}
void setInitialWeightChange(float init) {
initialWeightChange=init;
}
protected:
virtual inline void resize() {
if(slopes.size()!=network.size())
slopes.resize(network.size());
virtual inline void resize() override {
BatchPropagation::resize();
for(std::size_t i=0; i < network.size(); i++) {
if(slopes[i].size()!=network[i].size())
slopes[i].resize(network[i].size());
}
_lastGradients =_gradients;
if(gradients.size() != network.size())
gradients.resize(network.size());
bool resized = false;
for(std::size_t i = 0; i < network.size(); i++) {
if(gradients[i].size() != network[i].size()) {
gradients[i].resize(network[i].size());
resized = true;
if(i > 0) {
for(std::size_t j = 0; j < gradients[i].size(); j++) {
gradients[i][j].resize(network[i - 1].size());
std::fill(gradients[i][j].begin(),gradients[i][j].end(),0.0);
}
}
_changesOfWeightChanges = _lastGradients;
for(std::size_t i = 1; i < _network.size(); i++) {
for(std::size_t j = 0; j < _changesOfWeightChanges[i].size(); j++) {
std::fill(_changesOfWeightChanges[i][j].begin(),_changesOfWeightChanges[i][j].end(),initialWeightChange);
}
}
if(resized) {
lastGradients = gradients;
if(changesOfWeightChanges.size() != network.size())
changesOfWeightChanges.resize(network.size());
for(std::size_t i = 0; i < network.size(); i++) {
if(changesOfWeightChanges[i].size() != network[i].size()) {
changesOfWeightChanges[i].resize(network[i].size());
if(i > 0) {
for(std::size_t j = 0; j < changesOfWeightChanges[i].size(); j++) {
changesOfWeightChanges[i][j].resize(network[i - 1].size());
std::fill(changesOfWeightChanges[i][j].begin(),changesOfWeightChanges[i][j].end(),initialWeightChange);
}
}
}
}
}
if(resized) {
if(lastWeightChanges.size() != network.size())
lastWeightChanges.resize(network.size());
for(std::size_t i = 0; i < network.size(); i++) {
if(lastWeightChanges[i].size() != network[i].size()) {
lastWeightChanges[i].resize(network[i].size());
if(i > 0) {
for(std::size_t j = 0; j < lastWeightChanges[i].size(); j++) {
lastWeightChanges[i][j].resize(network[i - 1].size());
std::fill(lastWeightChanges[i][j].begin(),lastWeightChanges[i][j].end(),0.1);
}
}
}
_lastWeightChanges = _lastGradients;
for(std::size_t i = 1; i < _network.size(); i++) {
for(std::size_t j = 0; j < _lastWeightChanges[i].size(); j++) {
std::fill(_lastWeightChanges[i][j].begin(),_lastWeightChanges[i][j].end(),0.1);
}
}
}
virtual void computeSlopes(const std::vector<float> &expectation);
virtual void computeDeltas(const std::vector<float> &input);
void updateWeightsAndEndBatch() override;
void updateWeights();
virtual void endBatch() {
}
FeedForward::Network &network;
CorrectionFunction::CorrectionFunction *correctionFunction;
std::vector<std::vector<float>> slopes;
std::vector<std::vector<std::vector<float>>> gradients = {};
std::vector<std::vector<std::vector<float>>> lastGradients = {};
std::vector<std::vector<std::vector<float>>> lastWeightChanges = {};
std::vector<std::vector<std::vector<float>>> changesOfWeightChanges = {};
std::size_t batchSize = 1;
std::size_t currentBatchSize = 0;
std::vector<std::vector<std::vector<float>>> _lastGradients = {};
std::vector<std::vector<std::vector<float>>> _lastWeightChanges = {};
std::vector<std::vector<std::vector<float>>> _changesOfWeightChanges = {};
float maxChangeOfWeights = 50;
float minChangeOfWeights = 0.0001;