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
00024 #define RAND (double) ( (rand() / (RAND_MAX/2)) - 1)
00025
00027 #define BETA0 1.0
00028
00033 namespace neuralpp {
00034 class Synapsis;
00035 class Neuron;
00036 class Layer;
00037 class NeuralNet;
00038
00039 double df (double (*f)(double), double x);
00040 double __actv(double prop);
00041
00064 class NeuralNet {
00065 int epochs;
00066 int ref_epochs;
00067 double l_rate;
00068 double threshold;
00069 std::vector<double> expect;
00070
00075 void updateWeights();
00076
00082 void commitChanges (Layer& l);
00083
00089 double error (double ex);
00090
00095 double (*actv_f)(double);
00096
00097 public:
00098 Layer* input;
00099 Layer* hidden;
00100 Layer* output;
00101
00105 typedef enum { file, str } source;
00106
00110 NeuralNet() {}
00111
00125 NeuralNet (size_t in_size, size_t hidden_size, size_t out_size, double l,
00126 int e, double th = 0.0, double (*a)(double) = __actv);
00127
00133 NeuralNet (const std::string file) throw(NetworkFileNotFoundException);
00134
00140 double getOutput() const;
00141
00146 double getThreshold() const;
00147
00152 std::vector<double> getOutputs();
00153
00159 double expected() const;
00160
00166 std::vector<double> getExpected() const;
00167
00172 void setExpected(double ex);
00173
00178 void setExpected(std::vector<double> ex);
00179
00185 void update();
00186
00191 void propagate();
00192
00197 void setInput (std::vector<double> v);
00198
00203 void link();
00204
00211 void save (const char* fname) throw(NetworkFileWriteException);
00212
00220 void train (std::string xml, source src) throw(InvalidXMLException);
00221
00226 static void initXML (std::string& xml);
00227
00234 static std::vector<double> split (char delim, std::string str);
00235
00248 static std::string XMLFromSet (int& id, std::string set);
00249
00254 static void closeXML(std::string& xml);
00255 };
00256
00262 class Synapsis {
00263 double delta;
00264 double prev_delta;
00265 double weight;
00266
00267 Neuron *in;
00268 Neuron *out;
00269
00270 double (*actv_f)(double);
00271
00272 public:
00280 Synapsis(Neuron* i, Neuron* o, double w, double d);
00281
00288 Synapsis (Neuron* i, Neuron* o, double(*a)(double));
00289
00297 Synapsis (Neuron* i, Neuron* o,
00298 double w, double(*a)(double));
00299
00303 Neuron* getIn() const;
00304
00308 Neuron* getOut() const;
00309
00314 void setWeight(double w) throw(InvalidSynapticalWeightException);
00315
00321 void setDelta(double d) throw(InvalidSynapticalWeightException);
00322
00327 double getWeight() const;
00328
00333 double getDelta() const;
00334
00339 double getPrevDelta() const;
00340
00351 double momentum (int N, int x) const;
00352 };
00353
00359 class Neuron {
00360 double actv_val;
00361 double prop_val;
00362 double threshold;
00363
00364 std::vector< Synapsis > in;
00365 std::vector< Synapsis > out;
00366
00367 double (*actv_f)(double);
00368
00369 public:
00376 Neuron (double (*a)(double), double th = 0.0);
00377
00386 Neuron (std::vector<Synapsis> in, std::vector<Synapsis> out,
00387 double (*a)(double), double th = 0.0);
00388
00394 Synapsis& synIn (size_t i);
00395
00401 Synapsis& synOut (size_t i);
00402
00407 void push_in (Synapsis s);
00408
00413 void push_out (Synapsis s);
00414
00419 void setActv (double a);
00420
00425 void setProp (double p);
00426
00431 double getActv();
00432
00437 double getProp();
00438
00442 void propagate();
00443
00448 size_t nIn();
00449
00454 size_t nOut();
00455
00459 void synClear();
00460 };
00461
00467 class Layer {
00468 std::vector<Neuron> elements;
00469 double threshold;
00470
00471 void (*update_weights)();
00472 double (*actv_f)(double);
00473
00474 public:
00482 Layer (size_t sz, double (*a)(double), double th = 0.0);
00483
00492 Layer (std::vector<Neuron>& neurons, double(*a)(double), double th = 0.0);
00493
00499 Neuron& operator[] (size_t i) throw(NetworkIndexOutOfBoundsException);
00500
00505 void link (Layer& l);
00506
00511 void setInput (std::vector<double> v);
00512
00516 void propagate();
00517
00521 size_t size() const;
00522 };
00523
00524 struct netrecord {
00525 int input_size;
00526 int hidden_size;
00527 int output_size;
00528
00529 int epochs;
00530 double l_rate;
00531 double ex;
00532 };
00533
00534 struct neuronrecord {
00535 double prop;
00536 double actv;
00537 };
00538
00539 struct synrecord {
00540 double w;
00541 double d;
00542 };
00543 }
00544
00545 #endif
00546