backprop: momentums + decay, quickprop: renaming

This commit is contained in:
2016-09-07 22:26:50 +02:00
parent b6b7703299
commit 9fbcb727a2
4 changed files with 62 additions and 32 deletions

View File

@@ -9,9 +9,11 @@ void NeuralNetwork::Learning::BackPropagation::teach(const std::vector<float> &i
resize();
computeDeltas(expectation);
computeSlopes(expectation);
updateWeights(input);
std::swap(deltas,lastDeltas);
}
@@ -28,21 +30,25 @@ void NeuralNetwork::Learning::BackPropagation::updateWeights(const std::vector<f
float delta =slopes[layerIndex][j]*learningCoefficient;
layer[j].weight(0)+=delta;
//momentum
delta += momentumWeight * lastDeltas[layerIndex][j];
deltas[layerIndex][j]=delta;
layer[j].weight(0)+=delta - weightDecay *layer[j].weight(0);
for(std::size_t k=1;k<prevLayerSize;k++) {
if(layerIndex==1) {
layer[j].weight(k)+=delta*input[k-1];
layer[j].weight(k)+=delta*input[k-1] - weightDecay * layer[j].weight(k);
} else {
layer[j].weight(k)+=delta*prevLayer[k].output();
layer[j].weight(k)+=delta*prevLayer[k].output() - weightDecay * layer[j].weight(k);
}
}
}
}
}
void NeuralNetwork::Learning::BackPropagation::computeDeltas(const std::vector<float> &expectation) {
void NeuralNetwork::Learning::BackPropagation::computeSlopes(const std::vector<float> &expectation) {
auto& outputLayer=network[network.size()-1];
for(std::size_t j=1;j<outputLayer.size();j++) {
auto& neuron = outputLayer[j];

View File

@@ -18,17 +18,17 @@ void NeuralNetwork::Learning::QuickPropagation::updateWeights(const std::vector<
float newChange=0;
if(fabs (lastWeightChange[layerIndex][j])> 0.0001) {
if(std::signbit(lastWeightChange[layerIndex][j]) == std::signbit(slopes[layerIndex][j])) {
if(fabs (deltas[layerIndex][j])> 0.0001) {
if(std::signbit(deltas[layerIndex][j]) == std::signbit(slopes[layerIndex][j])) {
newChange+= slopes[layerIndex][j]*_epsilon;
if(fabs(slopes[layerIndex][j]) > fabs(shrinkFactor * previousSlopes[layerIndex][j])) {
newChange += _maxChange * lastWeightChange[layerIndex][j];
newChange += _maxChange * deltas[layerIndex][j];
}else {
newChange+=slopes[layerIndex][j]/(previousSlopes[layerIndex][j]-slopes[layerIndex][j]) * lastWeightChange[layerIndex][j];
newChange+=slopes[layerIndex][j]/(previousSlopes[layerIndex][j]-slopes[layerIndex][j]) * deltas[layerIndex][j];
}
} else {
newChange+=slopes[layerIndex][j]/(previousSlopes[layerIndex][j]-slopes[layerIndex][j]) * lastWeightChange[layerIndex][j];
newChange+=slopes[layerIndex][j]/(previousSlopes[layerIndex][j]-slopes[layerIndex][j]) * deltas[layerIndex][j];
}
} else {
newChange+= slopes[layerIndex][j]*_epsilon;
@@ -49,5 +49,5 @@ void NeuralNetwork::Learning::QuickPropagation::updateWeights(const std::vector<
}
slopes.swap(previousSlopes);
weightChange.swap(lastWeightChange);
weightChange.swap(deltas);
}