00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef __NEURALPP
00015 #define __NEURALPP
00016
00017 #include <vector>
00018 #include <string>
00019 #include <cmath>
00020
00021 #include "neural++_exception.hpp"
00022
00023 #define RAND (double) ( (rand() / 10.0) / ((double) RAND_MAX) )
00024 #define BETA0 0.8
00025
00030 namespace neuralpp {
00031 class Synapsis;
00032 class Neuron;
00033 class Layer;
00034 class NeuralNet;
00035
00036 double df (double (*f)(double), double x);
00037 double __actv(double prop);
00038
00067 class NeuralNet {
00068 int epochs;
00069 int ref_epochs;
00070 double l_rate;
00071 double threshold;
00072 std::vector<double> expect;
00073
00078 void updateWeights();
00079
00085 double error (double ex);
00086
00091 double (*actv_f)(double);
00092
00098 double expected() const;
00099
00105 std::vector<double> getExpected() const;
00106
00111 void setExpected(double ex);
00112
00117 void setExpected(std::vector<double> ex);
00118
00124 void update();
00125
00129 void link();
00130
00131 public:
00132 Layer* input;
00133 Layer* hidden;
00134 Layer* output;
00135
00139 typedef enum { file, str } source;
00140
00144 NeuralNet() {}
00145
00159 NeuralNet (size_t in_size, size_t hidden_size, size_t out_size, double l,
00160 int e, double th = 0.0, double (*a)(double) = __actv);
00161
00167 NeuralNet (const std::string file) throw(NetworkFileNotFoundException);
00168
00174 double getOutput() const;
00175
00180 std::vector<double> getOutputs();
00181
00186 double getThreshold() const;
00187
00192 void propagate();
00193
00198 void setInput (std::vector<double> v);
00199
00206 void save (const char* fname) throw(NetworkFileWriteException);
00207
00218 void loadFromBinary (const std::string fname) throw(NetworkFileNotFoundException);
00219
00230 void saveToBinary (const char* fname) throw(NetworkFileWriteException);
00231
00239 void train (std::string xml, source src) throw(InvalidXMLException);
00240
00245 static void initXML (std::string& xml);
00246
00259 static std::string XMLFromSet (int& id, std::string set);
00260
00265 static void closeXML(std::string& xml);
00266 };
00267
00273 class Synapsis {
00274 double delta;
00275 double prev_delta;
00276 double weight;
00277
00278 Neuron *in;
00279 Neuron *out;
00280
00281 double (*actv_f)(double);
00282
00283 public:
00287 Synapsis() {}
00288
00295 Synapsis (Neuron* i, Neuron* o, double(*a)(double));
00296
00304 Synapsis (Neuron* i, Neuron* o,
00305 double w, double(*a)(double));
00306
00310 Neuron* getIn() const;
00311
00315 Neuron* getOut() const;
00316
00321 void setWeight(double w) throw(InvalidSynapticalWeightException);
00322
00328 void setDelta(double d) throw(InvalidSynapticalWeightException);
00329
00334 double getWeight() const;
00335
00340 double getDelta() const;
00341
00346 double getPrevDelta() const;
00347
00358 double momentum (int N, int x) const;
00359 };
00360
00366 class Neuron {
00367 double actv_val;
00368 double prop_val;
00369 double threshold;
00370
00371 std::vector< Synapsis > in;
00372 std::vector< Synapsis > out;
00373
00374 double (*actv_f)(double);
00375
00376 public:
00383 Neuron (double (*a)(double), double th = 0.0);
00384
00393 Neuron (std::vector<Synapsis> in, std::vector<Synapsis> out,
00394 double (*a)(double), double th = 0.0);
00395
00401 Synapsis& synIn (size_t i);
00402
00408 Synapsis& synOut (size_t i);
00409
00414 void push_in (Synapsis s);
00415
00420 void push_out (Synapsis s);
00421
00426 void setActv (double a);
00427
00432 void setProp (double p);
00433
00434 void setSynIn (size_t n);
00435 void setSynOut (size_t n);
00436
00441 double getActv();
00442
00447 double getProp();
00448
00452 void propagate();
00453
00458 size_t nIn();
00459
00464 size_t nOut();
00465
00469 void synClear();
00470 };
00471
00477 class Layer {
00478 std::vector<Neuron> elements;
00479 double threshold;
00480
00481 void (*update_weights)();
00482 double (*actv_f)(double);
00483
00484 public:
00492 Layer (size_t sz, double (*a)(double), double th = 0.0);
00493
00502 Layer (std::vector<Neuron>& neurons, double(*a)(double), double th = 0.0);
00503
00509 Neuron& operator[] (size_t i) throw(NetworkIndexOutOfBoundsException);
00510
00515 void link (Layer& l);
00516
00521 void setInput (std::vector<double> v);
00522
00526 void propagate();
00527
00531 size_t size() const;
00532 };
00533
00534 struct netrecord {
00535 int input_size;
00536 int hidden_size;
00537 int output_size;
00538
00539 int epochs;
00540 double l_rate;
00541 double ex;
00542 };
00543
00544 struct neuronrecord {
00545 double prop;
00546 double actv;
00547 };
00548
00549 struct synrecord {
00550 double w;
00551 double d;
00552 };
00553
00554 namespace neuralutils {
00561 std::vector<double> split (char delim, std::string str);
00562
00568 std::vector<std::string> splitLines (std::string str);
00569
00574 void toLower (std::string& str);
00575
00580 void toUpper (std::string& str);
00581 }
00582 }
00583
00584 #endif
00585