KSquare Utilities
KKMLL::ModelDual Class Reference

Will implement the Dual Classifier Model. More...

#include <ModelDual.h>

+ Inheritance diagram for KKMLL::ModelDual:

Public Types

typedef ModelDualModelDualPtr
 
- Public Types inherited from KKMLL::Model
typedef ModelModelPtr
 
enum  ModelTypes : int {
  ModelTypes::Null = 0, ModelTypes::OldSVM = 1, ModelTypes::SvmBase = 2, ModelTypes::KNN = 3,
  ModelTypes::UsfCasCor = 4, ModelTypes::Dual = 5
}
 

Public Member Functions

 ModelDual ()
 
 ModelDual (FactoryFVProducerPtr _factoryFVProducer)
 
 ModelDual (const KKStr &_name, const ModelParamDual &_param, FactoryFVProducerPtr _factoryFVProducer)
 
 ModelDual (const ModelDual &_model)
 
virtual ~ModelDual ()
 Frees any memory allocated by, and owned by the ModelDual. More...
 
virtual KKStr Description () const
 
virtual ModelDualPtr Duplicate () const
 
virtual kkint32 MemoryConsumedEstimated () const
 
virtual ModelTypes ModelType () const
 
virtual kkint32 NumOfSupportVectors () const
 
ModelParamDualPtr Param ()
 
virtual MLClassPtr Predict (FeatureVectorPtr example, RunLog &log)
 
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)
 
virtual ClassProbListPtr ProbabilitiesByClass (FeatureVectorPtr example, RunLog &log)
 
virtual void ProbabilitiesByClass (FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &log)
 
virtual void ProbabilitiesByClass (FeatureVectorPtr _example, const MLClassList &_mlClasses, double *_probabilities, RunLog &_log)
 Derives predicted probabilities by class. More...
 
virtual void ProbabilitiesByClassDual (FeatureVectorPtr example, KKStr &classifier1Desc, KKStr &classifier2Desc, ClassProbListPtr &classifier1Results, ClassProbListPtr &classifier2Results, RunLog &log)
 Only applied to ModelDual classifier. More...
 
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 calling ReadXMLModelToken. More...
 
virtual void RetrieveCrossProbTable (MLClassList &classes, double **crossProbTable, RunLog &log)
 
TrainingProcess2Ptr Trainer1 ()
 
TrainingProcess2Ptr Trainer2 ()
 
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 in the specific derived implementation of 'Model'. More...
 
virtual void WriteXML (const KKStr &varName, ostream &o) const
 
- Public Member Functions inherited from KKMLL::Model
 Model ()
 
 Model (FactoryFVProducerPtr _factoryFVProducer)
 Use this when you are planning on creating a empty model without parameters. More...
 
 Model (const KKStr &_name, const ModelParam &_param, FactoryFVProducerPtr _factoryFVProducer)
 Construct a instance of 'Model' using the parameters specified in '_param'. More...
 
 Model (const Model &_madel)
 Copy Constructor. More...
 
virtual ~Model ()
 Frees any memory allocated by, and owned by the Model. More...
 
bool AlreadyNormalized () const
 
const FeatureEncoder2Encoder () const
 
FactoryFVProducerPtr FactoryFVProducer () const
 
virtual FeatureNumListConstPtr GetFeatureNums () const
 
MLClassListPtr MLClasses () const
 
MLClassListPtr MLClassesNewInstance () const
 
virtual KKStr ModelTypeStr () const
 
const KKStrName () const
 
void Name (const KKStr &_name)
 
virtual bool NormalizeNominalAttributes () const
 
ModelParamPtr Param () const
 
virtual void PredictRaw (FeatureVectorPtr example, MLClassPtr &predClass, double &dist)
 
virtual FeatureVectorPtr PrepExampleForPrediction (FeatureVectorPtr fv, bool &newExampleCreated)
 Every prediction method in every class that is inherited from this class should call this method before performing there prediction. Such things as Normalization and Feature Encoding. More...
 
const KKStrRootFileName () const
 
void RootFileName (const KKStr &_rootFileName)
 
virtual FeatureNumListConstPtr SelectedFeatures () const
 
const KKB::DateTimeTimeSaved () const
 
double TrainingTime () const
 
void TrainingTimeEnd ()
 Derived classes call this method to stop the clock for 'trainingTime'. More...
 
void TrainingTimeStart ()
 Derived classes call this method to start the clock for 'trainingTime'. More...
 
double TrianingPrepTime () const
 
bool ValidModel () const
 
void WriteModelXMLFields (std::ostream &o) const
 The "WriteXML" method in Derived classes call this method to include the parents classes fields in the XML data. More...
 
virtual void WriteXML (const KKStr &varName, std::ostream &o) const =0
 

Protected Member Functions

void DeleteExistingClassifiers ()
 
MLClassPtr ReconcilePredictions (MLClassPtr pred1, MLClassPtr pred2, RunLog &log)
 
void ReconcileProbAndVotes (Classifier2Ptr classifier, MLClassPtr predClass, FeatureVectorPtr encodedExample, double &predClassProb, kkint32 &predClassVotes)
 
- Protected Member Functions inherited from KKMLL::Model
void AddErrorMsg (const KKStr &errMsg, kkint32 lineNum)
 
void AllocatePredictionVariables ()
 
void DeAllocateSpace ()
 
void NormalizeProbabilitiesWithAMinumum (kkint32 numClasses, double *probabilities, double minProbability)
 
void ReadXMLModelPost (RunLog &log)
 
XmlTokenPtr ReadXMLModelToken (XmlTokenPtr t, RunLog &log)
 Will process any tokens that belong to 'ModelParam' and return NULL ones that are not will be passed back. More...
 
void ReduceTrainExamples (RunLog &log)
 Reduces the Training Images down to the size dictated by the 'examplesPerClass' parameter. More...
 

Protected Attributes

Classifier2Ptr classifier1
 
Classifier2Ptr classifier2
 
TrainingConfiguration2Ptr config1
 
TrainingConfiguration2Ptr config2
 
ModelParamDualPtr param
 
TrainingProcess2Ptr trainer1
 
KKStr trainer1StatusMsg
 
TrainingProcess2Ptr trainer2
 
KKStr trainer2StatusMsg
 
- Protected Attributes inherited from KKMLL::Model
bool alreadyNormalized
 
MLClassListPtr classes
 
MLClassIndexListPtr classesIndex
 
double * classProbs
 
double ** crossClassProbTable
 
kkint32 crossClassProbTableSize
 
FeatureEncoder2Ptr encoder
 
VectorKKStr errors
 
FactoryFVProducerPtr factoryFVProducer
 
FileDescPtr fileDesc
 
NormalizationParmsPtr normParms
 
kkint32 numOfClasses
 
ModelParamPtr param
 
KKStr rootFileName
 
FeatureVectorListPtr trainExamples
 
bool validModel
 
kkint32votes
 
bool weOwnTrainExamples
 

Additional Inherited Members

- Static Public Member Functions inherited from KKMLL::Model
static ModelPtr CreateAModel (ModelTypes _modelType, const KKStr &_name, const ModelParam &_param, FactoryFVProducerPtr _factoryFVProducer, VolConstBool &_cancelFlag, RunLog &_log)
 A factory method that will instantiate the appropriate class of training model based off '_modelType'. More...
 
static ModelTypes ModelTypeFromStr (const KKStr &_modelingTypeStr)
 
static KKStr ModelTypeToStr (ModelTypes _modelingType)
 

Detailed Description

Will implement the Dual Classifier Model.

This model will actually load two different training models specified in the parameters field. It will utilize both classifiers for to make a prediction. Unknown examples will be submitted to both classifiers. The returned class will be the common part of the class hierarchy of the two predictions. If there is nothing in common between the two predictions then it will return the "Other class.

Definition at line 66 of file ModelDual.h.

Member Typedef Documentation

Definition at line 70 of file ModelDual.h.

Constructor & Destructor Documentation

ModelDual::ModelDual ( )

Definition at line 39 of file ModelDual.cpp.

References classifier1, classifier2, config1, config2, KKMLL::Model::Model(), param, trainer1, and trainer2.

