backpropagation implementation
This commit is contained in:
33
include/NeuralNetwork/Learning/BackPropagation.h
Normal file
33
include/NeuralNetwork/Learning/BackPropagation.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <NeuralNetwork/FeedForward/Network.h>
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace Learning {
|
||||
|
||||
/** @class BackPropagation
|
||||
* @brief
|
||||
*/
|
||||
class BackPropagation {
|
||||
|
||||
public:
|
||||
BackPropagation(): learningCoefficient(0.4) {
|
||||
|
||||
}
|
||||
virtual ~BackPropagation() {
|
||||
}
|
||||
|
||||
void teach(FeedForward::Network &n,const std::vector<float> &input, const std::vector<float> &output);
|
||||
|
||||
inline virtual void setLearningCoefficient (const float& coefficient) { learningCoefficient=coefficient; }
|
||||
|
||||
protected:
|
||||
inline virtual float correction(const float & expected, const float &computed) {
|
||||
return expected-computed;
|
||||
};
|
||||
float learningCoefficient;
|
||||
};
|
||||
}
|
||||
}
|
||||
54
src/NeuralNetwork/Learning/BackPropagation.cpp
Normal file
54
src/NeuralNetwork/Learning/BackPropagation.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <NeuralNetwork/Learning/BackPropagation.h>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
void NeuralNetwork::Learning::BackPropagation::teach(FeedForward::Network &network,const std::vector<float> &input, const std::vector<float> &expectation) {
|
||||
|
||||
network.computeOutput(input);
|
||||
std::vector<std::vector<float>> deltas;
|
||||
|
||||
deltas.resize(network.size());
|
||||
|
||||
for(std::size_t i=0; i < network.size(); i++) {
|
||||
deltas[i].resize(network[i].size());
|
||||
deltas[i][0]=0.0;
|
||||
}
|
||||
|
||||
auto& outputLayer=network[network.size()-1];
|
||||
for(std::size_t j=1;j<outputLayer.size();j++) {
|
||||
auto& neuron = outputLayer[j];
|
||||
deltas[network.size()-1][j]=correction( expectation[j-1], neuron.output())*
|
||||
neuron.getActivationFunction().derivatedOutput(neuron.value(),neuron.output());
|
||||
}
|
||||
|
||||
for(int layerIndex=static_cast<int>(network.size()-2);layerIndex>0;layerIndex--) {
|
||||
auto &layer=network[layerIndex];
|
||||
|
||||
for(std::size_t j=1;j<layer.size();j++) {
|
||||
float deltasWeight = 0;
|
||||
for(std::size_t k=1;k<network[layerIndex+1].size();k++) {
|
||||
deltasWeight+=deltas[layerIndex+1][k]* network[layerIndex+1][k].getWeight(layer[j]);
|
||||
}
|
||||
float newDelta=deltasWeight*layer[j].getActivationFunction().derivatedOutput(layer[j].value(),layer[j].output());
|
||||
deltas[layerIndex][j]=newDelta;
|
||||
}
|
||||
}
|
||||
|
||||
for(std::size_t layerIndex=1;layerIndex<network.size();layerIndex++) {
|
||||
auto &layer=network[layerIndex];
|
||||
auto &prevLayer=network[layerIndex-1];
|
||||
|
||||
std::size_t max=prevLayer.size();
|
||||
|
||||
for(std::size_t j=1;j<layer.size();j++) {
|
||||
layer[j].setWeight(prevLayer[0],layer[j].getWeight(prevLayer[0])+deltas[layerIndex][j]*learningCoefficient);
|
||||
for(std::size_t k=1;k<max;k++) {
|
||||
if(layerIndex==1) {
|
||||
layer[j].setWeight(prevLayer[k], layer[j].getWeight(prevLayer[k])+learningCoefficient*deltas[layerIndex][j]*input[k-1]);
|
||||
} else {
|
||||
layer[j].setWeight(prevLayer[k], layer[j].getWeight(prevLayer[k])+learningCoefficient*deltas[layerIndex][j]*prevLayer[k].output());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
44
tests/backpropagation.cpp
Normal file
44
tests/backpropagation.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <NeuralNetwork/FeedForward/Network.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include "../include/NeuralNetwork/Learning/BackPropagation.h"
|
||||
|
||||
int main() {
|
||||
{ // XOR problem
|
||||
NeuralNetwork::FeedForward::Network n(2);
|
||||
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
|
||||
n.appendLayer(2,a);
|
||||
n.appendLayer(1,a);
|
||||
|
||||
n.randomizeWeights();
|
||||
|
||||
NeuralNetwork::Learning::BackPropagation prop;
|
||||
for(int i=0;i<10000;i++) {
|
||||
prop.teach(n,{1,0},{1});
|
||||
prop.teach(n,{1,1},{0});
|
||||
prop.teach(n,{0,0},{0});
|
||||
prop.teach(n,{0,1},{1});
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<float> ret =n.computeOutput({1,1});
|
||||
assert(ret[0] < 0.1);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<float> ret =n.computeOutput({0,1});
|
||||
assert(ret[0] > 0.9);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<float> ret =n.computeOutput({1,0});
|
||||
assert(ret[0] > 0.9);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<float> ret =n.computeOutput({0,0});
|
||||
assert(ret[0] < 0.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user