init commit

This commit is contained in:
2024-11-12 17:41:10 +01:00
parent 1e4f1f955b
commit 20bc9108d3
146 changed files with 24465 additions and 0 deletions

94
backprop/.#reseau.cpp.1.4 Normal file
View File

@@ -0,0 +1,94 @@
#include "reseau.h"
reseau::reseau(int In, int Hid, int Out)
{
Ocouche=OutputCouche(In,&Hcouche);
Hcouche=HiddenCouche(Hid,&Ocouche);
Icouche=InputCouche(Out,&Hcouche);
initshm();
}
reseau::~reseau()
{
}
void reseau::forward(bool input[],bool target[])
{
std::vector<bool> tmp;
/* on active les couches */
for(unsigned i=0;i<Icouche.getNumber();i++) tmp.push_back(input[i]);
Icouche.activate(tmp);
Hcouche.activate(Icouche);
Ocouche.activate();
/* on place le resultat dans la shm */
for(unsigned i=0;i<Ocouche.getNumber();i++) target[i]=util.accept(Ocouche[i].getWeight());
}
void reseau::backward(bool input[], bool target[])
{
std::vector<double> hidDelta;
std::vector<double> outDelta;
/* Calcul des delta pour la couche OUPUT */
for(unsigned i=0;i<Ocouche.getNumber();i++)
{
outDelta.push_back(util.dsigmoid((double)target[i] - Ocouche[i].getWeight()));
}
/* Calcul des delta pour la couche HIDDEN */
for(unsigned i=0;i<Hcouche.getNumber();i++)
{
hidDelta.push_back(util.dsigmoid((double)target[i] - Hcouche[i].getWeight()));
}
Icouche.backPropagate(hidDelta);
Ocouche.backPropagate(outDelta);
}
void reseau::initshm()
{
if ((shmid = shmget(SHMKEY, sizeof(struct shmdata), 0666)) < 0)
{
perror("Unable to get shm id \n");
exit(1);
}
if ((SData = (struct shmdata *)shmat(shmid, NULL, 0)) == (struct shmdata *) -1)
{
perror("Unable to attach shm segment\n");
exit(1);
}
}
double reseau::getError(bool target[])
{
double error=0.0;
for(unsigned i=0;i<Ocouche.getNumber();i++)
{
error += pow(((double)target[i] - Ocouche[i].getWeight()),2);
}
return sqrt(error);
}
double reseau::learnOne(bool input[], bool target[])
{
double error=0;
while (error>LEARNACCEPT)
{
forward(input,target);
backward(input,target);
error=getError(target);
cout << "erreur : " << error << endl ;
}
return error;
}
double reseau::learnAll(std::vector<bool *> inputs, std::vector<bool *> targets)
{
double error=0.0;
unsigned i;
for(i=0;i<inputs.size();i++)
{
error+=learnOne(inputs[i],targets[i]);
}
return (double)(error/i);
}
/* END */

23
backprop/CVS/Entries Normal file
View File

@@ -0,0 +1,23 @@
D/test////
/Makefile/1.11/Wed Apr 13 15:17:45 2005//
/Utils.cpp/1.5/Wed Apr 13 15:04:03 2005//
/Utils.h/1.3/Wed Apr 13 15:04:03 2005//
/couche.cpp/1.7/Wed Apr 13 16:23:02 2005//
/couche.h/1.6/Wed Apr 13 15:04:03 2005//
/global.h/1.13/Wed Apr 13 15:04:03 2005//
/hiddenCouche.cpp/1.3/Wed Apr 13 15:04:03 2005//
/hiddenCouche.h/1.8/Wed Apr 13 15:04:03 2005//
/inputCouche.cpp/1.7/Wed Apr 13 15:04:03 2005//
/inputCouche.h/1.7/Wed Apr 13 15:04:03 2005//
/inputtest.h/1.3/Wed Apr 13 15:04:03 2005//
/main.cpp/1.12/Wed Apr 13 15:04:03 2005//
/neurone.cpp/1.1.1.1/Wed Apr 13 15:04:03 2005//
/neurone.h/1.1.1.1/Wed Apr 13 15:04:03 2005//
/outputCouche.cpp/1.9/Wed Apr 13 15:04:03 2005//
/outputCouche.h/1.11/Wed Apr 13 15:04:03 2005//
/reseau.cpp/1.18/Wed Apr 13 15:04:03 2005//
/reseau.h/1.11/Wed Apr 13 15:04:03 2005//
/synapse.cpp/1.4/Wed Apr 13 15:04:03 2005//
/synapse.h/1.3/Wed Apr 13 15:04:03 2005//
/synapseMatrix.cpp/1.2/Wed Apr 13 15:04:03 2005//
/synapseMatrix.h/1.1/Wed Apr 13 15:04:03 2005//

1
backprop/CVS/Repository Normal file
View File

@@ -0,0 +1 @@
ia2005/backprop

1
backprop/CVS/Root Normal file
View File

@@ -0,0 +1 @@
:pserver:feeling@jfroche.be:/opt/cvsroot

48
backprop/Makefile Normal file
View File

@@ -0,0 +1,48 @@
#$Id: Makefile,v 1.11 2005/04/10 11:46:45 feeling Exp $'
CFLAGS= -Wall -g -fpermissive -std=c++14
CPP= g++
GCC= gcc
EXECUTABLE = backkprop
#INCLUDES= -I ./Include
all : buildobjs
test: backprop testall
backprop : neurone.o main.o couche.o synapse.o synapseMatrix.o inputCouche.o outputCouche.o hiddenCouche.o reseau.o Utils.o
$(CPP) $(CFLAGS) $(INCLUDES) -o $(EXECUTABLE) neurone.o main.o couche.o synapse.o synapseMatrix.o inputCouche.o outputCouche.o hiddenCouche.o reseau.o Utils.o
buildobjs : neurone.o couche.o synapse.o synapseMatrix.o inputCouche.o outputCouche.o hiddenCouche.o reseau.o Utils.o
neurone.o : neurone.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c neurone.cpp
couche.o : couche.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c couche.cpp
inputCouche.o : inputCouche.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c inputCouche.cpp
outputCouche.o : outputCouche.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c outputCouche.cpp
hiddenCouche.o : hiddenCouche.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c hiddenCouche.cpp
synapse.o : synapse.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c synapse.cpp
reseau.o : reseau.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c reseau.cpp
synapseMatrix.o : synapseMatrix.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c synapseMatrix.cpp
main.o : main.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c main.cpp
Utils.o : Utils.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c Utils.cpp
clean :
rm -f *.o
mrproper :
rm -rf *.o
rm -f $(EXECUTABLE)
cleano :
rm -f *.o
testall:
$(MAKE) -C test
cleantest:
rm -f *.o
$(MAKE) -C test clean

25
backprop/Utils.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include "Utils.h"
#include <math.h>
#include <iostream>
using namespace std;
Utils::Utils()
{
}
double Utils::sigmoid(const double val)
{
return tanh(val);
}
double Utils::dsigmoid(const double val)
{
return (1.0-(val*val));
}
bool Utils::accept(const double val)
{
if(val>ACCEPT) return 1;
return 0;
}

13
backprop/Utils.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef __UTILS_H
#define __UTILS_H
#include "global.h"
class Utils {
private:
public:
Utils();
double sigmoid(double val);
double dsigmoid(double val);
bool accept(double val);
};
#endif

BIN
backprop/Utils.o Normal file

Binary file not shown.

109
backprop/couche.cpp Normal file
View File

