KSquare Utilities
SVMparam.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <stdio.h>
3 #include <fstream>
4 #include <string>
5 #include <iostream>
6 #include <vector>
7 #include "MemoryDebug.h"
8 using namespace std;
9 
10 
11 #include "GlobalGoalKeeper.h"
12 #include "KKBaseTypes.h"
13 #include "OSservices.h"
14 #include "RunLog.h"
15 using namespace KKB;
16 
17 
18 #include "SVMparam.h"
19 #include "BinaryClassParms.h"
20 #include "ClassAssignments.h"
21 #include "FeatureVector.h"
22 #include "FileDesc.h"
23 #include "MLClass.h"
24 #include "KKMLLTypes.h"
25 using namespace KKMLL;
26 
27 
28 
29 SVMparam::SVMparam (KKStr& _cmdLineStr,
30  FeatureNumListConstPtr _selectedFeatures,
31  bool& _validFormat,
32  RunLog& _log
33  ):
34 
35  binaryParmsList (NULL),
36  encodingMethod (SVM_EncodingMethod::NoEncoding),
37  fileName (),
38  param (),
39  probClassPairs (),
40  samplingRate (0.0f),
41  selectedFeatures (),
42  selectionMethod (SVM_SelectionMethod::Voting),
43  useProbabilityToBreakTies (false),
44  validParam (false)
45 {
46  if (_selectedFeatures)
47  selectedFeatures = new FeatureNumList (*_selectedFeatures);
48  ParseCmdLine (_cmdLineStr, _validFormat, _log);
49 }
50 
51 
52 
53 
54 
56 
57  binaryParmsList (NULL),
58  encodingMethod (SVM_EncodingMethod::NoEncoding),
59  fileName (),
60  machineType (SVM_MachineType::OneVsOne),
61  param (),
62  probClassPairs (),
63  samplingRate (0.0f),
64  selectedFeatures (NULL),
65  selectionMethod (SVM_SelectionMethod::Voting),
66  useProbabilityToBreakTies (false),
67  validParam (false)
68 {
69 }
70 
71 
72 
73 
74 
75 SVMparam::SVMparam (const SVMparam& _svmParam):
76 
77  binaryParmsList (NULL),
78  encodingMethod (_svmParam.encodingMethod),
79  fileName (_svmParam.fileName),
80  machineType (_svmParam.machineType),
81  param (_svmParam.param),
82  probClassPairs (_svmParam.probClassPairs),
83  samplingRate (_svmParam.samplingRate),
84  selectedFeatures (_svmParam.selectedFeatures),
85  selectionMethod (_svmParam.selectionMethod),
86  useProbabilityToBreakTies (_svmParam.useProbabilityToBreakTies),
87  validParam (_svmParam.validParam)
88 {
89  if (_svmParam.selectedFeatures)
90  selectedFeatures = new FeatureNumList (*_svmParam.selectedFeatures);
91 
92  if (_svmParam.binaryParmsList)
93  {
94  binaryParmsList = _svmParam.binaryParmsList->DuplicateListAndContents ();
95 
96  // new BinaryClassParmsList (true, _svmParam.binaryParmsList->QueueSize ());
97  // binaryParmsList = new BinaryClassParmsList (*(_svmParam.binaryParmsList));
98  }
99  param = _svmParam.param;
100 }
101 
102 
103 
105 {
106  delete binaryParmsList; binaryParmsList = NULL;
107  delete selectedFeatures; selectedFeatures = NULL;
108 }
109 
110 
111 
113 {
114  if (!selectedFeatures)
115  {
116  selectedFeatures = new FeatureNumList (fileDesc);
117  selectedFeatures->SetAllFeatures (fileDesc);
118  }
119  return selectedFeatures;
120 }
121 
122 
123 
125 {
126  kkint32 memoryConsumedEstimated = sizeof (SVMparam) + fileName.MemoryConsumedEstimated ();
127  if (selectedFeatures)
128  memoryConsumedEstimated += selectedFeatures->MemoryConsumedEstimated ();
129 
130  if (binaryParmsList)
131  memoryConsumedEstimated += binaryParmsList->MemoryConsumedEstimated ();
132 
133  return memoryConsumedEstimated;
134 } /* MemoryConsumedEstimated */
135 
136 
137 
138 void SVMparam::SelectedFeatures (const FeatureNumList& _selectedFeatures)
139 {
140  delete selectedFeatures;
141  selectedFeatures = new FeatureNumList (_selectedFeatures);
142 }
143 
144 
146 {
147  delete selectedFeatures;
148  selectedFeatures = new FeatureNumList (*_selectedFeatures);
149 }
150 
151 
152 
154 {
155  kkuint32 numClasses = assignments.size ();
156  if (numClasses < 1)
157  return;
158 
159  kkuint32 numPairs = numClasses * (numClasses - 1) / 2;
160  probClassPairs.clear ();
161  if (binaryParmsList == NULL)
162  {
163  for (kkuint32 x = 0; x < numPairs; ++x)
164  probClassPairs.push_back (param.A);
165  }
166  else
167  {
168  for (kkuint32 class1IDX = 0; class1IDX < (numClasses - 1); ++class1IDX)
169  {
170  MLClassPtr class1 = assignments.GetMLClass ((kkint16)class1IDX);
171  for (kkuint32 class2IDX = class1IDX + 1; class2IDX < numClasses; ++class2IDX)
172  {
173  MLClassPtr class2 = assignments.GetMLClass ((kkint16)class2IDX);
174  BinaryClassParmsPtr bcp = binaryParmsList->LookUp (class1, class2);
175  if (bcp)
176  probClassPairs.push_back ((float)bcp->AParam ());
177  else
178  probClassPairs.push_back ((float)param.A);
179  }
180  }
181  }
182 } /* ProbClassPairsInitialize */
183 
184 
185 
186 
187 void SVMparam::A_Param (float _A)
188 {
189  param.A = _A;
190 }
191 
192 
193 
194 
195 void SVMparam::C_Param (double _CC)
196 {
197  param.C = _CC;
198 }
199 
200 
201 
202 void SVMparam::ParseCmdLineParameter (const KKStr& parameter,
203  const KKStr& value,
204  bool& parameterUsed,
205  bool& _validFormat,
206  RunLog& log
207  )
208 {
209  parameterUsed = true;
210  _validFormat = true;
211 
212  KKStr field = parameter;
213  double valueNum = value.ToDouble ();
214 
215  field.Upper ();
216  KKStr valueUpper (value);
217  valueUpper.Upper ();
218 
219  param.ProcessSvmParameter (parameter,value, valueNum, parameterUsed);
220  if (parameterUsed)
221  return;
222 
223  parameterUsed = true;
224 
225  if ((field == "-ENCODE"))
226  {
227  encodingMethod = EncodingMethodFromStr (valueUpper);
228  }
229 
230  else if (field == "-MT")
231  {
232  machineType = MachineTypeFromStr (valueUpper);
233  if (machineType == SVM_MachineType::Null)
234  {
235  _validFormat = false;
236  log.Level (-1) << endl
237  << "SVMparam::ParseCmdLineParameter *** ERROR ***" << endl
238  << " Invalid -MT Parm[" << value << "]" << endl
239  << endl;
240  }
241  }
242 
243  else if ((field == "-SM") || (field == "-SELECTIONMETHOD"))
244  {
245  selectionMethod = SelectionMethodFromStr (valueUpper);
246  if (selectionMethod == SVM_SelectionMethod::Null)
247  {
248  log.Level (-1) << endl
249  << "SVMparam::ParseCmdLineParameter *** ERROR ***" << endl
250  << " Invalid SelectionMethod (-SM)[" << value << "]." << endl
251  << endl;
252  _validFormat = false;
253  }
254  }
255 
256  else if ((field == "-SR") || (field == "-SAMPLINGRATE"))
257  {
258  samplingRate = float (atof (value.Str ()));
259  }
260 
261 
262  else if ((field == "-UP") || (field == "-USEPROBABILITY"))
263  {
264  useProbabilityToBreakTies = true;
265  }
266 
267 
268  else
269  {
270  parameterUsed = false;
271  }
272 } /* ParseCmdLineParameter */
273 
274 
275 
276 
277 void SVMparam::ParseCmdLine (KKStr _cmdLineStr,
278  bool& _validFormat,
279  RunLog& _log
280  )
281 {
282  _validFormat = true;
283 
284  {
285  // Will create a svm_parameter object from _cmdLineStr, the parameters that
286  // are not specific to svm_parameter will be left over and then we can
287  // process them.
288  svm_parameter tempParam (_cmdLineStr);
289  param = tempParam;
290  }
291 
292  //DecodeParamStr (_cmdLineStr, param);
293 
294  machineType = SVM_MachineType::OneVsOne;
295 
296  useProbabilityToBreakTies = false;
297 
298  KKStr field (_cmdLineStr.ExtractToken (" \t\n\r"));
299  KKStr value;
300 
301  double valueNum;
302 
303  while (!field.Empty () && _validFormat)
304  {
305  if (field[(kkint16)0] != '-')
306  {
307  _log.Level (-1) << "SVMparam::ParseCmdLine *** Invalid Parameter["
308  << field << "] ***"
309  << endl;
310  _validFormat = false;
311  break;
312  }
313 
314  // See if next field is a Switch field or a parameter.
315  _cmdLineStr.TrimLeft (" \t\n\r");
316  value == "";
317  if (_cmdLineStr.Len () > 0)
318  {
319  if (_cmdLineStr[(kkint16)0] != '-')
320  value = _cmdLineStr.ExtractToken (" \t\n\r");
321  }
322 
323  valueNum = atof (value.Str ());
324 
325  field.Upper ();
326  KKStr valueUpper (value);
327 
328  valueUpper.Upper ();
329 
330  bool parameterUsed = false;
331  ParseCmdLineParameter (field, value, parameterUsed, _validFormat, _log);
332  if (!parameterUsed)
333  {
334  _log.Level (-1) << "SVMparam::ParseCmdLine - Invalid Parameter["
335  << field << "] Value[" << value << "]."
336  << endl;
337  _validFormat = false;
338  break;
339  }
340  field = _cmdLineStr.ExtractToken (" \t\n\r");
341  }
342 
343 
344  validParam = _validFormat;
345 } /* ParseCmdLine */
346 
347 
348 
349 
350 /**
351  * @brief Convert a svm_parameter struct to a cmdline str.
352 */
354 {
355  KKStr cmdStr (300);
356 
357  cmdStr << _param.ToCmdLineStr ();
358 
359  return cmdStr;
360 } /* SvmParamToString */
361 
362 
363 
364 
365 /**
366  * @brief Convert all parameters to a command line string.
367 */
369 {
370  KKStr cmdStr (300);
371 
372  cmdStr = SvmParamToString (param);
373 
374  if (encodingMethod != SVM_EncodingMethod::NoEncoding)
375  cmdStr << " -Encode " + EncodingMethodToStr (encodingMethod);
376 
377  cmdStr << " -MT " << MachineTypeToStr (machineType);
378  cmdStr << " -SM " << SelectionMethodToStr (selectionMethod);
379 
380  if (machineType == SVM_MachineType::BoostSVM)
381  {
382  cmdStr << " -SamplingRate " << samplingRate;
383  }
384 
385  if (useProbabilityToBreakTies)
386  cmdStr << " -UseProbability";
387 
388  return cmdStr;
389 } /* ToString */
390 
391 
392 
393 
394 BinaryClassParmsPtr SVMparam::GetParamtersToUseFor2ClassCombo (MLClassPtr class1,
395  MLClassPtr class2
396  )
397 {
398  BinaryClassParmsPtr twoClassComboParms = NULL;
399 
400  if (binaryParmsList == NULL)
401  {
402  binaryParmsList = new BinaryClassParmsList (true);
403  twoClassComboParms = NULL;
404  }
405  else
406  {
407  twoClassComboParms = binaryParmsList->LookUp (class1, class2);
408  }
409 
410  if (!twoClassComboParms)
411  {
412  twoClassComboParms = new BinaryClassParms (class1, class2, param, selectedFeatures, 1.0f);
413  binaryParmsList->PushOnBack (twoClassComboParms);
414  }
415 
416  return twoClassComboParms;
417 } /* GetParamtersToUSeFor2ClassCombo */
418 
419 
420 
421 
422 
423 BinaryClassParmsPtr SVMparam::GetBinaryClassParms (MLClassPtr class1,
424  MLClassPtr class2
425  )
426 {
427  if (binaryParmsList == NULL)
428  return NULL;
429  else
430  return binaryParmsList->LookUp (class1, class2);
431 } /* GetBinaryClassParms */
432 
433 
434 
435 
437 {
438  return selectedFeatures;
439 }
440 
441 
442 
444 {
445  if (selectedFeatures == NULL)
446  return SelectedFeatures (fileDesc);
447  else
448  return selectedFeatures;
449 }
450 
451 
452 
454  MLClassPtr class1,
455  MLClassPtr class2
456  ) const
457 {
458  if (!binaryParmsList)
459  {
460  // This configuration file does not specify feature selection by binary classes,
461  // so return the general one specified for model.
462  return SelectedFeatures (fileDesc);
463  }
464 
465  BinaryClassParmsPtr twoClassComboParm = binaryParmsList->LookUp (class1, class2);
466  if (!twoClassComboParm)
467  return SelectedFeatures (fileDesc);
468  else
469  return twoClassComboParm->SelectedFeaturesFD (fileDesc);
470 } /* GetFeatureNums */
471 
472 
473 
474 
475 void SVMparam::AddBinaryClassParms (BinaryClassParmsPtr binaryClassParms)
476 {
477  if (!binaryParmsList)
478  binaryParmsList = new BinaryClassParmsList (true);
479 
480  binaryParmsList->PushOnBack (binaryClassParms);
481 } /* AddBinaryClassParms */
482 
483 
484 
485 float SVMparam::AvgMumOfFeatures (FileDescPtr fileDesc) const
486 {
487  float avgNumOfFeatures = 0.0f;
488 
489  if ((machineType == SVM_MachineType::BinaryCombos) && (binaryParmsList))
490  avgNumOfFeatures = binaryParmsList->FeatureCountNet (fileDesc);
491 
492  if (avgNumOfFeatures == 0.0f)
493  avgNumOfFeatures = (float)SelectedFeatures (fileDesc)->NumOfFeatures ();
494 
495  return avgNumOfFeatures;
496 } /* AvgMumOfFeatures */
497 
498 
499 
500 /**
501  *@brief Returns back the class weighted average number of features per training example.
502  *@details Will calculate the average number of features per training example. For each
503  * binary class combination, multiplies the number of training examples for that pair
504  * by the number of features for that pair. the sum of all class pairs are then
505  * divided by the total number of examples.
506  *@param[in] trainExamples List of training examples that were or are to be used to train with.
507  */
508 float SVMparam::AvgNumOfFeatures (FeatureVectorListPtr trainExamples) const
509 {
510  FileDescPtr fileDesc = trainExamples->FileDesc ();
511  if (!selectedFeatures)
512  {
513  // The method "SelectedFeatures" will set 'selectedFeatures' to a new instance using 'fileDesc'.
514  // This way we make sure that the variable exists.
515  SelectedFeatures (fileDesc);
516  }
517  if (machineType == SVM_MachineType::BinaryCombos)
518  {
519  kkint32 totalNumFeaturesUsed = 0;
520  kkint32 toatlNumExamples = 0;
521  ClassStatisticListPtr stats = trainExamples->GetClassStatistics ();
522  if (!stats)
523  {
524  return (float)selectedFeatures->NumOfFeatures ();
525  }
526 
527  kkuint32 idx1 = 0;
528  kkuint32 idx2 = 0;
529  for (idx1 = 0; idx1 < (stats->size() - 1); idx1++)
530  {
531  ClassStatisticPtr class1Stats = stats->IdxToPtr (idx1);
532  MLClassPtr class1 = class1Stats->MLClass ();
533  kkuint32 class1Qty = class1Stats->Count ();
534 
535  for (idx2 = idx1 + 1; idx2 < (stats->size()); idx2++)
536  {
537  ClassStatisticPtr class2Stats = stats->IdxToPtr (idx2);
538  MLClassPtr class2 = class2Stats->MLClass ();
539  kkuint32 class2Qty = class2Stats->Count ();
540 
541  kkint32 numFeaturesThisCombo = 0;
542  FeatureNumListConstPtr cpfn = GetFeatureNums (fileDesc, class1, class2);
543  if (cpfn)
544  numFeaturesThisCombo = cpfn->NumSelFeatures ();
545 
546  kkint32 numExamplesThisCombo = class1Qty + class2Qty;
547 
548  totalNumFeaturesUsed += numFeaturesThisCombo * numExamplesThisCombo;
549  toatlNumExamples += numExamplesThisCombo;
550  }
551  }
552  delete stats; stats = NULL;
553  return (float)totalNumFeaturesUsed / (float)toatlNumExamples;
554  }
555  else if (!selectedFeatures)
556  {
557  return 0.0f;
558  }
559  else
560  {
561  return (float)selectedFeatures->NumOfFeatures ();
562  }
563 } /* AvgMumOfFeatures */
564 
565 
566 
568  RunLog& log
569  ) const
570 {
571  if (!selectedFeatures)
572  {
573  // The method "SelectedFeatures" will set 'selectedFeatures' to a new instance using 'fileDesc'.
574  // This way we make sure that the variable exists.
575  SelectedFeatures (fileDesc);
576  }
577 
578  if (!selectedFeatures)
579  return 0;
580 
581  kkint32 z;
582  kkint32 numFeaturesAfterEncoding = 0;
583  kkint32 numOfFeaturesSelected = selectedFeatures->NumOfFeatures ();
584 
585  switch (EncodingMethod ())
586  {
588  for (z = 0; z < numOfFeaturesSelected; z++)
589  {
590  kkint32 fieldNum = (*selectedFeatures)[z];
591  if ((fileDesc->Type (fieldNum) == AttributeType::Nominal) ||
592  (fileDesc->Type (fieldNum) == AttributeType::Symbolic)
593  )
594  numFeaturesAfterEncoding += fileDesc->Cardinality (fieldNum);
595  else
596  numFeaturesAfterEncoding ++;
597  }
598  break;
599 
602  default:
603  //numFeaturesAfterEncoding = fileDesc->NumOfFields ( );
604  numFeaturesAfterEncoding = selectedFeatures->NumOfFeatures ();
605  break;
606  }
607 
608  return numFeaturesAfterEncoding;
609 } /* NumOfFeaturesAfterEncoding */
610 
611 
612 
613 
614 void SVMparam::SetFeatureNums (MLClassPtr _class1,
615  MLClassPtr _class2,
616  FeatureNumListConstPtr _features,
617  float _weight
618  )
619 {
620  if (!binaryParmsList)
621  {
622  if (_weight < 0)
623  _weight = 1;
624  AddBinaryClassParms (_class1, _class2, param, _features, _weight);
625  }
626  else
627  {
628  BinaryClassParmsPtr binaryParms = binaryParmsList->LookUp (_class1, _class2);
629  if (binaryParms)
630  {
631  if (_weight < 0)
632  _weight = binaryParms->Weight ();
633  binaryParms->SelectedFeatures (_features);
634  }
635  else
636  {
637  if (_weight < 0)
638  _weight = 1.0f;
639  AddBinaryClassParms (_class1, _class2, param, _features, _weight);
640  }
641  }
642 } /* SetFeatureNums */
643 
644 
645 
646 void SVMparam::SetFeatureNums (const FeatureNumList& _features)
647 {
648  delete selectedFeatures;
649  selectedFeatures = new FeatureNumList (_features);
650 } /* SetFeatureNums */
651 
652 
653 
654 
656 {
657  delete selectedFeatures;
658  selectedFeatures = new FeatureNumList (*_features);
659 } /* SetFeatureNums */
660 
661 
662 
663 double SVMparam::C_Param (MLClassPtr class1,
664  MLClassPtr class2
665  ) const
666 {
667  if (!binaryParmsList)
668  {
669  cerr << endl << endl
670  << "SVMparam::C_Param ***ERROR*** 'binaryParmsList' is not defined." << endl
671  << endl;
672  return 0.0;
673  }
674 
675  BinaryClassParmsPtr binaryParms = binaryParmsList->LookUp (class1, class2);
676  if (!binaryParms)
677  {
678  cerr << endl << endl
679  << "SVMparam::C_Param ***ERROR*** No entry for Class[" << class1->Name () << ", " << class2->Name () << "]" << endl
680  << endl;
681  return 0.0;
682  }
683  else
684  {
685  return binaryParms->C ();
686  }
687 }
688 
689 
690 
691 
692 void SVMparam::C_Param (MLClassPtr class1,
693  MLClassPtr class2,
694  double cParam
695  )
696 {
697  if (!binaryParmsList)
698  {
699  cerr << endl << endl
700  << "SVMparam::C_Param ***ERROR*** 'binaryParmsList' is not defined." << endl
701  << endl;
702  return;
703  }
704 
705  BinaryClassParmsPtr binaryParms = binaryParmsList->LookUp (class1, class2);
706  if (!binaryParms)
707  {
708  svm_parameter binaryParam = param;
709  binaryParam.C = C_Param ();
710  AddBinaryClassParms (class1, class2, binaryParam, SelectedFeatures (), 1.0f);
711  }
712  else
713  {
714  binaryParms->C (cParam);
715  }
716 } /* C_Param */
717 
718 
719 
720 
721 void SVMparam::SetBinaryClassFields (MLClassPtr _class1,
722  MLClassPtr _class2,
723  const svm_parameter& _param,
724  FeatureNumListConstPtr _features,
725  float _weight
726  )
727 {
728  if (!binaryParmsList)
729  {
730  AddBinaryClassParms (_class1, _class2, _param, _features, _weight);
731  }
732  else
733  {
734  BinaryClassParmsPtr binaryParms = binaryParmsList->LookUp (_class1, _class2);
735  if (binaryParms)
736  {
737  binaryParms->Param (_param);
738  binaryParms->SelectedFeatures (_features);
739  binaryParms->Weight (_weight);
740  }
741  else
742  {
743  AddBinaryClassParms (_class1, _class2, _param, _features, _weight);
744  }
745  }
746 } /* SetBinaryClassFields */
747 
748 
749 
750 
751 
752 /**
753  * @brief Add a Binary parameters using svm_parametr cmd line str.
754  * Typically used by TrainingConfiguration.
755 */
756 void SVMparam::AddBinaryClassParms (MLClassPtr _class1,
757  MLClassPtr _class2,
758  const svm_parameter& _param,
759  FeatureNumListConstPtr _selectedFeatures, /**< We will NOT be taking ownership; we will make our own copy. */
760  float _weight
761  )
762 {
763  AddBinaryClassParms (new BinaryClassParms (_class1, _class2, _param, _selectedFeatures, _weight));
764 } /* AddBinaryClassParms */
765 
766 
767 
768 
769 void SVMparam::WriteXML (const KKStr& varName,
770  ostream& o
771  ) const
772 {
773  XmlTag startTag ("SVMparam", XmlTag::TagTypes::tagStart);
774  if (!varName.Empty ())
775  startTag.AddAtribute ("VarName", varName);
776  startTag.WriteXML (o);
777  o << endl;
778 
779  {
781 
782  headerFields->Add ("EncodingMethod", EncodingMethodToStr (encodingMethod));
783  headerFields->Add ("FileName", fileName);
784  headerFields->Add ("MachineType", MachineTypeToStr (machineType));
785  if (selectedFeatures)
786  headerFields->Add ("SelectedFeatures", selectedFeatures->ToString ());
787  headerFields->Add ("param", param.ToTabDelStr ());
788 
789  headerFields->Add ("samplingRate", samplingRate);
790  headerFields->Add ("selectionMethod", SelectionMethodToStr (selectionMethod));
791 
792 
793  headerFields->Add ("useProbabilityToBreakTies", useProbabilityToBreakTies);
794 
795 
796  headerFields->WriteXML ("HeaderFields", o);
797  delete headerFields;
798  headerFields = NULL;
799  }
800 
801  XmlElementVectorFloat::WriteXML (probClassPairs, "probClassPairs", o);
802 
803  if (binaryParmsList)
804  binaryParmsList->WriteXML ("binaryParmsList", o);
805 
806  XmlTag endTag ("SVMparam", XmlTag::TagTypes::tagEnd);
807  endTag.WriteXML (o);
808  o << endl;
809 } /* WriteXML */
810 
811 
812 
813 
814 
816  XmlTagConstPtr tag,
817  VolConstBool& cancelFlag,
818  RunLog& log
819  )
820 {
821  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
822  while (t && (!cancelFlag))
823  {
824  const KKStr& varName = t->VarName ();
825 
826  if (varName.EqualIgnoreCase ("HeaderFields") && (typeid (*t) == typeid (XmlElementKeyValuePairs)))
827  {
829  if (kvp && (kvp->Value() != NULL))
830  {
831  for (auto idx: *(kvp->Value ()))
832  {
833  if (idx.first.EqualIgnoreCase ("EncodingMethod"))
834  encodingMethod = EncodingMethodFromStr (idx.second);
835 
836  else if (idx.first.EqualIgnoreCase ("FileName"))
837  fileName = idx.second;
838 
839  else if (idx.first.EqualIgnoreCase ("MachineType"))
840  machineType = MachineTypeFromStr (idx.second);
841 
842  else if (idx.first.EqualIgnoreCase ("SelectedFeatures"))
843  {
844  bool successful = false;
845  selectedFeatures = new FeatureNumList (idx.second, successful);
846  }
847 
848  else if (idx.first.EqualIgnoreCase ("Param"))
849  param.ParseTabDelStr (idx.second);
850 
851  else if (idx.first.EqualIgnoreCase ("SamplingRate"))
852  samplingRate = idx.second.ToFloat ();
853 
854  else if (idx.first.EqualIgnoreCase ("SelectionMethod"))
855  selectionMethod = SelectionMethodFromStr (idx.second);
856 
857  else if (idx.first.EqualIgnoreCase ("UseProbabilityToBreakTies"))
858  useProbabilityToBreakTies = idx.second.ToBool ();
859 
860  else
861  {
862  log.Level (-1) << endl
863  << "SVMparam::ReadXML ***ERROR*** UnRecognozed Header Field: " << idx.first << ":" << idx.second << endl
864  << endl;
865  }
866  }
867  }
868  }
869  delete t;
870  t = s.GetNextToken (cancelFlag, log);
871  }
872  delete t;
873  t = NULL;
874 } /* ReadXML */
875 
876 
878 
879 
880 
881 
882 
883 
884 
886 {
887  if (encodingMethod == SVM_EncodingMethod::Binary)
888  return "Binary";
889 
890  else if (encodingMethod == SVM_EncodingMethod::Scaled)
891  return "Scale";
892 
893  else
894  return "None";
895 } /* EncodingMethodToStr */
896 
897 
898 
900 {
901  KKStr encodingMethodUpper = encodingMethodStr.ToUpper ();
902 
903  if ((encodingMethodUpper == "BINARY") || (encodingMethodUpper == "BIN"))
905 
906  if (encodingMethodUpper == "SCALE")
908 
909  if (encodingMethodUpper == "NONE")
911 
913 } /* EncodingMethodFromStr */
914 
915 
916 
918 {
919  switch (kernalType)
920  {
921  case SVM_KernalType::Linear: return "Linear";
922  case SVM_KernalType::Polynomial: return "Polynomial";
923  case SVM_KernalType::RBF: return "RBF";
924  default: return "UnKnown";
925  }
926 
927  return "";
928 } /* KernalTypeToStr */
929 
930 
931 
933 {
934  KKStr kernalTypeUpper = kernalTypeStr;
935  kernalTypeUpper.Upper ();
936 
937  if ((kernalTypeUpper == "LINEAR") ||
938  (kernalTypeUpper == "LIN") ||
939  (kernalTypeUpper == "L")
940  )
941  return SVM_KernalType::Linear;
942 
943  if ((kernalTypeUpper == "POLYNOMIAL") ||
944  (kernalTypeUpper == "POLY") ||
945  (kernalTypeUpper == "P") ||
946  (kernalTypeUpper == "PN")
947  )
949 
950 
951  if ((kernalTypeUpper == "RBF") ||
952  (kernalTypeUpper == "R") ||
953  (kernalTypeUpper == "RADIALBASIS") ||
954  (kernalTypeUpper == "RADIALBASISFUNC") ||
955  (kernalTypeUpper == "RADIALBASISFUNCTION")
956  )
957  return SVM_KernalType::RBF;
958 
959  return SVM_KernalType::Linear;
960 } /* KernalTypeToStr */
961 
962 
963 
964 
966 {
967  if (machineType == SVM_MachineType::OneVsOne)
968  return "OneVsOne";
969 
970  else if (machineType == SVM_MachineType::OneVsAll)
971  return "OneVsAll";
972 
973  else if (machineType == SVM_MachineType::BinaryCombos)
974  return "Binary";
975 
976  else if (machineType == SVM_MachineType::BoostSVM)
977  return "BoostSVM";
978 
979  return "";
980 } /* MachineTypeToStr */
981 
982 
983 
984 
985 
987 {
988  KKStr machineTypeUpper = machineTypeStr.ToUpper ();
989 
990  if (machineTypeUpper == "ONEVSONE")
992 
993  else if (machineTypeUpper == "ONEVSALL")
995 
996  else if (machineTypeUpper == "BINARY")
998 
999  else if (machineTypeUpper == "BOOSTSVM")
1001 
1002  return SVM_MachineType::Null;
1003 } /* MachineTypeFromStr */
1004 
1005 
1006 
1007 
1009 {
1010  if (selectionMethod == SVM_SelectionMethod::Voting)
1011  return "Voting";
1012 
1013  if (selectionMethod == SVM_SelectionMethod::Probability)
1014  return "Probability";
1015 
1016  return "";
1017 } /* SelectionMethodToStr */
1018 
1019 
1020 
1021 
1023 {
1024  KKStr selectionMethodUpper = selectionMethodStr.ToUpper ();
1025 
1026  if ((selectionMethodUpper == "VOTE") || (selectionMethodUpper == "VOTING"))
1028 
1029  if ((selectionMethodUpper == "P") ||
1030  (selectionMethodUpper == "PROB") ||
1031  (selectionMethodUpper == "PROBABILITY")
1032  )
1034 
1036 
1037 } /* SelectionMethodFromStr */
__int16 kkint16
16 bit signed integer.
Definition: KKBaseTypes.h:85
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
KKStr SvmParamToString(const svm_parameter &_param) const
Convert a svm_parameter struct to a cmdline str.
Definition: SVMparam.cpp:353
char operator[](kkint16 i) const
Definition: KKStr.cpp:3379
bool EqualIgnoreCase(const char *s2) const
Definition: KKStr.cpp:1257
BinaryClassParms(MLClassPtr _class1, MLClassPtr _class2, const svm_parameter &_param, FeatureNumListConstPtr _selectedFeatures, float _weight)
Constructor for &#39;BinaryClassParms&#39; where caller supplies the two classes and parameters for that spec...
void ParseCmdLineParameter(const KKStr &parameter, const KKStr &value, bool &parameterUsed, bool &_validFormat, RunLog &log)
Definition: SVMparam.cpp:202
kkint32 MemoryConsumedEstimated() const
Definition: KKStr.cpp:766
SVM_SelectionMethod
Definition: SVMparam.h:34
__int32 kkint32
Definition: KKBaseTypes.h:88
SVM_KernalType
Definition: SVMparam.h:54
FeatureNumListConstPtr SelectedFeatures() const
Definition: SVMparam.h:165
void SetBinaryClassFields(MLClassPtr _class1, MLClassPtr _class2, const svm_parameter &_param, FeatureNumListConstPtr _features, float _weight)
Definition: SVMparam.cpp:721
MLClassPtr MLClass() const
void ProbClassPairsInitialize(const ClassAssignments &assignments)
Definition: SVMparam.cpp:153
Similar to SVMparam except it is specialized for two classes.
void Add(const KKStr &key, float v)
Definition: XmlStream.cpp:1212
MLClassPtr GetMLClass(kkint16 num) const
Keeps track of selected features.
FeatureNumList(FileDescPtr _fileDesc)
kkint32 NumOfFeaturesAfterEncoding(FileDescPtr fileDesc, RunLog &log) const
Definition: SVMparam.cpp:567
BinaryClassParmsPtr LookUp(MLClassPtr _class1, MLClassPtr _class2) const
KKMLL::AttributeType Type(kkint32 fieldNum) const
Definition: FileDesc.cpp:370
SVM_MachineType
Definition: SVMparam.h:26
KKStr ExtractToken(const char *delStr="\n\t\r ")
Definition: KKStr.cpp:2969
void SelectedFeatures(FeatureNumListConst &_selectedFeatures)
KKStr EncodingMethodToStr(SVM_EncodingMethod encodingMethod)
FeatureNumListConstPtr GetFeatureNums(FileDescPtr fileDesc) const
Definition: SVMparam.cpp:443
SVMparam(const SVMparam &_svmParam)
Definition: SVMparam.cpp:75
KKStr ToCmdLineStr() const
Definition: svm.cpp:1069
void AddBinaryClassParms(BinaryClassParmsPtr binaryClassParms)
Definition: SVMparam.cpp:475
void A_Param(float _A)
Definition: SVMparam.cpp:187
void Weight(float _weight)
KKStr KernalTypeToStr(SVM_KernalType kernalType)
Definition: SVMparam.cpp:917
BinaryClassParmsPtr GetParamtersToUseFor2ClassCombo(MLClassPtr class1, MLClassPtr class2)
Definition: SVMparam.cpp:394
const FileDescPtr FileDesc() const
SVM_EncodingMethod EncodingMethodFromStr(const KKStr &encodingMethodStr)
Definition: SVMparam.cpp:899
SVM_MachineType MachineTypeFromStr(const KKStr &machineTypeStr)
Definition: SVMparam.cpp:986
bool operator==(const char *rtStr) const
Definition: KKStr.cpp:1588
KKStr ToUpper() const
Definition: KKStr.cpp:2517
double C_Param(MLClassPtr class1, MLClassPtr class2) const
Definition: SVMparam.cpp:663
void SetFeatureNums(MLClassPtr _class1, MLClassPtr _class2, FeatureNumListConstPtr _features, float _weight=-1)
Sets the selected Features and Weight for the binary class SVM specified by _class1 and _class2...
Definition: SVMparam.cpp:614
ClassStatistic * ClassStatisticPtr
SVM_EncodingMethod
Definition: SVMparam.h:46
float AvgMumOfFeatures(FileDescPtr fileDesc) const
Definition: SVMparam.cpp:485
SVM_EncodingMethod EncodingMethod() const
Definition: SVMparam.h:143
void Param(const svm_parameter &_param)
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
SVMparam(KKStr &_cmdLineStr, FeatureNumListConstPtr _selectedFeatures, bool &_validFormat, RunLog &_log)
Definition: SVMparam.cpp:29
void SetFeatureNums(FeatureNumListConstPtr _features)
Definition: SVMparam.cpp:655
float FeatureCountNet(FileDescPtr fileDesc) const
Returns the Average number of selected features.
FeatureNumList(const FeatureNumList &featureNumList)
Copy constructor.
void Add(const KKStr &key, bool v)
Definition: XmlStream.cpp:1232
double C_Param() const
Definition: SVMparam.h:137
KKStr & operator=(KKStr &&src)
Definition: KKStr.cpp:1369
kkuint32 Len() const
Returns the number of characters in the string.
Definition: KKStr.h:366
KKStr ToString() const
Convert all parameters to a command line string.
Definition: SVMparam.cpp:368
KKTHread * KKTHreadPtr
svm_parameter(KKStr &paramStr)
Definition: svm.cpp:983
kkuint16 operator[](kkint32 idx) const
Returns back the selected feature.
KKStr operator+(const char *left, const KKStr &right)
Definition: KKStr.cpp:3976
void Add(const KKStr &key, const KKStr &v)
Definition: XmlStream.cpp:1193
kkuint32 Count() const
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Definition: XmlStream.cpp:602
kkint32 NumOfFeatures() const
void SelectedFeatures(FeatureNumListConstPtr _selectedFeatures)
void TrimLeft(const char *whiteSpaceChars="\n\r\t ")
Definition: KKStr.cpp:1745
FeatureNumListConstPtr GetFeatureNums() const
Definition: SVMparam.cpp:436
bool Empty() const
Definition: KKStr.h:241
KKStr ToTabDelStr() const
Definition: svm.cpp:1104
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 void WriteXML(const KKStr &varName, std::ostream &o) const
void SetAllFeatures(FileDescPtr fileDesc)
Selects all features except those flagged as &#39;IgnoreAttribute&#39; in the associated FileDesc.
Binds MLClass objects to the appropriate number that the Learning Algorithm expects.
BinaryClassParmsPtr GetBinaryClassParms(MLClassPtr class1, MLClassPtr class2)
Definition: SVMparam.cpp:423
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
ClassStatisticListPtr GetClassStatistics() const
Returns the number of FeatureVectors per class.
void Upper()
Converts all characters in string to their Upper case equivalents via &#39;toupper&#39;.
Definition: KKStr.cpp:2461
SVM_SelectionMethod SelectionMethodFromStr(const KKStr &selectionMethodStr)
Definition: SVMparam.cpp:1022
AttributeType
Definition: Attribute.h:36
This class encapsulates are the information necessary to build a SVMModel class.
Definition: SVMparam.h:74
SVM_KernalType KernalTypeFromStr(const KKStr &kernalTypeStr)
Definition: SVMparam.cpp:932
virtual void PushOnBack(BinaryClassParmsPtr binaryParms)
FileDesc * FileDescPtr
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
Definition: SVMparam.cpp:815
void C_Param(MLClassPtr class1, MLClassPtr class2, double cParam)
Definition: SVMparam.cpp:692
ClassStatisticList * ClassStatisticListPtr
void SetFeatureNums(FeatureNumListConst &_features)
double ToDouble() const
Definition: KKStr.cpp:3541
XmlElementKeyValuePairs * XmlElementKeyValuePairsPtr
Definition: XmlStream.h:605
kkint32 MemoryConsumedEstimated() const
KKStr ToString() const
Returns comma delimited list of all features selected; will make use of range specification.
kkint32 NumSelFeatures() const
void ProcessSvmParameter(KKStr cmd, KKStr value, double valueNum, bool &parmUsed)
Definition: svm.cpp:900
float AvgNumOfFeatures(FeatureVectorListPtr trainExamples) const
Returns back the class weighted average number of features per training example.
Definition: SVMparam.cpp:508
void SelectedFeatures(FeatureNumListConstPtr _selectedFeatures)
Definition: SVMparam.cpp:145
void AddBinaryClassParms(MLClassPtr _class1, MLClassPtr _class2, const svm_parameter &_param, FeatureNumListConstPtr _selectedFeatures, float _weight)
Adding parameters that are specific to a class pair; this is used when using the BFS version of SVM...
Definition: SVMparam.cpp:756
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
BinaryClassParmsList * DuplicateListAndContents() const
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
svm_parameter & operator=(const svm_parameter &right)
Definition: svm.cpp:828
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
kkint32 Cardinality(kkint32 fieldNum) const
Definition: FileDesc.cpp:341
svm_parameter(const svm_parameter &parameters)
Definition: svm.cpp:768
FeatureNumListConstPtr GetFeatureNums(FileDescPtr fileDesc, MLClassPtr class1, MLClassPtr class2) const
Definition: SVMparam.cpp:453
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: SVMparam.cpp:769
void C_Param(double _CC)
Definition: SVMparam.cpp:195
XmlElementKeyValuePairs()
Used to construct an instance that will be written out to a XML file.
Definition: XmlStream.cpp:1134
kkint32 MemoryConsumedEstimated() const
FeatureNumListConstPtr SelectedFeaturesFD(FileDescPtr fileDesc) const
virtual const KKStr & VarName() const
Definition: XmlStream.h:269
#define XmlFactoryMacro(NameOfClass)
Definition: XmlStream.h:688
kkint32 MemoryConsumedEstimated() const
Definition: SVMparam.cpp:124
FeatureNumListConstPtr SelectedFeatures(FileDescPtr fileDesc) const
Definition: SVMparam.cpp:112
void WriteXML(const KKStr &varName, std::ostream &o)
Definition: XmlStream.cpp:1252
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163