modified BP interface
This commit is contained in:
@@ -94,6 +94,9 @@ set_property(TEST activation PROPERTY LABELS unit)
|
||||
add_test(backpropagation tests/backpropagation)
|
||||
set_property(TEST backpropagation PROPERTY LABELS unit)
|
||||
|
||||
add_test(backpropagation_function_cmp tests/backpropagation_function_cmp)
|
||||
set_property(TEST backpropagation_function_cmp PROPERTY LABELS unit)
|
||||
|
||||
add_test(basis tests/basis)
|
||||
set_property(TEST basis PROPERTY LABELS unit)
|
||||
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
|
||||
#include <NeuralNetwork/FeedForward/Network.h>
|
||||
#include "CorrectionFunction/Linear.h"
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace Learning {
|
||||
@@ -13,21 +15,23 @@ namespace Learning {
|
||||
class BackPropagation {
|
||||
|
||||
public:
|
||||
inline BackPropagation(FeedForward::Network &feedForwardNetwork): network(feedForwardNetwork), learningCoefficient(0.4), deltas() {
|
||||
inline BackPropagation(FeedForward::Network &feedForwardNetwork, CorrectionFunction::CorrectionFunction *correction = new CorrectionFunction::Linear()):
|
||||
network(feedForwardNetwork), correctionFunction(correction),learningCoefficient(0.4), deltas() {
|
||||
resize();
|
||||
}
|
||||
|
||||
virtual ~BackPropagation() {
|
||||
delete correctionFunction;
|
||||
}
|
||||
|
||||
BackPropagation(const BackPropagation&)=delete;
|
||||
BackPropagation& operator=(const NeuralNetwork::Learning::BackPropagation&) = delete;
|
||||
|
||||
void teach(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) const {
|
||||
return expected-computed;
|
||||
};
|
||||
|
||||
inline void resize() {
|
||||
if(deltas.size()!=network.size())
|
||||
@@ -41,6 +45,8 @@ namespace Learning {
|
||||
|
||||
FeedForward::Network &network;
|
||||
|
||||
CorrectionFunction::CorrectionFunction *correctionFunction;
|
||||
|
||||
float learningCoefficient;
|
||||
|
||||
std::vector<std::vector<float>> deltas;
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include "CorrectionFunction.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace Learning {
|
||||
namespace CorrectionFunction {
|
||||
class ArcTangent : public CorrectionFunction {
|
||||
public:
|
||||
ArcTangent (const float &c=1.0): coefficient(c) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief operator returns error for values
|
||||
*
|
||||
*/
|
||||
inline virtual float operator()(const float &expected, const float &computed) const override final {
|
||||
//std::cout << (expected-computed) << ":" << atan(expected-computed) << "\n";
|
||||
return atan(coefficient*(expected-computed));
|
||||
}
|
||||
private:
|
||||
const float coefficient;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace Learning {
|
||||
namespace CorrectionFunction {
|
||||
class CorrectionFunction {
|
||||
public:
|
||||
virtual ~ CorrectionFunction() {
|
||||
|
||||
}
|
||||
/**
|
||||
* @brief operator returns error for values
|
||||
*
|
||||
*/
|
||||
virtual float operator()(const float & expected, const float &computed) const = 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
20
include/NeuralNetwork/Learning/CorrectionFunction/Linear.h
Normal file
20
include/NeuralNetwork/Learning/CorrectionFunction/Linear.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "CorrectionFunction.h"
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace Learning {
|
||||
namespace CorrectionFunction {
|
||||
class Linear : public CorrectionFunction {
|
||||
public:
|
||||
/**
|
||||
* @brief operator returns error for values
|
||||
*
|
||||
*/
|
||||
inline virtual float operator()(const float &expected, const float &computed) const override final {
|
||||
return expected-computed;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
22
include/NeuralNetwork/Learning/CorrectionFunction/Optical.h
Normal file
22
include/NeuralNetwork/Learning/CorrectionFunction/Optical.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "CorrectionFunction.h"
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace Learning {
|
||||
namespace CorrectionFunction {
|
||||
class Optical : public CorrectionFunction {
|
||||
public:
|
||||
/**
|
||||
* @brief operator returns error for values
|
||||
*
|
||||
*/
|
||||
inline virtual float operator()(const float &expected, const float &computed) const override final {
|
||||
register float tmp=(expected-computed);
|
||||
register float ret=1+exp(tmp*tmp);
|
||||
return tmp < 0? -ret:ret;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "./BackPropagation.h"
|
||||
#include "./CorrectionFunction/Optical.h"
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace Learning {
|
||||
@@ -11,18 +12,12 @@ namespace Learning {
|
||||
class OpticalBackPropagation : public BackPropagation {
|
||||
|
||||
public:
|
||||
OpticalBackPropagation(FeedForward::Network &feedForwardNetwork): BackPropagation(feedForwardNetwork) {
|
||||
OpticalBackPropagation(FeedForward::Network &feedForwardNetwork): BackPropagation(feedForwardNetwork,new CorrectionFunction::Optical()) {
|
||||
|
||||
}
|
||||
|
||||
virtual ~OpticalBackPropagation() {
|
||||
}
|
||||
|
||||
protected:
|
||||
inline virtual float correction(const float & expected, const float &computed) const override {
|
||||
register float tmp=(expected-computed);
|
||||
register float ret=1+exp(tmp*tmp);
|
||||
return tmp < 0? -ret:ret;
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ void NeuralNetwork::Learning::BackPropagation::teach(const std::vector<float> &i
|
||||
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())*
|
||||
deltas[network.size()-1][j]=correctionFunction->operator()( expectation[j-1], neuron.output())*
|
||||
neuron.getActivationFunction().derivatedOutput(neuron.value(),neuron.output());
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,9 @@ target_link_libraries(basis NeuralNetwork)
|
||||
add_executable(backpropagation backpropagation.cpp)
|
||||
target_link_libraries(backpropagation NeuralNetwork)
|
||||
|
||||
add_executable(backpropagation_function_cmp backpropagation_function_cmp.cpp)
|
||||
target_link_libraries(backpropagation_function_cmp NeuralNetwork)
|
||||
|
||||
add_executable(backpropagation_perf backpropagation_perf.cpp)
|
||||
target_link_libraries(backpropagation_perf NeuralNetwork)
|
||||
|
||||
|
||||
85
tests/backpropagation_function_cmp.cpp
Normal file
85
tests/backpropagation_function_cmp.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#include <NeuralNetwork/FeedForward/Network.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include "../include/NeuralNetwork/Learning/BackPropagation.h"
|
||||
#include "../include/NeuralNetwork/Learning/CorrectionFunction/Optical.h"
|
||||
#include "../include/NeuralNetwork/Learning/CorrectionFunction/ArcTangent.h"
|
||||
|
||||
#define LEARN(A,AR,B,BR,C,CR,D,DR,FUN,COEF) \
|
||||
({\
|
||||
srand(rand);\
|
||||
NeuralNetwork::FeedForward::Network n(2);\
|
||||
NeuralNetwork::ActivationFunction::Sigmoid a(-1);\
|
||||
n.appendLayer(2,a);\
|
||||
n.appendLayer(1,a);\
|
||||
n.randomizeWeights();\
|
||||
NeuralNetwork::Learning::BackPropagation prop(n,FUN);\
|
||||
prop.setLearningCoefficient(COEF);\
|
||||
int error=1; int steps = 0; \
|
||||
while(error > 0 && steps <99999) {\
|
||||
steps++;\
|
||||
error=0;\
|
||||
prop.teach(A,{AR});\
|
||||
prop.teach(B,{BR});\
|
||||
prop.teach(C,{CR});\
|
||||
prop.teach(D,{DR});\
|
||||
error+=fabs(n.computeOutput(A)[0]-AR) > 0.1 ? 1:0;\
|
||||
error+=fabs(n.computeOutput(B)[0]-BR) > 0.1 ? 1:0;\
|
||||
error+=fabs(n.computeOutput(C)[0]-CR) > 0.1 ? 1:0;\
|
||||
error+=fabs(n.computeOutput(D)[0]-DR) > 0.1 ? 1:0;\
|
||||
}\
|
||||
steps;\
|
||||
})
|
||||
|
||||
int main() {
|
||||
long rand=(time(NULL));
|
||||
|
||||
const float linearCoef=0.7;
|
||||
const float opticalCoef=0.11;
|
||||
const float arcTangentCoef=0.6;
|
||||
const float arcTangent=1.5;
|
||||
|
||||
{
|
||||
std::cout << "XOR:\n";
|
||||
|
||||
std::cout << "\tLinear: " <<
|
||||
LEARN(std::vector<float>({1,0}),1,std::vector<float>({1,1}),0,std::vector<float>({0,0}),0,std::vector<float>({0,1}),1,
|
||||
new NeuralNetwork::Learning::CorrectionFunction::Linear,linearCoef) << "\n";
|
||||
|
||||
std::cout << "\tOptical: " <<
|
||||
LEARN(std::vector<float>({1,0}),1,std::vector<float>({1,1}),0,std::vector<float>({0,0}),0,std::vector<float>({0,1}),1,
|
||||
new NeuralNetwork::Learning::CorrectionFunction::Optical,opticalCoef) << "\n";
|
||||
std::cout << "\tArcTangent: " <<
|
||||
LEARN(std::vector<float>({1,0}),1,std::vector<float>({1,1}),0,std::vector<float>({0,0}),0,std::vector<float>({0,1}),1,
|
||||
new NeuralNetwork::Learning::CorrectionFunction::ArcTangent(arcTangent),arcTangentCoef) << "\n";
|
||||
}
|
||||
{
|
||||
std::cout << "AND:\n";
|
||||
std::cout << "\tLinear: " <<
|
||||
LEARN(std::vector<float>({1,0}),0,std::vector<float>({1,1}),1,std::vector<float>({0,0}),0,std::vector<float>({0,1}),0,
|
||||
new NeuralNetwork::Learning::CorrectionFunction::Linear,linearCoef) << "\n";
|
||||
|
||||
std::cout << "\tOptical: " <<
|
||||
LEARN(std::vector<float>({1,0}),0,std::vector<float>({1,1}),1,std::vector<float>({0,0}),0,std::vector<float>({0,1}),0,
|
||||
new NeuralNetwork::Learning::CorrectionFunction::Optical,opticalCoef) << "\n";
|
||||
|
||||
std::cout << "\tArcTangent: " <<
|
||||
LEARN(std::vector<float>({1,0}),0,std::vector<float>({1,1}),1,std::vector<float>({0,0}),0,std::vector<float>({0,1}),0,
|
||||
new NeuralNetwork::Learning::CorrectionFunction::ArcTangent(arcTangent),arcTangentCoef) << "\n";
|
||||
}
|
||||
{
|
||||
std::cout << "AND:\n";
|
||||
std::cout << "\tLinear: " <<
|
||||
LEARN(std::vector<float>({1,0}),1,std::vector<float>({1,1}),0,std::vector<float>({0,0}),1,std::vector<float>({0,1}),1,
|
||||
new NeuralNetwork::Learning::CorrectionFunction::Linear,linearCoef) << "\n";
|
||||
|
||||
std::cout << "\tOptical: " <<
|
||||
LEARN(std::vector<float>({1,0}),1,std::vector<float>({1,1}),0,std::vector<float>({0,0}),1,std::vector<float>({0,1}),1,
|
||||
new NeuralNetwork::Learning::CorrectionFunction::Optical,opticalCoef) << "\n";
|
||||
|
||||
std::cout << "\tArcTangent: " <<
|
||||
LEARN(std::vector<float>({1,0}),1,std::vector<float>({1,1}),0,std::vector<float>({0,0}),1,std::vector<float>({0,1}),1,
|
||||
new NeuralNetwork::Learning::CorrectionFunction::ArcTangent(arcTangent),arcTangentCoef) << "\n";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user