@@ -0,0 +1,109 @@
#include "couche.h"
Couche::Couche(){
nbrNeurone=0;
nextCouche=NULL;
listNeurone=NULL;
synSortantes = SynapseMatrix();
}
Couche::~Couche(){
deleteList();
}
Couche::Couche(const unsigned int nbr,const Couche* next){
nbrNeurone=(unsigned int)nbr;
if(next!=NULL){
setNextCouche(next);
}
else{
nextCouche = NULL;
synSortantes = SynapseMatrix();
}
listNeurone=NULL;
for(unsigned int i=0;i<nbr;++i)
listNeurone=new Neurone(0.0,listNeurone);
}
const unsigned int Couche::getNumber() const{
return nbrNeurone;
}
Neurone& Couche::operator[](const unsigned int i) const throw(const unsigned int){
if(i>(getNumber()-1))
throw(i);
Neurone* res=listNeurone;
for(unsigned int j=0;j<i;++j)
res=res->getNextNeurone();
return *res;
}
bool Couche::operator==(const Couche c){
return ((this)==(&c));
}
Couche& Couche::operator=(const Couche& c){
if(this!=&c){
nbrNeurone=c.getNumber();
nextCouche=c.getNextCouche();
deleteList();
listNeurone=copy(c.listNeurone);
synSortantes=c.synSortantes;
}
return *this;
}
void Couche::setNextCouche(const Couche* c) throw(std::string){
//cout << "setnextcouche : " << this << " -> " << c << endl;
if(c==NULL){
throw(std::string("Passing NULL as next couche forbiden"));
}
else{
nextCouche=(Couche*)c;
synSortantes = SynapseMatrix(getNumber(),c->getNumber());
synSortantes.randomize();
}
}
void Couche::randomizeSynapseMatrix(){
synSortantes.randomize();
}
Couche* Couche::getNextCouche() const{
return nextCouche;
}
void Couche::deleteN(Neurone* n){
if(n){
deleteN(n->getNextNeurone());
delete n;
n=NULL;
}
}
void Couche::deleteList(){
deleteN(listNeurone);
}
Neurone* Couche::copy(Neurone* n){
if(n){
return new Neurone(n->getWeight(),copy(n->getNextNeurone()));
}
return NULL;
}
Synapse& Couche::getSynapse(const unsigned int neurThisCouche,const unsigned int neurNextCouche) const throw(const unsigned int,std::string){
return synSortantes(neurThisCouche,neurNextCouche);
}
ostream & operator<<(ostream& os,const Couche & c){
unsigned int size = c.getNumber();
for(unsigned int i=0 ; i<size ; i++)
os << setw(6) << c[i].getWeight() << " ";
os << endl;
return os;
}
void Couche::printSynapseMatrix(){
cout << synSortantes;
}

35
backprop/couche.h Normal file
View File

@@ -0,0 +1,35 @@
#ifndef _COUCHE_H
#define _COUCHE_H
#include <iostream>
#include "neurone.h"
#include "synapseMatrix.h"
#include "Utils.h"
using namespace std;
class Couche{
private:
Neurone* listNeurone;
Couche* nextCouche;
unsigned int nbrNeurone;
void deleteN(Neurone* n);
void deleteList();
Neurone* copy(Neurone* n);
protected:
Utils util;
SynapseMatrix synSortantes;
public:
Couche();
~Couche();
Couche(const unsigned int nbr,const Couche* next=NULL);
const unsigned int getNumber() const;
Neurone& operator [](const unsigned int i) const throw(const unsigned int);
bool operator ==(const Couche c);
Couche& operator=(const Couche& c);
void setNextCouche(const Couche* c) throw(std::string);
Couche* getNextCouche() const;
void randomizeSynapseMatrix();
Synapse& getSynapse(const unsigned int neurThisCouche,const unsigned int neurNextCouche) const throw(const unsigned int,std::string);
friend ostream& operator<<(ostream& os,const Couche &c);
void printSynapseMatrix();
};
#endif

BIN
backprop/couche.o Normal file

Binary file not shown.

11
backprop/global.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef _GLOBAL_H
#define _GLOBAL_H
#define SYNAPSE_WEIGHT_MAX 2.0
#define SYNAPSE_WEIGHT_MIN -2.0
#define LEARNING_RATE 0.05
#define MOMENTUM 0.1
#define ACCEPT 0.65 /* Was 0.95 */
#define LEARNACCEPT 0.15 /* Was 0.05 */
#endif

11
backprop/global.h~ Normal file
View File

@@ -0,0 +1,11 @@
#ifndef _GLOBAL_H
#define _GLOBAL_H
#define SYNAPSE_WEIGHT_MAX 2.0
#define SYNAPSE_WEIGHT_MIN -2.0
#define LEARNING_RATE 0.01
#define MOMENTUM 0.1
#define ACCEPT 0.65 /* Was 0.95 */
#define LEARNACCEPT 0.15 /* Was 0.05 */
#endif

15
backprop/hiddenCouche.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include "hiddenCouche.h"
void HiddenCouche::activate(const Couche &prevCouche){
unsigned int prevSize = prevCouche.getNumber();
unsigned int thisSize = getNumber();
double sum;
for(unsigned int j=0; j < thisSize ; ++j){
sum = 0.0;
for(unsigned int i=0; i < prevSize ; ++i){
sum += prevCouche[i].getWeight() * prevCouche.getSynapse(i,j).getWeight();
}
(*this)[j] = util.sigmoid(sum);
}
}

15
backprop/hiddenCouche.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef __HIDDNCOUCHE_H
#define __HIDDNCOUCHE_H
#include "couche.h"
#include <math.h>
class HiddenCouche : public Couche{
private:
public:
HiddenCouche() : Couche(){};
HiddenCouche(const unsigned int nbr,const Couche* next=NULL) : Couche(nbr,next){};
void activate(const Couche &prevCouche);
};
#endif

BIN
backprop/hiddenCouche.o Normal file

Binary file not shown.

60
backprop/inputCouche.cpp Normal file
View File

@@ -0,0 +1,60 @@
#include "inputCouche.h"
InputCouche::InputCouche() : Couche(){
//cout << " Create empty PrevChange Matrix [in InputCouche()]\n";
prevChange = SynapseMatrix();
//cout << " Done\n";
}
InputCouche::InputCouche(const unsigned int nbr,const Couche* next) : Couche(nbr,next){
//cout << " Create PrevChange Matrix [in InputCouche(const unsigned int nbr,const Couche* next)]\n";
if(next!=NULL)
prevChange = SynapseMatrix(getNumber(),next->getNumber());
else
prevChange = SynapseMatrix();
//cout << prevChange;
//cout << " Done\n";
}
void InputCouche::setNextCouche(const HiddenCouche* c) throw(std::string){
Couche::setNextCouche(c);
prevChange = SynapseMatrix(getNumber(),c->getNumber());
}
void InputCouche::activate(const std::vector<bool> &v) throw(std::string){
unsigned int size = v.size();
if(size != getNumber()-1){
throw(std::string("Wrong number of Input passed to InputCouche"));
}
for(unsigned int i=0 ; i<size ; ++i){
(*this)[i].setWeight((double)v[i]);
}
}
void InputCouche::backPropagate(const std::vector<double> deltaHidden){
unsigned int size = deltaHidden.size();
double change;
if(size != getNextCouche()->getNumber()) // prend les delta de la couche cachée!
throw(std::string("Wrong number of delta passed to InputCouche for back propagation"));
for(unsigned int i=0; i < getNumber() ; ++i)
for(unsigned int j=0; j < getNextCouche()->getNumber(); ++j){
change = (deltaHidden[j] * (*this)[i].getWeight());
synSortantes(i,j) = (getSynapse(i,j).getWeight()) + (LEARNING_RATE*change) + (MOMENTUM*getChange(i,j));
prevChange(i,j) = change;
}
}
double InputCouche::getChange(const unsigned int neurThisCouche,const unsigned int neurNextCouche) const throw(const unsigned int,std::string){
return prevChange(neurThisCouche,neurNextCouche).getWeight();
}
void InputCouche::printMatrix(){
cout << prevChange;
}
InputCouche& InputCouche::operator=(const InputCouche& c){
if(this!=&c){
Couche::operator=(c);
prevChange = c.prevChange;
}
return (*this);
};

