nnrbm.h
00001 #ifndef __NNRBM_
00002 #define __NNRBM_
00003
00004 #include "StreamOutput.h"
00005 #include "Framework.h"
00006
00007 #include <math.h>
00008 #include <assert.h>
00009 #include <sstream>
00010 #include <vector>
00011
00012 using namespace std;
00013
00039 class NNRBM : public Framework
00040 {
00041 public:
00042 NNRBM();
00043 ~NNRBM();
00044
00045
00046 void setNrTargets ( int n );
00047 void setNrInputs ( int n );
00048 void setNrExamplesTrain ( int n );
00049 void setNrExamplesProbe ( int n );
00050 void setTrainInputs ( REAL* inputs );
00051 void setTrainTargets ( REAL* targets );
00052 void setProbeInputs ( REAL* inputs );
00053 void setProbeTargets ( REAL* targets );
00054
00055
00056 void setInitWeightFactor ( REAL factor );
00057 void setLearnrate ( REAL learnrate );
00058 void setLearnrateMinimum ( REAL learnrateMin );
00059 void setLearnrateSubtractionValueAfterEverySample ( REAL learnrateDecreaseRate );
00060 void setLearnrateSubtractionValueAfterEveryEpoch ( REAL learnrateDecreaseRate );
00061 void setMomentum ( REAL momentum );
00062 void setWeightDecay ( REAL weightDecay );
00063 void setBatchSize ( int size );
00064 void setMinUpdateErrorBound ( REAL minUpdateBound );
00065 void setMaxEpochs ( int epochs );
00066 void setRPROPPosNeg ( REAL etaPos, REAL etaNeg );
00067 void setRPROPMinMaxUpdate ( REAL min, REAL max );
00068 void setL1Regularization ( bool en );
00069 void initNNWeights ( time_t seed );
00070 void enableErrorFunctionMAE ( bool en );
00071 void setRBMLearnParams ( REAL learnrateWeights, REAL learnrateBiasVis, REAL learnrateBiasHid, REAL weightDecay, int maxEpoch );
00072
00073
00074 void setNNStructure ( int nrLayer, int* neuronsPerLayer, bool lastLinearLayer = false, int* layerType = 0 );
00075
00076
00077 void printLearnrate();
00078 void setScaleOffset ( REAL scale, REAL offset );
00079 void setNormalTrainStopping ( bool en );
00080 void setGlobalEpochs ( int e );
00081 void enableRPROP ( bool en );
00082 void useBLASforTraining ( bool enable );
00083 void trainOneEpoch ( bool* updateLayer = 0 );
00084 int trainNN();
00085 REAL getRMSETrain();
00086 REAL getRMSEProbe();
00087 void predictSingleInput ( REAL* input, REAL* output );
00088 REAL* getWeightPtr();
00089 void setWeights ( REAL* w );
00090 int getNrWeights();
00091 double m_sumSquaredError;
00092 double m_sumSquaredErrorSamples;
00093
00094 int getWeightIndex ( int layer, int neuron, int weight );
00095 int getBiasIndex ( int layer, int neuron );
00096 int getOutputIndex ( int layer, int neuron );
00097
00098 void rbmPretraining ( REAL* input, REAL* target, int nSamples, int nTarget, int firstNLayer = -1, int crossRun = -1, int nFinetuningEpochs = -1, double finetuningLearnRate = 0.0 );
00099 vector<int> getEncoder();
00100
00101
00102 void printMiddleLayerToFile(string fname, REAL* input, REAL* target, int nSamples, int nTarget);
00103 void printAutoencoderWeightsToJavascript(string fname);
00104
00105 NNRBM* m_nnAuto;
00106
00107 private:
00108
00109 void saveWeights();
00110 REAL calcRMSE ( REAL* inputs, REAL* targets, int examples );
00111 void forwardCalculation ( REAL* input );
00112 void forwardCalculationBLAS ( REAL* input );
00113 void backpropBLAS ( REAL* input, REAL* target, bool* updateLayer = 0 );
00114 void backprop ( REAL* input, REAL* target, bool* updateLayer = 0 );
00115
00116
00117 REAL getInitWeight ( int fanIn );
00118
00119
00120 int m_nrTargets;
00121 int m_nrInputs;
00122 int m_nrExamplesTrain;
00123 int m_nrExamplesProbe;
00124 REAL* m_inputsTrain;
00125 REAL* m_inputsProbe;
00126 REAL* m_targetsTrain;
00127 REAL* m_targetsProbe;
00128
00129
00130 REAL m_initWeightFactor;
00131 int m_globalEpochs;
00132 REAL m_RPROP_etaPos;
00133 REAL m_RPROP_etaNeg;
00134 REAL m_RPROP_updateMin;
00135 REAL m_RPROP_updateMax;
00136 REAL m_learnRate;
00137 REAL m_learnRateMin;
00138 REAL m_learnrateDecreaseRate;
00139 REAL m_learnrateDecreaseRateEpoch;
00140 REAL m_momentum;
00141 REAL m_weightDecay;
00142 REAL m_minUpdateBound;
00143 int m_batchSize;
00144 REAL m_rbmLearnrateWeights;
00145 REAL m_rbmLearnrateBiasVis;
00146 REAL m_rbmLearnrateBiasHid;
00147 REAL m_rbmWeightDecay;
00148 int m_rbmMaxEpochs;
00149
00150
00151 REAL m_scaleOutputs;
00152 REAL m_offsetOutputs;
00153 int m_maxEpochs;
00154 bool m_useBLAS;
00155 bool m_enableRPROP;
00156 bool m_normalTrainStopping;
00157 bool m_enableL1Regularization;
00158 bool m_errorFunctionMAE;
00159
00160
00161 int m_nrLayer;
00162 int* m_neuronsPerLayer;
00163 int m_nrWeights;
00164 int m_nrOutputs;
00165 int* m_nrLayWeights;
00166 int* m_nrLayWeightOffsets;
00167 REAL* m_outputs;
00168 REAL* m_outputsTmp;
00169 REAL* m_derivates;
00170 REAL* m_d1;
00171 REAL* m_weights;
00172 REAL* m_weightsTmp0;
00173 REAL* m_weightsTmp1;
00174 REAL* m_weightsTmp2;
00175 REAL* m_weightsBatchUpdate;
00176 REAL* m_weightsOld;
00177 REAL* m_weightsOldOld;
00178 REAL* m_deltaW;
00179 REAL* m_deltaWOld;
00180 REAL* m_adaptiveRPROPlRate;
00181 int* m_activationFunctionPerLayer;
00182 bool m_enableAutoencoder;
00183
00184 };
00185
00186 #endif