KSquare Utilities
ModelOldSVM.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 
3 #include <stdio.h>
4 #include <string>
5 #include <iostream>
6 #include <fstream>
7 #include <math.h>
8 #include <vector>
9 #include <sstream>
10 #include <iomanip>
11 #include <set>
12 #include <vector>
13 #include "MemoryDebug.h"
14 using namespace std;
15 
16 
17 #include "GlobalGoalKeeper.h"
18 #include "KKBaseTypes.h"
19 #include "KKException.h"
20 #include "OSservices.h"
21 #include "RunLog.h"
22 #include "KKStr.h"
23 using namespace KKB;
24 
25 
26 #include "ModelOldSVM.h"
27 #include "BinaryClassParms.h"
28 #include "ClassAssignments.h"
29 #include "ClassProb.h"
30 #include "FeatureEncoder2.h"
31 #include "FeatureNumList.h"
32 #include "FeatureVector.h"
33 #include "KKMLLTypes.h"
34 #include "ModelParamOldSVM.h"
36 #include "SVMparam.h"
37 using namespace KKMLL;
38 
39 
40 
42  Model (),
43  assignments (NULL),
44  svmModel (NULL)
45 {
47 }
48 
49 
50 
51 
53  Model (_factoryFVProducer),
54  assignments (NULL),
55  svmModel (NULL)
56 {
58 }
59 
60 
61 
62 ModelOldSVM::ModelOldSVM (const KKStr& _name,
63  const ModelParamOldSVM& _param, // Create new model from
64  FactoryFVProducerPtr _factoryFVProducer
65  )
66 :
67  Model (_name, _param, _factoryFVProducer),
68  assignments (NULL),
69  svmModel (NULL)
70 {
71 }
72 
73 
74 
76 :
77  Model (_model),
78  assignments (NULL),
79  svmModel (NULL)
80 
81 {
82 }
83 
84 
85 
86 
88 {
89  delete svmModel;
90  svmModel = NULL;
91 
92  delete assignments;
93  assignments = NULL;
94 }
95 
96 
98 {
99  kkint32 memoryConsumedEstimated = Model::MemoryConsumedEstimated () +
100  sizeof (ClassAssignmentsPtr) +
101  sizeof (SVMModelPtr);
102 
103  if (assignments) memoryConsumedEstimated += assignments->MemoryConsumedEstimated ();
104  if (svmModel) memoryConsumedEstimated += svmModel->MemoryConsumedEstimated ();
105  return memoryConsumedEstimated;
106 }
107 
108 
109 
110 
111 ModelOldSVMPtr ModelOldSVM::Duplicate () const
112 {
113  return new ModelOldSVM (*this);
114 }
115 
116 
118 {
119  KKStr result = "SVM(" + Name () + ")";
120 
121  if (svmModel)
122  {
123  SVMparam const * p = svmModel->SVMParameters ();
124  result << " " << MachineTypeToStr (p->MachineType ())
126  }
127  return result;
128 }
129 
130 
131 
133 {
134  return svmModel->Assignments ();
135 }
136 
137 
138 
140  MLClassPtr class1,
141  MLClassPtr class2
142  )
143 {
144  return svmModel->GetFeatureNums (filedesc, class1, class2);
145 } /* GetFeatureNums */
146 
147 
148 
149 
151 {
152  return svmModel->GetFeatureNums ();
153 }
154 
155 
156 
158 {
159  return svmModel->NumOfSupportVectors ();
160 }
161 
162 
163 
165  kkint32& totalNumSVs
166  )
167 {
168  return svmModel->SupportVectorStatistics (numSVs, totalNumSVs);
169 }
170 
171 
173 {
174  if (param == NULL)
175  {
176  KKStr errMsg = "ModelOldSVM::Param ***ERROR*** param not defined (param == NULL).";
177  cerr << endl << errMsg << endl << endl;
178  throw KKException (errMsg);
179  }
180 
182  {
183  KKStr errMsg = "ModelOldSVM::Param ***ERROR*** param variable of wrong type.";
184  cerr << endl << errMsg << endl << endl;
185  throw KKException (errMsg);
186  }
187 
188  return dynamic_cast<ModelParamOldSVMPtr> (param);
189 }
190 
191 
192 
194 {
195  return svmModel->SelectionMethod ();
196 }
197 
198 
199 
200 
202  MLClassPtr knownClass,
203  MLClassPtr& predClass1,
204  MLClassPtr& predClass2,
205  kkint32& predClass1Votes,
206  kkint32& predClass2Votes,
207  double& probOfKnownClass,
208  double& predClass1Prob,
209  double& predClass2Prob,
210  kkint32& numOfWinners,
211  bool& knownClassOneOfTheWinners,
212  double& breakTie,
213  RunLog& log
214  )
215 {
216  bool newExampleCreated = false;
217  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
218  svmModel->Predict (encodedExample, knownClass, predClass1, predClass2,
219  predClass1Votes, predClass2Votes,
220  probOfKnownClass,
221  predClass1Prob, predClass2Prob,
222  numOfWinners,
223  knownClassOneOfTheWinners,
224  breakTie
225  );
226  if (newExampleCreated)
227  {
228  delete encodedExample;
229  encodedExample = NULL;
230  }
231 
232  return;
233 } /* Predict */
234 
235 
236 
237 
238 
239 MLClassPtr ModelOldSVM::Predict (FeatureVectorPtr example,
240  RunLog& log
241  )
242 {
243  bool newExampleCreated = false;
244  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
245  MLClassPtr c = svmModel->Predict (example);
246 
247  if (newExampleCreated)
248  {
249  delete encodedExample;
250  encodedExample = NULL;
251  }
252 
253  return c;
254 } /* Predict */
255 
256 
257 
258 
259 
260 
262  MLClassPtr & predClass,
263  double& dist
264  )
265 {
266  svmModel->PredictRaw (example, predClass, dist);
267 } /* PredictRaw */
268 
269 
270 
271 
272 
274  RunLog& log
275  )
276 {
277  if (!svmModel)
278  {
279  KKStr errMsg = "ModelOldSVM::ProbabilitiesByClass ***ERROR*** (svmModel == NULL)";
280  log.Level (-1) << endl << errMsg << endl;
281  throw KKException (errMsg);
282  }
283 
284  bool newExampleCreated = false;
285  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
286  svmModel->ProbabilitiesByClass (encodedExample, *classes, votes, classProbs, log);
287 
288  if (newExampleCreated)
289  {
290  delete encodedExample;
291  encodedExample = NULL;
292  }
293 
294  ClassProbListPtr results = new ClassProbList ();
295  kkint32 idx = 0;
296  for (idx = 0; idx < numOfClasses; idx++)
297  {
298  MLClassPtr ic = classes->IdxToPtr (idx);
299  results->PushOnBack (new ClassProb (ic, classProbs[idx], (float)votes[idx]));
300  }
301 
303  results->SortByVotes (true);
304  else
305  results->SortByProbability (true);
306 
307  return results;
308 } /* ProbabilitiesByClass */
309 
310 
311 
312 
313 
314 
316  const MLClassList& _mlClasses,
317  double* _probabilities,
318  RunLog& _log
319  )
320 {
321  bool newExampleCreated = false;
322  FeatureVectorPtr encodedExample = PrepExampleForPrediction (_example, newExampleCreated);
323 
324  svmModel->ProbabilitiesByClass (encodedExample, _mlClasses, votes, _probabilities, _log);
325  if (newExampleCreated)
326  {
327  delete encodedExample;
328  encodedExample = NULL;
329  }
330 
331  return;
332 }
333 
334 
335 
336 
338  const MLClassList& _mlClasses,
339  kkint32* _votes,
340  double* _probabilities,
341  RunLog& _log
342  )
343 {
344  bool newExampleCreated = false;
345  FeatureVectorPtr encodedExample = PrepExampleForPrediction (example, newExampleCreated);
346  svmModel->ProbabilitiesByClass (encodedExample, _mlClasses, _votes, _probabilities, _log);
347  if (newExampleCreated)
348  {
349  delete encodedExample;
350  encodedExample = NULL;
351  }
352 
353  return;
354 } /* ProbabilitiesByClass */
355 
356 
357 
358 
359 
360 
361 
362 vector<KKStr> ModelOldSVM::SupportVectorNames (MLClassPtr c1,
363  MLClassPtr c2
364  ) const
365 {
366  return svmModel->SupportVectorNames (c1, c2);
367 } /* SupportVectorNames */
368 
369 
370 
371 
372 
373 
374 
376 {
377  return svmModel->SupportVectorNames ();
378 } /* SupportVectorNames */
379 
380 
381 
382 
383 
384 
385 
388  MLClassPtr c1,
389  MLClassPtr c2
390  )
391 {
392  return svmModel->FindWorstSupportVectors (example, numToFind, c1, c2);
393 } /* FindWorstSupportVectors */
394 
395 
396 
397 
398 
399 
402  MLClassPtr c1,
403  MLClassPtr c2
404  )
405 {
406  return svmModel->FindWorstSupportVectors2 (example, numToFind, c1, c2);
407 } /* FindWorstSupportVectors2 */
408 
409 
410 
411 
412 
414 {
415  return svmModel->NormalizeNominalAttributes ();
416 } /* NormalizeNominalAttributes */
417 
418 
419 
420 
422  double** crossProbTable, /**< two dimension matrix that needs to be classes.QueueSize () squared. */
423  RunLog& log
424  )
425 {
426  svmModel->RetrieveCrossProbTable (classes, crossProbTable, log);
427  return;
428 } /* RetrieveCrossProbTable */
429 
430 
431 
432 
434  bool _alreadyNormalized,
435  bool _takeOwnership, /*!< Model will take ownership of these examples */
436  VolConstBool& _cancelFlag,
437  RunLog& _log
438  )
439 {
440  _log.Level (20) << "ModelOldSVM::TrainModel - Constructing From Training Data, Model[" << rootFileName << "]" << endl;
441  // We do not bother with the base class 'TrainModel' like we do with other models.
442  // 'ModelOldSVM' is a special case. All we are trying to do is create a pass through
443  // for 'svmModel'.
444 
445  delete svmModel;
446  svmModel = NULL;
447 
448  Model::TrainModel (_trainExamples, _alreadyNormalized, _takeOwnership, _cancelFlag, _log);
449  // The "Model::TrainModel" may have manipulated the '_trainExamples'. It will have also
450  // updated 'Model::trainExamples. So from this point forward we use 'trainExamples'.
451  _trainExamples = NULL;
452  if (trainExamples == NULL)
453  {
454  validModel = false;
455  KKStr errMsg = " ModelOldSVM::TrainModel ***ERROR*** (trainExamples == NULL).";
456  _log.Level (-1) << endl << errMsg << endl << endl;
457  throw KKException (errMsg);
458  }
459 
460  if ((!fileDesc) && trainExamples)
462 
464  if (!svmParam)
465  {
466  validModel = false;
467  KKStr errMsg = " ModelOldSVM::TrainModel ***ERROR*** (svmParam == NULL).";
468  _log.Level (-1) << endl << errMsg << endl << endl;
469  throw KKException (errMsg);
470  }
471 
472  {
473  delete classes;
476  numOfClasses = classes->QueueSize ();
477  delete assignments;
478  assignments = new ClassAssignments (*classes);
479  }
480 
481  try
482  {
484  svmModel = new SVMModel (*svmParam, *trainExamples, *assignments, fileDesc, _log);
486  }
487  catch (...)
488  {
489  _log.Level (-1) << endl << "ModelOldSVM::TrainModel Exception occurred building training model." << endl << endl;
490  validModel = false;
491  delete svmModel;
492  svmModel = NULL;
493  }
494 
495  if (weOwnTrainExamples)
496  {
497  // We are done with the training examples and since we were to take ownership, we can delete them.
498  delete trainExamples;
499  trainExamples = NULL;
500  }
501 } /* TrainModel */
502 
503 
504 
505 
507  bool& newExampleCreated
508  )
509 {
510  FeatureVectorPtr oldFV = NULL;
511  newExampleCreated = false;
512  if ((!alreadyNormalized) && (normParms))
513  {
514  oldFV = fv;
516  if (newExampleCreated)
517  {
518  delete oldFV;
519  oldFV = NULL;
520  }
521  newExampleCreated = true;
522  }
523 
524  // Since 'SvmModel' will be doing the encoding we do not need to do it here.
525 
526  return fv;
527 } /* PrepExampleForPrediction */
528 
529 
530 
531 
532 
533 
534 void ModelOldSVM::WriteXML (const KKStr& varName,
535  ostream& o
536  ) const
537 {
538  XmlTag startTag ("ModelOldSVM", XmlTag::TagTypes::tagStart);
539  if (!varName.Empty ())
540  startTag.AddAtribute ("VarName", varName);
541  startTag.WriteXML (o);
542  o << endl;
543 
544  WriteModelXMLFields (o); // Write the PArent class fields 1st.
545 
546  if (assignments)
547  assignments->ToString ().WriteXML ("Assignments", o);
548 
549  if (svmModel)
550  svmModel->WriteXML ("svmModel", o);
551 
552  XmlTag endTag ("ModelOldSVM", XmlTag::TagTypes::tagEnd);
553  endTag.WriteXML (o);
554  o << endl;
555 } /* WriteXML */
556 
557 
558 
559 
560 
562  XmlTagConstPtr tag,
563  VolConstBool& cancelFlag,
564  RunLog& log
565  )
566 {
567  delete svmModel;
568  svmModel = NULL;
569  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
570  while (t && (!cancelFlag))
571  {
572  t = ReadXMLModelToken (t, log); // 1st see if the base class has this data field.
573  if (t)
574  {
575  if ((t->VarName ().EqualIgnoreCase ("Assignments")) && (typeid(*t) == typeid(XmlElementKKStr)))
576  {
577  XmlElementKKStrPtr s = dynamic_cast<XmlElementKKStrPtr> (t);
578  delete assignments;
579  assignments = new ClassAssignments ();
580  assignments->ParseToString (*(s->Value ()), log);
581  }
582 
583  else if ((t->VarName ().EqualIgnoreCase ("SvmModel")) && (typeid(*t) == typeid(XmlElementSVMModel)))
584  {
585  delete svmModel;
586  svmModel = dynamic_cast<XmlElementSVMModelPtr> (t)->TakeOwnership ();
587  }
588 
589  else
590  {
591  KKStr errMsg (128);
592  errMsg << "ModelOldSVM::ReadXML ***ERROR*** Unexpected Token; Section:" << t->SectionName () << " VarName: " << t->VarName ();
593  AddErrorMsg (errMsg, 0);
594  log.Level (-1) << endl << errMsg << endl << endl;
595  }
596  }
597  delete t;
598  t = s.GetNextToken (cancelFlag, log);
599  }
600 
601  delete t;
602  t = NULL;
603 
604  if (cancelFlag)
605  AddErrorMsg ("ModelOldSVM::ReadXML ***CANCELED***", 0);
606 
607  if (!param)
608  param = dynamic_cast<ModelParamOldSVMPtr> (Model::param);
609 
610  if (Model::param == NULL)
611  {
612  KKStr errMsg (128);
613  errMsg << "ModelOldSVM::ReadXML ***ERROR*** Base class 'Model' does not have 'param' defined.";
614  AddErrorMsg (errMsg, 0);
615  log.Level (-1) << endl << errMsg << endl << endl;
616  }
617 
618  else if (typeid (*Model::param) != typeid(ModelParamOldSVM))
619  {
620  KKStr errMsg (128);
621  errMsg << "ModelOldSVM::ReadXML ***ERROR*** Base class 'Model' param parameter is of the wrong type; found: " << Model::param->ModelParamTypeStr ();
622  AddErrorMsg (errMsg, 0);
623  log.Level (-1) << endl << errMsg << endl << endl;
624  }
625 
626  if ((!svmModel) || (!svmModel->ValidModel ()))
627  {
628  KKStr errMsg (128);
629  errMsg << "ModelOldSVM::ReadXML ***ERROR*** 'SvmModel' was not defined or is not valid.";
630  AddErrorMsg (errMsg, 0);
631  log.Level (-1) << endl << errMsg << endl << endl;
632  }
633 
634  else
635  {
636  param = dynamic_cast<ModelParamOldSVMPtr> (Model::param);
637  }
638 
639  ReadXMLModelPost (log);
640 } /* ReadXML */
641 
642 
643 
644 
646 {
647 public:
648  XmlFactoryModelOldSVM (): XmlFactory ("ModelOldSVM") {}
649 
651  XmlStream& s,
652  VolConstBool& cancelFlag,
653  RunLog& log
654  )
655  {
656  return new XmlElementModelOldSVM(tag, s, cancelFlag, log);
657  }
658 
660 
662  {
663  if (factoryInstance == NULL)
664  {
666  if (!factoryInstance)
667  {
670  }
672  }
673  return factoryInstance;
674  }
675 };
676 
677 
ModelOldSVM(const KKStr &_name, const ModelParamOldSVM &_param, FactoryFVProducerPtr _factoryFVProducer)
Creates a new svm model from the provided example (example) data.
Definition: ModelOldSVM.cpp:62
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for &#39;size&#39; characters.
Definition: KKStr.cpp:655
XmlTag(const KKStr &_name, TagTypes _tagType)
Definition: XmlStream.cpp:586
const KKStr & Name() const
Definition: Model.h:178
Base class to all Learning Algorithms.
Definition: Model.h:82
virtual bool NormalizeNominalAttributes() const
kkint32 MemoryConsumedEstimated() const
Definition: SVMModel.cpp:437
virtual XmlElementModelOldSVM * ManufatureXmlElement(XmlTagPtr tag, XmlStream &s, VolConstBool &cancelFlag, RunLog &log)
ClassAssignments * ClassAssignmentsPtr
SVMModel * SVMModelPtr
Definition: SVMModel.h:557
kkint32 * votes
Definition: Model.h:428
bool EqualIgnoreCase(const char *s2) const
Definition: KKStr.cpp:1257
bool alreadyNormalized
Definition: Model.h:388
std::vector< ProbNamePair > FindWorstSupportVectors(FeatureVectorPtr example, kkint32 numToFind, MLClassPtr c1, MLClassPtr c2)
For a given two class pair return the names of the &#39;numToFind&#39; worst S/V&#39;s.
std::vector< KKStr > SupportVectorNames() const
SVM_SelectionMethod
Definition: SVMparam.h:34
__int32 kkint32
Definition: KKBaseTypes.h:88
FeatureVector * FeatureVectorPtr
Definition: Model.h:44
void SupportVectorStatistics(kkint32 &numSVs, kkint32 &totalNumSVs)
Definition: SVMModel.cpp:1328
virtual ~ModelOldSVM()
Definition: ModelOldSVM.cpp:87
ModelParamOldSVMPtr Param() const
ModelOldSVM(FactoryFVProducerPtr _factoryFVProducer)
Definition: ModelOldSVM.cpp:52
NormalizationParmsPtr normParms
Definition: Model.h:408
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
FeatureNumListConstPtr GetFeatureNums() const
Definition: SVMModel.cpp:701
ModelParamPtr param
Definition: Model.h:412
virtual void PushOnBack(ClassProbPtr cp)
Definition: ClassProb.cpp:212
FeatureNumListConstPtr GetFeatureNums() const
MLClassListPtr ExtractListOfClasses() const
FeatureVectorListPtr trainExamples
Definition: Model.h:424
SVM_SelectionMethod SelectionMethod() const
Definition: SVMModel.h:148
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 ...
const FileDescPtr FileDesc() const
void AddErrorMsg(const KKStr &errMsg, kkint32 lineNum)
Definition: Model.cpp:239
void SupportVectorStatistics(kkint32 &numSVs, kkint32 &totalNumSVs)
MLClassPtr Predict(FeatureVectorPtr example)
Definition: SVMModel.cpp:1131
const ClassAssignments & Assignments() const
Definition: SVMModel.h:126
static void RegisterFactory(XmlFactory *factory)
Definition: XmlStream.cpp:897
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 PredictRaw(FeatureVectorPtr example, MLClassPtr &predClass, double &dist)
double * classProbs
Definition: Model.h:394
SVMparam * SVMparamPtr
Definition: SVMparam.h:286
SVM_MachineType MachineType() const
Definition: SVMparam.h:151
KKStr operator+(const char *right) const
Definition: KKStr.cpp:3986
virtual void ProbabilitiesByClass(FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &log)
Will get the probabilities assigned to each class.
virtual ModelOldSVMPtr Duplicate() const
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
FeatureNumListConstPtr GetFeatureNums(FileDescPtr filedesc, MLClassPtr class1, MLClassPtr class2)
kkint32 numOfClasses
Definition: Model.h:410
XmlFactory(const KKStr &_clasName)
Definition: XmlStream.cpp:931
kkint32 NumOfSupportVectors() const
virtual ClassProbListPtr ProbabilitiesByClass(FeatureVectorPtr example, RunLog &log)
FeatureVectorPtr ToNormalized(FeatureVectorPtr example) const
KKTHread * KKTHreadPtr
KKStr operator+(const char *left, const KKStr &right)
Definition: KKStr.cpp:3976
void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable, RunLog &log)
void SortByProbability(bool highToLow=true)
Definition: ClassProb.cpp:143
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)
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Definition: XmlStream.cpp:602
Used to record probability for a specified class; and a list of classes.
Definition: ClassProb.h:25
bool Empty() const
Definition: KKStr.h:241
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
static XmlFactoryModelOldSVM * factoryInstance
virtual ModelParamTypes ModelParamType() const =0
kkint32 MemoryConsumedEstimated() const
bool validModel
Definition: Model.h:426
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
Definition: XmlStream.h:46
void TrainingTimeEnd()
Derived classes call this method to stop the clock for &#39;trainingTime&#39;.
Definition: Model.cpp:452
virtual kkint32 MemoryConsumedEstimated() const
Definition: Model.cpp:208
Binds MLClass objects to the appropriate number that the Learning Algorithm expects.
std::vector< KKStr > SupportVectorNames(MLClassPtr c1, MLClassPtr c2) const
std::vector< ProbNamePair > FindWorstSupportVectors2(FeatureVectorPtr example, kkint32 numToFind, MLClassPtr c1, MLClassPtr c2)
For a given two class pair return the names of the &#39;numToFind&#39; worst S/V&#39;s.
KKStr MachineTypeToStr(SVM_MachineType machineType)
Definition: SVMparam.cpp:965
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
virtual KKStr Description() const
This class encapsulates are the information necessary to build a SVMModel class.
Definition: SVMparam.h:74
virtual void TrainModel(FeatureVectorListPtr _trainExamples, bool _alreadyNormalized, bool _takeOwnership, VolConstBool &_cancelFlag, RunLog &_log)
Use given training data to create a trained Model that can be used for classifying examples...
void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable, RunLog &log)
Will return the probabilities for all pairs of the classes listed in &#39;classes&#39;.
Definition: SVMModel.cpp:2279
FileDesc * FileDescPtr
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: SVMModel.cpp:2354
SVM_SelectionMethod SelectionMethod() const
static XmlFactoryModelOldSVM * FactoryInstance()
void ProbabilitiesByClass(FeatureVectorPtr example, const MLClassList &_mlClasses, kkint32 *_votes, double *_probabilities, RunLog &_log)
Will get the probabilities assigned to each class.
Definition: SVMModel.cpp:1354
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)
Will predict the two most likely classes of &#39;example&#39;.
Definition: SVMModel.cpp:792
void SortByVotes(bool highToLow=true)
Definition: ClassProb.cpp:151
ModelParamOldSVM * ModelParamOldSVMPtr
Definition: ModelOldSVM.h:24
Model(const KKStr &_name, const ModelParam &_param, FactoryFVProducerPtr _factoryFVProducer)
Construct a instance of &#39;Model&#39; using the parameters specified in &#39;_param&#39;.
Definition: Model.cpp:152
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: KKStr.cpp:4420
const ClassAssignments & Assignments() const
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
SVM_SelectionMethod SelectionMethod() const
Definition: SVMparam.h:169
FileDescPtr fileDesc
Definition: Model.h:406
ModelOldSVM(const ModelOldSVM &_madel)
Definition: ModelOldSVM.cpp:75
virtual MLClassPtr Predict(FeatureVectorPtr image, RunLog &log)
XmlElementModelTemplate< ModelOldSVM > XmlElementModelOldSVM
Definition: ModelOldSVM.h:269
virtual FeatureVectorPtr PrepExampleForPrediction(FeatureVectorPtr fv, bool &newExampleCreated)
ModelOldSVM Specific &#39;PrepExampleForPrediction&#39; will only normalize data.
XmlElementKKStr * XmlElementKKStrPtr
Definition: XmlStream.h:670
void WriteXML(std::ostream &o)
Definition: XmlStream.cpp:723
FeatureNumListConst * FeatureNumListConstPtr
KKStr SelectionMethodToStr(SVM_SelectionMethod selectionMethod)
Definition: SVMparam.cpp:1008
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)
ClassAssignments(const MLClassList &classes)
Creates assignment for all classes in &#39;classes.
SVMModel(const SVMparam &_svmParam, FeatureVectorList &_examples, ClassAssignments &_assignments, FileDescPtr _fileDesc, RunLog &_log)
Constructor that will create a svmlib training model using the features and classes for training purp...
Definition: SVMModel.cpp:203
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 PredictRaw(FeatureVectorPtr example, MLClassPtr &predClass, double &dist)
Returns the distance from the decision border of the SVM.
Definition: SVMModel.cpp:921
void TrainingTimeStart()
Derived classes call this method to start the clock for &#39;trainingTime&#39;.
Definition: Model.cpp:445
void ReadXMLModelPost(RunLog &log)
Definition: Model.cpp:987
virtual KKStr ModelParamTypeStr() const
Definition: ModelParam.h:108
Model(const Model &_madel)
Copy Constructor.
Definition: Model.cpp:69
FeatureNumListConstPtr GetFeatureNums(FileDescPtr fileDesc, MLClassPtr class1, MLClassPtr class2) const
Definition: SVMModel.cpp:715
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
KKException(const KKStr &_exceptionStr)
Definition: KKException.cpp:45
ClassProb(MLClassPtr _classLabel, double _probability, float _votes)
Definition: ClassProb.cpp:22
Maintains a list of MLClass instances.
Definition: MLClass.h:233
FeatureVectorList * FeatureVectorListPtr
Definition: Model.h:46
bool weOwnTrainExamples
Definition: Model.h:430
Maintains one instance of a GoalKeeper object that can be used anywhere in the application.
Abstract Base class for Machine Learning parameters.
Definition: ModelParam.h:35
virtual bool NormalizeNominalAttributes()
Definition: SVMModel.cpp:2246
virtual const KKStr & VarName() const
Definition: XmlStream.h:269
bool ValidModel() const
Definition: SVMModel.h:152
kkint32 NumOfSupportVectors() const
Definition: SVMModel.cpp:1304
FactoryFVProducer * FactoryFVProducerPtr
Definition: Model.h:75
MLClassListPtr classes
Definition: Model.h:390
virtual SVMparamPtr SvmParameters() const
SVMparam const * SVMParameters() const
Definition: SVMModel.h:143
Model(FactoryFVProducerPtr _factoryFVProducer)
Use this when you are planning on creating a empty model without parameters.
Definition: Model.cpp:117
virtual void ProbabilitiesByClass(FeatureVectorPtr _example, const MLClassList &_mlClasses, double *_probabilities, RunLog &_log)
Retrieves probabilities assigned to each class.
virtual kkint32 MemoryConsumedEstimated() const
Definition: ModelOldSVM.cpp:97
std::vector< KKStr > SupportVectorNames(MLClassPtr c1, MLClassPtr c2) const
Definition: SVMModel.cpp:1576
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163