25
backprop/inputCouche.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef __INPUTCOUCHE_H
#define __INPUTCOUCHE_H
#include <vector>
#include <string>
#include "couche.h"
#include "hiddenCouche.h"
#include "global.h"
using namespace std;
class InputCouche : public Couche{
private:
SynapseMatrix prevChange;
public:
InputCouche();
InputCouche(const unsigned int nbr,const Couche* next=NULL);
void activate(const std::vector<bool> &v) throw(std::string);
void setNextCouche(const HiddenCouche* c) throw(std::string);
void backPropagate(const std::vector<double> deltaHidden);
double getChange(const unsigned int i, const unsigned int j) const throw(const unsigned int,std::string);
void printMatrix();
InputCouche& operator=(const InputCouche& c);
};
#endif

BIN
backprop/inputCouche.o Normal file

Binary file not shown.

152
backprop/inputtest.h Normal file
View File

@@ -0,0 +1,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1},
{1,0,1},
{1,0,1},
{1,0,1},
{1,0,1},
{1,0,1},
{1,0,1},
{1,0,1},
{1,0,1},
{1,0,1},
{1,0,1}
};
static bool AndI[4][2]=
{
{0,0},
{0,1},
{1,0},
{1,1}
};
static bool AndR[4][1]=
{
{0},
{0},
{0},
{1}
};
static bool XorI[4][2]=
{
{0,0},
{0,1},
{1,0},
{1,1}
};
static bool XorR[4][1]=
{
{0},
{1},
{1},
{0}
};

127
backprop/main.cpp Normal file
View File

@@ -0,0 +1,127 @@
#include "neurone.h"
#include "couche.h"
#include "inputCouche.h"
#include "synapse.h"
#include "synapseMatrix.h"
#include "reseau.h"
#include <iostream>
#include "inputtest.h"
//using namespace std;
void testafroche()
{
try {
Neurone NewNeu();
Couche Output(4);
Couche Input(3,&Output);
Synapse s(2.324232);
SynapseMatrix m(3,2);
InputCouche InputC(3);
cout << InputC ;
cout << "get syn:" << Input.getSynapse(2,2).getWeight() << endl;
m(1,1) = 2.5;
cout<< "Test: " << m(1,1).getWeight() << endl;
m.randomize();
cout<< m ;
//Input[4];
Input[2]=2.132;
Input.getSynapse(1,2).getWeight();
cout << "Synapse weight: " << s << endl;
}
catch(std::string str){
cout << "Exception: " <<endl;
cout << " "<< str << endl;
}
catch(const unsigned int index){
cout << "Exception: " <<endl;
cout << " Index " << index << " is out of range" << endl;
}
}
void testlau()
{
Utils u;
int inputsize = /*16; */ 64; /* */ // nombre de neurones d'input
int outputsize = /*3; */ 3; /* */ // nombre de neurones d'output
int nbtests = /*9; */ 32; /* */ // nombre de tests (xor = 4)
int intermid = /*8; */ 8; /* */ // nombre de neurones intermediaires
int nbtests2 = /*8; */ 8; /* */ // nombre de tests différents du learn
bool Input[nbtests][inputsize];
bool Output[nbtests][outputsize];
bool InputTest[nbtests2][inputsize];
for(int i=0;i<nbtests;i++)
for(int j=0;j<inputsize;j++)
Input[i][j] = /*Inp[i][j]; /**/ VoitureI[i][j];
for(int i=0;i<nbtests2;i++)
for(int j=0;j<inputsize;j++)
InputTest[i][j] = /*Inp_[i][j]; /**/ VoitureI_[i][j];
for(int i=0;i<nbtests;i++)
for(int j=0;j<outputsize;j++)
Output[i][j] = /*Rep[i][j];/**/ VoitureR[i][j];
cout << "Init Network ...." << endl;
Reseau R(inputsize,intermid,outputsize);
cout << "Done ...." << endl;
std::vector<bool *> in;
std::vector<bool *> r;
for(int i=0;i<nbtests;i++)
{
in.push_back(Input[i]);
r.push_back(Output[i]);
}
double erreur ;
int cpt=0;
while(1)
{
++cpt;
erreur = R.learnAll(in,r);
if(erreur < LEARNACCEPT)
break;
if(cpt%100 == 0)
cout << " : step " << cpt << " error:" << erreur << endl;
}
cout << "final error: " << erreur << endl;
/* et on teste */
cout << "tests sur les jeux de données : \n";
std::vector<double> V;
for(int i=0;i<nbtests;i++)
{
V=R.forward(Input[i]);
cout << "pour : ";
for(int j=0;j<inputsize;j++) cout << Input[i][j] ;
for(unsigned i=0;i<V.size();i++)
cout << " [" << u.accept(V[i]) << "]";
cout << " bool " ;
for(unsigned i=0;i<V.size();i++)
cout << " [" << V[i] << "] " ;
cout << "\n";
}
cout << "tests différents : \n";
V.clear();
for(int i=0;i<nbtests2;i++)
{
V=R.forward(InputTest[i]);
cout << "pour : ";
for(int j=0;j<inputsize;j++) cout << InputTest[i][j];
for(unsigned i=0;i<V.size();i++)
cout << " [" << u.accept(V[i]) << "]";
cout << " bool " ;
for(unsigned i=0;i<V.size();i++)
cout << " [" << V[i] << "] " ;
cout << "\n";
}
}
int main(void){
testlau();
return 0;
}

43
backprop/neurone.cpp Normal file
View File

@@ -0,0 +1,43 @@
#include "neurone.h"
Neurone::Neurone(){
nextNeurone=NULL;
weight=0.0;
}
Neurone::Neurone(const double w,const Neurone* next){
nextNeurone=(Neurone*)next;
weight=(double)w;
}
bool Neurone::getState() const{
return bool(weight > ACTIVE);
}
double Neurone::getWeight() const{
return weight;
}
void Neurone::setWeight(const double a){
weight=a;
}
void Neurone::setNextNeurone(const Neurone* next){
nextNeurone=(Neurone*)next;
}
Neurone* Neurone::getNextNeurone() const{
return nextNeurone;
}
void Neurone::operator =(double w){
weight = w;
}
Neurone& Neurone::operator =(const Neurone& n){
if(this!=&n){
weight = n.getWeight();
nextNeurone = n.getNextNeurone();
}
return *this;
}

23
backprop/neurone.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef _NEURONE_H
#define _NEURONE_H
#include <iostream>
#include <stdlib.h>
#define ACTIVE 0.9
using namespace std;
class Neurone{
private:
double weight;
Neurone* nextNeurone;
public:
Neurone();
Neurone(const double w,const Neurone* next=NULL);
bool getState() const;
double getWeight() const;
void setWeight(const double a);
void setNextNeurone(const Neurone* next);
Neurone* getNextNeurone() const;
void operator =(double w);
Neurone& operator =(const Neurone& n);
};
#endif

BIN
backprop/neurone.o Normal file

Binary file not shown.

66
backprop/outputCouche.cpp Normal file
View File

