KSquare Utilities
ModelDual.h
Go to the documentation of this file.
1 #ifndef _MODELDUAL_
2 #define _MODELDUAL_
3 /**
4  *@class KKMLL::ModelDual
5  *@brief Will implement the Dual Classifier Model.
6  *@details This model will actually load two different training models specified in the parameters
7  * field. It will utilize both classifiers for to make a prediction. Unknown examples will be
8  * submitted to both classifiers. The returned class will be the common part of the class hierarchy
9  * of the two predictions. If there is nothing in common between the two predictions then it will
10  * return the "Other class.
11  */
12 
13 #include "RunLog.h"
14 #include "KKStr.h"
15 
16 #include "Model.h"
17 
18 
19 namespace KKMLL
20 {
21  #if !defined(_MLCLASS_)
22  class MLClass;
23  typedef MLClass* MLClassPtr;
24  class MLClassList;
25  typedef MLClassList* MLClassListPtr;
26  #endif
27 
28  #if !defined(_FILEDESC_)
29  class FileDesc;
30  typedef FileDesc* FileDescPtr;
31  #endif
32 
33  #if !defined(_FEATUREVECTOR_)
34  class FeatureVector;
36  class FeatureVectorList;
38  #endif
39 
40  #if !defined(_FEATURENUMLIST_)
41  class FeatureNumList;
43  #endif
44 
45  #if !defined(_TRAININGPROCESS2_)
46  class TrainingProcess2;
47  typedef TrainingProcess2* TrainingProcess2Ptr;
48  #endif
49 
50  #if !defined(_CLASSIFIER2_)
51  class Classifier2;
52  typedef Classifier2* Classifier2Ptr;
53  #endif
54 
55  #if !defined(_MODELPARAMDUAL_)
58  #endif
59 
60  #if !defined(_TRAININGCONFIGURATION2_)
62  typedef TrainingConfiguration2* TrainingConfiguration2Ptr;
63  #endif
64 
65 
66  class ModelDual: public Model
67  {
68  public:
69 
71 
72 
73  ModelDual ();
74 
75  ModelDual (FactoryFVProducerPtr _factoryFVProducer);
76 
77  ModelDual (const KKStr& _name,
78  const ModelParamDual& _param, // Create new model from
79  FactoryFVProducerPtr _factoryFVProducer
80  );
81 
82  ModelDual (const ModelDual& _model);
83 
84  virtual
85  ~ModelDual ();
86 
87  virtual
89 
90  virtual
91  KKStr Description () const; /**< Return short user readable description of model. */
92 
93  virtual ModelTypes ModelType () const {return ModelTypes::Dual;}
94 
95  virtual
97 
98  TrainingProcess2Ptr Trainer1 () {return trainer1;}
99 
100  TrainingProcess2Ptr Trainer2 () {return trainer2;}
101 
102  virtual
103  ModelDualPtr Duplicate () const;
104 
106 
107  virtual
108  MLClassPtr Predict (FeatureVectorPtr example,
109  RunLog& log
110  );
111 
112  virtual
113  void Predict (FeatureVectorPtr example,
114  MLClassPtr knownClass,
115  MLClassPtr& predClass1,
116  MLClassPtr& predClass2,
117  kkint32& predClass1Votes,
118  kkint32& predClass2Votes,
119  double& probOfKnownClass,
120  double& predClass1Prob,
121  double& predClass2Prob,
122  kkint32& numOfWinners,
123  bool& knownClassOneOfTheWinners,
124  double& breakTie,
125  RunLog& log
126  );
127 
128  virtual
130  RunLog& log
131  );
132 
133 
134  virtual
136  KKStr& classifier1Desc,
137  KKStr& classifier2Desc,
138  ClassProbListPtr& classifier1Results,
139  ClassProbListPtr& classifier2Results,
140  RunLog& log
141  );
142 
143  virtual
145  const MLClassList& _mlClasses,
146  kkint32* _votes,
147  double* _probabilities,
148  RunLog& log
149  );
150 
151  /**
152  *@brief Derives predicted probabilities by class.
153  *@details Will get the probabilities assigned to each class by the classifier. The
154  * '_mlClasses' parameter dictates the order of the classes. That is the
155  * probabilities for any given index in '_probabilities' will be for the class
156  * specified in the same index in '_mlClasses'.
157  *@param[in] _example FeatureVector object to calculate predicted probabilities for.
158  *@param[in] _mlClasses List of classes that caller is aware of. This should be the
159  * same list that was used when constructing this Model object. The list must
160  * be the same but not necessarily in the same order as when Model was 1st
161  * constructed.
162  *@param[out] _probabilities An array that must be as big as the number of classes as in
163  * mlClasses. The probability of class in mlClasses[x] will be returned
164  * in probabilities[x].
165  */
166  virtual
167  void ProbabilitiesByClass (FeatureVectorPtr _example,
168  const MLClassList& _mlClasses,
169  double* _probabilities,
170  RunLog& _log
171  );
172 
173 
174  virtual
175  void RetrieveCrossProbTable (MLClassList& classes,
176  double** crossProbTable, /**< two dimension matrix that needs to be classes.QueueSize () squared. */
177  RunLog& log
178  );
179 
180 
181 
182  virtual void TrainModel (FeatureVectorListPtr _trainExamples,
183  bool _alreadyNormalized,
184  bool _takeOwnership, /**< Model will take ownership of these examples */
185  VolConstBool& _cancelFlag,
186  RunLog& _log
187  );
188 
189 
190  virtual void ReadXML (XmlStream& s,
191  XmlTagConstPtr tag,
192  VolConstBool& cancelFlag,
193  RunLog& log
194  );
195 
196 
197  virtual void WriteXML (const KKStr& varName,
198  ostream& o
199  ) const;
200 
201 
202  protected:
204 
205  MLClassPtr ReconcilePredictions (MLClassPtr pred1,
206  MLClassPtr pred2,
207  RunLog& log
208  );
209 
210  void ReconcileProbAndVotes (Classifier2Ptr classifier,
211  MLClassPtr predClass,
212  FeatureVectorPtr encodedExample,
213  double& predClassProb,
214  kkint32& predClassVotes
215  );
216 
217  TrainingConfiguration2Ptr config1;
218  TrainingConfiguration2Ptr config2;
219 
220  TrainingProcess2Ptr trainer1;
221  TrainingProcess2Ptr trainer2;
222 
223  Classifier2Ptr classifier1;
224  Classifier2Ptr classifier2;
225 
228 
229  ModelParamDualPtr param; /*!< We will NOT own this instance. It will point to same instance defined in parent class Model. */
230  }; /* ModelDual */
231 
232 
233  typedef ModelDual::ModelDualPtr ModelDualPtr;
234 
235 
238 
239 } /* namespace KKMLL */
240 
241 #endif
KKStr trainer2StatusMsg
Definition: ModelDual.h:227
Base class to all Learning Algorithms.
Definition: Model.h:82
ModelDual * ModelDualPtr
Definition: ModelDual.h:70
Provides a detailed description of the attributes of a dataset.
Definition: FileDesc.h:72
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
TrainingConfiguration2Ptr config2
Definition: ModelDual.h:218
__int32 kkint32
Definition: KKBaseTypes.h:88
FeatureVector * FeatureVectorPtr
Definition: Model.h:44
XmlElementModelTemplate< ModelDual > XmlElementModelDual
Definition: ModelDual.h:236
Keeps track of selected features.
virtual ClassProbListPtr ProbabilitiesByClass(FeatureVectorPtr example, RunLog &log)
Definition: ModelDual.cpp:591
virtual void WriteXML(const KKStr &varName, ostream &o) const
Definition: ModelDual.cpp:875
virtual ModelTypes ModelType() const
Definition: ModelDual.h:93
void ReconcileProbAndVotes(Classifier2Ptr classifier, MLClassPtr predClass, FeatureVectorPtr encodedExample, double &predClassProb, kkint32 &predClassVotes)
Definition: ModelDual.cpp:396
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
virtual void ProbabilitiesByClassDual(FeatureVectorPtr example, KKStr &classifier1Desc, KKStr &classifier2Desc, ClassProbListPtr &classifier1Results, ClassProbListPtr &classifier2Results, RunLog &log)
Only applied to ModelDual classifier.
Definition: ModelDual.cpp:628
Classifier2Ptr classifier1
Definition: ModelDual.h:223
ModelParamDualPtr Param()
Definition: ModelDual.cpp:158
TrainingProcess2Ptr Trainer1()
Definition: ModelDual.h:98
void DeleteExistingClassifiers()
Definition: ModelDual.cpp:164
virtual KKStr Description() const
Definition: ModelDual.cpp:133
Container class for FeatureVector derived objects.
FeatureNumList * FeatureNumListPtr
virtual void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable, RunLog &log)
Definition: ModelDual.cpp:800
virtual MLClassPtr Predict(FeatureVectorPtr example, RunLog &log)
Definition: ModelDual.cpp:430
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
Definition: XmlStream.h:46
virtual ModelDualPtr Duplicate() const
Definition: ModelDual.cpp:151
XmlElementModelDual * XmlElementModelDualPtr
Definition: ModelDual.h:237
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
ClassProbList * ClassProbListPtr
Definition: Classifier2.h:30
TrainingProcess2Ptr Trainer2()
Definition: ModelDual.h:100
Classifier2Ptr classifier2
Definition: ModelDual.h:224
virtual void Predict(FeatureVectorPtr example, MLClassPtr knownClass, MLClassPtr &predClass1, MLClassPtr &predClass2, kkint32 &predClass1Votes, kkint32 &predClass2Votes, double &probOfKnownClass, double &predClass1Prob, double &predClass2Prob, kkint32 &numOfWinners, bool &knownClassOneOfTheWinners, double &breakTie, RunLog &log)
Definition: ModelDual.cpp:462
FileDesc * FileDescPtr
ModelParamDual * ModelParamDualPtr
Definition: ModelDual.h:56
Will implement the Dual Classifier Model.
Definition: ModelDual.h:66
TrainingConfiguration2Ptr config1
Definition: ModelDual.h:217
ModelParamDualPtr param
Definition: ModelDual.h:229
ModelDual(const ModelDual &_model)
Definition: ModelDual.cpp:86
virtual ~ModelDual()
Frees any memory allocated by, and owned by the ModelDual.
Definition: ModelDual.cpp:106
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
To be implemented by derived classes; the parent classes fields will be updated by the derived class ...
Definition: ModelDual.cpp:902
virtual kkint32 MemoryConsumedEstimated() const
Definition: ModelDual.cpp:120
Used for logging messages.
Definition: RunLog.h:49
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
KKStr trainer1StatusMsg
Definition: ModelDual.h:226
virtual kkint32 NumOfSupportVectors() const
Definition: ModelDual.cpp:847
Maintains a list of MLClass instances.
Definition: MLClass.h:233
virtual void ProbabilitiesByClass(FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &log)
Definition: ModelDual.cpp:676
FeatureVectorList * FeatureVectorListPtr
Definition: Model.h:46
Represents a Feature Vector of a single example, labeled or unlabeled.
Definition: FeatureVector.h:59
virtual void ProbabilitiesByClass(FeatureVectorPtr _example, const MLClassList &_mlClasses, double *_probabilities, RunLog &_log)
Derives predicted probabilities by class.
Definition: ModelDual.cpp:741
MLClassPtr ReconcilePredictions(MLClassPtr pred1, MLClassPtr pred2, RunLog &log)
Definition: ModelDual.cpp:320
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220
FactoryFVProducer * FactoryFVProducerPtr
Definition: Model.h:75
ModelDual(const KKStr &_name, const ModelParamDual &_param, FactoryFVProducerPtr _factoryFVProducer)
Definition: ModelDual.cpp:67
ModelDual(FactoryFVProducerPtr _factoryFVProducer)
Definition: ModelDual.cpp:54
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163
virtual void TrainModel(FeatureVectorListPtr _trainExamples, bool _alreadyNormalized, bool _takeOwnership, VolConstBool &_cancelFlag, RunLog &_log)
Performs operations such as FeatureEncoding, and Normalization. The actual training of models occurs ...
Definition: ModelDual.cpp:177