PandaRoot
PndLVQTrain.h
Go to the documentation of this file.
1 /* ***************************************
2  * LVQ Training functions *
3  * Author: M.Babai@rug.nl *
4  * Version: *
5  * LICENSE: *
6  * ***************************************
7  */
8 //#pragma once
9 #ifndef PND_LVQ_TRAIN_H
10 #define PND_LVQ_TRAIN_H
11 
12 #ifdef _OPENMP
13 #include <omp.h>
14 #endif
15 
16 // Local includes
17 #include "PndMvaTrainer.h"
18 #include "PndMvaCluster.h"
19 
20 // ____ Local CPP definitions _________
21 #define DEBUG_LVQ_TRAIN 0
22 // ____________________________________
23 
25 typedef enum ProtoInitType {
26  RAND_FROM_DATA = 0, // Select randomly from data vector.
27  CCM_PR = 1, // Random init around Class Conditional Mean.
28  KMEANS_PR = 10, // Init using K-Means clustering.
29  FILE_PR = 20 // Read pre-init from file.
31 
33 class PndLVQTrain : public PndMvaTrainer {
34  //----------------------------------------
35  //================== public ==============
36  public:
43  explicit PndLVQTrain(std::vector<std::pair<std::string, std::vector<float> *>> const &InputEvtsParam, std::vector<std::string> const &ClassNames,
44  std::vector<std::string> const &VarNames, bool trim = false);
51  explicit PndLVQTrain(std::string const &InPut, std::vector<std::string> const &ClassNames, std::vector<std::string> const &VarNames, bool trim = true);
55  virtual ~PndLVQTrain();
56 
61  void storeWeights();
62 
66  void Train();
67 
71  void Train21();
72 
77  inline void setProtoInitType(ProtoInitType iniTypeVal = RAND_FROM_DATA);
78 
84  inline void SetInitProtoFileName(std::string const &fileName);
85 
94  inline void SetLearnPrameters(double const initConst, double const etZ, double const etF, unsigned int const Nswp);
95 
102  void SetNumberOfProto(size_t const numProto);
103 
109  void SetNumberOfProto(std::map<std::string, size_t> const &labelMap);
110 
117  inline void SetErrorStepSize(unsigned int const val = 1000);
118 
123  inline void SetLVQ2_1WindowSize(float const Wsize = 0.3);
124 
128  void EvalClassifierError();
129 
137  inline void SetPerEpochEval(bool val);
138 
143  inline bool GetPerEpochEval() const;
144 
146 #if DEBUG_LVQ_TRAIN == 1
147  inline std::vector<std::pair<std::string, std::vector<float> *>> const &train1sec()
148  {
149  InitProtoK_Means();
150  return m_LVQProtos;
151  };
152 
153  inline std::vector<std::pair<std::string, std::vector<float> *>> const &train2sec()
154  {
155  InitProtoRand();
156  return m_LVQProtos;
157  };
158 
166  float EvalClassifierError(std::vector<std::pair<std::string, std::vector<float> *>> const &TestEvts) const;
167 #endif
168 
170  //----------------------------------------
171  // protected:
172  //================== private =============
173  private:
174  // To avoid mistakes, :).
175  PndLVQTrain(PndLVQTrain const &other);
176  PndLVQTrain &operator=(PndLVQTrain const &other);
177 
179  void EvalClassifierError(unsigned int stp);
180 
185  void InitProtoRand();
186 
191  void InitRandProtoFromData();
192 
196  void InitProtoK_Means();
197 
201  void InitProtoTypes();
202 
206  void cleanProtoList();
207 
211  void UpdateProto(std::vector<float> const &EvtData, std::vector<float> &proto, int const delta, double const ethaT);
212 
219  void ValidateProtoUpdate(std::vector<float> &p);
220 
225  void ReadProtoFromFile();
226  //=====================================
227 
229  std::vector<std::pair<std::string, std::vector<float> *>> m_LVQProtos;
230 
232  std::vector<PndMvaDistObj> m_distances;
233 
238  double m_initConst;
239  double m_ethaZero;
240  double m_ethaFinal;
241  float m_WindowSize;
242 
244  unsigned int m_NumSweep;
245 
247  ProtoInitType m_proto_init;
248 
250  std::string m_initProtoFile;
251 
253  unsigned int m_ErrorStep;
254  unsigned int m_ProgStep;
255 
257  std::map<std::string, size_t> m_numProtoPerClass;
258 
260  bool m_PerEpoch;
261 };
262 // END Interface definition
263 
264 //_______________________ Inline functions _________________________
266 {
267  m_proto_init = iniTypeVal;
268 };
269 
270 inline void PndLVQTrain::SetInitProtoFileName(std::string const &fileName)
271 {
272  m_initProtoFile = fileName;
273 };
274 
275 inline void PndLVQTrain::SetLearnPrameters(double const initConst, double const etZ, double const etF, unsigned int const Nswp)
276 {
277  m_initConst = initConst;
278  m_ethaZero = etZ;
279  m_ethaFinal = etF;
280  m_NumSweep = Nswp;
281 };
282 
283 inline void PndLVQTrain::SetErrorStepSize(unsigned int const val)
284 {
285  m_ErrorStep = val;
286 };
287 
288 inline void PndLVQTrain::SetLVQ2_1WindowSize(float const Wsize)
289 {
290  m_WindowSize = Wsize;
291 };
292 
293 inline void PndLVQTrain::SetPerEpochEval(bool val)
294 {
295  m_PerEpoch = val;
296 };
297 
298 inline bool PndLVQTrain::GetPerEpochEval() const
299 {
300  return m_PerEpoch;
301 };
302 #endif // End of interface definition
void SetPerEpochEval(bool val)
Definition: PndLVQTrain.h:293
bool GetPerEpochEval() const
Definition: PndLVQTrain.h:298
void storeWeights()
void Train21()
void Train()
virtual ~PndLVQTrain()
ProtoInitType
How to initialize LVQ code books.
Definition: PndLVQTrain.h:25
void SetLVQ2_1WindowSize(float const Wsize=0.3)
Definition: PndLVQTrain.h:288
void EvalClassifierError()
PndLVQTrain(std::vector< std::pair< std::string, std::vector< float > *>> const &InputEvtsParam, std::vector< std::string > const &ClassNames, std::vector< std::string > const &VarNames, bool trim=false)
Interface definition for LVQ trainers.
Definition: PndLVQTrain.h:33
void SetLearnPrameters(double const initConst, double const etZ, double const etF, unsigned int const Nswp)
Definition: PndLVQTrain.h:275
void setProtoInitType(ProtoInitType iniTypeVal=RAND_FROM_DATA)
Definition: PndLVQTrain.h:265
void SetNumberOfProto(size_t const numProto)
void SetInitProtoFileName(std::string const &fileName)
Definition: PndLVQTrain.h:270
void SetErrorStepSize(unsigned int const val=1000)
Definition: PndLVQTrain.h:283