bp: refactored

This commit is contained in:
2016-10-30 20:40:07 +01:00
parent f1ea858f32
commit 006810a49c
2 changed files with 95 additions and 48 deletions

View File

@@ -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<float> &input);
virtual void computeDeltas(const std::vector<float> &input);
void updateWeights();
virtual void computeSlopes(const std::vector<float> &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<std::vector<float>> slopes;
std::vector<std::vector<float>> deltas;
std::vector<std::vector<float>> lastDeltas;
std::vector<std::vector<std::vector<float>>> deltas = {};
std::vector<std::vector<std::vector<float>>> lastDeltas = {};
};
}
}