@@ -0,0 +1,66 @@
#include "outputCouche.h"
OutputCouche::OutputCouche() : Couche(){
//cout << " Create empty PrevChange Matrix [in OutputCouche()]\n";
prevChange = SynapseMatrix();
//cout << " Done\n";
}
OutputCouche::OutputCouche(const unsigned int nbr,const HiddenCouche *prev) : Couche(nbr,NULL){
prevCouche = prev;
if(prev)
prevChange = SynapseMatrix(prev->getNumber(),getNumber());
else
prevChange = SynapseMatrix();
}
void OutputCouche::setPrevCouche(const HiddenCouche *prev){
prevCouche = prev;
//cout << " Create Synapse PrevChange [in OutputCouche.setPrevCouche(const HiddenCouche *prev)]\n";
prevChange = SynapseMatrix(prev->getNumber(),getNumber());
//cout << prevChange;
//cout << " Done\n";
}
void OutputCouche::activate(){
unsigned int prevSize = prevCouche->getNumber();
unsigned int thisSize = getNumber();
double sum;
for(unsigned int i=0; i < thisSize ; ++i){
sum = 0.0;
for(unsigned int j=0; j < prevSize ; ++j){
sum += (*prevCouche)[j].getWeight() * (*prevCouche).getSynapse(j,i).getWeight();
}
(*this)[i] = util.sigmoid(sum);
}
}
void OutputCouche::backPropagate(const std::vector<double> deltaOutput){
unsigned int prevSize = prevCouche->getNumber();
unsigned int thisSize = getNumber();
double change;
for(unsigned int i=0; i < prevSize ; ++i){
for(unsigned int j=0; j < thisSize ; ++j){
change = (deltaOutput[j] * (*prevCouche)[i].getWeight());
(*prevCouche).getSynapse(i,j) = (*prevCouche).getSynapse(i,j).getWeight() + LEARNING_RATE*change + MOMENTUM*getChange(i,j);
prevChange(i,j) = change;
}
}
}
double OutputCouche::getChange(const unsigned int neurPrevCouche,const unsigned int neurThisCouche) const throw(const unsigned int, std::string){
return prevChange(neurPrevCouche,neurThisCouche).getWeight();
}
void OutputCouche::printMatrix(){
cout << prevChange;
}
OutputCouche& OutputCouche::operator=(const OutputCouche& c){
if(this!=&c){
Couche::operator=(c);
prevChange = c.prevChange;
}
return (*this);
};

20
backprop/outputCouche.h Normal file
View File

@@ -0,0 +1,20 @@
#include "couche.h"
#include "hiddenCouche.h"
#include <vector>
using namespace std;
class OutputCouche : public Couche{
private:
SynapseMatrix prevChange;
const HiddenCouche *prevCouche;
public:
OutputCouche();
OutputCouche(const unsigned int nbr,const HiddenCouche *prev=NULL);
void activate();
void backPropagate(const std::vector<double> deltaOutput);
double getChange(const unsigned int neurPrevCouche,const unsigned int neurThisCouche) const throw(const unsigned int, std::string);
/* void OutputCouche::setPrevCouche(const HiddenCouche *prev); */
void setPrevCouche(const HiddenCouche *prev);
void printMatrix();
OutputCouche& operator=(const OutputCouche& c);
};

BIN
backprop/outputCouche.o Normal file

Binary file not shown.

184
backprop/reseau.cpp Normal file
View File

@@ -0,0 +1,184 @@
#include "reseau.h"
Reseau::Reseau()
{
}
Reseau::Reseau(int In, int Hid, int Out)
{
In++; // pour le noeud de bias
Ocouche=OutputCouche(Out);
Hcouche=HiddenCouche(Hid,&Ocouche);
Icouche=InputCouche(In,&Hcouche);
Ocouche.setPrevCouche(&Hcouche);
//Icouche.printSynapseMatrix();
//Hcouche.printSynapseMatrix();
}
void Reseau::rebuild(int In, int Hid, int Out) // METHODE BOURRAIN SPOTTED
{
In++;
Ocouche=OutputCouche(Out);
Hcouche=HiddenCouche(Hid,&Ocouche);
Icouche=InputCouche(In,&Hcouche);
Ocouche.setPrevCouche(&Hcouche);
Icouche.printSynapseMatrix();
Hcouche.printSynapseMatrix();
}
Reseau::~Reseau(){
}
void Reseau::saveState(const char* filename) throw(std::string){
unsigned sizeinput = Icouche.getNumber();
unsigned sizehidden = Hcouche.getNumber();
unsigned sizeoutput = Ocouche.getNumber();
std::ofstream outFile(filename);
if(outFile.fail()){
throw(std::string("Couldn't open output file"));
}
outFile << sizeinput << " " << sizehidden << " " << sizeoutput << endl;
for( unsigned int i=0 ; i < sizeinput ; ++i )
for( unsigned int j=0 ; j < sizehidden ; ++j )
outFile << Icouche.getSynapse(i,j).getWeight() << " " << endl;
for( unsigned int i=0 ; i < sizehidden ; ++i )
for( unsigned int j=0 ; j < sizeoutput ; ++j )
outFile << Hcouche.getSynapse(i,j).getWeight() << " " << endl;
outFile.close();
}
void Reseau::loadState(const char* filename) throw(std::string){
std::ifstream inpFile(filename);
unsigned int sizeinput,sizehidden,sizeoutput;
if(inpFile.fail()){
cout << "Can't open input file" << endl;
throw(std::string("Couldn't open output file"));
}
if(!(inpFile >> sizeinput >> sizehidden >> sizeoutput))
cout<<"Super error!!!" << endl;
if(sizeinput!=Icouche.getNumber() || sizehidden!=Hcouche.getNumber() || sizeoutput != Ocouche.getNumber()){
throw(std::string("Wrong size of Couche in file"));
}
double tmpDouble;
//for( unsigned int i=0 ; i < sizeinput ; ++i )
for( unsigned int i=0 ; i < sizeinput ; ++i ){
for( unsigned int j=0 ; j < sizehidden ; ++j ){
if(!(inpFile >> tmpDouble)){
cout << "Input ERROR" << endl;
}
Icouche.getSynapse(i,j).setWeight(tmpDouble);
}
}
for( unsigned int i=0 ; i < sizehidden ; ++i ){
for( unsigned int j=0 ; j < sizeoutput ; ++j ){
if(!(inpFile >> tmpDouble)){
cout << "Input ERROR" << endl;
}
Hcouche.getSynapse(i,j).setWeight(tmpDouble);
}
}
//cout << "==========After=========" << endl;
Icouche.printSynapseMatrix();
inpFile.close();
//cout << sizeinput << " " << sizehidden << " " << sizeoutput << endl;
}
std::vector<double> Reseau::forward(bool input[])
{
std::vector<bool> tmp;
/* on active les couches */
for(unsigned i=0;i<(Icouche.getNumber()-1);i++) /* faut passer n-1 brol dans le vecteur d'activation car il y a le neurone de bias ... */
{
tmp.push_back(input[i]);
}
Icouche.activate(tmp);
Hcouche.activate(Icouche);
Ocouche.activate();
/* on place le resultat dans la shm */
//for(unsigned i=0;i<Ocouche.getNumber();i++) target[i]=util.accept(Ocouche[i].getWeight());
std::vector<double> resultat;
for(unsigned i=0;i<Ocouche.getNumber();i++) resultat.push_back(Ocouche[i].getWeight());
return resultat;
}
void Reseau::backward(bool input[], bool target[])
{
std::vector<double> hidDelta;
std::vector<double> outDelta;
double error;
hidDelta.clear();
outDelta.clear();
/* Calcul des delta pour la couche OUPUT */
for(unsigned i=0;i<Ocouche.getNumber();i++)
{
error = (double)target[i] - Ocouche[i].getWeight();
outDelta.push_back(util.dsigmoid(Ocouche[i].getWeight()) * error);
}
/* Calcul des delta pour la couche HIDDEN */
for(unsigned i=0;i<Hcouche.getNumber();i++){
error = 0.0;
for(unsigned j=0;j<Ocouche.getNumber();++j){
error += outDelta[j] * Hcouche.getSynapse(i,j).getWeight();
}
hidDelta.push_back(util.dsigmoid(Hcouche[i].getWeight()) * error);
}
Ocouche.backPropagate(outDelta);
Icouche.backPropagate(hidDelta);
}
/*
void Reseau::initshm()
{
if ((shmid = shmget(SHMKEY, sizeof(struct shmdata), 0666)) < 0)
{
perror("Unable to get shm id \n");
exit(1);
}
if ((SData = (struct shmdata *)shmat(shmid, NULL, 0)) == (struct shmdata *) -1)
{
perror("Unable to attach shm segment\n");
exit(1);
}
}
*/
double Reseau::getError(bool target[])
{
double error=0.0;
for(unsigned i=0;i<Ocouche.getNumber();i++)
{
error += pow(((double)target[i] - Ocouche[i].getWeight()),2);
}
return sqrt(error);
}
double Reseau::learnOne(bool input[], bool target[])
{
double error=0;
forward(input);
backward(input,target);
error=getError(target);
return error;
}
double Reseau::learnAll(std::vector<bool *> inputs, std::vector<bool *> targets)
{
double error=0.0;
unsigned i;
for(i=0;i<inputs.size();i++)
{
error+=learnOne(inputs[i],targets[i]);
}
return (double)(error/i);
}
Reseau& Reseau::operator=(const Reseau& c){
if(this!=&c){
Icouche=c.Icouche;
Hcouche=c.Hcouche;
Ocouche=c.Ocouche;
SData=c.SData;
shmid=c.shmid;
}
return *this;
}
/* END */

