remove temp file
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
#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
|
||||
@@ -1,184 +0,0 @@
|
||||
#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 */
|
||||
@@ -1,46 +0,0 @@
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user