FairRoot/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 
34 {
35  //----------------------------------------
36  //================== public ==============
37  public:
38 
45  explicit PndLVQTrain(std::vector< std::pair<std::string, std::vector<float>* > > const& InputEvtsParam,
46  std::vector<std::string> const& ClassNames,
47  std::vector<std::string> const& VarNames,
48  bool trim = false);
55  explicit PndLVQTrain(std::string const& InPut,
56  std::vector<std::string> const& ClassNames,
57  std::vector<std::string> const& VarNames,
58  bool trim = true);
62  virtual ~PndLVQTrain();
63 
68  void storeWeights();
69 
73  void Train();
74 
78  void Train21();
79 
84  inline void setProtoInitType(ProtoInitType iniTypeVal = RAND_FROM_DATA);
85 
91  inline void SetInitProtoFileName(std::string const& fileName);
92 
101  inline void SetLearnPrameters(double const initConst, double const etZ,
102  double const etF, unsigned int const Nswp);
103 
110  void SetNumberOfProto(size_t const numProto);
111 
117  void SetNumberOfProto(std::map<std::string, size_t> const& labelMap);
118 
125  inline void SetErrorStepSize(unsigned int const val = 1000);
126 
131  inline void SetLVQ2_1WindowSize(float const Wsize = 0.3);
132 
136  void EvalClassifierError();
137 
145  inline void SetPerEpochEval(bool val);
146 
151  inline bool GetPerEpochEval() const;
152 
154 #if DEBUG_LVQ_TRAIN == 1
155  inline std::vector< std::pair<std::string, std::vector<float>*> > const& train1sec()
156  {
158  return m_LVQProtos;
159  };
160 
161  inline std::vector< std::pair<std::string, std::vector<float>*> > const& train2sec()
162  {
163  InitProtoRand();
164  return m_LVQProtos;
165  };
166 
174  float EvalClassifierError(std::vector< std::pair< std::string, std::vector<float>* > > const& TestEvts) const;
175 #endif
176 
178  //----------------------------------------
179  // protected:
180  //================== private =============
181  private:
182  // To avoid mistakes, :).
183  PndLVQTrain(PndLVQTrain const& other);
184  PndLVQTrain& operator=(PndLVQTrain const& other);
185 
187  void EvalClassifierError(unsigned int stp);
188 
193  void InitProtoRand();
194 
199  void InitRandProtoFromData();
200 
204  void InitProtoK_Means();
205 
209  void InitProtoTypes();
210 
214  void cleanProtoList();
215 
219  void UpdateProto(std::vector<float> const& EvtData,
220  std::vector<float>& proto,
221  int const delta, double const ethaT);
222 
229  void ValidateProtoUpdate(std::vector<float>& p);
230 
235  void ReadProtoFromFile();
236  //=====================================
237 
239  std::vector< std::pair<std::string, std::vector<float>*> > m_LVQProtos;
240 
242  std::vector <PndMvaDistObj> m_distances;
243 
248  double m_initConst;
249  double m_ethaZero;
250  double m_ethaFinal;
252 
254  unsigned int m_NumSweep;
255 
258 
260  std::string m_initProtoFile;
261 
263  unsigned int m_ErrorStep;
264  unsigned int m_ProgStep;
265 
267  std::map < std::string, size_t> m_numProtoPerClass;
268 
271 };
272 //END Interface definition
273 
274 //_______________________ Inline functions _________________________
276 {
277  m_proto_init = iniTypeVal;
278 };
279 
280 inline void PndLVQTrain::SetInitProtoFileName(std::string const& fileName)
281 {
282  m_initProtoFile = fileName;
283 };
284 
285 inline void PndLVQTrain::SetLearnPrameters(double const initConst, double const etZ,
286  double const etF, unsigned int const Nswp)
287 {
288  m_initConst = initConst;
289  m_ethaZero = etZ;
290  m_ethaFinal = etF;
291  m_NumSweep = Nswp;
292 };
293 
294 inline void PndLVQTrain::SetErrorStepSize(unsigned int const val)
295 {
296  m_ErrorStep = val;
297 };
298 
299 inline void PndLVQTrain::SetLVQ2_1WindowSize(float const Wsize)
300 {
301  m_WindowSize = Wsize;
302 };
303 
305 {
306  m_PerEpoch = val;
307 };
308 
309 inline bool PndLVQTrain::GetPerEpochEval() const
310 {
311  return m_PerEpoch;
312 };
313 #endif// End of interface definition
Double_t p
Definition: anasim.C:58
void SetPerEpochEval(bool val)
Definition: PndLVQTrain.h:304
double m_ethaZero
Definition: PndLVQTrain.h:249
void cleanProtoList()
unsigned int m_ProgStep
Definition: PndLVQTrain.h:264
Double_t val[nBoxes][nFEBox]
Definition: createCalib.C:11
PndLVQTrain & operator=(PndLVQTrain const &other)
void storeWeights()
std::string m_initProtoFile
initial protypes, when reading from file.
Definition: PndLVQTrain.h:260
unsigned int m_ErrorStep
Each #ErrorStep, steps evaluate the trained classifier.
Definition: PndLVQTrain.h:263
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)
void Train21()
void Train()
ProtoInitType m_proto_init
Proto init type.
Definition: PndLVQTrain.h:257
void InitProtoRand()
float m_WindowSize
Definition: PndLVQTrain.h:251
double m_ethaFinal
Definition: PndLVQTrain.h:250
void ValidateProtoUpdate(std::vector< float > &p)
virtual ~PndLVQTrain()
ProtoInitType
How to initialize LVQ code books.
Definition: PndLVQTrain.h:25
void SetLVQ2_1WindowSize(float const Wsize=0.3)
Definition: PndLVQTrain.h:299
void InitProtoK_Means()
void EvalClassifierError()
unsigned int m_NumSweep
Number of sweeps through example set.
Definition: PndLVQTrain.h:254
void UpdateProto(std::vector< float > const &EvtData, std::vector< float > &proto, int const delta, double const ethaT)
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:285
void setProtoInitType(ProtoInitType iniTypeVal=RAND_FROM_DATA)
Definition: PndLVQTrain.h:275
bool m_PerEpoch
If evaluate per epoch.
Definition: PndLVQTrain.h:270
void SetNumberOfProto(size_t const numProto)
double m_initConst
Definition: PndLVQTrain.h:248
void SetInitProtoFileName(std::string const &fileName)
Definition: PndLVQTrain.h:280
void InitProtoTypes()
void SetErrorStepSize(unsigned int const val=1000)
Definition: PndLVQTrain.h:294
std::map< std::string, size_t > m_numProtoPerClass
Map labels (classes) to number of prototypes.
Definition: PndLVQTrain.h:267
void InitRandProtoFromData()
std::vector< std::pair< std::string, std::vector< float > * > > m_LVQProtos
Holds the LVQ proto-types.
Definition: PndLVQTrain.h:239
void ReadProtoFromFile()
std::vector< PndMvaDistObj > m_distances
Container to store distances.
Definition: PndLVQTrain.h:242
bool GetPerEpochEval() const
Definition: PndLVQTrain.h:309