184
backprop/reseau.cpp~ Normal file
View File

@@ -0,0 +1,184 @@
#include "reseau.h"
Reseau::Reseau()
{
}
Reseau::Reseau(int In, int Hid, int Out)
{
In++; // pour le noeud de bias
Ocouche=OutputCouche(Out);
Hcouche=HiddenCouche(Hid,&Ocouche);
Icouche=InputCouche(In,&Hcouche);
Ocouche.setPrevCouche(&Hcouche);
//Icouche.printSynapseMatrix();
//Hcouche.printSynapseMatrix();
}
void Reseau::rebuild(int In, int Hid, int Out) // METHODE BOURRAIN SPOTTED
{
In++;
Ocouche=OutputCouche(Out);
Hcouche=HiddenCouche(Hid,&Ocouche);
Icouche=InputCouche(In,&Hcouche);
Ocouche.setPrevCouche(&Hcouche);
Icouche.printSynapseMatrix();
Hcouche.printSynapseMatrix();
}
Reseau::~Reseau(){
}
void Reseau::saveState(const char* filename) throw(std::string){
unsigned sizeinput = Icouche.getNumber();
unsigned sizehidden = Hcouche.getNumber();
unsigned sizeoutput = Ocouche.getNumber();
std::ofstream outFile(filename);
if(outFile.fail()){
throw(std::string("Couldn't open output file"));
}
outFile << sizeinput << " " << sizehidden << " " << sizeoutput << endl;
for( unsigned int i=0 ; i < sizeinput ; ++i )
for( unsigned int j=0 ; j < sizehidden ; ++j )
outFile << Icouche.getSynapse(i,j).getWeight() << " " << endl;
for( unsigned int i=0 ; i < sizehidden ; ++i )
for( unsigned int j=0 ; j < sizeoutput ; ++j )
outFile << Hcouche.getSynapse(i,j).getWeight() << " " << endl;
outFile.close();
}
void Reseau::loadState(const char* filename) throw(std::string){
std::ifstream inpFile(filename);
unsigned int sizeinput,sizehidden,sizeoutput;
if(inpFile.fail()){
cout << "Can't open input file" << endl;
throw(std::string("Couldn't open output file"));
}
if(!(inpFile >> sizeinput >> sizehidden >> sizeoutput))
cout<<"Super error!!!" << endl;
if(sizeinput!=Icouche.getNumber() || sizehidden!=Hcouche.getNumber() || sizeoutput != Ocouche.getNumber()){
throw(std::string("Wrong size of Couche in file"));
}
double tmpDouble;
//for( unsigned int i=0 ; i < sizeinput ; ++i )
for( unsigned int i=0 ; i < sizeinput ; ++i ){
for( unsigned int j=0 ; j < sizehidden ; ++j ){
if(!(inpFile >> tmpDouble)){
cout << "Input ERROR" << endl;
}
Icouche.getSynapse(i,j).setWeight(tmpDouble);
}
}
for( unsigned int i=0 ; i < sizehidden ; ++i ){
for( unsigned int j=0 ; j < sizeoutput ; ++j ){
if(!(inpFile >> tmpDouble)){
cout << "Input ERROR" << endl;
}
Hcouche.getSynapse(i,j).setWeight(tmpDouble);
}
}
//cout << "==========After=========" << endl;
Icouche.printSynapseMatrix();
inpFile.close();
//cout << sizeinput << " " << sizehidden << " " << sizeoutput << endl;
}
std::vector<double> Reseau::forward(bool input[])
{
std::vector<bool> tmp;
/* on active les couches */
for(unsigned i=0;i<(Icouche.getNumber()-1);i++) /* faut passer n-1 brol dans le vecteur d'activation car il y a le neurone de bias ... */
{
tmp.push_back(input[i]);
}
Icouche.activate(tmp);
Hcouche.activate(Icouche);
Ocouche.activate();
/* on place le resultat dans la shm */
//for(unsigned i=0;i<Ocouche.getNumber();i++) target[i]=util.accept(Ocouche[i].getWeight());
std::vector<double> resultat;
for(unsigned i=0;i<Ocouche.getNumber();i++) resultat.push_back(Ocouche[i].getWeight());
return resultat;
}
void Reseau::backward(bool input[], bool target[])
{
std::vector<double> hidDelta;
std::vector<double> outDelta;
double error;
hidDelta.clear();
outDelta.clear();
/* Calcul des delta pour la couche OUPUT */
for(unsigned i=0;i<Ocouche.getNumber();i++)
{
error = (double)target[i] - Ocouche[i].getWeight();
outDelta.push_back(util.dsigmoid(Ocouche[i].getWeight()) * error);
}
/* Calcul des delta pour la couche HIDDEN */
for(unsigned i=0;i<Hcouche.getNumber();i++){
error = 0.0;
for(unsigned j=0;j<Ocouche.getNumber();++j){
error += outDelta[j] * Hcouche.getSynapse(i,j).getWeight();
}
hidDelta.push_back(util.dsigmoid(Hcouche[i].getWeight()) * error);
}
Ocouche.backPropagate(outDelta);
Icouche.backPropagate(hidDelta);
}
void Reseau::initshm()
{
if ((shmid = shmget(SHMKEY, sizeof(struct shmdata), 0666)) < 0)
{
perror("Unable to get shm id \n");
exit(1);
}
if ((SData = (struct shmdata *)shmat(shmid, NULL, 0)) == (struct shmdata *) -1)
{
perror("Unable to attach shm segment\n");
exit(1);
}
}
double Reseau::getError(bool target[])
{
double error=0.0;
for(unsigned i=0;i<Ocouche.getNumber();i++)
{
error += pow(((double)target[i] - Ocouche[i].getWeight()),2);
}
return sqrt(error);
}
double Reseau::learnOne(bool input[], bool target[])
{
double error=0;
forward(input);
backward(input,target);
error=getError(target);
return error;
}
double Reseau::learnAll(std::vector<bool *> inputs, std::vector<bool *> targets)
{
double error=0.0;
unsigned i;
for(i=0;i<inputs.size();i++)
{
error+=learnOne(inputs[i],targets[i]);
}
return (double)(error/i);
}
Reseau& Reseau::operator=(const Reseau& c){
if(this!=&c){
Icouche=c.Icouche;
Hcouche=c.Hcouche;
Ocouche=c.Ocouche;
SData=c.SData;
shmid=c.shmid;
}
return *this;
}
/* END */