39  :
40  Model (),
41  param (NULL),
42  config1 (NULL),
43  config2 (NULL),
44  trainer1 (NULL),
45  trainer2 (NULL),
46  classifier1 (NULL),
47  classifier2 (NULL)
48 {
49 }
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
TrainingConfiguration2Ptr config2
Definition: ModelDual.h:218
Classifier2Ptr classifier1
Definition: ModelDual.h:223
Classifier2Ptr classifier2
Definition: ModelDual.h:224
TrainingConfiguration2Ptr config1
Definition: ModelDual.h:217
ModelParamDualPtr param
Definition: ModelDual.h:229
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220
ModelDual::ModelDual ( FactoryFVProducerPtr  _factoryFVProducer)

Definition at line 54 of file ModelDual.cpp.

References classifier1, classifier2, config1, config2, KKMLL::Model::Model(), param, trainer1, and trainer2.

54  :
55  Model (_factoryFVProducer),
56  param (NULL),
57  config1 (NULL),
58  config2 (NULL),
59  trainer1 (NULL),
60  trainer2 (NULL),
61  classifier1 (NULL),
62  classifier2 (NULL)
63 {
64 }
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
TrainingConfiguration2Ptr config2
Definition: ModelDual.h:218
Classifier2Ptr classifier1
Definition: ModelDual.h:223
Classifier2Ptr classifier2
Definition: ModelDual.h:224
TrainingConfiguration2Ptr config1
Definition: ModelDual.h:217
ModelParamDualPtr param
Definition: ModelDual.h:229
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220
ModelDual::ModelDual ( const KKStr _name,
const ModelParamDual _param,
FactoryFVProducerPtr  _factoryFVProducer 
)

Definition at line 67 of file ModelDual.cpp.

References classifier1, classifier2, config1, config2, KKMLL::Model::Model(), param, KKMLL::Model::param, trainer1, and trainer2.

Referenced by KKMLL::Model::CreateAModel().

70  :
71  Model (_name, _param, _factoryFVProducer),
72  param (NULL),
73  config1 (NULL),
74  config2 (NULL),
75  trainer1 (NULL),
76  trainer2 (NULL),
77  classifier1 (NULL),
78  classifier2 (NULL)
79 {
80  param = dynamic_cast<ModelParamDualPtr> (Model::param);
81 }
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
TrainingConfiguration2Ptr config2
Definition: ModelDual.h:218
ModelParamPtr param
Definition: Model.h:412
Classifier2Ptr classifier1
Definition: ModelDual.h:223
Classifier2Ptr classifier2
Definition: ModelDual.h:224
TrainingConfiguration2Ptr config1
Definition: ModelDual.h:217
ModelParamDualPtr param
Definition: ModelDual.h:229
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220
ModelDual::ModelDual ( const ModelDual _model)

Definition at line 86 of file ModelDual.cpp.

References classifier1, classifier2, config1, config2, KKMLL::Model::Model(), param, KKMLL::Model::param, trainer1, and trainer2.

Referenced by Duplicate().

86  :
87  Model (_model),
88  param (NULL),
89  config1 (NULL),
90  config2 (NULL),
91  trainer1 (NULL),
92  trainer2 (NULL),
93  classifier1 (NULL),
94  classifier2 (NULL)
95 {
96  param = dynamic_cast<ModelParamDualPtr> (Model::param);
97 }
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
TrainingConfiguration2Ptr config2
Definition: ModelDual.h:218
ModelParamPtr param
Definition: Model.h:412
Classifier2Ptr classifier1
Definition: ModelDual.h:223
Classifier2Ptr classifier2
Definition: ModelDual.h:224
TrainingConfiguration2Ptr config1
Definition: ModelDual.h:217
ModelParamDualPtr param
Definition: ModelDual.h:229
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220
ModelDual::~ModelDual ( )
virtual

Frees any memory allocated by, and owned by the ModelDual.

Definition at line 106 of file ModelDual.cpp.

References classifier1, classifier2, config1, config2, DeleteExistingClassifiers(), trainer1, and trainer2.

107 {
108  // The base class owns param, so we do not delete it.
110  delete config1; config1 = NULL;
111  delete config2; config2 = NULL;
112  delete trainer1; trainer1 = NULL;
113  delete trainer2; trainer2 = NULL;
114  delete classifier1; classifier1 = NULL;
115  delete classifier2; classifier2 = NULL;
116 
117 }
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
TrainingConfiguration2Ptr config2
Definition: ModelDual.h:218
Classifier2Ptr classifier1
Definition: ModelDual.h:223
void DeleteExistingClassifiers()
Definition: ModelDual.cpp:164
Classifier2Ptr classifier2
Definition: ModelDual.h:224
TrainingConfiguration2Ptr config1
Definition: ModelDual.h:217
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220

Member Function Documentation

void ModelDual::DeleteExistingClassifiers ( )
protected

Definition at line 164 of file ModelDual.cpp.

References classifier1, classifier2, config1, config2, trainer1, and trainer2.

Referenced by TrainModel(), and ~ModelDual().

165 {
166  delete config1; config1 = NULL;
167  delete config2; config2 = NULL;
168  delete trainer1; trainer1 = NULL;
169  delete trainer2; trainer2 = NULL;
170  delete classifier1; classifier1 = NULL;
171  delete classifier2; classifier2 = NULL;
172 } /* DeleteExistingClassifiers */
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
TrainingConfiguration2Ptr config2
Definition: ModelDual.h:218
Classifier2Ptr classifier1
Definition: ModelDual.h:223
Classifier2Ptr classifier2
Definition: ModelDual.h:224
TrainingConfiguration2Ptr config1
Definition: ModelDual.h:217
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220
KKStr ModelDual::Description ( ) const
virtual

Return short user readable description of model.

Reimplemented from KKMLL::Model.

Definition at line 133 of file ModelDual.cpp.

References KKB::KKStr::Concat(), KKMLL::TrainingProcess2::ConfigFileName(), KKMLL::TrainingProcess2::ModelTypeStr(), KKMLL::Model::Name(), KKB::KKStr::operator+(), KKB::operator+(), KKB::osGetRootName(), trainer1, and trainer2.

134 {
135  KKStr result = "Dual(" + Name () + ")";
136  if (trainer1)
137  result << " " << trainer1->ModelTypeStr () + "(" + osGetRootName (trainer1->ConfigFileName ()) + ")";
138  else if (config1)
139  result << " " << config1->ModelTypeStr () + "(" + osGetRootName (config1->FileName ()) + ")";
140 
141  if (trainer2)
142  result << " " << trainer2->ModelTypeStr () + "(" + osGetRootName (trainer2->ConfigFileName ()) + ")";
143  else if (config1)
144  result << " " << config2->ModelTypeStr () + "(" + osGetRootName (config2->FileName ()) + ")";
145 
146  return result;
147 }
const KKStr & Name() const
Definition: Model.h:178
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
TrainingConfiguration2Ptr config2
Definition: ModelDual.h:218
const KKB::KKStr & FileName() const
Definition: Configuration.h:96
TrainingConfiguration2Ptr config1
Definition: ModelDual.h:217
const KKStr & ConfigFileName() const
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220
KKStr osGetRootName(const KKStr &fullFileName)
ModelDualPtr ModelDual::Duplicate ( ) const
virtual

Implements KKMLL::Model.

Definition at line 151 of file ModelDual.cpp.

References ModelDual().

152 {
153  return new ModelDual (*this);
154 }
kkint32 ModelDual::MemoryConsumedEstimated ( ) const
virtual

Reimplemented from KKMLL::Model.

Definition at line 120 of file ModelDual.cpp.

References classifier1, classifier2, KKMLL::Classifier2::MemoryConsumedEstimated(), KKMLL::Model::MemoryConsumedEstimated(), KKMLL::TrainingProcess2::MemoryConsumedEstimated(), trainer1, and trainer2.

