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
00024 #include <cmath>
00025 #include <ctime>
00026 #include <cstdio>
00027
00028 #include "neural++_exception.hpp"
00029 using namespace std;
00030
00032 #define RAND ( abs( sin(rand()) ) )
00033
00035 #define BETA0 0.7
00036
00041 namespace neuralpp {
00042 class Synapsis;
00043 class Neuron;
00044 class Layer;
00045 class NeuralNet;
00046 class NetworkFileNotFoundException;
00047 class InvalidXMLException;
00048
00053 class NeuralNet {
00054 int epochs;
00055 int ref_epochs;
00056 double l_rate;
00057 double ex;
00058
00059 Layer* input;
00060 Layer* hidden;
00061 Layer* output;
00062
00067 void updateWeights();
00068
00074 void commitChanges (Layer *l);
00075
00081 double error(double ex);
00082
00087 double (*actv_f)(double);
00088
00093 double (*deriv)(double);
00094
00095 public:
00099 typedef enum { file, str } source;
00100
00104 NeuralNet() {}
00105
00116 NeuralNet (size_t in_size, size_t hidden_size, size_t out_size, double l, int e);
00117
00123 NeuralNet (const char* file) throw(NetworkFileNotFoundException);
00124
00125
00138 NeuralNet (size_t in_size, size_t hidden_size, size_t out_size,
00139 double(*actv)(double), double(*deriv)(double), double l, int e);
00140
00146 double getOutput();
00147
00152 vector<double> getOutputs();
00153
00159 double expected();
00160
00165 void setExpected(double ex);
00166
00172 void update();
00173
00178 void propagate();
00179
00184 void setInput (vector<double>& v);
00185
00190 void link();
00191
00196 bool save(const char* fname);
00197
00205 void train(string xml, source xrc) throw(InvalidXMLException);
00206
00211 static void initXML (string& xml);
00212
00219 static vector<double> split (char delim, string str);
00220
00233 static string XMLFromSet (int id, string set);
00234
00239 static void closeXML(string& xml);
00240 };
00241
00247 class Synapsis {
00248 double delta;
00249 double prev_delta;
00250 double weight;
00251
00252 Neuron *in;
00253 Neuron *out;
00254
00255 double (*actv_f)(double);
00256 double (*deriv)(double);
00257
00258 public:
00266 Synapsis(Neuron* i, Neuron* o, double w, double d);
00267
00275 Synapsis (Neuron* i, Neuron* o, double(*a)(double), double(*d)(double));
00276
00285 Synapsis (Neuron* i, Neuron* o,
00286 double w, double(*a)(double), double(*d)(double));
00287
00291 Neuron* getIn();
00292
00296 Neuron* getOut();
00297
00302 void setWeight(double w);
00303
00309 void setDelta(double d);
00310
00315 double getWeight();
00316
00321 double getDelta();
00322
00327 double getPrevDelta();
00328
00339 double momentum (int N, int x);
00340 };
00341
00347 class Neuron {
00348 double actv_val;
00349 double prop_val;
00350
00351 vector< Synapsis > in;
00352 vector< Synapsis > out;
00353
00354 double (*actv_f)(double);
00355 double (*deriv)(double);
00356 public:
00362 Neuron (double (*a)(double), double(*d)(double));
00363
00371 Neuron (vector<Synapsis> in, vector<Synapsis> out,
00372 double (*a)(double), double(*d)(double));
00373
00379 Synapsis& synIn (size_t i);
00380
00386 Synapsis& synOut (size_t i);
00387
00392 void push_in (Synapsis& s);
00393
00398 void push_out (Synapsis& s);
00399
00404 void setActv (double a);
00405
00410 void setProp (double p);
00411
00416 double getActv();
00417
00422 double getProp();
00423
00427 double propagate();
00428
00433 size_t nIn();
00434
00439 size_t nOut();
00440
00444 void synClear();
00445 };
00446
00452 class Layer {
00453 vector<Neuron> elements;
00454
00455 void (*update_weights)();
00456 double (*actv_f)(double);
00457 double (*deriv)(double);
00458
00459 public:
00466 Layer (size_t sz, double (*a)(double), double(*d)(double));
00467
00475 Layer (vector<Neuron>& neurons, double(*a)(double), double(*d)(double));
00476
00482 Neuron& operator[] (size_t i);
00483
00488 void link (Layer& l);
00489
00494 void setProp (vector<double>& v);
00495
00500 void setActv (vector<double>& v);
00501
00505 void propagate();
00506
00510 size_t size();
00511 };
00512
00513 struct netrecord {
00514 int input_size;
00515 int hidden_size;
00516 int output_size;
00517
00518 int epochs;
00519 double l_rate;
00520 double ex;
00521 };
00522
00523 struct neuronrecord {
00524 double prop;
00525 double actv;
00526 };
00527
00528 struct synrecord {
00529 double w;
00530 double d;
00531 };
00532 }
00533
00534 #endif
00535
00536