Perceptron in C++ [Neuronal Network]
Übersicht Andere Programmiersprachen Allgemein
CykidBetreff: Perceptron in C++ [Neuronal Network] |
Do, Nov 16, 2017 20:07 Antworten mit Zitat |
|
---|---|---|
Hallo zusammen,
ich beschäftige mich im Moment mit Lernkonzepten für Systeme und da kommt man an Neuronalen Netzwerken nicht vorbei. Hier zu sehen ist ein Perceptron, das (glaub ich) simpelste Konzept eine Single Layer Networks. Es ist in der Lage alle Linear separierebaren Probleme zu lösen. main.cpp Code: [AUSKLAPPEN] #include <iostream>
#include <windows.h> #include <stdlib.h> #include <perceptron.h> #include <time.h> using namespace std; int main() { srand(time(NULL)); perceptron p; for(int i = 0 ; i < 100;i++) { float a = rand() % 2; float b = rand() % 2; float desired = 0; if(a || b) { desired = 1; } else{ desired = -1; } p.training(a,b,desired); } cout<<p.guess(1,1)<<endl; cout<<p.guess(0,1)<<endl; cout<<p.guess(1,0)<<endl; cout<<p.guess(0,0)<<endl; return 0; } perceptron.cpp Code: [AUSKLAPPEN] #include "perceptron.h"
perceptron::perceptron() { //ctor } perceptron::~perceptron() { //dtor } float perceptron::sigm(float in){ return (in >0.0)?1:-1; } float perceptron::sum(){ this->summe = (this->inputs[0] * this->weights[0])+(this->inputs[1] * this->weights[1])+ (this->inputs[2] * this->weights[2]); return this->summe; } float perceptron::guess(float a,float b) { this->inputs[0] = a; this->inputs[1] = b; return this->sigm(this->sum()); } float perceptron::training(float a,float b,float desired) { float result = this->guess(a,b); float error = desired - result; this->weights[0] = this->weights[0] + this->c * error * inputs[0]; this->weights[1] = this->weights[1] + this->c * error * inputs[1]; this->weights[2] = this->weights[2] + this->c * error * inputs[2]; } perceptron.h Code: [AUSKLAPPEN] #ifndef PERCEPTRON_H
#define PERCEPTRON_H #include <iostream> using namespace std; class perceptron { public: perceptron(); virtual ~perceptron(); float inputs[3] = {0,0,1}; float weights[3] = {0.2,-0.2,0.4}; int output = 0; float sigm(float in); float sum(); float summe = 0; float guess(float a,float b); float training(float a,float b,float desired); float c = 0.0001; protected: private: }; #endif // PERCEPTRON_H Viel Spaß beim Aufgaben stellen^^ |
||
Cykid |
So, Nov 19, 2017 11:11 Antworten mit Zitat |
|
---|---|---|
Hier ist eine alternative main.cpp um per multi layer perceptron das XOR Problem zu lösen. Das Perceptron selbst bleibt unverändert.
Code: [AUSKLAPPEN] #include <iostream>
#include <windows.h> #include <stdlib.h> #include <perceptron.h> #include <time.h> using namespace std; int main() { srand(time(NULL)); perceptron aBlock; perceptron bBlock; perceptron outBlock; for(int i = 0; i< 100000; i++) { float a = rand() % 2; float b = rand() % 2; float desired = 0; desired = (a && b)? 1: -1; outBlock.training(a,b,desired); } for(int i = 0; i< 100000; i++) { float a = rand() % 2; float b = rand() % 2; float desired = 0; desired = !(a && b)? 1: -1; aBlock.training(a,b,desired); } for(int i = 0; i< 100000; i++) { float a = rand() % 2; float b = rand() % 2; float desired = 0; desired = (a || b)? 1: -1; bBlock.training(a,b,desired); } //Multilayer XOR float a = 0; float b = 1; cout<<outBlock.guess(aBlock.guess(a,b),bBlock.guess(a,b)); return 0; } |
||
Übersicht Andere Programmiersprachen Allgemein
Powered by phpBB © 2001 - 2006, phpBB Group