121 {
122  kkint32 memoryConsumedEstimated = Model::MemoryConsumedEstimated ();
123  if (trainer1) memoryConsumedEstimated += trainer1->MemoryConsumedEstimated ();
124  if (trainer2) memoryConsumedEstimated += trainer2->MemoryConsumedEstimated ();
125  if (classifier1) memoryConsumedEstimated += classifier1->MemoryConsumedEstimated ();
126  if (classifier2) memoryConsumedEstimated += classifier2->MemoryConsumedEstimated ();
127  return memoryConsumedEstimated;
128 }
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
__int32 kkint32
Definition: KKBaseTypes.h:88
Classifier2Ptr classifier1
Definition: ModelDual.h:223
virtual kkint32 MemoryConsumedEstimated() const
virtual kkint32 MemoryConsumedEstimated() const
Definition: Model.cpp:208
kkint32 MemoryConsumedEstimated() const
Classifier2Ptr classifier2
Definition: ModelDual.h:224
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220
virtual ModelTypes KKMLL::ModelDual::ModelType ( ) const
inlinevirtual

Implements KKMLL::Model.

Definition at line 93 of file ModelDual.h.

References KKMLL::Model::Dual.

kkint32 ModelDual::NumOfSupportVectors ( ) const
virtual

Definition at line 847 of file ModelDual.cpp.

References KKMLL::TrainingProcess2::NumOfSupportVectors(), trainer1, and trainer2.

848 {
849  float totalSVs = 0.0;
850  kkuint32 count = 0;
851 
852  if (trainer1)
853  {
854  totalSVs += (float)(trainer1->NumOfSupportVectors ());
855  ++count;
856  }
857 
858  if (trainer2)
859  {
860  totalSVs += (float)(trainer2->NumOfSupportVectors ());
861  ++count;
862  }
863 
864  if (count > 0)
865  return (kkint32)(0.5f + totalSVs / (float)count);
866  else
867  return 0;
868 
869 } /* NumOfSupportVectors */
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
kkint32 NumOfSupportVectors() const
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220
ModelParamDualPtr ModelDual::Param ( )

Definition at line 158 of file ModelDual.cpp.

References param.

159 {
160  return param;
161 }
ModelParamDualPtr param
Definition: ModelDual.h:229
MLClassPtr ModelDual::Predict ( FeatureVectorPtr  example,
RunLog log 
)
virtual
Todo:
ModelDual::Predict Make sure that the call to the first classifier does not modify the encodedExample.

Implements KKMLL::Model.

Definition at line 430 of file ModelDual.cpp.

References classifier1, classifier2, KKMLL::Classifier2::ClassifyAExample(), KKMLL::Model::PrepExampleForPrediction(), and ReconcilePredictions().

433 {
434  if ((!classifier1) || (!classifier2))
435  {
436  log.Level (-1) << endl << endl << "ModelDual::Predict ***ERROR*** Both Classifiers are not defined." << endl << endl;
437  return NULL;
438  }
439 
440  bool newExampleCreated = false;
441 
442  MLClassPtr pred1 = NULL;
443  MLClassPtr pred2 = NULL;
444 
445  /**@todo ModelDual::Predict Make sure that the call to the first classifier does not modify the encodedExample. */
446  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
447  pred1 = classifier1->ClassifyAExample (*encodedExample);
448  pred2 = classifier2->ClassifyAExample (*encodedExample);
449 
450  if (newExampleCreated)
451  {
452  delete encodedExample;
453  encodedExample = NULL;
454  }
455 
456  return ReconcilePredictions (pred1, pred2, log);
457 } /* Predict */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
Classifier2Ptr classifier1
Definition: ModelDual.h:223
RunLog & Level(kkint32 _level)
Definition: RunLog.cpp:220
virtual FeatureVectorPtr PrepExampleForPrediction(FeatureVectorPtr fv, bool &newExampleCreated)
Every prediction method in every class that is inherited from this class should call this method befo...
Definition: Model.cpp:574
Classifier2Ptr classifier2
Definition: ModelDual.h:224
Represents a Feature Vector of a single example, labeled or unlabeled.
Definition: FeatureVector.h:59
MLClassPtr ReconcilePredictions(MLClassPtr pred1, MLClassPtr pred2, RunLog &log)
Definition: ModelDual.cpp:320
MLClassPtr ClassifyAExample(FeatureVector &example)
void ModelDual::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 
)
virtual
Todo:
Make sure that 'classifier1->ClassifyAExample' does not modify the feature data.

Implements KKMLL::Model.

Definition at line 462 of file ModelDual.cpp.

References classifier1, classifier2, KKMLL::Classifier2::ClassifyAExample(), KKMLL::Model::PrepExampleForPrediction(), ReconcilePredictions(), and ReconcileProbAndVotes().

476 {
477  if ((!classifier1) || (!classifier2))
478  {
479  predClass1 = predClass2 = NULL;
480  log.Level (-1) << endl << endl << "ModelDual::Predict ***ERROR*** Both Classifiers are not defined." << endl << endl;
481  return;
482  }
483 
484  MLClassPtr predClass1C1 = NULL;
485  MLClassPtr predClass2C1 = NULL;
486  kkint32 predClass1VotesC1 = 0;
487  kkint32 predClass2VotesC1 = 0;
488  double probOfKnownClassC1 = 0.0;
489  double predClass1ProbC1 = 0.0;
490  double predClass2ProbC1 = 0.0;
491  kkint32 numOfWinnersC1 = 0;
492  bool knownClassOneOfTheWinnersC1 = false;
493  double breakTieC1 = 0.0;
494 
495  MLClassPtr predClass1C2 = NULL;
496  MLClassPtr predClass2C2 = NULL;
497  kkint32 predClass1VotesC2 = 0;
498  kkint32 predClass2VotesC2 = 0;
499  double probOfKnownClassC2 = 0.0;
500  double predClass1ProbC2 = 0.0;
501  double predClass2ProbC2 = 0.0;
502  kkint32 numOfWinnersC2 = 0;
503  bool knownClassOneOfTheWinnersC2 = false;
504  double breakTieC2 = 0.0;
505 
506  bool newExampleCreated = false;
507  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
508 
509  /**@todo Make sure that 'classifier1->ClassifyAExample' does not modify the feature data. */
510 
511  classifier1->ClassifyAExample (*encodedExample,
512  predClass1C1, predClass2C1,
513  predClass1VotesC1, predClass2VotesC1,
514  probOfKnownClassC1,
515  predClass1ProbC1, predClass2ProbC1,
516  numOfWinnersC1,
517  breakTieC1
518  );
519 
520  classifier2->ClassifyAExample (*encodedExample,
521  predClass1C2, predClass2C2,
522  predClass1VotesC2, predClass2VotesC2,
523  probOfKnownClassC2,
524  predClass1ProbC2, predClass2ProbC2,
525  numOfWinnersC2,
526  breakTieC2
527  );
528 
529  predClass1 = ReconcilePredictions (predClass1C1, predClass1C2, log);
530  predClass2 = ReconcilePredictions (predClass2C1, predClass2C2, log);
531 
532  if (predClass1C1 != predClass1C2)
533  {
535  predClass1,
536  encodedExample,
537  predClass1ProbC1,
538  predClass1VotesC1
539  );
540 
542  predClass1,
543  encodedExample,
544  predClass1ProbC2,
545  predClass1VotesC2
546  );
547  }
548 
549 
550  if (predClass2C1 != predClass2C2)
551  {
553  predClass2,
554  encodedExample,
555  predClass2ProbC1,
556  predClass2VotesC1
557  );
558 
560  predClass2,
561  encodedExample,
562  predClass2ProbC2,
563  predClass2VotesC2
564  );
565  }
566 
567  predClass1Prob = (predClass1ProbC1 + predClass1ProbC2) / 2.0;
568  predClass2Prob = (predClass2ProbC1 + predClass2ProbC2) / 2.0;
569  predClass1Votes = (kkint32)((0.5f + predClass1VotesC1 + predClass1VotesC2) / 2.0f);
570  predClass2Votes = (kkint32)((0.5f + predClass2VotesC1 + predClass2VotesC2) / 2.0f);
571 
572 
573  probOfKnownClass = (probOfKnownClassC1 + probOfKnownClassC2) / 2.0;
574  breakTie = (breakTieC1 + breakTieC2) / 2.0;
575  numOfWinners = (numOfWinnersC1 + numOfWinnersC2) / 2;
576  knownClassOneOfTheWinners = (knownClass == predClass1C1) || (knownClass == predClass1C2);
577 
578  if (newExampleCreated)
579  {
580  delete encodedExample;
581  encodedExample = NULL;
582  }
583 
584  return;
585 } /* Predict */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
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
Classifier2Ptr classifier1
Definition: ModelDual.h:223
RunLog & Level(kkint32 _level)
Definition: RunLog.cpp:220
virtual FeatureVectorPtr PrepExampleForPrediction(FeatureVectorPtr fv, bool &newExampleCreated)
Every prediction method in every class that is inherited from this class should call this method befo...
Definition: Model.cpp:574
Classifier2Ptr classifier2
Definition: ModelDual.h:224
Represents a Feature Vector of a single example, labeled or unlabeled.
Definition: FeatureVector.h:59
MLClassPtr ReconcilePredictions(MLClassPtr pred1, MLClassPtr pred2, RunLog &log)
Definition: ModelDual.cpp:320
MLClassPtr ClassifyAExample(FeatureVector &example)
ClassProbListPtr ModelDual::ProbabilitiesByClass ( FeatureVectorPtr  example,
RunLog log 
)
virtual