45
backprop/reseau.h Normal file
View File

@@ -0,0 +1,45 @@
#ifndef __RESEAU_H
#define __RESEAU_H
#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <vector>
#include <string>
#include <fstream>
#include "couche.h"
#include "inputCouche.h"
#include "hiddenCouche.h"
#include "outputCouche.h"
#include "../InterfaceCpp/shmdata.h"
#include "Utils.h"
#include "global.h"
class Reseau
{
private:
struct shmdata *SData;
int shmid;
Utils util;
public:
InputCouche Icouche;
HiddenCouche Hcouche;
OutputCouche Ocouche;
Reseau();
Reseau(int In, int Hid, int Out);
void rebuild(int In, int Hid, int Out);
~Reseau();
// void initshm();
std::vector<double> forward(bool input[]);
void backward(bool input[], bool target[]); /* */
double getError(bool target[]);
double learnOne(bool input[], bool target[]); /* */
double learnAll(std::vector<bool *> inputs, std::vector<bool *> targets);
void saveState(const char* filename) throw(std::string);
void loadState(const char* filename) throw(std::string);
Reseau& operator=(const Reseau& c);
};
#endif

46
backprop/reseau.h~ Normal file
View File

@@ -0,0 +1,46 @@
#ifndef __RESEAU_H
#define __RESEAU_H
#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <vector>
#include <string>
#include <fstream>
#include "couche.h"
#include "inputCouche.h"
#include "hiddenCouche.h"
#include "outputCouche.h"
#include "../InterfaceCpp/shmdata.h"
#include "Utils.h"
#include "global.h"
class Reseau {
private:
struct shmdata *SData;
int shmid;
Utils util;
public:
InputCouche Icouche;
HiddenCouche Hcouche;
OutputCouche Ocouche;
Reseau();
Reseau(int In, int Hid, int Out);
void rebuild(int In, int Hid, int Out);
~Reseau();
// void initshm();
std::vector<double> forward(bool input[]);
void backward(bool input[], bool target[]); /* */
double getError(bool target[]);
double learnOne(bool input[], bool target[]); /* */
double learnAll(std::vector<bool *> inputs, std::vector<bool *> targets);
void saveState(const char* filename) throw(std::string);
void loadState(const char* filename) throw(std::string);
Reseau& operator=(const Reseau& c);
};
#endif

BIN
backprop/reseau.o Normal file

Binary file not shown.

51
backprop/synapse.cpp Normal file
View File

@@ -0,0 +1,51 @@
#include "synapse.h"
Synapse::Synapse(const double w){
setWeight(w);
}
double Synapse::getWeight() const{
return weight;
}
void Synapse::setWeight(const double w){
weight =(double) w;
}
void Synapse::operator=(const double w){
setWeight(w);
}
Synapse& Synapse::operator =(const Synapse& s){
if(this!=&s){
setWeight(s.getWeight());
}
return *this;
}
void Synapse::setRandomWeight(){
//double range = SYNAPSE_WEIGHT_MAX - SYNAPSE_WEIGHT_MIN + 1;
double range = SYNAPSE_WEIGHT_MAX - SYNAPSE_WEIGHT_MIN; //lau
double res;
do{
//res = (double)(range * rand()/(RAND_MAX + 1.0)) + SYNAPSE_WEIGHT_MIN;
res=(double)(((rand()/2.0)/(RAND_MAX/2.0))*range)+SYNAPSE_WEIGHT_MIN;
}while(res > SYNAPSE_WEIGHT_MAX && res < SYNAPSE_WEIGHT_MIN);
setWeight(res);
}
ostream & operator<<(ostream & os,const Synapse & syn){
os << syn.getWeight();
return os;
}
istream& operator>>(istream & inp,Synapse & syn){
double tmp;
inp >> tmp;
syn.setWeight(tmp);
return inp;
}

23
backprop/synapse.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef _SYNAPSE_H
#define _SYNAPSE_H
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include "global.h"
using namespace std;
class Synapse{
private:
double weight;
public:
Synapse(const double w=0.0);
double getWeight() const;
void setWeight(const double w);
void operator =(const double w);
Synapse& operator =(const Synapse& s);
void setRandomWeight();
friend ostream& operator<<(ostream& os,const Synapse &s);
istream& operator >>(double d);
};
#endif

BIN
backprop/synapse.o Normal file

Binary file not shown.

View File

@@ -0,0 +1,88 @@
#include "synapseMatrix.h"
SynapseMatrix::SynapseMatrix(){
nbrLine = 0;
nbrCol = 0;
}
SynapseMatrix::SynapseMatrix(unsigned int nbrL, unsigned int nbrC){
nbrLine = nbrL;
nbrCol = nbrC;
createMatrix(nbrLine,nbrCol);
}
void SynapseMatrix::createMatrix(unsigned int line,unsigned int col){
matrix = new Synapse*[line];
for(unsigned int i=0 ; i<line; ++i){
matrix[i] = new Synapse[col];
}
}
unsigned int SynapseMatrix::getLineCount() const{
return nbrLine;
}
unsigned int SynapseMatrix::getColumnCount() const{
return nbrCol;
}
Synapse& SynapseMatrix::operator()(const unsigned int i,const unsigned int j) const throw(const unsigned int,std::string){
if(nbrLine==0 || nbrCol==0)
throw(std::string("Access on non initialized matrix"));
if(i>(getLineCount()-1))
throw(i);
if(j>(getColumnCount()-1))
throw(j);
return matrix[i][j];
}
void SynapseMatrix::randomize(){
// cout << " -> randomize " << this << endl;
unsigned int nbrL = getLineCount();
unsigned int nbrC = getColumnCount();
srand(static_cast<unsigned>(time(0)));
for(unsigned int i=0 ; i < nbrL ; ++i)
for(unsigned int j=0 ; j < nbrC ; ++j){
(*this)(i,j).setRandomWeight();
}
}
void SynapseMatrix::deleteMatrix(){
unsigned int nbrL = getLineCount();
unsigned int nbrC = getColumnCount();
if(nbrL&&nbrC){
for(unsigned int i=0 ; i < nbrL ; ++i)
delete[] matrix[i];
delete[] matrix;
}
}
SynapseMatrix::~SynapseMatrix(){
deleteMatrix();
}
SynapseMatrix& SynapseMatrix::operator =(const SynapseMatrix& s){
if(this!=&s){
deleteMatrix();
nbrLine = s.getLineCount();
nbrCol = s.getColumnCount();
createMatrix(nbrLine,nbrCol);
for(unsigned int i=0 ; i < nbrLine ; ++i)
for(unsigned int j=0 ; j < nbrCol ; ++j){
(*this)(i,j) = s(i,j);
}
}
return *this;
}
ostream & operator<<(ostream & os,const SynapseMatrix & mat){
unsigned int nbrC = mat.getColumnCount();
unsigned int nbrL = mat.getLineCount();
for(unsigned int i=0 ; i < nbrL ; ++i){
for(unsigned int j=0 ; j < nbrC ; ++j){
os << setw(6) << mat(i,j) << " ";
}
os << endl;
}
return os;
}

