View previous topic :: View next topic |
Author |
Message |
neoairus n00b

Joined: 09 Feb 2006 Posts: 12
|
Posted: Fri Nov 16, 2007 4:08 pm Post subject: C++, Class Object Factory, Template e .... mal di testa :) |
|
|
Salve a tutti
Sto portando avanti un pseudo-libreria per reti neurali. Per renderne
più semplice l'utilizzo volevo fare in modo di instanziare la classe
che utilizza lo specifico algoritmo specificandolo attraverso una
stringa...
Code: |
/* ------Neuron.h--------*/
#include <math.h>
namespace Mnetlib
{
class Neuron
{
public:
virtual ~Neuron(){};
virtual double Run(double arg)=0;
virtual double RunBack(double o,double e)=0;
protected:
double outvalue;
int mode;
double input;
};
/* ------Layer.h--------*/
#include <vector>
#include "Neuron.h"
#include "Exception.h"
namespace Mnetlib
{
class Layer
{
public:
/**
* Costruttore di base.
* @param n Numero di neuroni presenti nel layer.
*/
Layer(int n);
virtual ~Layer()=0;
virtual void backprop(){throw new
InstantiationException();};
protected:
std::vector<Neuron*> vect;
int num;
};
}
/* ------Registry.h--------*/
#include "NeuronLib.h"
#include "LayerLib.h"
#include "Factory.h"
#include <map>
namespace Mnetlib
{
class Registry
{
public:
Registry();
~Registry();
Neuron * getNewNeuron(const std::string& name);
void registerNeuronFactory( NeuronClassFactory* factory)
{
_neuronRegistry[factory->name()]=factory;
}
void registerLayerFactory( LayerClassFactory* factory)
{
_layerRegistry[factory->name()]=factory;
}
Layer* getLayer (const std::string& lName, const std::string& nName,
int n);
protected:
std::map<std::string,NeuronClassFactory*> _neuronRegistry;
std::map<std::string, LayerClassFactory*> _layerRegistry;
NeuronFactoryWrapper<SigmoidNeuron> sigmoidFactory;
NeuronFactoryWrapper<LinearNeuron> linearFactory;
LayerFactoryWrapper<OffLineLayer> offFactory;
LayerFactoryWrapper<OnLineLayer> onFactory;
};
}
/* ------Factory.h--------*/
#include "Neuron.h"
#include "Layer.h"
namespace Mnetlib
{
class NeuronClassFactory
{
public:
virtual ~NeuronClassFactory(){};
virtual Neuron* create()=0;
virtual std::string name()=0;
};
template < class T >
class NeuronFactoryWrapper : public NeuronClassFactory
{
public:
virtual ~NeuronFactoryWrapper(){};
virtual Neuron* create(){ return T::create();}
virtual std::string name(){ return T::name();}
};
class LayerClassFactory
{
public:
virtual ~LayerClassFactory(){};
virtual Layer* create()=0;
virtual std::string name()=0;
};
template < class T >
class LayerFactoryWrapper : public LayerClassFactory
{
public:
virtual ~LayerFactoryWrapper(){};
virtual Layer* create(){ return T::create();}
virtual std::string name(){ return T::name();}
void build();
};
}
/* ------NeuronLib.h--------*/
#include "Neuron.h"
#include <string>
namespace Mnetlib
{
class LinearNeuron: public Neuron
{
public:
~LinearNeuron(){};
double Run(double arg);
double RunBack(double o,double e);
static LinearNeuron* create(){ return new LinearNeuron();};
static std::string name(){ return "linear";} ;
};
class SigmoidNeuron: public Neuron
{
public:
~SigmoidNeuron(){};
double Run(double arg);
double RunBack(double o,double e);
static SigmoidNeuron* create(){ return new SigmoidNeuron();};
static std::string name(){ return "sigmoid";} ;
};
}
/* ------LayerLib.h--------*/
#include <string>
#include "Layer.h"
namespace Mnetlib
{
class OnLineLayer : public Layer
{
public:
OnLineLayer(int n);
~OnLineLayer(){};
static OnLineLayer* create(int n){ return new OnLineLayer(n);};
static std::string name(){ return "online";} ;
void backprop();
};
class OffLineLayer : public Layer
{
public:
OffLineLayer();
~OffLineLayer(){};
static OffLineLayer* create(){ return new OffLineLayer();};
static std::string name(){ return "offline";} ;
void backprop();
};
}
|
Tutto ciò ha il semplice scopo di poter instanziare un layer
semplicemente con:
Code: |
Registry* reg;
Layer* l= reg->getLayer ("offline", "linear", n);
|
Il codice qui sopra ovviamente ha problemi (bhè, voi direte, se non
avevi problemi mica postavi no?? ) durante il linking, non trova
il riferimento ai costruttori dei layer definiti in LayerLib.
Mi chiedevo se qualcuno di voi ha qualche soluzione da proporre,
qualsiasi essa sia, anche un drastico cambiamento del codice è ben
accetta, così magari riesco a salvaguardare quella piccola quota di
capelli rimasti dell'essere strappato
Grazie a chiunche voglia rispondere. |
|
Back to top |
|
 |
skypjack l33t


Joined: 05 Aug 2006 Posts: 884 Location: Italia - Firenze
|
Posted: Fri Nov 16, 2007 5:45 pm Post subject: |
|
|
Dovresti postare almeno l'errore preciso che ricevi oppure, se non è un problema, mi puoi mandare il codice e lo guardo per te (sperando di poterti aiutare, s'intende). Giuro che non lo divulgherò a nome mio!  |
|
Back to top |
|
 |
Apetrini Veteran


Joined: 09 Feb 2005 Posts: 1158
|
Posted: Fri Nov 16, 2007 8:32 pm Post subject: |
|
|
Usi il Makefile per compilare? se si lo dovresti postare.
Se non lo usi, posta il comando che usi. _________________ Linux ape 2.6.31-vanilla. Paludis since 0.28.0. |
|
Back to top |
|
 |
skypjack l33t


Joined: 05 Aug 2006 Posts: 884 Location: Italia - Firenze
|
Posted: Sat Nov 17, 2007 7:35 pm Post subject: |
|
|
Sparito? Risolto?
Chissà ... |
|
Back to top |
|
 |
Apetrini Veteran


Joined: 09 Feb 2005 Posts: 1158
|
Posted: Mon Nov 19, 2007 9:20 pm Post subject: |
|
|
Che palle... speriamo non sia un altro di quei post che hanno un utilià pari a 0. _________________ Linux ape 2.6.31-vanilla. Paludis since 0.28.0. |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|