Implements KKMLL::Model.

Definition at line 591 of file ModelDual.cpp.

References classifier1, classifier2, KKMLL::ClassProbList::MergeIn(), KKMLL::ClassProbList::NormalizeToOne(), and KKMLL::Classifier2::ProbabilitiesByClass().

594 {
595  if ((!classifier1) || (!classifier2))
596  {
597  log.Level (-1) << endl << endl << "ModelDual::ProbabilitiesByClass ***ERROR*** Both Classifiers are not defined." << endl << endl;
598  return NULL;
599  }
600 
601  ClassProbListPtr predictions1 = classifier1->ProbabilitiesByClass (example);
602  ClassProbListPtr predictions2 = classifier1->ProbabilitiesByClass (example);
603 
604  if (predictions1 == NULL)
605  return predictions2;
606 
607  else if (predictions2 == NULL)
608  return predictions1;
609 
611  for (idx = predictions2->begin (); idx != predictions2->end (); ++idx)
612  {
613  ClassProbPtr cp = *idx;
614  predictions1->MergeIn (cp);
615  }
616  delete predictions2;
617  predictions2 = NULL;
618 
619  predictions1->NormalizeToOne ();
620 
621  return predictions1;
622 } /* ProbabilitiesByClass */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
std::vector< ClassProb * >::iterator iterator
Definition: KKQueue.h:88
void MergeIn(const ClassProbPtr cp)
Adds the Prediction in &#39;cp&#39; into this list.
Definition: ClassProb.cpp:268
Classifier2Ptr classifier1
Definition: ModelDual.h:223
RunLog & Level(kkint32 _level)
Definition: RunLog.cpp:220
Used to record probability for a specified class; and a list of classes.
Definition: ClassProb.h:25
Classifier2Ptr classifier2
Definition: ModelDual.h:224
void NormalizeToOne()
Will normalize the list of predictions such that the total probability will equal 1...
Definition: ClassProb.cpp:336
void ProbabilitiesByClass(const MLClassList &classes, FeatureVectorPtr example, kkint32 *votes, double *probabilities)
For a given feature vector return back the probabilities and votes for each class.
void ModelDual::ProbabilitiesByClass ( FeatureVectorPtr  example,
const MLClassList _mlClasses,
kkint32 _votes,
double *  _probabilities,
RunLog log 
)
virtual

Implements KKMLL::Model.

Definition at line 676 of file ModelDual.cpp.

References KKMLL::ClassProbList::AddIn(), classifier1, classifier2, KKMLL::ClassProbList::LookUp(), KKMLL::ClassProbList::NormalizeToOne(), KKMLL::Classifier2::ProbabilitiesByClass(), KKMLL::ClassProb::probability, and KKMLL::ClassProb::votes.

682 {
683  kkint32 numClasses = _mlClasses.QueueSize ();
684  for (kkint32 idx = 0; idx < numClasses; idx++)
685  {
686  _probabilities[idx] = 0.0;
687  _votes [idx] = 0;
688  }
689 
690  if ((!classifier1) || (!classifier2))
691  {
692  _log.Level (-1) << endl << "ModelDual::ProbabilitiesByClass ***ERROR*** Both Classifiers are not defined." << endl << endl;
693  return;
694  }
695 
696  ClassProbListPtr predictions1 = classifier1->ProbabilitiesByClass (example);
697  ClassProbListPtr predictions2 = classifier2->ProbabilitiesByClass (example);
698 
699  if (predictions1 == NULL)
700  {
701  predictions1 = predictions2;
702  predictions2 = NULL;
703  }
704 
705  else if (predictions2 == NULL)
706  {
707  // 'predictions1' already has the total of both.
708  }
709 
710  else
711  {
712  predictions1->AddIn (predictions2);
713  delete predictions2;
714  predictions2 = NULL;
715  }
716 
717  kkuint32 x = 0;
718  predictions1->NormalizeToOne ();
720  for (idx2 = _mlClasses.begin (), x = 0; idx2 != _mlClasses.end (); ++idx2, ++x)
721  {
722  MLClassPtr c =*idx2;
723  ClassProbPtr cp = predictions1->LookUp (c);
724  if (cp)
725  {
726  _votes [x] = (kkint32)(0.5f + cp->votes);
727  _probabilities[x] = cp->probability;
728  }
729  }
730 
731  delete predictions1;
732  predictions1 = NULL;
733 
734  return;
735 } /* ProbabilitiesByClass */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
std::vector< MLClass * >::const_iterator const_iterator
Definition: KKQueue.h:89
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
Classifier2Ptr classifier1
Definition: ModelDual.h:223
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
const ClassProbPtr LookUp(MLClassPtr targetClass) const
Definition: ClassProb.cpp:160
Used to record probability for a specified class; and a list of classes.
Definition: ClassProb.h:25
double probability
Definition: ClassProb.h:36
Classifier2Ptr classifier2
Definition: ModelDual.h:224
void AddIn(const ClassProbListPtr otherPredictions)
Adds the contents of &#39;otherPredictions&#39; to this list.
Definition: ClassProb.cpp:246
kkint32 QueueSize() const
Definition: KKQueue.h:313
void NormalizeToOne()
Will normalize the list of predictions such that the total probability will equal 1...
Definition: ClassProb.cpp:336
void ProbabilitiesByClass(const MLClassList &classes, FeatureVectorPtr example, kkint32 *votes, double *probabilities)
For a given feature vector return back the probabilities and votes for each class.
void ModelDual::ProbabilitiesByClass ( FeatureVectorPtr  _example,
const MLClassList _mlClasses,
double *  _probabilities,
RunLog _log 
)
virtual

Derives predicted probabilities by class.

Will get the probabilities assigned to each class by the classifier. The '_mlClasses' parameter dictates the order of the classes. That is the probabilities for any given index in '_probabilities' will be for the class specified in the same index in '_mlClasses'.

Parameters
[in]_exampleFeatureVector object to calculate predicted probabilities for.
[in]_mlClassesList of classes that caller is aware of. This should be the same list that was used when constructing this Model object. The list must be the same but not necessarily in the same order as when Model was 1st constructed.
[out]_probabilitiesAn array that must be as big as the number of classes as in mlClasses. The probability of class in mlClasses[x] will be returned in probabilities[x].

Implements KKMLL::Model.

Definition at line 741 of file ModelDual.cpp.

References KKMLL::ClassProbList::AddIn(), classifier1, classifier2, KKMLL::ClassProbList::LookUp(), KKMLL::ClassProbList::NormalizeToOne(), KKMLL::Classifier2::ProbabilitiesByClass(), and KKMLL::ClassProb::probability.

