86 lines
3.5 KiB
C++
86 lines
3.5 KiB
C++
#include <NeuralNetwork/FeedForward/Network.h>
|
|
|
|
#include <cassert>
|
|
#include <iostream>
|
|
#include <NeuralNetwork/Learning/BackPropagation.h>
|
|
#include <NeuralNetwork/Learning/CorrectionFunction/Optical.h>
|
|
#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,
|
|
std::make_shared<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,
|
|
std::make_shared<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,
|
|
std::make_shared<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,
|
|
std::make_shared<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,
|
|
std::make_shared<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,
|
|
std::make_shared<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,
|
|
std::make_shared<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,
|
|
std::make_shared<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,
|
|
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::ArcTangent>(arcTangent),arcTangentCoef) << "\n";
|
|
}
|
|
}
|