29
backprop/synapseMatrix.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef _SYNAPSEMATRIX_H
#define _SYNAPSEMATRIX_H
#include "synapse.h"
#include <iostream>
#include <iomanip>
using namespace std;
class SynapseMatrix{
private:
Synapse** matrix;
unsigned int nbrLine;
unsigned int nbrCol;
void createMatrix(unsigned int line,unsigned int col);
void deleteMatrix();
public:
SynapseMatrix();
SynapseMatrix(unsigned int nbrL,unsigned int nbrC);
unsigned int getLineCount() const;
unsigned int getColumnCount() const;
void randomize();
friend ostream & operator<<(ostream & os,const SynapseMatrix & mat);
Synapse& operator()(const unsigned int i,const unsigned int j) const throw(const unsigned int,std::string);
SynapseMatrix& operator =(const SynapseMatrix& s);
~SynapseMatrix();
};
#endif

BIN
backprop/synapseMatrix.o Normal file

Binary file not shown.

13
backprop/test/CVS/Entries Normal file
View File

@@ -0,0 +1,13 @@
/Makefile/1.7/Wed Apr 13 15:04:03 2005//
/coucheTest.cpp/1.2/Wed Apr 13 15:04:03 2005//
/coucheTest.h/1.2/Wed Apr 13 15:04:03 2005//
/inputCoucheTest.cpp/1.3/Wed Apr 13 15:04:03 2005//
/inputCoucheTest.h/1.2/Wed Apr 13 15:04:03 2005//
/neuroneTest.cpp/1.1.1.1/Wed Apr 13 15:04:03 2005//
/neuroneTest.h/1.1.1.1/Wed Apr 13 15:04:03 2005//
/synapseMatrixTest.cpp/1.2/Wed Apr 13 15:04:03 2005//
/synapseMatrixTest.h/1.2/Wed Apr 13 15:04:03 2005//
/synapseTest.cpp/1.1/Wed Apr 13 15:04:03 2005//
/synapseTest.h/1.1/Wed Apr 13 15:04:03 2005//
/test.cpp/1.5/Wed Apr 13 15:04:03 2005//
D

View File

@@ -0,0 +1 @@
ia2005/backprop/test

1
backprop/test/CVS/Root Normal file
View File

@@ -0,0 +1 @@
:pserver:feeling@jfroche.be:/opt/cvsroot

26
backprop/test/Makefile Normal file
View File

@@ -0,0 +1,26 @@
#Id: Makefile,v 1.4 2005/04/04 22:58:08 jfroche Exp $'
CFLAGS= -Wall -g -ansi
CPP= g++
GCC= gcc
TESTINCLUDES=-lcppunit -ldl
all : test
test : test.o neuroneTest.o coucheTest.o synapseTest.o synapseMatrixTest.o inputCoucheTest.o reseauTest.o ../couche.o ../neurone.o ../synapse.o ../synapseMatrix.o ../inputCouche.o ../Utils.o ../reseau.o ../outputCouche.o ../hiddenCouche.o
$(CPP) $(CFLAGS) $(TESTINCLUDES) -o test test.o neuroneTest.o coucheTest.o synapseTest.o inputCoucheTest.o synapseMatrixTest.o reseauTest.o ../synapse.o ../neurone.o ../couche.o ../synapseMatrix.o ../inputCouche.o ../Utils.o ../reseau.o ../outputCouche.o ../hiddenCouche.o
neuroneTest.o : neuroneTest.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c neuroneTest.cpp
coucheTest.o : coucheTest.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c coucheTest.cpp
inputCoucheTest.o : inputCoucheTest.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c inputCoucheTest.cpp
synapseTest.o : synapseTest.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c synapseTest.cpp
synapseMatrixTest.o : synapseMatrixTest.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c synapseMatrixTest.cpp
reseauTest.o : reseauTest.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c reseauTest.cpp
test.o : test.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c test.cpp
clean :
rm -f test test.o neuroneTest.o coucheTest.o synapseTest.o synapseMatrixTest.o
cleano :
rm -f *.o

View File

@@ -0,0 +1,52 @@
#include "coucheTest.h"
void CoucheTest::setUp(){
// construction du test
Output = Couche(10);
Input = Couche(5,&Output);
}
void CoucheTest::tearDown(){
// destruction du test
// delete Output;
// delete Input;
}
void CoucheTest::testInit(){
// verifie les assignations du constructeur de la classe
CPPUNIT_ASSERT( Input.getNumber()==5 );
CPPUNIT_ASSERT( Output.getNumber()==10 );
CPPUNIT_ASSERT( Input.getNextCouche() == &Output );
CPPUNIT_ASSERT( Output.getNextCouche() == NULL );
}
void CoucheTest::testGetSynapse(){
Input.getSynapse(1,1);
}
void CoucheTest::testGetSynapseOverflow(){
Input.getSynapse(5,10);
}
void CoucheTest::testGetSynapseUninitialized(){
Output.getSynapse(1,1);
}
void CoucheTest::testNextCouche(){
// verifie l assignation du pointeur vers le suivant
CPPUNIT_ASSERT( Output.getNextCouche()==NULL );
Output.setNextCouche(&Input);
CPPUNIT_ASSERT( Output.getNextCouche() == &Input );
}
void CoucheTest::testOperator(){
// verifie l'assignation d'un poid à un neurone
CPPUNIT_ASSERT( Input[0].getWeight()==0.0 );
Input[0] = 3.0;
CPPUNIT_ASSERT( Input[0].getWeight()==3.0 );
Input[0].setWeight(4.0);
CPPUNIT_ASSERT( Input[0].getWeight()==4.0 );
}
void CoucheTest::testOperatorThrow(){
Input[5];
}

View File

@@ -0,0 +1,32 @@
#include <cppunit/TestFixture.h>
#include <cppunit/TestCaller.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>
#include "../couche.h"
#include "../neurone.h"
class CoucheTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE( CoucheTest );
CPPUNIT_TEST( testInit ); // on enregistre les tests à faire dans l ordre
CPPUNIT_TEST( testNextCouche );
CPPUNIT_TEST( testOperator );
CPPUNIT_TEST( testGetSynapse );
CPPUNIT_TEST_EXCEPTION( testOperatorThrow, const unsigned int );
CPPUNIT_TEST_EXCEPTION( testGetSynapseOverflow, const unsigned int);
CPPUNIT_TEST_EXCEPTION( testGetSynapseUninitialized, std::string);
CPPUNIT_TEST_SUITE_END();
private:
Couche Input;// un evenement a tester
Couche Output;// un deuxieme evenement a tester
public:
void setUp();
void tearDown();
void testInit();
void testNextCouche();
void testOperator();
void testOperatorThrow();
void testGetSynapse();
void testActive();
void testGetSynapseOverflow();
void testGetSynapseUninitialized();
};

View File

@@ -0,0 +1,38 @@
#include "inputCoucheTest.h"
void InputCoucheTest::setUp(){
// construction du test
Input2 = InputCouche(10);
Input1 = InputCouche(5,&Input2);
}
void InputCoucheTest::tearDown(){
// destruction du test
// delete Output;
// delete Input;
}
void InputCoucheTest::testActivate(){
vector<bool> v;
v.push_back(1);
v.push_back(0);
v.push_back(1);
v.push_back(1);
Input1.activate(v);
CPPUNIT_ASSERT( Input1[0].getWeight() == 1.0 );
CPPUNIT_ASSERT( Input1[1].getWeight() == 0.0 );
}
void InputCoucheTest::testGetChange(){
CPPUNIT_ASSERT( Input1.getChange(2,2) == 0.0 );
}
void InputCoucheTest::testActivateWrongSize(){
vector<bool> v(6);
Input1.activate(v);
}
void InputCoucheTest::testCopy(){
Input2[2] = 3.2;
Input1 = Input2;
CPPUNIT_ASSERT( Input1[2].getWeight() == 3.2 );
}

View File