746 {
747  kkint32 numClasses = _mlClasses.QueueSize ();
748  for (kkint32 idx = 0; idx < numClasses; idx++)
749  {
750  _probabilities[idx] = 0.0;
751  }
752 
753  if ((!classifier1) || (!classifier2))
754  {
755  _log.Level (-1) << endl << "ModelDual::ProbabilitiesByClass ***ERROR*** Both Classifiers are not defined." << endl << endl;
756  return;
757  }
758 
759  ClassProbListPtr predictions1 = classifier1->ProbabilitiesByClass (_example);
760  ClassProbListPtr predictions2 = classifier2->ProbabilitiesByClass (_example);
761 
762  if (predictions1 == NULL)
763  {
764  predictions1 = predictions2;
765  predictions2 = NULL;
766  }
767 
768  else if (predictions2 == NULL)
769  {
770  // 'predictions1' already has the total of both.
771  }
772 
773  else
774  {
775  predictions1->AddIn (predictions2);
776  delete predictions2;
777  predictions2 = NULL;
778  }
779 
780  kkuint32 x = 0;
781  predictions1->NormalizeToOne ();
783  for (idx2 = _mlClasses.begin (), x = 0; idx2 != _mlClasses.end (); ++idx2, ++x)
784  {
785  MLClassPtr c =*idx2;
786  ClassProbPtr cp = predictions1->LookUp (c);
787  if (cp)
788  _probabilities[x] = cp->probability;
789  }
790 
791  delete predictions1;
792  predictions1 = NULL;
793 
794  return;
795 } /* ProbabilitiesByClass */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
std::vector< MLClass * >::const_iterator const_iterator
Definition: KKQueue.h:89
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
Classifier2Ptr classifier1
Definition: ModelDual.h:223
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
RunLog & Level(kkint32 _level)
Definition: RunLog.cpp:220
const ClassProbPtr LookUp(MLClassPtr targetClass) const
Definition: ClassProb.cpp:160
Used to record probability for a specified class; and a list of classes.
Definition: ClassProb.h:25
double probability
Definition: ClassProb.h:36
Classifier2Ptr classifier2
Definition: ModelDual.h:224
void AddIn(const ClassProbListPtr otherPredictions)
Adds the contents of &#39;otherPredictions&#39; to this list.
Definition: ClassProb.cpp:246
kkint32 QueueSize() const
Definition: KKQueue.h:313
void NormalizeToOne()
Will normalize the list of predictions such that the total probability will equal 1...
Definition: ClassProb.cpp:336
void ProbabilitiesByClass(const MLClassList &classes, FeatureVectorPtr example, kkint32 *votes, double *probabilities)
For a given feature vector return back the probabilities and votes for each class.
void ModelDual::ProbabilitiesByClassDual ( FeatureVectorPtr  example,
KKStr classifier1Desc,
KKStr classifier2Desc,
ClassProbListPtr classifier1Results,
ClassProbListPtr classifier2Results,
RunLog log 
)
virtual

Only applied to ModelDual classifier.

Reimplemented from KKMLL::Model.

Definition at line 628 of file ModelDual.cpp.

References classifier1, classifier2, KKMLL::TrainingProcess2::ModelDescription(), KKB::KKStr::operator=(), KKMLL::Model::PrepExampleForPrediction(), KKMLL::Classifier2::ProbabilitiesByClass(), trainer1, and trainer2.

635 {
636  delete classifier1Results; classifier1Results = NULL;
637  delete classifier2Results; classifier2Results = NULL;
638 
639  if ((!classifier1) || (!classifier2))
640  {
641  log.Level (-1) << endl << endl << "ModelDual::ProbabilitiesByClassDual ***ERROR*** Both Classifiers are not defined." << endl << endl;
642  return;
643  }
644 
645  classifier1Desc = trainer1->ModelDescription ();
646  classifier2Desc = trainer2->ModelDescription ();
647 
648  {
649  bool newExampleCreated = false;
650  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
651  classifier1Results = classifier1->ProbabilitiesByClass (encodedExample);
652  if (newExampleCreated)
653  {
654  delete encodedExample;
655  encodedExample = NULL;
656  }
657  }
658 
659  {
660  bool newExampleCreated = false;
661  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
662  classifier2Results = classifier2->ProbabilitiesByClass (encodedExample);
663  if (newExampleCreated)
664  {
665  delete encodedExample;
666  encodedExample = NULL;
667  }
668  }
669 
670  return;
671 } /* ProbabilitiesByClass */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
Classifier2Ptr classifier1
Definition: ModelDual.h:223
RunLog & Level(kkint32 _level)
Definition: RunLog.cpp:220
virtual FeatureVectorPtr PrepExampleForPrediction(FeatureVectorPtr fv, bool &newExampleCreated)
Every prediction method in every class that is inherited from this class should call this method befo...
Definition: Model.cpp:574
Classifier2Ptr classifier2
Definition: ModelDual.h:224
void ProbabilitiesByClass(const MLClassList &classes, FeatureVectorPtr example, kkint32 *votes, double *probabilities)
For a given feature vector return back the probabilities and votes for each class.
Represents a Feature Vector of a single example, labeled or unlabeled.
Definition: FeatureVector.h:59
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220
void ModelDual::ReadXML ( XmlStream s,
XmlTagConstPtr  tag,
VolConstBool cancelFlag,
RunLog log 
)
virtual

To be implemented by derived classes; the parent classes fields will be updated by the derived class calling ReadXMLModelToken.

Todo:
I need the Config1 configuration read from the XML file to be correct; but to deal with this problem for now; will reload the original.
Todo:
I need the Config1 configuration read from the XML file to be correct; but to deal with this problem for now; will reload the original.

Implements KKMLL::Model.

Definition at line 902 of file ModelDual.cpp.

References KKMLL::Model::AddErrorMsg(), classifier1, KKMLL::Classifier2::Classifier2(), classifier2, KKB::KKStr::Concat(), config1, config2, KKB::XmlStream::GetNextToken(), KKB::KKStr::KKStr(), KKMLL::ModelParam::ModelParamTypeStr(), param, KKMLL::Model::param, KKMLL::Model::ReadXMLModelPost(), KKMLL::Model::ReadXMLModelToken(), KKB::XmlToken::SectionName(), KKB::XmlElement::SectionName(), KKB::XmlToken::tokElement, KKB::XmlToken::TokenType(), trainer1, trainer2, KKMLL::Model::validModel, KKB::XmlToken::VarName(), and KKB::XmlElement::VarName().

