16 #define _SCL_SECURE_NO_WARNINGS 17 #pragma warning(disable:4996
) 35 using namespace KKMLL;
223 const char*
_(
const char* str)
231 UsfCasCor::UsfCasCor ():
238 number_of_trials (1),
239 number_of_rounds (-1),
240 normalization_method (0),
246 number_of_classes (-1),
251 load_weights (
false),
258 SigmoidPrimeOffset (0.1f),
259 WeightMultiplier (1.0f),
261 OutputShrinkFactor (0.0f),
262 OutputEpsilon (0.35f),
263 OutputDecay (0.0001f),
265 OutputChangeThreshold (0.01f),
267 InputShrinkFactor (0.0f),
271 InputChangeThreshold (0.03f),
276 ScoreThreshold (0.35f),
279 DummySumErrors (NULL),
281 BestCandidateScore (0.0f),
289 NonRandomSeed (
false),
297 NTrainingPatterns (0),
299 TrainingInputs (NULL),
300 TrainingOutputs (NULL),
303 example_weight (NULL),
340 OutputWeights (NULL),
343 OutputPrevSlopes (NULL),
350 CandSumValues (NULL),
356 CandPrevSlopes (NULL),
363 AllConnections (NULL),
368 NtrainingOutputValues (0),
369 NtestOutputValues (0),
370 TrainingStdDev (1.0f),
373 ErrorIndexThreshold (0.2f),
381 InterruptPending (
false),
383 selectedFeatures (NULL)
392 UsfCasCor::~UsfCasCor ()
394 delete classes; classes = NULL;
395 delete selectedFeatures; selectedFeatures = NULL;
401 void UsfCasCor::Delete2DArray (T** &A,
408 for (kkuint32 x = 0; x < numRows; ++x)
419 void UsfCasCor::CleanUpMemory ()
421 delete ExtraValues; ExtraValues = NULL;
422 delete Nconnections; Nconnections = NULL;
423 delete Connections; Connections = NULL;
425 Delete2DArray (Weights, MaxUnits);
427 delete ExtraErrors; ExtraErrors = NULL;
428 delete SumErrors; SumErrors = NULL;
429 delete DummySumErrors; DummySumErrors = NULL;
431 delete Outputs; Outputs = NULL;
433 Delete2DArray (OutputWeights, Noutputs);
434 Delete2DArray (OutputDeltas, Noutputs);
435 Delete2DArray (OutputSlopes, Noutputs);
436 Delete2DArray (OutputPrevSlopes, Noutputs);
438 delete CandValues; CandValues = NULL;
439 delete CandSumValues; CandSumValues = NULL;
441 Delete2DArray (CandCor, Ncandidates);
442 Delete2DArray (CandPrevCor, Ncandidates);
443 Delete2DArray (CandWeights, Ncandidates);
444 Delete2DArray (CandDeltas, Ncandidates);
445 Delete2DArray (CandSlopes, Ncandidates);
446 Delete2DArray (CandPrevSlopes, Ncandidates);
447 Delete2DArray (TrainingInputs, NTrainingPatterns);
448 Delete2DArray (TrainingOutputs, NTrainingPatterns);
449 Delete2DArray (TestInputs, NTestPatterns);
450 Delete2DArray (TestOutputs, NTestPatterns);
451 Delete2DArray (ValuesCache, MaxCases);
452 Delete2DArray (ErrorsCache, MaxCases);
454 delete feature_type; feature_type = NULL;
455 delete AllConnections; AllConnections = NULL;
465 kkint32 UsfCasCor::MemoryConsumedEstimated ()
const 467 kkint32 memoryConsumedEstimated =
sizeof (*
this);
469 if (feature_type) memoryConsumedEstimated += Ninputs *
sizeof (
int);
470 if (SumErrors) memoryConsumedEstimated += Noutputs *
sizeof (
float);
471 if (DummySumErrors) memoryConsumedEstimated += Noutputs *
sizeof (
float);
472 if (TrainingInputs) memoryConsumedEstimated += NTrainingPatterns * Ninputs *
sizeof (
float);
473 if (TrainingOutputs) memoryConsumedEstimated += NTrainingPatterns * Noutputs *
sizeof (
float);
475 if (AllConnections) memoryConsumedEstimated += MaxUnits *
sizeof (
int);
476 if (Nconnections) memoryConsumedEstimated += MaxUnits *
sizeof (
int);
477 if (Connections) memoryConsumedEstimated += MaxUnits *
sizeof (
int*);
481 memoryConsumedEstimated += MaxUnits *
sizeof (
float*);
482 for (
int x = 0; x < MaxUnits; ++x)
483 memoryConsumedEstimated += Nconnections[x] *
sizeof (
float);
486 if (ExtraValues) memoryConsumedEstimated += MaxUnits *
sizeof (
float);
487 if (example_weight) memoryConsumedEstimated += NTrainingPatterns *
sizeof (
float);
488 if (ValuesCache) memoryConsumedEstimated += MaxCases * MaxUnits *
sizeof (
float);
489 if (ErrorsCache) memoryConsumedEstimated += MaxCases * Noutputs *
sizeof (
float);
490 if (Outputs) memoryConsumedEstimated += Noutputs *
sizeof (
float);
493 if (OutputWeights) memoryConsumedEstimated += MaxCases * Noutputs *
sizeof (
float);
494 if (OutputDeltas) memoryConsumedEstimated += MaxCases * Noutputs *
sizeof (
float);
495 if (OutputSlopes) memoryConsumedEstimated += MaxCases * Noutputs *
sizeof (
float);
496 if (OutputPrevSlopes) memoryConsumedEstimated += MaxCases * Noutputs *
sizeof (
float);
498 if (ExtraErrors) memoryConsumedEstimated += Noutputs *
sizeof (
float);
499 if (CandValues) memoryConsumedEstimated += Noutputs *
sizeof (
float);
500 if (CandSumValues) memoryConsumedEstimated += Noutputs *
sizeof (
float);
502 if (CandCor) memoryConsumedEstimated += Ncandidates * Noutputs *
sizeof (
float);
503 if (CandPrevCor) memoryConsumedEstimated += Ncandidates * Noutputs *
sizeof (
float);
504 if (CandWeights) memoryConsumedEstimated += Ncandidates * MaxUnits *
sizeof (
float);
505 if (CandDeltas) memoryConsumedEstimated += Ncandidates * MaxUnits *
sizeof (
float);
506 if (CandSlopes) memoryConsumedEstimated += Ncandidates * MaxUnits *
sizeof (
float);
507 if (CandPrevSlopes) memoryConsumedEstimated += Ncandidates * MaxUnits *
sizeof (
float);
509 return memoryConsumedEstimated;
516 const char* UsfCasCor::version=
"5.0";
517 const char* UsfCasCor::release_date=
"07-07-2012";
518 const char* UsfCasCor::progname=
"UsfCasCor";
522 MLClassPtr UsfCasCor::PredictClass (FeatureVectorPtr example)
525 _load_test_data (example);
533 float old_ST = ScoreThreshold;
534 float old_TE = TrueError;
535 float *old_SE = SumErrors;
536 float old_SSE = SumSqError;
541 Values = ExtraValues;
542 Errors = ExtraErrors;
546 Goal = TestOutputs[i];
547 FULL_FORWARD_PASS (TestInputs[i]);
551 for (
int j = 0; j < Noutputs; j++)
553 if (Outputs[max_pred] < Outputs[j])
557 if (max_pred < classes->QueueSize ())
558 predictedClass = classes->IdxToPtr (max_pred);
563 ScoreThreshold = old_ST;
566 SumSqError = old_SSE;
568 return predictedClass;
574 void UsfCasCor::PredictConfidences (FeatureVectorPtr example,
577 float& predClass1Prob,
579 float& predClass2Prob,
580 float& knownClassProb,
586 _load_test_data (example);
592 float old_ST = ScoreThreshold;
593 float old_TE = TrueError;
594 float* old_SE = SumErrors;
595 float old_SSE = SumSqError;
600 Values = ExtraValues;
601 Errors = ExtraErrors;
605 SumErrors = DummySumErrors;
608 float totalDelta = 0.0f;
611 predClass1Prob = -9999.99f;
612 predClass2Prob = -9999.99f;
616 Goal = TestOutputs[i];
617 FULL_FORWARD_PASS (TestInputs[i]);
621 for (
int j = 0; j < Noutputs; j++)
622 totalDelta += (Outputs[j] - SigmoidMin);
625 probabilities.clear ();
626 for (
int j = 0; j < Noutputs; j++)
630 float prob = (Outputs[j] - SigmoidMin) / totalDelta;
632 if (ic == knownClass)
633 knownClassProb = prob;
635 if (prob > predClass1Prob)
637 predClass2 = predClass1;
638 predClass2Prob = predClass1Prob;
640 predClass1Prob = prob;
642 else if (prob > predClass2Prob)
645 predClass2Prob = prob;
649 probabilities.clear ();
650 MLClassList::const_iterator idx;
651 for (idx = classOrder.begin (); idx != classOrder.end (); ++idx)
654 int j = classes->PtrToIdx (ic);
657 float prob = (Outputs[j] - SigmoidMin) / totalDelta;
658 probabilities.push_back (prob);
662 probabilities.push_back (0.0f);
668 ScoreThreshold = old_ST;
671 SumSqError = old_SSE;
681 ClassProbListPtr UsfCasCor::PredictClassConfidences (FeatureVectorPtr example)
684 _load_test_data (example);
690 float old_ST = ScoreThreshold;
691 float old_TE = TrueError;
692 float* old_SE = SumErrors;
693 float old_SSE = SumSqError;
698 Values = ExtraValues;
699 Errors = ExtraErrors;
703 SumErrors = DummySumErrors;
706 float totalDelta = 0.0f;
709 Goal = TestOutputs[i];
710 FULL_FORWARD_PASS (TestInputs[i]);
711 for (
int j = 0; j < Noutputs; j++)
712 totalDelta += (Outputs[j] - SigmoidMin);
717 if (totalDelta == 0.0f)
719 float p = 1.0f / Noutputs;
720 for (
int j = 0; j < Noutputs; j++)
728 for (
int j = 0; j < Noutputs; j++)
731 float prob = (Outputs[j] - SigmoidMin) / totalDelta;
738 ScoreThreshold = old_ST;
741 SumSqError = old_SSE;
750 void UsfCasCor::TrainNewClassifier (
kkint32 _in_limit,
756 FeatureVectorListPtr _trainData,
762 _log.Level (10) <<
"Cascade Correlation: Version[" << version <<
"]" << endl;
764 if (_selectedFeatures)
770 FeatureVectorListPtr filteredTrainData = FilterOutExtremeExamples (_trainData);
776 INITIALIZE_GLOBALS ();
778 in_limit = _in_limit;
779 out_limit = _out_limit;
780 number_of_rounds = _number_of_rounds;
781 number_of_trials = _number_of_trials;
782 the_random_seed = _the_random_seed;
789 setup_network (filteredTrainData, _log);
791 train_network (_cancelFlag, _log);
793 delete filteredTrainData;
804 FeatureVectorListPtr UsfCasCor::FilterOutExtremeExamples (FeatureVectorListPtr trainExamples)
812 FeatureVectorList::iterator idx;
813 for (idx = trainExamples->begin (); idx != trainExamples->end (); ++idx)
815 bool extremeValuesFound =
false;
816 FeatureVectorPtr fv = *idx;
818 for (
kkint32 x = 0; x < numSelFeatures; ++x)
822 extremeValuesFound =
true;
827 if (!extremeValuesFound)
840 void UsfCasCor::setup_network (FeatureVectorListPtr trainExamples,
848 load_namesfile (trainExamples, selectedFeatures);
858 if (number_of_rounds == -1)
862 MaxUnits = Ninputs + 1 + number_of_rounds;
867 allocate_network (log);
868 load_data (trainExamples, log);
875 if (the_random_seed <= 0)
876 the_random_seed = time(NULL);
881 log.Level (10) <<
"Starting seed " << ((NonRandomSeed)?
"fixed":
"random") <<
" at " << the_random_seed << endl;
884 initialize_network ();
892 void UsfCasCor::train_network (
VolConstBool& cancelFlag,
898 long total_epochs, total_units, total_trials;
899 long min_units, max_units, min_epochs, max_epochs;
913 log.Level (10) <<
"UsfCasCor::train_network Starting run, " 914 <<
"Ilim[" << in_limit <<
"] " 915 <<
"Olim [" << MaxUnits <<
"] " 916 <<
"NumberOfRounds[" << number_of_rounds <<
"] " 917 <<
"NumberOfTrials[" << number_of_trials <<
"]." 920 for (i = 0; (i < number_of_trials) && (!cancelFlag); i++)
924 if (number_of_trials > 1)
925 log.Level (10) <<
"train_network Trial " << Trial << endl;
927 switch (TRAIN (out_limit, in_limit, number_of_rounds, cancelFlag, log))
940 TEST_EPOCH (ScoreThreshold, log);
943 printf (
" Connection Crossings: %d\n\n", conx);
947 nhidden = Nunits - Ninputs - 1;
948 total_epochs += Epoch;
949 total_units += nhidden;
951 min_epochs = (Epoch < min_epochs) ? Epoch : min_epochs;
952 max_epochs = (Epoch > max_epochs) ? Epoch : max_epochs;
953 min_units = (nhidden < min_units) ? nhidden : min_units;
954 max_units = (nhidden > max_units) ? nhidden : max_units;
958 log.Level (10) << endl <<
"train_network TRAINING STATS" << endl;
960 log.Level (10) <<
"Victories: " << vics <<
", Defeats: " << defs << endl;
962 log.Level (10) <<
"Training Epochs - " 963 <<
"Min: " << min_epochs <<
" " 964 <<
"Avg: " << (total_epochs / total_trials) <<
" " 965 <<
"Max: " << max_epochs
968 log.Level (10) <<
"Hidden Units - " 969 <<
"Min: " << min_units <<
" " 970 <<
"Avg: " << ((
float)total_units /total_trials) <<
" " 971 <<
"Max: " << max_units
983 void UsfCasCor::allocate_network (
RunLog& log)
988 if (NTrainingPatterns > NTestPatterns)
989 MaxCases = NTrainingPatterns;
991 MaxCases = NTestPatterns;
993 Ncases = NTrainingPatterns;
995 Nunits = 1 + Ninputs;
999 NtrainingOutputValues = Noutputs * NTrainingPatterns;
1000 NtestOutputValues = Noutputs * NTestPatterns;
1002 if (Nunits > MaxUnits)
1004 log.Level (-1) << endl
1005 <<
"UsfCasCor::allocate_network ***ERROR*** MaxUnits[" << MaxUnits <<
"] must be greater than Ninputs[" << Ninputs <<
"]." << endl
1006 <<
" Adjusting MaxUnits to [" << (Nunits + 1) <<
"]." << endl
1009 MaxUnits = Nunits + 1;
1014 ExtraValues =
new float[MaxUnits];
1015 Values = ExtraValues;
1017 Nconnections =
new int[MaxUnits];
1018 Connections =
new int*[MaxUnits];
1019 Weights =
new float*[MaxUnits];
1021 ExtraErrors =
new float[Noutputs];
1022 SumErrors =
new float[Noutputs];
1023 DummySumErrors =
new float[Noutputs];
1024 Errors = ExtraErrors;
1026 Outputs =
new float[Noutputs];
1027 OutputWeights =
new float*[Noutputs];
1028 OutputDeltas =
new float*[Noutputs];
1029 OutputSlopes =
new float*[Noutputs];
1030 OutputPrevSlopes =
new float*[Noutputs];
1032 CandValues =
new float[Ncandidates];
1033 CandSumValues =
new float[Ncandidates];
1034 CandCor =
new float*[Ncandidates];
1035 CandPrevCor =
new float*[Ncandidates];
1036 CandWeights =
new float*[Ncandidates];
1037 CandDeltas =
new float*[Ncandidates];
1038 CandSlopes =
new float*[Ncandidates];
1039 CandPrevSlopes =
new float*[Ncandidates];
1041 TrainingInputs =
new float*[NTrainingPatterns];
1042 TrainingOutputs =
new float*[NTrainingPatterns];
1046 TestInputs =
new float*[NTestPatterns];
1047 TestOutputs =
new float*[NTestPatterns];
1052 TestInputs = TrainingInputs;
1053 TestOutputs = TrainingOutputs;
1059 ValuesCache =
new float*[MaxCases];
1060 ErrorsCache =
new float*[MaxCases];
1061 for (i = 0; i < MaxCases; i++)
1063 ValuesCache[i] =
new float[MaxUnits];
1064 ErrorsCache[i] =
new float[Noutputs];
1069 for (i = 0; i < Noutputs; i++)
1071 OutputWeights [i] =
new float[MaxUnits];
1072 OutputDeltas [i] =
new float[MaxUnits];
1073 OutputSlopes [i] =
new float[MaxUnits];
1074 OutputPrevSlopes[i] =
new float[MaxUnits];
1077 for (i = 0; i < Ncandidates; i++)
1079 CandCor[i] =
new float[Noutputs];
1080 CandPrevCor[i] =
new float[Noutputs];
1082 CandWeights[i] =
new float[MaxUnits];
1083 CandDeltas[i] =
new float[MaxUnits];
1084 CandSlopes[i] =
new float[MaxUnits];
1085 CandPrevSlopes[i] =
new float[MaxUnits];
1089 for (i = 0; i < NTrainingPatterns; i++)
1091 TrainingInputs[i] =
new float[Ninputs];
1092 TrainingOutputs[i] =
new float[Noutputs];
1095 for (i = 0; i < NTestPatterns; i++)
1097 TestInputs[i] =
new float[Ninputs];
1098 TestOutputs[i] =
new float[Noutputs];
1102 AllConnections =
new int[MaxUnits];
1120 const KKStr UsfCasCor::type_strings[]={
"SIGMOID",
"GAUSSIAN",
"LINEAR",
"ASYMSIGMOID",
"VARSIGMOID",
"WIN",
"STAGNANT",
"TIMEOUT",
"LOSE",
"BITS",
"INDEX",
"Bad Type"};
1126 const KKStr& UsfCasCor::type_to_string (
int var)
const 1130 case SIGMOID:
return(type_strings[0]);
1131 case GAUSSIAN:
return(type_strings[1]);
1132 case LINEAR:
return(type_strings[2]);
1135 case WIN:
return(type_strings[5]);
1136 case STAGNANT:
return(type_strings[6]);
1137 case TIMEOUT:
return(type_strings[7]);
1138 case LOSE:
return(type_strings[8]);
1139 case BITS:
return(type_strings[9]);
1140 case INDEX:
return(type_strings[10]);
1142 default:
return(type_strings[11]);
1151 int UsfCasCor::string_to_type (
const KKStr& s)
1192 char const * UsfCasCor::boolean_to_string (
bool var)
const 1203 Boolean UsfCasCor::string_to_boolean (
const char* s)
1222 void UsfCasCor::strdncase (
char *s)
1225 for (i = 0; s[i] !=
EOL; i++)
1228 s[i] = tolower(s[i]);
1242 int UsfCasCor::_type_convert (
char *input)
1246 if (!strcmp (input,
"true"))
1249 else if (!strcmp (input,
"1"))
1252 else if (!strcmp (input,
"false"))
1255 else if (!strcmp(input,
"0"))
1258 else if (!strcmp (input,
"sigmoid"))
1261 else if (!strcmp (input,
"gaussian"))
1264 else if (!strcmp (input,
"linear"))
1267 else if (!strcmp (input,
"asymsigmoid"))
1270 else if (!strcmp (input,
"varsigmoid"))
1273 else if (!strcmp (input,
"bits"))
1276 else if (!strcmp (input,
"index"))
1285 Boolean UsfCasCor::_boolean_convert (
char *input)
1288 if (!strcmp (input,
"true") ||
1289 !strcmp (input,
"1") ||
1290 !strcmp (input,
"t")
1294 if (!strcmp (input,
"false") || !strcmp(input,
"0"))
1307 float UsfCasCor::random_weight ()
1309 return ( (
float) (WeightRange * (
LRand48 () % 1000 / 500.0)) - WeightRange);
1318 #define BEGIN_PARAMETER_STRING "# Parameters\n" 1319 #define END_PARAMETER_STRING "# End Parameters\n" 1320 #define BEGIN_CONNECTION_STRING "# Connections\n" 1321 #define END_CONNECTION_STRING "# End Connections\n" 1322 #define BEGIN_INPUT_WEIGHTS_STRING "# Input Weights\n" 1323 #define END_INPUT_WEIGHTS_STRING "# End Input Weights\n" 1324 #define BEGIN_OUTPUT_WEIGHTS_STRING "# Output Weights\n" 1325 #define END_OUTPUT_WEIGHTS_STRING "# End Output Weights\n" 1333 void UsfCasCor::load_namesfile (FeatureVectorListPtr trainExamples,
1338 NTrainingPatterns = -1;
1344 number_of_classes = classes->QueueSize ();
1346 NTrainingPatterns = trainExamples->QueueSize ();
1352 delete feature_type;
1353 feature_type =
new int[feature_count];
1354 for (
int i = 0; i < feature_count; ++i)
1356 feature_type[i] =
REAL;
1359 Noutputs = number_of_classes;
1361 Ninputs = feature_count;
1378 float UsfCasCor::ACTIVATION (
float sum)
1389 else if (sum > 15.0)
1393 return (1.0f / (1.0f + exp (-sum)) - 0.5f);
1397 temp = -0.5f * sum * sum;
1401 return (exp (temp));
1407 else if (sum > 15.0f)
1410 return (1.0f / (1.0f + exp(-sum)));
1416 return (SigmoidMin);
1418 else if (sum > 15.0f)
1419 return (SigmoidMax);
1422 return ((SigmoidMax - SigmoidMin)/ (1.0f + exp(-sum)) + SigmoidMin);
1439 float UsfCasCor::ACTIVATION_PRIME (
float value,
1447 return (0.25f - value * value);
1451 return (sum * (- value));
1455 return (value * (1.0f - value));
1459 return ((value - SigmoidMin) * (1.0f - (value - SigmoidMin) /
1460 (SigmoidMax - SigmoidMin)));
1472 float UsfCasCor::OUTPUT_FUNCTION (
float sum)
1481 else if (sum > 15.0f)
1485 return (1.0f / (1.0f + exp (-sum)) - 0.5f);
1496 else if (sum > 15.0f)
1500 return (1.0f / (1.0f + exp (-sum)));
1505 return (SigmoidMin);
1507 else if (sum > 15.0f)
1511 return ((SigmoidMax - SigmoidMin) / (1.0f + exp (-sum)) + SigmoidMin);
1525 float UsfCasCor::OUTPUT_PRIME (
float output)
1531 return (SigmoidPrimeOffset + 0.25f - output * output);
1539 return (SigmoidPrimeOffset + output * (1.0f - output));
1543 return (SigmoidPrimeOffset +
1544 (output - SigmoidMin) * (1.0f - (output - SigmoidMin) /
1545 (SigmoidMax - SigmoidMin)));
1562 void UsfCasCor::QUICKPROP_UPDATE (
int i,
1573 float w,d,s,p, next_step;
1578 s = slopes[i] + decay * w;
1588 next_step -= epsilon * s;
1590 if(s >= (shrink_factor*p))
1591 next_step += mu * d;
1593 next_step += d * s / (p - s);
1599 next_step -= epsilon * s;
1601 if(s <= (shrink_factor*p))
1602 next_step += mu * d;
1604 next_step += d * s / (p - s);
1608 next_step -= epsilon * s;
1611 deltas[i] = next_step;
1612 weights[i] = w + next_step;
1623 void UsfCasCor::SETUP_INPUTS (
float inputs[])
1629 for(i=0; i < Ninputs; i++)
1630 Values[i+1] = inputs[i];
1639 void UsfCasCor::OUTPUT_FORWARD_PASS ()
1646 for (j = 0; j < Noutputs; j++)
1649 ow = OutputWeights[j];
1651 for(i=0; i<Nunits; i++)
1652 sum += Values[i] * ow[i];
1658 Outputs[j] = OUTPUT_FUNCTION(sum);
1668 void UsfCasCor::COMPUTE_UNIT_VALUE (
int j)
1679 for (i = 0; i < Nconnections[j]; i++)
1680 sum += Values[c[i]] * w[i];
1683 conx += Nconnections[j];
1686 Values[j] = ACTIVATION (sum);
1695 void UsfCasCor::FULL_FORWARD_PASS (
float input[])
1700 SETUP_INPUTS (input);
1705 for (j = 1 + Ninputs; j < Nunits; j++)
1706 COMPUTE_UNIT_VALUE (j);
1708 OUTPUT_FORWARD_PASS ();
1720 void UsfCasCor::COMPUTE_ERRORS (
float goal[],
1730 float err_prime = 0.0f;
1739 for (i = 1; i < Noutputs; i++)
1741 if ( Outputs[output_winner] < Outputs[i])
1744 if ( goal[goal_winner] < goal[i] )
1748 if (goal_winner != output_winner)
1749 ErrorMisclassifications++;
1751 for (j = 0; j < Noutputs; j++)
1754 dif = out - goal[j];
1755 if (load_weights && xw >= 0 && example_weight[xw] != 1.0 )
1756 dif *= example_weight[xw];
1758 err_prime = dif * OUTPUT_PRIME(out);
1759 os = OutputSlopes[j];
1761 Errors[j] = err_prime;
1765 if (fabs(dif) > ScoreThreshold)
1767 TrueError += dif * dif;
1768 SumErrors[j] += err_prime;
1769 SumSqError += err_prime * err_prime;
1774 for (i = 0; i < Nunits; i++)
1775 os[i] += err_prime * Values[i];
1787 void UsfCasCor::UPDATE_OUTPUT_WEIGHTS ()
1793 eps = OutputEpsilon / Ncases;
1795 for (j = 0; j < Noutputs; j++)
1796 for (i = 0; i < Nunits; i++)
1797 QUICKPROP_UPDATE (i,
1801 OutputPrevSlopes[j],
1824 void UsfCasCor::TRAIN_OUTPUTS_EPOCH ()
1832 ErrorMisclassifications = 0;
1833 for (i = 0; i < Noutputs; i++)
1840 OutputShrinkFactor = OutputMu / (1.0f + OutputMu);
1842 for (i= FirstCase; i < (FirstCase+Ncases); i++)
1844 Goal = TrainingOutputs[i];
1848 Values = ValuesCache[i];
1849 Errors = ErrorsCache[i];
1850 OUTPUT_FORWARD_PASS();
1854 Values = ExtraValues;
1855 Errors = ExtraErrors;
1856 FULL_FORWARD_PASS(TrainingInputs[i]);
1858 COMPUTE_ERRORS (Goal,
true,
true, i);
1861 switch (ErrorMeasure)
1867 UPDATE_OUTPUT_WEIGHTS();
1874 ErrorIndex = ERROR_INDEX(TrainingStdDev, NtrainingOutputValues);
1875 if (ErrorIndex > ErrorIndexThreshold)
1877 UPDATE_OUTPUT_WEIGHTS();
1897 int UsfCasCor::TRAIN_OUTPUTS (
int max_epochs,
1903 float last_error = 0.0;
1904 int quit_epoch = Epoch + OutputPatience;
1908 for(i = 0; (i < max_epochs) && (!cancelFlag); ++i)
1910 TRAIN_OUTPUTS_EPOCH ();
1912 if ((ErrorMeasure ==
BITS) && (ErrorBits == 0))
1918 else if ((ErrorMeasure ==
INDEX) && (ErrorIndex <= ErrorIndexThreshold))
1924 else if (OutputPatience == 0)
1927 else if (first_time)
1930 last_error = TrueError;
1933 else if (fabs(TrueError - last_error) > (last_error * OutputChangeThreshold))
1936 last_error = TrueError;
1937 quit_epoch = Epoch + OutputPatience;
1940 else if (Epoch >= quit_epoch)
1975 void UsfCasCor::INIT_CANDIDATES ()
1980 for (i = 0; i < Ncandidates; i++)
1982 CandValues[i] = 0.0;
1983 CandSumValues[i] = 0.0;
1985 for (j = 0; j < Nunits; j++)
1987 CandWeights[i][j] = random_weight();
1988 CandDeltas[i][j] = 0.0;
1989 CandSlopes[i][j] = 0.0;
1990 CandPrevSlopes[i][j] = 0.0;
1993 for(o=0; o<Noutputs; o++)
1995 CandCor[i][o] = 0.0;
1996 CandPrevCor[i][o] = 0.0;
2007 void UsfCasCor::INSTALL_NEW_UNIT (
RunLog& log)
2015 if (Nunits >= MaxUnits)
2017 log.Level (-1) << endl
2018 <<
"UsfCasCor::INSTALL_NEW_UNIT ***ERROR*** Can not add more units; limit of MaxUnits[" << MaxUnits <<
"] has been reached." << endl
2023 Nconnections[Nunits] = Nunits;
2024 Connections[Nunits] = AllConnections;
2026 w =
new float[Nunits];
2027 cw = CandWeights[BestCandidate];
2028 for (i = 0; i < Nunits; i++)
2030 Weights[Nunits] = w;
2040 if (ErrorMeasure ==
BITS)
2041 wm = WeightMultiplier;
2043 wm = WeightMultiplier / (
float)Nunits;
2045 for (o = 0; o < Noutputs; o++)
2046 OutputWeights[o][Nunits] = -CandPrevCor[BestCandidate][o] * wm;
2050 for (i = 0; i < NTrainingPatterns; i++)
2052 Values = ValuesCache[i];
2053 COMPUTE_UNIT_VALUE(Nunits);
2090 void UsfCasCor::COMPUTE_CORRELATIONS ()
2097 for(u=0; u<Ncandidates; u++){
2101 for(i=0; i<Nunits; i++)
2102 sum += CandWeights[u][i] * Values[i];
2106 v = ACTIVATION(sum);
2108 CandSumValues[u] += v;
2110 for(o=0; o<Noutputs; o++)
2111 CandCor[u][o] += v * Errors[o];
2124 void UsfCasCor::ADJUST_CORRELATIONS ()
2133 BestCandidateScore = 0.0;
2134 for(u=0; u<Ncandidates; u++)
2136 avg_value = CandSumValues[u] / Ncases;
2140 cpc = CandPrevCor[u];
2141 for(o=0; o<Noutputs; o++)
2143 cor = (cc[o] - avg_value * SumErrors[o]) / SumSqError;
2150 CandSumValues[u] = 0.0;
2152 if(score > BestCandidateScore){
2153 BestCandidateScore = score;
2166 void UsfCasCor::COMPUTE_SLOPES ()
2169 float sum, value, actprime, direction, error, change;
2172 for (u=0; u<Ncandidates; u++)
2180 for(i=0; i<Nunits; i++)
2181 sum += CandWeights[u][i] * Values[i];
2185 value = ACTIVATION(sum);
2186 actprime = ACTIVATION_PRIME(value, sum);
2187 CandSumValues[u] += value;
2190 for(o=0; o<Noutputs; o++){
2192 direction = (CandPrevCor[u][o] < 0.0f) ? -1.0f : 1.0f;
2193 change -= direction * actprime *((error -SumErrors[o])/SumSqError);
2194 CandCor[u][o] += error * value;
2196 for(i=0; i<Nunits; i++)
2197 CandSlopes[u][i] += change * Values[i];
2207 void UsfCasCor::UPDATE_INPUT_WEIGHTS ()
2211 float *cw, *cd, *cs, *cp;
2214 eps = InputEpsilon / (
float)(Ncases * Nunits);
2215 for(u=0; u<Ncandidates; u++)
2217 cw = CandWeights[u];
2220 cp = CandPrevSlopes[u];
2221 for(i=0; i<Nunits; i++)
2222 QUICKPROP_UPDATE(i, cw, cd, cs, cp, eps, InputDecay, InputMu,
2235 void UsfCasCor::TRAIN_INPUTS_EPOCH ()
2240 for(i=FirstCase; i<(Ncases+FirstCase); i++)
2242 Goal = TrainingOutputs[i];
2244 Values = ValuesCache[i];
2245 Errors = ErrorsCache[i];
2248 Values = ExtraValues;
2249 Errors = ExtraErrors;
2250 FULL_FORWARD_PASS(TrainingInputs[i]);
2251 COMPUTE_ERRORS (Goal,
false,
false, i);
2256 InputShrinkFactor = InputMu / (1.0f + InputMu);
2259 UPDATE_INPUT_WEIGHTS();
2262 ADJUST_CORRELATIONS();
2273 void UsfCasCor::CORRELATIONS_EPOCH ()
2278 for (i=FirstCase; i<(Ncases+FirstCase); i++)
2280 Goal = TrainingOutputs[i];
2283 Values = ValuesCache[i];
2284 Errors = ErrorsCache[i];
2288 Values = ExtraValues;
2289 Errors = ExtraErrors;
2290 FULL_FORWARD_PASS(TrainingInputs[i]);
2291 COMPUTE_ERRORS(Goal,
false,
false, i);
2293 COMPUTE_CORRELATIONS();
2296 ADJUST_CORRELATIONS();
2310 int UsfCasCor::TRAIN_INPUTS (
int max_epochs)
2313 float last_score = 0.0;
2314 int quit = max_epochs;
2318 for(i=0; i<Noutputs; i++)
2319 SumErrors[i] /= Ncases;
2321 CORRELATIONS_EPOCH();
2323 for(i=0; i<max_epochs; i++){
2324 TRAIN_INPUTS_EPOCH();
2326 if(InputPatience == 0)
2328 else if(first_time){
2330 last_score = BestCandidateScore;
2332 else if(fabs(BestCandidateScore - last_score) >
2333 (last_score * InputChangeThreshold)){
2334 last_score = BestCandidateScore;
2335 quit = i + InputPatience;
2357 void UsfCasCor::LIST_PARAMETERS ()
2389 int UsfCasCor::TRAIN (
int outlimit,
2400 initialize_network();
2404 for(i=0; i<NTrainingPatterns; i++)
2406 Values = ValuesCache[i];
2407 SETUP_INPUTS(TrainingInputs[i]);
2410 for (r = 0; (r < rounds) && (!cancelFlag); r++)
2412 log.Level (10) <<
"TRAIN Round " << r << endl;
2413 switch (TRAIN_OUTPUTS (outlimit, cancelFlag))
2417 log.Level (10) <<
"Victory at " 2418 << Epoch <<
"epochs, " 2419 << Nunits <<
" units, " 2420 << (Nunits - Ninputs - 1) <<
" hidden, " 2421 << TrueError <<
" Error " 2422 << ErrorIndex <<
" EI" 2427 log.Level (10) <<
"TRAIN -Output: Epoch " << Epoch <<
" Timeout" << endl
2428 <<
" train:" << PRINT_SUMMARY (NTrainingPatterns) << endl;
2432 log.Level (10) <<
"TRAIN +Output: Epoch " << Epoch <<
" Timeout" << endl
2433 <<
" train:" << PRINT_SUMMARY (NTrainingPatterns) << endl;
2437 log.Level (10) <<
"Bad return from TRAIN_OUTPUTS" << endl;
2442 if (Test) TEST_EPOCH(0.49, log);
2444 switch (TRAIN_INPUTS (inlimit))
2447 log.Level (10) <<
"TRAIN -Input : Epoch " << Epoch <<
" Timeout (Correlation " << BestCandidateScore <<
")" << endl;
2451 log.Level (10) <<
"TRAIN -Input : Epoch " << Epoch <<
" Timeout (Correlation " << BestCandidateScore <<
")" << endl;
2455 log.Level (10) <<
"TRAIN Bad return from TRAIN_INPUTS" << endl;
2459 INSTALL_NEW_UNIT (log);
2460 log.Level (10) <<
"ADDED UNIT: " << (r + 1) << endl;
2465 switch (TRAIN_OUTPUTS (outlimit, cancelFlag))
2468 log.Level (10) <<
"TRAIN Victory at " << Epoch <<
" epochs, " << Nunits <<
" units, " << (Nunits - Ninputs - 1) <<
" hidden, Error " << TrueError <<
" EI " << ErrorIndex << endl;
2473 log.Level (10) <<
"TRAIN Defeat at " << Nunits <<
" units " << PRINT_SUMMARY (NTrainingPatterns) << endl;
2477 log.Level (10) <<
"TRAIN Bad return from TRAIN_OUTPUTS" << endl;
2492 void UsfCasCor::TEST_EPOCH (
double test_threshold,
2502 float old_ST = ScoreThreshold;
2503 float old_TE = TrueError;
2504 float *old_SE = SumErrors;
2505 float old_SSE = SumSqError;
2507 if ((test_threshold > FLT_MAX) || (test_threshold < FLT_MIN))
2508 cerr << endl <<
"UsfCasCor::TEST_EPOCH test_threshold[" << test_threshold <<
"] has exceeded capacity of a float variabnle." << endl << endl;
2510 ScoreThreshold = (
float)test_threshold;
2513 Values = ExtraValues;
2514 Errors = ExtraErrors;
2525 ErrorMisclassifications=0;
2527 SumErrors = DummySumErrors;
2531 for (i = 0; i < NTrainingPatterns; ++i)
2533 Goal = TrainingOutputs[i];
2534 FULL_FORWARD_PASS (TrainingInputs[i]);
2535 COMPUTE_ERRORS (Goal,
false,
true, -1);
2538 if (ErrorMeasure ==
INDEX)
2539 ErrorIndex = ERROR_INDEX (TestStdDev, NtestOutputValues);
2541 log.Level (10) <<
"TEST_EPOCH test :" << PRINT_SUMMARY (NTrainingPatterns) << endl;
2545 ScoreThreshold = old_ST;
2548 SumSqError = old_SSE;
2556 void UsfCasCor::OUT_PASS_OUTPUT ()
2560 fprintf (stderr,
" Outputs: ");
2561 for (i = 0; i < Noutputs; i++)
2562 fprintf(stderr,
"%6.4f ", Outputs[i]);
2564 fprintf (stderr,
"\n Errors: ");
2565 for (i = 0; i < Noutputs; i++)
2566 fprintf(stderr,
"%6.4f ", Errors[i]);
2568 fprintf(stderr,
"\n Values: ");
2569 for(i=0;i<Nunits;i++)
2570 fprintf(stderr,
"%6.4f ", Values[i]);
2572 fprintf(stderr,
"\n\n");
2579 KKStr UsfCasCor::PRINT_SUMMARY (
int n)
2582 switch (ErrorMeasure)
2585 result <<
"errbits " << ErrorBits <<
"\t" <<
"error " << TrueError;
2589 result <<
" ErrorIndex " << ErrorIndex <<
", TrueError " << TrueError;
2593 double acc = 100.0 - (100.0 * ErrorMisclassifications) / n;
2594 result <<
" accuracy " 2596 << ErrorMisclassifications <<
"/" << n;
2607 void UsfCasCor::initialize_network ()
2613 for (i = 0; i < MaxUnits; i++)
2614 AllConnections[i] = i;
2617 for (i = 0; i < MaxUnits; i++)
2619 ExtraValues [i] = 0.0;
2620 Nconnections [i] = 0;
2621 Connections [i] = NULL;
2626 for (i = 0; i < Noutputs; i++)
2629 ExtraErrors[i] = 0.0;
2630 for(j=0; j<MaxUnits; j++)
2632 OutputWeights [i][j] = 0.0;
2633 OutputDeltas [i][j] = 0.0;
2634 OutputSlopes [i][j] = 0.0;
2635 OutputPrevSlopes [i][j] = 0.0;
2638 for (j = 0; j < (Ninputs + 1); ++j)
2639 OutputWeights[i][j] = random_weight();
2645 for (j = 0; j < MaxCases; ++j)
2647 for (i = 0; i < MaxUnits; ++i)
2648 ValuesCache[j][i] = 0.0;
2650 for (i = 0; i < Noutputs; ++i)
2651 ErrorsCache[j][i] = 0.0;
2658 ExtraValues[0] = 1.0;
2660 Nunits = Ninputs + 1;
2664 for (i = 0; i < Noutputs; ++i)
2667 DummySumErrors[i] = 0.0;
2671 BestCandidateScore = 0.0;
2677 if(ErrorMeasure ==
INDEX){
2680 TrainingStdDev = STANDARD_DEV(TrainingOutputs, NTrainingPatterns,
2681 NtrainingOutputValues);
2683 TestStdDev = STANDARD_DEV(TestOutputs, NTestPatterns,
2693 float UsfCasCor::STANDARD_DEV (
float** outputs,
2699 float sum_o_sqs = 0.0;
2702 float fnum = (
float)nvalues;
2705 for(i=0;i<npatterns;i++)
2706 for(j=0;j<Noutputs;j++){
2707 cur = outputs[i][j];
2709 sum_o_sqs += cur * cur;
2712 return (sqrt((fnum * sum_o_sqs - sum * sum)/
2713 (fnum * (fnum - 1.0f))));
2722 float UsfCasCor::ERROR_INDEX (
double std_dev,
2726 return (sqrt( TrueError / (
float)num) / (
float)std_dev);
2753 void UsfCasCor::INITIALIZE_GLOBALS ()
2755 OutputShrinkFactor=OutputMu / (1.0f + OutputMu);
2756 InputShrinkFactor=InputMu / (1.0f + InputMu);
2771 void UsfCasCor::load_data (FeatureVectorListPtr trainExamples,
2775 _load_training_data (trainExamples);
2777 log.Level (10) <<
"UsfCasCor::load_data Read in [" << NTrainingPatterns <<
"] training patterns of dimension[" << Ninputs <<
"]." << endl;
2787 void UsfCasCor::_load_training_data (FeatureVectorListPtr trainExamples)
2789 for (kkint32 i = 0; i < NTrainingPatterns; ++i)
2790 _load_training_example (trainExamples->IdxToPtr (i), i);
2797 void UsfCasCor::_load_training_example (FeatureVectorPtr example,
2804 <<
"UsfCasCor::_load_training_example ***ERROR*** Ninputs[" << Ninputs
2805 <<
"] != selectedFeatures->NumSelFeatures ()[" << selectedFeatures->NumSelFeatures () <<
"]" << endl
2815 for (j = 0; j < Ninputs; ++j)
2816 TrainingInputs[i][j] = featureData[featureNums[j]];
2818 kkint32 k = classes->PtrToIdx (example->MLClass ());
2820 for (j = 0; j < Noutputs; j++)
2823 TrainingOutputs[i][j] = SigmoidMax;
2825 TrainingOutputs[i][j] = SigmoidMin;
2834 void UsfCasCor::_load_test_data (FeatureVectorPtr example)
2840 for (
int j = 0; j < Ninputs; ++j)
2841 TestInputs[0][j] = featureData[featureNums[j]];
2846 for (
int j = 0; j < Noutputs; j++)
2849 TestOutputs[0][j] = SigmoidMax;
2851 TestOutputs[0][j] = SigmoidMin;
2860 void UsfCasCor::WriteXML (
const KKStr& varName,
2870 XmlElementInt32::WriteXML (number_of_classes,
"number_of_classes", o);
2875 XmlElementInt32::WriteXML (MaxUnits,
"MaxUnits", o);
2876 XmlElementInt32::WriteXML (Ninputs,
"Ninputs", o);
2877 XmlElementInt32::WriteXML (Noutputs,
"Noutputs", o);
2878 XmlElementInt32::WriteXML (Nunits,
"Nunits", o);
2879 XmlElementInt32::WriteXML (Ncandidates ,
"Ncandidates", o);
2880 XmlElementInt32::WriteXML (MaxCases,
"MaxCases", o);
2881 XmlElementInt32::WriteXML (Ncases,
"Ncases", o);
2882 XmlElementInt32::WriteXML (FirstCase,
"FirstCase", o);
2883 XmlElementInt32::WriteXML (line_length,
"line_length", o);
2884 XmlElementInt64::WriteXML (the_random_seed,
"the_random_seed", o);
2885 XmlElementInt32::WriteXML (in_limit,
"in_limit", o);
2886 XmlElementInt32::WriteXML (out_limit,
"out_limit", o);
2887 XmlElementInt32::WriteXML (number_of_trials,
"number_of_trials", o);
2888 XmlElementInt32::WriteXML (number_of_rounds,
"number_of_rounds", o);
2889 XmlElementInt32::WriteXML (normalization_method,
"normalization_method", o);
2890 XmlElementInt32::WriteXML (my_mpi_rank,
"my_mpi_rank", o);
2891 XmlElementInt32::WriteXML (NTrainingPatterns,
"NTrainingPatterns", o);
2892 XmlElementInt32::WriteXML (NTestPatterns,
"NTestPatterns", o);
2893 XmlElementInt32::WriteXML (ErrorMisclassifications,
"ErrorMisclassifications", o);
2894 XmlElementInt32::WriteXML (OutputPatience,
"OutputPatience", o);
2895 XmlElementInt32::WriteXML (InputPatience,
"InputPatience", o);
2896 XmlElementInt32::WriteXML (ErrorBits,
"ErrorBits", o);
2897 XmlElementInt32::WriteXML (BestCandidate,
"BestCandidate", o);
2898 XmlElementInt32::WriteXML (Epoch,
"Epoch", o);
2899 XmlElementInt32::WriteXML (Trial,
"Trial", o);
2900 XmlElementInt32::WriteXML (NtrainingOutputValues,
"NtrainingOutputValues", o);
2901 XmlElementInt32::WriteXML (NtestOutputValues,
"NtestOutputValues", o);
2902 XmlElementInt32::WriteXML (ErrorMeasure,
"ErrorMeasure", o);
2906 XmlElementFloat::WriteXML (SigmoidMax,
"SigmoidMax", o);
2907 XmlElementFloat::WriteXML (WeightRange,
"WeightRange", o);
2908 XmlElementFloat::WriteXML (SigmoidPrimeOffset,
"SigmoidPrimeOffset", o);
2909 XmlElementFloat::WriteXML (WeightMultiplier,
"WeightMultiplier", o);
2910 XmlElementFloat::WriteXML (OutputMu,
"OutputMu", o);
2911 XmlElementFloat::WriteXML (OutputShrinkFactor,
"OutputShrinkFactor", o);
2912 XmlElementFloat::WriteXML (OutputEpsilon,
"OutputEpsilon", o);
2913 XmlElementFloat::WriteXML (OutputDecay,
"OutputDecay", o);
2914 XmlElementFloat::WriteXML (OutputChangeThreshold,
"OutputChangeThreshold", o);
2915 XmlElementFloat::WriteXML (InputMu,
"InputMu", o);
2916 XmlElementFloat::WriteXML (InputShrinkFactor,
"InputShrinkFactor", o);
2917 XmlElementFloat::WriteXML (InputEpsilon,
"InputEpsilon", o);
2918 XmlElementFloat::WriteXML (InputDecay,
"InputDecay", o);
2919 XmlElementFloat::WriteXML (InputChangeThreshold,
"InputChangeThreshold", o);
2920 XmlElementFloat::WriteXML (TrueError,
"TrueError", o);
2921 XmlElementFloat::WriteXML (ScoreThreshold,
"ScoreThreshold", o);
2922 XmlElementFloat::WriteXML (SumSqError,
"SumSqError", o);
2923 XmlElementFloat::WriteXML (BestCandidateScore,
"BestCandidateScore", o);
2924 XmlElementFloat::WriteXML (TrainingStdDev,
"TrainingStdDev", o);
2925 XmlElementFloat::WriteXML (TestStdDev,
"TestStdDev", o);
2926 XmlElementFloat::WriteXML (ErrorIndex,
"ErrorIndex", o);
2927 XmlElementFloat::WriteXML (ErrorIndexThreshold,
"ErrorIndexThreshold", o);
2929 XmlElementKKStr::WriteXML (type_to_string (UnitType),
"UnitType", o);
2930 XmlElementKKStr::WriteXML (type_to_string (OutputType),
"OutputType", o);
2942 XmlElementArrayInt32::WriteXML (Ninputs, feature_type,
"feature_type", o);
2944 XmlElementArrayFloat::WriteXML (Noutputs, SumErrors,
"SumErrors", o);
2945 XmlElementArrayFloat::WriteXML (Noutputs, DummySumErrors,
"DummySumErrors", o);
2947 if (AllConnections) XmlElementArrayInt32::WriteXML (MaxUnits, AllConnections,
"AllConnections", o);
2948 if (Nconnections) XmlElementArrayInt32::WriteXML (MaxUnits, Nconnections,
"Nconnections", o);
2952 for (
int x = 0; x < MaxUnits; ++x)
2954 if (Connections[x] == NULL)
2955 connectionsVector.push_back (
"NULL");
2957 else if (Connections[x] == AllConnections)
2958 connectionsVector.push_back (
"AC");
2961 connectionsVector.push_back (
"Other");
2970 if (ExtraValues) XmlElementArrayFloat::WriteXML (MaxUnits, ExtraValues,
"ExtraValues", o);
2971 if (Outputs) XmlElementArrayFloat::WriteXML (Noutputs, Outputs,
"Outputs", o);
2974 XmlElementArrayFloat2D::WriteXML (Noutputs, MaxUnits, OutputWeights,
"OutputWeights", o);
2976 if (ExtraErrors) XmlElementArrayFloat::WriteXML (Noutputs, ExtraErrors,
"ExtraErrors", o);
2993 Delete2DArray (Weights, MaxUnits);
2996 Delete2DArray (OutputWeights, Noutputs);
2999 while (t && (!cancelFlag))
3001 bool tokenProcessed =
false;
3005 tokenProcessed =
true;
3014 number_of_classes = valueInt32;
3025 delete selectedFeatures;
3026 selectedFeatures =
dynamic_cast<XmlElementFeatureNumListPtr> (t)->TakeOwnership ();
3041 else if (varName
.EqualIgnoreCase ("number_of_trials")) number_of_trials = valueInt32;
3042 else if (varName
.EqualIgnoreCase ("number_of_rounds")) number_of_rounds = valueInt32;
3043 else if (varName
.EqualIgnoreCase ("normalization_method")) normalization_method = valueInt32;
3045 else if (varName
.EqualIgnoreCase ("NTrainingPatterns")) NTrainingPatterns = valueInt32;
3047 else if (varName
.EqualIgnoreCase ("ErrorMisclassifications")) ErrorMisclassifications = valueInt32;
3054 else if (varName
.EqualIgnoreCase ("NtrainingOutputValues")) NtrainingOutputValues = valueInt32;
3055 else if (varName
.EqualIgnoreCase ("NtestOutputValues")) NtestOutputValues = valueInt32;
3056 else if (varName
.EqualIgnoreCase ("NtestOutputValues")) NtestOutputValues = valueInt32;
3061 else if (varName
.EqualIgnoreCase ("SigmoidPrimeOffset")) SigmoidPrimeOffset = valueFloat;
3062 else if (varName
.EqualIgnoreCase ("WeightMultiplier")) WeightMultiplier = valueFloat;
3064 else if (varName
.EqualIgnoreCase ("OutputShrinkFactor")) OutputShrinkFactor = valueFloat;
3067 else if (varName
.EqualIgnoreCase ("OutputChangeThreshold")) OutputChangeThreshold = valueFloat;
3069 else if (varName
.EqualIgnoreCase ("InputShrinkFactor")) InputShrinkFactor = valueFloat;
3072 else if (varName
.EqualIgnoreCase ("InputChangeThreshold")) InputChangeThreshold = valueFloat;
3076 else if (varName
.EqualIgnoreCase ("BestCandidateScore")) BestCandidateScore = valueFloat;
3080 else if (varName
.EqualIgnoreCase ("ErrorIndexThreshold")) ErrorIndexThreshold = valueFloat;
3108 tokenProcessed =
false;
3114 delete feature_type;
3115 feature_type =
dynamic_cast<XmlElementArrayInt32Ptr> (t)->TakeOwnership ();
3118 else if ((varName
.EqualIgnoreCase ("SumErrors")) && (
typeid (*t) ==
typeid (XmlElementArrayFloat)))
3121 SumErrors =
dynamic_cast<XmlElementArrayFloatPtr> (t)->TakeOwnership ();
3124 else if (varName
.EqualIgnoreCase ("DummySumErrors") && (
typeid (*t) ==
typeid (XmlElementArrayFloat)))
3126 delete DummySumErrors;
3127 DummySumErrors =
dynamic_cast<XmlElementArrayFloatPtr> (t)->TakeOwnership ();
3130 else if (varName
.EqualIgnoreCase ("AllConnections") && (
typeid (*t) ==
typeid (XmlElementArrayInt32)))
3132 delete AllConnections;
3133 AllConnections =
dynamic_cast<XmlElementArrayInt32Ptr> (t)->TakeOwnership ();
3136 else if (varName
.EqualIgnoreCase ("Nconnections") && (
typeid (*t) ==
typeid (XmlElementArrayInt32)))
3138 delete Nconnections;
3139 Nconnections =
dynamic_cast<XmlElementArrayInt32Ptr> (t)->TakeOwnership ();
3142 else if (varName.EqualIgnoreCase (
"Connections") && (
typeid (*t) ==
typeid (XmlElementVectorKKStr)))
3144 VectorKKStr* connectionsStr =
dynamic_cast<XmlElementVectorKKStrPtr> (t)->TakeOwnership ();
3147 kkuint32 count = connectionsStr->size ();
3149 Connections =
new int*[MaxUnits];
3151 for (kkuint32 x = 0; x < count; ++x)
3153 if ((*connectionsStr)[x] ==
"NULL")
3154 Connections[x] = NULL;
3155 else if ((*connectionsStr)[x] ==
"AC")
3156 Connections[x] = AllConnections;
3158 Connections[x] = NULL;
3161 delete connectionsStr;
3162 connectionsStr = NULL;
3165 else if (varName.EqualIgnoreCase (
"Weights"))
3167 if (
typeid (*t) ==
typeid (XmlElementArrayFloat2DVarying))
3169 XmlElementArrayFloat2DVaryingPtr w =
dynamic_cast<XmlElementArrayFloat2DVaryingPtr> (t);
3170 if (w->Height () != MaxUnits)
3172 log.Level (-1) << endl
3173 <<
"UsfCasCor::ReadXML ***ERROR*** Height[" << w->Height () <<
"] of Weights array does not MaxUnits[" << MaxUnits <<
"]." << endl
3178 Weights = w->TakeOwnership ();
3183 else if ((varName.EqualIgnoreCase (
"ExtraValues")) && (
typeid (*t) ==
typeid (XmlElementArrayFloat)))
3186 ExtraValues =
dynamic_cast<XmlElementArrayFloatPtr> (t)->TakeOwnership ();
3189 else if ((varName.EqualIgnoreCase (
"Outputs")) && (
typeid (*t) ==
typeid (XmlElementArrayFloat)))
3192 Outputs =
dynamic_cast<XmlElementArrayFloatPtr> (t)->TakeOwnership ();
3195 else if ((varName.EqualIgnoreCase (
"ExtraErrors")) && (
typeid (*t) ==
typeid (XmlElementArrayFloat)))
3198 ExtraErrors =
dynamic_cast<XmlElementArrayFloatPtr> (t)->TakeOwnership ();
3201 else if ((varName.EqualIgnoreCase (
"OutputWeights")) && (
typeid (*t) ==
typeid (XmlElementArrayFloat2D)))
3203 XmlElementArrayFloat2DPtr array2D =
dynamic_cast<XmlElementArrayFloat2DPtr> (t);
3204 kkuint32 owHeight = array2D->Height ();
3205 kkuint32 owWidth = array2D->Width ();
3206 if ((owHeight != Noutputs) || (owWidth != MaxUnits))
3208 log.Level (-1) << endl
3209 <<
"UsfCasCor::ReadXML ***ERROR*** OutputWeights read Dimensions[" << owHeight <<
", " << owWidth <<
"] not the expected [" << Noutputs <<
", " << MaxUnits <<
"]." 3214 delete OutputWeights;
3215 OutputWeights = array2D->TakeOwnership ();
3230 if (NTestPatterns > 0)
3232 TestInputs =
new float*[NTestPatterns];
3233 TestOutputs =
new float*[NTestPatterns];
3234 for (
int z = 0; z < NTestPatterns; ++z)
3236 TestInputs[z] =
new float[Ninputs];
3237 TestOutputs[z] =
new float[Noutputs];
3242 TestInputs = TrainingInputs;
3243 TestOutputs = TrainingOutputs;
3247 ExtraValues =
new float[MaxUnits];
3250 Values = ExtraValues;
3259 if (ValuesCache == NULL )
3261 ValuesCache =
new float*[MaxCases];
3262 ErrorsCache =
new float*[MaxCases];
3264 for (
kkint32 i = 0; i < MaxCases; i++)
3266 ValuesCache[i] =
new float[MaxUnits];
3267 ErrorsCache[i] =
new float[Noutputs];
3271 for (
kkint32 i = 0; i < NTrainingPatterns; i++)
3273 Values = ValuesCache[i];
3277 for (
kkint32 j = 1 + Ninputs; j < Nunits; j++)
3278 COMPUTE_UNIT_VALUE(j);
__int16 kkint16
16 bit signed integer.
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for 'size' characters.
XmlTag(const KKStr &_name, TagTypes _tagType)
void PushOnBack(FeatureVectorPtr image)
Overloading the PushOnBack function in KKQueue so we can monitor the Version and Sort Order...
bool EqualIgnoreCase(const char *s2) const
kkint32 LRand48()
A implementations of the Unix version of rand48 returning a 32 bit integer.
const float * FeatureData() const
Returns as a pointer to the feature data itself.
virtual KKStr ToKKStr() const
float FeatureData(kkint32 featureNum) const
Keeps track of selected features.
FeatureNumList(FileDescPtr _fileDesc)
virtual float ToFloat() const
virtual void PushOnBack(ClassProbPtr cp)
XmlElementBool * XmlElementBoolPtr
MLClassListPtr ExtractListOfClasses() const
unsigned __int16 kkuint16
16 bit unsigned integer.
static void WriteXML(const MLClassList &mlClassList, const KKStr &varName, std::ostream &o)
const FileDescPtr FileDesc() const
kkint32 STRICMP(const char *left, const char *right)
static void WriteXML(const bool b, const KKStr &varName, std::ostream &o)
bool EqualIgnoreCase(const KKStr &s2) const
unsigned __int32 kkuint32
FeatureNumList(const FeatureNumList &featureNumList)
Copy constructor.
FeatureVectorList(FileDescPtr _fileDesc, bool _owner)
Will create a new empty list of FeatureVector's.
Container class for FeatureVector derived objects.
virtual kkint32 ToInt32() const
XmlElement * XmlElementPtr
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
kkint32 NumOfFeatures() const
Used to record probability for a specified class; and a list of classes.
XmlTag const * XmlTagConstPtr
std::vector< float > VectorFloat
MLClassListPtr TakeOwnership()
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
static void WriteXML(kkuint32 height, const kkint32 *widths, float **const mat, const KKStr &varName, std::ostream &o)
ClassProbList(bool owner)
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of 'std::string' strings.
void WriteXML(const KKStr &varName, std::ostream &o) const
virtual const KKStr & VarName() const
float TotalOfFeatureData() const
Returns the total of all Feature Attributes for this feature vector.
kkint32 NumSelFeatures() const
KKStr StrFormatDouble(double val, const char *mask)
void WriteXML(std::ostream &o)
FeatureNumListConst * FeatureNumListConstPtr
Used for logging messages.
void EncodeProblem(const struct svm_paramater ¶m, struct svm_problem &prob_in, struct svm_problem &prob_out)
void SRand48(kkint64 _seed)
Seeds the Lrand48 functions with the parameters passed to it.
virtual TokenTypes TokenType()=0
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
ClassProb(MLClassPtr _classLabel, double _probability, float _votes)
Maintains a list of MLClass instances.
const char * _(const char *str)
Will only write the ClassName rather than complete MLClass instances.
#define INT_MAX
Adapted by Kurt Kramer be a 'class' definition so as to make it more usable in th ePices software wor...
#define XmlFactoryMacro(NameOfClass)
void WriteXML(const KKStr &varName, std::ostream &o) const
const kkuint16 * FeatureNums() const
XmlElementMLClassNameList * XmlElementMLClassNameListPtr
volatile const bool VolConstBool