@@ -0,0 +1,25 @@
#include <cppunit/TestFixture.h>
#include <cppunit/TestCaller.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>
#include "../inputCouche.h"
#include <vector>
class InputCoucheTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE( InputCoucheTest );
CPPUNIT_TEST( testActivate );
CPPUNIT_TEST( testGetChange );
CPPUNIT_TEST( testCopy );
CPPUNIT_TEST_EXCEPTION( testActivateWrongSize, std::string);
CPPUNIT_TEST_SUITE_END();
private:
InputCouche Input1;// un evenement a tester
InputCouche Input2;// un deuxieme evenement a tester
public:
void setUp();
void tearDown();
void testActivate();
void testActivateWrongSize();
void testGetChange();
void testCopy();
};

Binary file not shown.

View File

@@ -0,0 +1,46 @@
#include "neuroneTest.h"
void NeuroneTest::setUp(){
// construction du test
Neur1 = new Neurone();
Neur2 = new Neurone(100.0,Neur1);
}
void NeuroneTest::tearDown(){
// destruction du test
delete Neur1;
delete Neur2;
}
void NeuroneTest::testInit(){
// verifie les assignations du constructeur de la classe
CPPUNIT_ASSERT( Neur1->getWeight()==0.0 );
CPPUNIT_ASSERT( Neur2->getWeight()==100.0 );
CPPUNIT_ASSERT( Neur1->getNextNeurone()==NULL );
CPPUNIT_ASSERT( Neur2->getNextNeurone()==Neur1 );
}
void NeuroneTest::testNextNeurone(){
// verifie l assignation du pointeur vers le suivant
CPPUNIT_ASSERT( Neur1->getNextNeurone()==NULL );
Neur1->setNextNeurone(Neur2);
CPPUNIT_ASSERT( Neur1->getNextNeurone()==Neur2 );
}
void NeuroneTest::testSetWeight(){
// verifie l'assignation d'un poid à un neurone
CPPUNIT_ASSERT( Neur1->getWeight()==0.0 );
Neur1->setWeight(1.0);
CPPUNIT_ASSERT( Neur1->getWeight()==1.0 );
}
void NeuroneTest::testActive(){
// verifie l'activation d'un neurone
CPPUNIT_ASSERT( Neur2->getWeight()==100.0 );
CPPUNIT_ASSERT( Neur2->getState()==true );
}
void NeuroneTest::testOperotorAssign(){
Neurone bla(3.0);
bla = 3.0;
CPPUNIT_ASSERT(bla.getWeight()==3);
}

View File

@@ -0,0 +1,27 @@
#include <cppunit/TestFixture.h>
#include <cppunit/TestCaller.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>
#include "../couche.h"
class NeuroneTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE( NeuroneTest );
CPPUNIT_TEST( testInit ); // on enregistre les tests à faire dans l ordre
CPPUNIT_TEST( testNextNeurone );
CPPUNIT_TEST( testSetWeight );
CPPUNIT_TEST( testActive );
CPPUNIT_TEST( testOperotorAssign );
CPPUNIT_TEST_SUITE_END();
private:
Neurone* Neur1;// un evenement a tester
Neurone* Neur2;// un deuxieme evenement a tester
public:
void setUp();
void tearDown();
void testInit();
void testNextNeurone();
void testSetWeight();
void testOperotorAssign();
void testActive();
};

View File

@@ -0,0 +1,36 @@
#include "synapseMatrixTest.h"
void SynapseMatrixTest::setUp(){
// construction du test
SynM1 = new SynapseMatrix(3,2);
SynM2 = new SynapseMatrix(4,4);
}
void SynapseMatrixTest::tearDown(){
// destruction du test
delete SynM1;
delete SynM2;
}
void SynapseMatrixTest::testOperatorIndex(){
SynapseMatrix SynM3(3,5);
SynM3(0,4) = 2.1;
CPPUNIT_ASSERT( SynM3(0,4).getWeight()==2.1 );
}
void SynapseMatrixTest::testOperatorAssign(){
SynapseMatrix SynM4(5,4);
SynapseMatrix SynM5(2,2);
SynM4(3,2) = 3.2;
SynM5 = SynM4;
CPPUNIT_ASSERT( SynM5(3,2).getWeight()==3.2 );
}
void SynapseMatrixTest::testInit(){
// verifie les assignations du constructeur de la classe
CPPUNIT_ASSERT( SynM1->getLineCount()==3 );
CPPUNIT_ASSERT( SynM2->getLineCount()==4 );
CPPUNIT_ASSERT( SynM1->getColumnCount()==2 );
CPPUNIT_ASSERT( SynM2->getColumnCount()==4 );
}

View File

@@ -0,0 +1,23 @@
#include <cppunit/TestFixture.h>
#include <cppunit/TestCaller.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>
#include "../synapseMatrix.h"
class SynapseMatrixTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE( SynapseMatrixTest );
CPPUNIT_TEST( testInit ); // on enregistre les tests à faire dans l ordre
CPPUNIT_TEST( testOperatorIndex );
CPPUNIT_TEST( testOperatorAssign );
CPPUNIT_TEST_SUITE_END();
private:
SynapseMatrix* SynM1;// une matrice de synapse
SynapseMatrix* SynM2;
public:
void setUp();
void tearDown();
void testInit();
void testOperatorIndex();
void testOperatorAssign();
};

View File

@@ -0,0 +1,25 @@
#include "synapseTest.h"
void SynapseTest::setUp(){
// construction du test
Syn1 = new Synapse();
Syn2 = new Synapse(100.0);
}
void SynapseTest::tearDown(){
// destruction du test
delete Syn1;
delete Syn2;
}
void SynapseTest::testInit(){
// verifie les assignations du constructeur de la classe
CPPUNIT_ASSERT( Syn1->getWeight()==0.0 );
CPPUNIT_ASSERT( Syn2->getWeight()==100.0 );
}
void SynapseTest::testOperotorAssign(){
Synapse bla(3.0);
bla = 5.0;
CPPUNIT_ASSERT(bla.getWeight()==5.0);
}

View File

@@ -0,0 +1,21 @@
#include <cppunit/TestFixture.h>
#include <cppunit/TestCaller.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>
#include "../synapse.h"
class SynapseTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE( SynapseTest );
CPPUNIT_TEST( testInit ); // on enregistre les tests à faire dans l ordre
CPPUNIT_TEST( testOperotorAssign );
CPPUNIT_TEST_SUITE_END();
private:
Synapse* Syn1;// une synapse
Synapse* Syn2;
public:
void setUp();
void tearDown();
void testInit();
void testOperotorAssign();
};

35
backprop/test/test.cpp Normal file
View File

@@ -0,0 +1,35 @@
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/TestFixture.h>
#include <cppunit/TestSuite.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestResult.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>
#include <iostream>
#include <stdexcept>
#include "neuroneTest.h"
#include "coucheTest.h"
#include "synapseTest.h"
#include "synapseMatrixTest.h"
#include "inputCoucheTest.h"
#include "reseauTest.h"
using namespace std;
CPPUNIT_TEST_SUITE_REGISTRATION( NeuroneTest ); // teste les neurones
CPPUNIT_TEST_SUITE_REGISTRATION( CoucheTest ); // teste les couches
CPPUNIT_TEST_SUITE_REGISTRATION( SynapseTest );
CPPUNIT_TEST_SUITE_REGISTRATION( SynapseMatrixTest );
CPPUNIT_TEST_SUITE_REGISTRATION( InputCoucheTest );
CPPUNIT_TEST_SUITE_REGISTRATION( ReseauTest );
int main(void){
CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
CppUnit::TextUi::TestRunner runner;
runner.addTest( suite );
runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
std::cout ) );
bool wasSucessful = runner.run(); // lance les testes
return wasSucessful;
}