907 {
908  delete config1; config1 = NULL;
909  delete config2; config2 = NULL;
910 
911  KKStr config1Name = "";
912  KKStr config2Name = "";
913 
914  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
915  while (t && (!cancelFlag))
916  {
917  t = ReadXMLModelToken (t, log);
918  bool tokenFound = true;
919  if ((t != NULL) && (t->TokenType () == XmlToken::TokenTypes::tokElement))
920  {
921  XmlElementPtr e = dynamic_cast<XmlElementPtr> (t);
922  if (!e)
923  {
924  tokenFound = false;
925  }
926  else
927  {
928  const KKStr& varName = e->VarName();
929  const KKStr& sectionName = e->SectionName ();
930 
931  if ((varName.EqualIgnoreCase ("config1")) && (typeid (*e) == typeid (XmlElementTrainingConfiguration2)))
932  {
933  delete config1;
934  config1 = dynamic_cast<XmlElementTrainingConfiguration2Ptr>(e)->TakeOwnership ();
935  config1Name = config1->ConfigRootName ();
936 
937  /**@todo I need the Config1 configuration read from the XML file to be correct; but to deal with this problem for now; will reload the original. */
938  delete config1;
940  config1 ->Load (config1Name, false, log);
941  }
942 
943  else if ((varName.EqualIgnoreCase ("config2")) && (typeid (*e) == typeid (XmlElementTrainingConfiguration2)))
944  {
945  delete config2;
946  config2 = dynamic_cast<XmlElementTrainingConfiguration2Ptr>(e)->TakeOwnership ();
947  config2Name = config1->ConfigRootName ();
948  /**@todo I need the Config1 configuration read from the XML file to be correct; but to deal with this problem for now; will reload the original. */
949  delete config2;
951  config2 ->Load (config2Name, false, log);
952  }
953 
954  else if ((varName.EqualIgnoreCase ("trainer1")) && (typeid (*e) == typeid (XmlElementTrainingProcess2)))
955  {
956  delete trainer1;
957  trainer1 = dynamic_cast<XmlElementTrainingProcess2Ptr>(e)->TakeOwnership ();
958  }
959 
960  else if ((varName.EqualIgnoreCase ("trainer2")) && (typeid (*e) == typeid (XmlElementTrainingProcess2)))
961  {
962  delete trainer2;
963  trainer2 = dynamic_cast<XmlElementTrainingProcess2Ptr>(e)->TakeOwnership ();
964  }
965  else
966  {
967  tokenFound = false;
968  }
969  }
970  }
971  if (!tokenFound)
972  {
973  KKStr errMsg (128);
974  errMsg << "ModelDual::ReadXML ***ERROR*** Unexpected Element: Section: " << t->SectionName () << " VarName:" << t->VarName ();
975  log.Level (-1) << endl << errMsg << endl << endl;
976  AddErrorMsg (errMsg, 0);
977  }
978 
979  delete t;
980  t = s.GetNextToken (cancelFlag, log);
981  }
982 
983  delete t;
984  t = NULL;
985 
986  if (cancelFlag)
987  validModel = false;
988 
989  if (validModel)
990  {
991  if (!trainer1)
992  {
993  validModel = false;
994  KKStr errMsg = "ModelDual::ReadXML ***ERROR*** trainer1 not loaded!!!";
995  log.Level (-1) << endl << errMsg << endl << endl;
996  AddErrorMsg (errMsg, 0);
997  }
998 
999  if (!trainer2)
1000  {
1001  validModel = false;
1002  KKStr errMsg = "ModelDual::ReadXML ***ERROR*** trainer2 not loaded!!!";
1003  log.Level (-1) << endl << errMsg << endl << endl;
1004  AddErrorMsg (errMsg, 0);
1005  }
1006 
1007  if (validModel)
1008  {
1009  delete classifier1; classifier1 = new Classifier2 (trainer1, log);
1010  delete classifier2; classifier2 = new Classifier2 (trainer2, log);
1011  }
1012  }
1013 
1014  if (!param)
1015  param = dynamic_cast<ModelParamDualPtr> (Model::param);
1016 
1017  if (Model::param == NULL)
1018  {
1019  KKStr errMsg (128);
1020  errMsg << "ModelDual::ReadXML ***ERROR*** Base class 'Model' does not have 'param' defined.";
1021  AddErrorMsg (errMsg, 0);
1022  log.Level (-1) << endl << errMsg << endl << endl;
1023  }
1024 
1025  else if (typeid (*Model::param) != typeid(ModelParamDual))
1026  {
1027  KKStr errMsg (128);
1028  errMsg << "ModelDual::ReadXML ***ERROR*** Base class 'Model' param parameter is of the wrong type; found: " << Model::param->ModelParamTypeStr ();
1029  AddErrorMsg (errMsg, 0);
1030  log.Level (-1) << endl << errMsg << endl << endl;
1031  }
1032 
1033  else
1034  {
1035  param = dynamic_cast<ModelParamDualPtr> (Model::param);
1036  }
1037 
1038  ReadXMLModelPost (log);
1039 } /* ReadXML */
virtual void Load(const KKStr &_configFileName, bool _validateDirectories, RunLog &log)
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
TrainingConfiguration2Ptr config2
Definition: ModelDual.h:218
const KKStr & ConfigRootName() const
XmlTokenPtr ReadXMLModelToken(XmlTokenPtr t, RunLog &log)
Will process any tokens that belong to &#39;ModelParam&#39; and return NULL ones that are not will be passed ...
Definition: Model.cpp:884
ModelParamPtr param
Definition: Model.h:412
void AddErrorMsg(const KKStr &errMsg, kkint32 lineNum)
Definition: Model.cpp:239
Classifier2Ptr classifier1
Definition: ModelDual.h:223
bool EqualIgnoreCase(const KKStr &s2) const
Definition: KKStr.cpp:1250
virtual const KKStr & SectionName() const
Definition: XmlStream.h:265
RunLog & Level(kkint32 _level)
Definition: RunLog.cpp:220
XmlElementTemplate< TrainingProcess2 > XmlElementTrainingProcess2
bool validModel
Definition: Model.h:426
Classifier2Ptr classifier2
Definition: ModelDual.h:224
virtual const KKStr & VarName() const
Definition: XmlStream.cpp:794
virtual const KKStr & SectionName() const
Definition: XmlStream.cpp:785
TrainingConfiguration2Ptr config1
Definition: ModelDual.h:217
ModelParamDualPtr param
Definition: ModelDual.h:229
void ReadXMLModelPost(RunLog &log)
Definition: Model.cpp:987
virtual KKStr ModelParamTypeStr() const
Definition: ModelParam.h:108
virtual TokenTypes TokenType()=0
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
virtual const KKStr & VarName() const
Definition: XmlStream.h:269
XmlElementTemplate< TrainingConfiguration2 > XmlElementTrainingConfiguration2
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220
MLClassPtr ModelDual::ReconcilePredictions ( MLClassPtr  pred1,
MLClassPtr  pred2,
RunLog log 
)
protected

Definition at line 320 of file ModelDual.cpp.

References KKB::KKStr::Append(), KKB::KKStr::Concat(), KKMLL::MLClass::CreateNewMLClass(), KKB::KKStr::Empty(), KKB::KKStr::ExtractToken2(), KKMLL::ModelParamDual::FullHierarchyMustMatch(), KKB::KKStr::KKStr(), KKB::KKStr::Len(), KKMLL::MLClass::Name(), KKB::KKStr::operator+=(), KKB::KKStr::operator==(), KKMLL::ModelParamDual::OtherClass(), and param.

Referenced by Predict().

324 {
325  if (pred1 == pred2)
326  return pred1;
327 
328  else if (param->FullHierarchyMustMatch ())
329  {
330  if (param->OtherClass ())
331  return param->OtherClass ();
332  else
333  return MLClass::CreateNewMLClass ("NoAgreement");
334  }
335 
336  if (!pred1)
337  {
338  if (!pred2)
339  {
340  log.Level (-1) << "ModelDual::ReconcilePredictions ***WARNING*** Classifier[" << param->ConfigFileName1 () << "] nor Classifier[" << param->ConfigFileName2 () << "] return predictions." << endl;
341  return NULL;
342  }
343  else
344  {
345  log.Level (-1) << "ModelDual::ReconcilePredictions ***WARNING*** Classifier[" << param->ConfigFileName1 () << "] did not return a prediction." << endl;
346  return pred2;
347  }
348  }
349  else
350  {
351  if (!pred2)
352  {
353  log.Level (-1) << "ModelDual::ReconcilePredictions ***WARNING*** Classifier[" << param->ConfigFileName2 () << "] did not return a prediction." << endl;
354  return pred1;
355  }
356  }
357 
358  // We need to find the common part of the predictions.
359  KKStr name1 = pred1->Name ();
360  KKStr name2 = pred2->Name ();
361  kkint32 maxLen = Min (name1.Len (), name2.Len ());
362  KKStr commonPart (maxLen + 1);
363 
364  while ((!name1.Empty ()) && (!name2.Empty ()))
365  {
366  KKStr name1Token = name1.ExtractToken2 ("_");
367  KKStr name2Token = name2.ExtractToken2 ("_");
368  if (name1Token == name2Token)
369  {
370  if (!commonPart.Empty ())
371  commonPart.Append ('_');
372  commonPart += name1Token;
373  }
374  else
375  {
376  break;
377  }
378  }
379 
380  if (commonPart.Len () < 1)
381  {
382  if (param->OtherClass ())
383  return param->OtherClass ();
384  else
385  return MLClass::CreateNewMLClass ("NoAgreement");
386  }
387  else
388  {
389  return MLClass::CreateNewMLClass (commonPart);
390  }
391 } /* ReconcilePredictions */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
KKStr ExtractToken2(const char *delStr="\n\t\r ")
Extract first Token from the string.
Definition: KKStr.cpp:3026
const KKStr & ConfigFileName2() const
RunLog & Level(kkint32 _level)
Definition: RunLog.cpp:220
kkuint32 Len() const
Returns the number of characters in the string.
Definition: KKStr.h:366
void Append(const char *buff)
Definition: KKStr.cpp:1783
bool Empty() const
Definition: KKStr.h:241
static MLClassPtr CreateNewMLClass(const KKStr &_name, kkint32 _classId=-1)
Static method used to create a new instance of a MLClass object.
Definition: MLClass.cpp:100
ModelParamDualPtr param
Definition: ModelDual.h:229
const KKStr & ConfigFileName1() const
bool FullHierarchyMustMatch() const
kkint32 Min(kkint32 x1, kkint32 x2)
Definition: Raster.cpp:229
MLClassPtr OtherClass() const
void ModelDual::ReconcileProbAndVotes ( Classifier2Ptr  classifier,
MLClassPtr  predClass,
FeatureVectorPtr  encodedExample,
double &  predClassProb,
kkint32 predClassVotes 
)
protected

