00001
00027 #ifndef MLP_HPP_
00028 #define MLP_HPP_
00029
00030 #include "SynapticRandomizer.hpp"
00031
00032 #include<vector>
00033 #include<cstdlib>
00034 #include<iostream>
00035 #include<random>
00036 #include<string>
00037
00038 using std::vector;
00039 using std::cout;
00040 using std::ostream;
00041 using std::istream;
00042 using std::string;
00043
00044 namespace jymlp{
00045
00048 namespace actf{
00049 enum ActF : int {Unset = 0, linear = 1, hyptan = 2, logsig = 3};
00050 }
00056 namespace errt{
00057 enum ErrT : int {Unset = 0, q2a2 = 1, q2a1 = 2, q1a1 = 3};
00058 }
00059 using jymlp::actf::ActF;
00060 using jymlp::errt::ErrT;
00061
00062 enum PrettyPrintStyle : int {plaintext = 0, latexeq = 1};
00063
00064 class Mlp{
00065 protected:
00067 vector<double> weights;
00069 vector<size_t> nneur;
00071 vector<ActF> actf;
00072
00074 void initRnd(double a, SynapticRandomizer & sr);
00075
00076 public:
00077
00082 Mlp();
00083
00087 Mlp(const vector<size_t>& inneur);
00088
00090 Mlp(const vector<size_t>& inneur, const vector<ActF>& iactf, const vector<double>& iweights);
00091
00093 Mlp(const Mlp &other);
00094
00099 Mlp(const vector<size_t>& inneur, const vector<ActF>& iactf, SynapticRandomizer& sr);
00100
00105 size_t getWorkspaceSize();
00106
00108 size_t getNLayers() const {return nneur.size();}
00109
00111 size_t getNWeights() const {return weights.size();}
00112
00114 size_t getNNeurons(int layer) const {return nneur[layer];}
00115
00117 size_t getNHiddenNeurons() const;
00118
00124 size_t getNumNonzeroWeights() const;
00125
00130 size_t getNumConnectedInputs() const;
00131
00133 size_t getNumInputs() const {return nneur[0];}
00134
00136 size_t getNumOutputs() const {return nneur[nneur.size()-1];}
00137
00144 void forward(const vector<double> &input, double * workspace) const;
00145
00151 void errorVec(const vector<double> &target, double * workspace) const;
00152
00157 vector <double>
00158 copyOutputVec(double * workspace) const;
00159
00163 size_t
00164 outputVecAsClassIndex(const double * workspace) const;
00165
00166 #if 0
00167
00170 void addEuc2(double coeff, double * dest, double * workspace) const;
00171 #endif
00172
00185 void backwardEucSq(double coeff,
00186 double * destE,
00187 double * destG,
00188 double * workspace,
00189 ErrT errortype) const;
00190
00191
00197 void weightDecaySq(double coeff,
00198 double *destE,
00199 double *destG,
00200 bool excludeOutputBias) const;
00201
00207 void weightDecayAbs(double coeff,
00208 double *destE,
00209 double *destG,
00210 bool excludeOutputBias) const;
00211
00219 void update(double coeff, const double *wupd);
00220
00240 void addErrorAndGradient(const vector<double> &input,
00241 const vector<double> &target,
00242 double coefficient,
00243 double * sqerror,
00244 double * error,
00245 vector<double> * mseGrad,
00246 vector<double> * meeGrad) const;
00247
00248
00264 void toStream(ostream & o);
00265
00267 void fromStream(istream & ins);
00268
00270 string prettyPrintLayerSizes();
00271
00273 string prettyPrintLayerActivations();
00274
00276 string prettyPrintWeights() const;
00277
00279 string prettyPrintGradient(const vector <double> & grad) const;
00280
00282 string prettyPrint();
00283 };
00284
00285 }
00286 #endif