00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __NEURALPP
00019 #define __NEURALPP
00020
00021 #include <vector>
00022 #include <string>
00023 #include <cmath>
00024
00025 #include "neural++_exception.hpp"
00026 using namespace std;
00027
00029 #define RAND (double) ( (rand() / (RAND_MAX/2)) - 1)
00030
00032 #define BETA0 0.8
00033
00038 namespace neuralpp {
00039 class Synapsis;
00040 class Neuron;
00041 class Layer;
00042 class NeuralNet;
00043
00044 double df (double (*f)(double), double x);
00045
00050 class NeuralNet {
00051 int epochs;
00052 int ref_epochs;
00053 double l_rate;
00054 double ex;
00055
00060 void updateWeights();
00061
00067 void commitChanges (Layer *l);
00068
00074 double error(double ex) const;
00075
00080 double (*actv_f)(double);
00081
00082 public:
00083 Layer* input;
00084 Layer* hidden;
00085 Layer* output;
00086
00090 typedef enum { file, str } source;
00091
00095 NeuralNet() {}
00096
00107 NeuralNet (size_t in_size, size_t hidden_size, size_t out_size, double l, int e);
00108
00114 NeuralNet (const string file) throw(NetworkFileNotFoundException);
00115
00116
00128 NeuralNet (size_t in_size, size_t hidden_size, size_t out_size,
00129 double(*actv)(double), double l, int e);
00130
00136 double getOutput() const;
00137
00142 vector<double> getOutputs();
00143
00149 double expected() const;
00150
00155 void setExpected(double ex);
00156
00162 void update();
00163
00168 void propagate();
00169
00174 void setInput (vector<double>& v);
00175
00180 void link();
00181
00188 void save(const char* fname) throw(NetworkFileWriteException);
00189
00197 void train (string xml, source src) throw(InvalidXMLException);
00198
00203 static void initXML (string& xml);
00204
00211 static vector<double> split (char delim, string str);
00212
00225 static string XMLFromSet (int id, string set);
00226
00231 static void closeXML(string& xml);
00232 };
00233
00239 class Synapsis {
00240 double delta;
00241 double prev_delta;
00242 double weight;
00243
00244 Neuron *in;
00245 Neuron *out;
00246
00247 double (*actv_f)(double);
00248
00249 public:
00257 Synapsis(Neuron* i, Neuron* o, double w, double d);
00258
00265 Synapsis (Neuron* i, Neuron* o, double(*a)(double));
00266
00274 Synapsis (Neuron* i, Neuron* o,
00275 double w, double(*a)(double));
00276
00280 Neuron* getIn();
00281
00285 Neuron* getOut();
00286
00291 void setWeight(double w) throw(InvalidSynapticalWeightException);
00292
00298 void setDelta(double d) throw(InvalidSynapticalWeightException);
00299
00304 double getWeight();
00305
00310 double getDelta();
00311
00316 double getPrevDelta();
00317
00328 double momentum (int N, int x);
00329 };
00330
00336 class Neuron {
00337 double actv_val;
00338 double prop_val;
00339
00340 vector< Synapsis > in;
00341 vector< Synapsis > out;
00342
00343 double (*actv_f)(double);
00344
00345 public:
00350 Neuron (double (*a)(double));
00351
00358 Neuron (vector<Synapsis> in, vector<Synapsis> out,
00359 double (*a)(double));
00360
00366 Synapsis& synIn (size_t i);
00367
00373 Synapsis& synOut (size_t i);
00374
00379 void push_in (Synapsis& s);
00380
00385 void push_out (Synapsis& s);
00386
00391 void setActv (double a);
00392
00397 void setProp (double p);
00398
00403 double getActv();
00404
00409 double getProp();
00410
00414 double propagate();
00415
00420 size_t nIn();
00421
00426 size_t nOut();
00427
00431 void synClear();
00432 };
00433
00439 class Layer {
00440 vector<Neuron> elements;
00441
00442 void (*update_weights)();
00443 double (*actv_f)(double);
00444
00445 public:
00451 Layer (size_t sz, double (*a)(double));
00452
00459 Layer (vector<Neuron>& neurons, double(*a)(double));
00460
00466 Neuron& operator[] (size_t i) throw(NetworkIndexOutOfBoundsException);
00467
00472 void link (Layer& l);
00473
00478 void setProp (vector<double>& v);
00479
00484 void setActv (vector<double>& v);
00485
00489 void propagate();
00490
00494 size_t size() const;
00495 };
00496
00497 struct netrecord {
00498 int input_size;
00499 int hidden_size;
00500 int output_size;
00501
00502 int epochs;
00503 double l_rate;
00504 double ex;
00505 };
00506
00507 struct neuronrecord {
00508 double prop;
00509 double actv;
00510 };
00511
00512 struct synrecord {
00513 double w;
00514 double d;
00515 };
00516 }
00517
00518
00519 #endif
00520