Definition at line 396 of file ModelDual.cpp.

References KKMLL::ClassProb::classLabel, KKB::KKStr::Concat(), KKMLL::MLClass::Name(), KKMLL::Classifier2::ProbabilitiesByClass(), KKMLL::ClassProb::probability, KKB::KKStr::StartsWith(), and KKMLL::ClassProb::votes.

Referenced by Predict().

402 {
403  const KKStr& name = predClass->Name ();
404 
405  predClassProb = 0.0;
406  predClassVotes = 0;
407 
408  ClassProbListPtr predictions = classifier->ProbabilitiesByClass (encodedExample);
410  for (idx = predictions->begin (); idx != predictions->end (); ++idx)
411  {
412  ClassProbPtr p = *idx;
413  if (p->classLabel->Name ().StartsWith (name))
414  {
415  predClassProb += p->probability;
416  predClassVotes += (kkint32)(p->votes);
417  }
418  }
419 
420  delete predictions;
421  predictions = NULL;
422 
423  return;
424 } /* ReconcileProbAndVotes */
std::vector< ClassProb * >::iterator iterator
Definition: KKQueue.h:88
__int32 kkint32
Definition: KKBaseTypes.h:88
Used to record probability for a specified class; and a list of classes.
Definition: ClassProb.h:25
double probability
Definition: ClassProb.h:36
void ProbabilitiesByClass(const MLClassList &classes, FeatureVectorPtr example, kkint32 *votes, double *probabilities)
For a given feature vector return back the probabilities and votes for each class.
MLClassPtr classLabel
Definition: ClassProb.h:35
void ModelDual::RetrieveCrossProbTable ( MLClassList classes,
double **  crossProbTable,
RunLog log 
)
virtual
Parameters
crossProbTabletwo dimension matrix that needs to be classes.QueueSize () squared.

Reimplemented from KKMLL::Model.

Definition at line 800 of file ModelDual.cpp.

References classifier1, classifier2, and KKMLL::Classifier2::RetrieveCrossProbTable().

804 {
805  if ((!classifier1) || (!classifier2))
806  {
807  _log.Level (-1) << endl << "ModelDual::RetrieveCrossProbTable ***ERROR*** Both Classifiers are not defined." << endl << endl;
808  return;
809  }
810 
811  kkint32 x = 0, y = 0;
812 
813  kkint32 numClasses = _classes.QueueSize ();
814 
815  double** crossProbTableC1 = new double*[numClasses];
816  double** crossProbTableC2 = new double*[numClasses];
817 
818  for (x = 0; x < numClasses; ++x)
819  {
820  crossProbTableC1[x] = new double[numClasses];
821  crossProbTableC2[x] = new double[numClasses];
822  }
823 
824  classifier1->RetrieveCrossProbTable (_classes, crossProbTableC1);
825  classifier2->RetrieveCrossProbTable (_classes, crossProbTableC2);
826 
827  for (x = 0; x < numClasses; ++x)
828  {
829  for (y = 0; y < numClasses; ++y)
830  _crossProbTable[x][y] = (crossProbTableC1[x][y] + crossProbTableC2[x][y]) / 2.0;
831  }
832 
833  for (x = 0; x < numClasses; ++x)
834  {
835  delete crossProbTableC1[x]; crossProbTableC1[x] = NULL;
836  delete crossProbTableC2[x]; crossProbTableC2[x] = NULL;
837  }
838 
839  delete[] crossProbTableC1; crossProbTableC1 = NULL;
840  delete[] crossProbTableC2; crossProbTableC2 = NULL;
841 
842 } /* RetrieveCrossProbTable */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
Classifier2Ptr classifier1
Definition: ModelDual.h:223
Classifier2Ptr classifier2
Definition: ModelDual.h:224
void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable)
TrainingProcess2Ptr KKMLL::ModelDual::Trainer1 ( )
inline

Definition at line 98 of file ModelDual.h.

References trainer1.

Referenced by KKMLL::TrainingProcess2::TrainingProcessLeft().

98 {return trainer1;}
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220
TrainingProcess2Ptr KKMLL::ModelDual::Trainer2 ( )
inline

Definition at line 100 of file ModelDual.h.

References trainer2.

Referenced by KKMLL::TrainingProcess2::TrainingProcessRight().

100 {return trainer2;}
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
void ModelDual::TrainModel ( FeatureVectorListPtr  _trainExamples,
bool  _alreadyNormalized,
bool  _takeOwnership,
VolConstBool _cancelFlag,
RunLog _log 
)
virtual

Performs operations such as FeatureEncoding, and Normalization. The actual training of models occurs in the specific derived implementation of 'Model'.

Performs operations such as FeatureEncoding, and Normalization; the actual training of models occurs in the specific implementation of 'Model'.

Parameters
[in]_trainExamplesTraining data that classifier will be built from. If the examples need to be normalized or encoded and we are not taking ownership then a duplicate list of examples will be created that this method and class will be free to modify.
[in]_alreadyNormalizedIndicates if contents of '_trainExamples' are normalized already; if not they will be normalized.
[in]_takeOwnershipThis instance of Model will take ownership of '_examples' and is free to modify its contents.

< false = DON'T take ownership of 'trainExamples'.

< true = Features are already normalized.

< false = DON'T take ownership of 'trainExamples'.

< true = Features are already normalized.

Parameters
_takeOwnershipModel will take ownership of these examples

Reimplemented from KKMLL::Model.

Definition at line 177 of file ModelDual.cpp.

References KKMLL::TrainingProcess2::Abort(), classifier1, KKMLL::Classifier2::Classifier2(), classifier2, KKB::KKStr::Concat(), config1, config2, KKMLL::TrainingConfiguration2::ConfigFileExists(), KKMLL::ModelParamDual::ConfigFileName1(), KKMLL::ModelParamDual::ConfigFileName2(), KKMLL::TrainingProcess2::CreateTrainingProcessFromTrainingExamples(), DeleteExistingClassifiers(), KKMLL::Model::factoryFVProducer, KKB::KKException::KKException(), KKMLL::TrainingConfiguration2::Load(), KKMLL::FactoryFVProducer::ManufacturTrainingConfiguration(), KKB::KKStr::operator+(), KKB::operator+(), param, trainer1, trainer2, KKMLL::Model::trainExamples, KKMLL::Model::TrainingTimeEnd(), KKMLL::Model::TrainingTimeStart(), KKMLL::Model::TrainModel(), and KKMLL::Model::validModel.

183 {
184  _log.Level (10) << "ModelDual::TrainModel Model[" << param->FileName () << "] _trainExamples->size()[" << _trainExamples->size() << "]" << endl;
185 
186  if (param == NULL)
187  {
188  validModel = false;
189  KKStr errMsg = "ModelDual::TrainModel (param == NULL)";
190  _log.Level (-1) << endl << errMsg << endl << endl;
191  throw KKException (errMsg);
192  }
193 
195 
196  try
197  {
198  Model::TrainModel (_trainExamples, _alreadyNormalized, _takeOwnership, _cancelFlag, _log);
199  }
200  catch (const KKException& e)
201  {
202  validModel = false;
203  KKStr errMsg = "ModelDual::TrainModel ***ERROR*** Exception occurred calling 'Model::TrainModel'.";
204  _log.Level (-1) << endl << errMsg << endl << e.ToString () << endl << endl;
205  throw KKException (errMsg, e);
206  }
207  catch (const exception& e2)
208  {
209  validModel = false;
210  KKStr errMsg = "ModelDual::TrainModel ***ERROR*** Exception occurred calling 'Model::TrainModel'.";
211  _log.Level (-1) << endl << errMsg << endl << e2.what () << endl << endl;
212  throw KKException (errMsg, e2);
213  }
214  catch (...)
215  {
216  validModel = false;
217  KKStr errMsg = "ModelDual::TrainModel ***ERROR*** Exception occurred calling 'Model::TrainModel'.";
218  _log.Level (-1) << endl << errMsg << endl << endl;
219  throw KKException (errMsg);
220  }
221 
222  // 'Model::TrainModel' Will have performed any BitReduction and Feature Encoding
223  // that needed to be done.
224  // Also the data structures 'classes', 'encoder', and 'fileDesc' will have been built.
225  // 'classes' will already be sorted in name order.
226  // The Prediction variables 'probabilities', 'votes', and 'crossClassProbTable' will
227  // have been built.
228 
229  {
230  // Make sure that the configuration files actually exist.
232  {
233  validModel = false;
234  KKStr errMsg = "ModelDual::TrainModel ***ERROR*** Could not find Configuration[" + param->ConfigFileName1 () + "].";
235  _log.Level (-1) << endl << errMsg << endl << endl;
236  throw KKException (errMsg);
237  }
238 
240  {
241  validModel = false;
242  KKStr errMsg = "ModelDual::TrainModel ***ERROR*** Could not find Configuration[" + param->ConfigFileName2 () + "].";
243  _log.Level (-1) << endl << errMsg << endl << endl;
244  throw KKException (errMsg);
245  }
246  }
247 
250  false, // false = Do NOT validate directories.
251  _log
252  );
253  if (!config1->FormatGood ())
254  {
255  validModel = false;
256  KKStr errMsg = "ModelDual::TrainModel ***ERROR*** Configuration[" + param->ConfigFileName1 () + "] is not valid.";
257  _log.Level (-1) << endl << errMsg << endl << endl;
258  throw KKException (errMsg);
259  }
260 
263  false, // false = Do NOT validate directories.
264  _log
265  );
266  if (!config2->FormatGood ())
267  {
268  validModel = false;
269  KKStr errMsg = "ModelDual::TrainModel ***ERROR*** Configuration[" + param->ConfigFileName2 () + "] is not valid.";
270  _log.Level (-1) << endl << errMsg << endl << endl;
271  throw KKException (errMsg);
272  }
273 
274  KKStr statusMsg;
275 
278  (config1,
279  trainExamples,
280  false, /**< false = DON'T take ownership of 'trainExamples'. */
281  true, /**< true = Features are already normalized. */
282  _cancelFlag,
283  _log
284  );
285  TrainingTimeEnd ();
286 
287  if ((!trainer1) || trainer1->Abort ())
288  {
289  validModel = false;
290  KKStr errMsg = "ModelDual::TrainModel ***ERROR*** Error building TrainingProcess for [" + param->ConfigFileName1 () + "].";
291  _log.Level (-1) << endl << errMsg << endl << endl;
292  throw KKException (errMsg);
293  }
294 
297  (config2,
298  trainExamples,
299  false, /**< false = DON'T take ownership of 'trainExamples'. */
300  true, /**< true = Features are already normalized. */
301  _cancelFlag,
302  _log
303  );
304  TrainingTimeEnd ();
305 
306  if ((!trainer2) || trainer2->Abort ())
307  {
308  validModel = false;
309  KKStr errMsg = "ModelDual::TrainModel ***ERROR*** Error building TrainingProcess for [" + param->ConfigFileName1 () + "].";
310  _log.Level (-1) << endl << errMsg << endl << endl;
311  throw KKException (errMsg);
312  }
313 
314  classifier1 = new Classifier2 (trainer1, _log);
315  classifier2 = new Classifier2 (trainer2, _log);
316 } /* TrainModel */
virtual void Load(const KKStr &_configFileName, bool _validateDirectories, RunLog &log)
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
TrainingConfiguration2Ptr config2
Definition: ModelDual.h:218
const KKStr & ConfigFileName2() const
FeatureVectorListPtr trainExamples
Definition: Model.h:424
Classifier2Ptr classifier1
Definition: ModelDual.h:223
FactoryFVProducerPtr factoryFVProducer
Definition: Model.h:404
RunLog & Level(kkint32 _level)
Definition: RunLog.cpp:220
void DeleteExistingClassifiers()
Definition: ModelDual.cpp:164
static TrainingProcess2Ptr CreateTrainingProcessFromTrainingExamples(TrainingConfiguration2Const *config, FeatureVectorListPtr trainingExamples, bool takeOwnershipOfTrainingExamples, bool featuresAlreadyNormalized, VolConstBool &cancelFlag, RunLog &log)
Will Construct an instance using provided list of examples rather than loading from training library...
static bool ConfigFileExists(const KKStr &_configFileName)
bool validModel
Definition: Model.h:426
void TrainingTimeEnd()
Derived classes call this method to stop the clock for &#39;trainingTime&#39;.
Definition: Model.cpp:452
virtual const KKStr & FileName() const
Definition: ModelParam.h:114
void Abort(bool _abort)
Classifier2Ptr classifier2
Definition: ModelDual.h:224
bool FormatGood() const
Definition: Configuration.h:64
TrainingConfiguration2Ptr config1
Definition: ModelDual.h:217
virtual TrainingConfiguration2Ptr ManufacturTrainingConfiguration() const
Returns a &#39;TrainingConfiguration2&#39; derived instance.
ModelParamDualPtr param
Definition: ModelDual.h:229
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: Model.cpp:467
void TrainingTimeStart()
Derived classes call this method to start the clock for &#39;trainingTime&#39;.
Definition: Model.cpp:445
const KKStr & ConfigFileName1() const
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220
virtual const KKStr & ToString() const
Definition: KKException.cpp:98
void ModelDual::WriteXML ( const KKStr varName,
ostream &  o 
) const
virtual

Definition at line 875 of file ModelDual.cpp.

References KKB::XmlTag::AddAtribute(), config1, config2, KKB::KKStr::Empty(), KKB::XmlTag::tagEnd, KKB::XmlTag::tagStart, trainer1, trainer2, KKMLL::Model::WriteModelXMLFields(), KKB::XmlTag::WriteXML(), KKMLL::TrainingProcess2::WriteXML(), KKMLL::TrainingConfiguration2::WriteXML(), and KKB::XmlTag::XmlTag().

878 {
879  XmlTag startTag ("ModelDual", XmlTag::TagTypes::tagStart);
880  if (!varName.Empty ())
881  startTag.AddAtribute ("VarName", varName);
882  startTag.WriteXML (o);
883  o << endl;
884 
885  WriteModelXMLFields (o); // Write the base class data fields 1st.
886 
887  config1->WriteXML ("config1", o);
888  config2->WriteXML ("config2", o);
889 
890  trainer1->WriteXML ("trainer1", o);
891  trainer2->WriteXML ("trainer2", o);
892 
893  XmlTag endTag ("ModelDual", XmlTag::TagTypes::tagEnd);
894  endTag.WriteXML (o);
895  o << endl;
896 } /* WriteXML */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
TrainingProcess2Ptr trainer2
Definition: ModelDual.h:221
TrainingConfiguration2Ptr config2
Definition: ModelDual.h:218
void WriteModelXMLFields(std::ostream &o) const
The "WriteXML" method in Derived classes call this method to include the parents classes fields in th...
Definition: Model.cpp:854
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
bool Empty() const
Definition: KKStr.h:241
void WriteXML(const KKStr &varName, std::ostream &o) const
TrainingConfiguration2Ptr config1
Definition: ModelDual.h:217
TrainingProcess2Ptr trainer1
Definition: ModelDual.h:220

Member Data Documentation

TrainingConfiguration2Ptr KKMLL::ModelDual::config1
protected
TrainingConfiguration2Ptr KKMLL::ModelDual::config2
protected
ModelParamDualPtr KKMLL::ModelDual::param
protected

We will NOT own this instance. It will point to same instance defined in parent class Model.

Definition at line 229 of file ModelDual.h.

Referenced by ModelDual(), Param(), ReadXML(), ReconcilePredictions(), and TrainModel().

KKStr KKMLL::ModelDual::trainer1StatusMsg
protected

Definition at line 226 of file ModelDual.h.

KKStr KKMLL::ModelDual::trainer2StatusMsg
protected

Definition at line 227 of file ModelDual.h.


The documentation for this class was generated from the following files: