KSquare Utilities
UsfCasCor.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <math.h>
5 #include <ctype.h>
6 #include <signal.h>
7 #include <time.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <iostream>
11 #include <fstream>
12 #if defined(WIN32)
13 #include <LIMITS.H>
14 #include <FLOAT.H>
15 #include <windows.h>
16 #define _SCL_SECURE_NO_WARNINGS
17 #pragma warning(disable:4996)
18 #endif
19 #include "MemoryDebug.h"
20 using namespace std;
21 
22 
23 #include "GlobalGoalKeeper.h"
24 #include "KKBaseTypes.h"
25 #include "OSservices.h"
26 #include "XmlStream.h"
27 using namespace KKB;
28 
29 
30 #include "MLClass.h"
31 #include "FeatureVector.h"
32 #include "ClassProb.h"
33 #include "XmlStream.h"
34 #include "UsfCasCor.h"
35 using namespace KKMLL;
36 
37 
38 
39 /*
40  Cascade Correlation Neural Network
41 
42  See notes below for original author, etc.
43 
44  Modified by Kurt Kramer 2012-09-10:
45  Originally written by R. Scott Crowder, III Se below for his comments.
46 
47  I turned in this implementation as found at USF into a c++ Class and integrated into the Pices
48  application.
49  1) Restructured code as a c++ class.
50  2) A trained classifier are written to disk and can be reread in a instance.
51  3) Integrated into the Pices application.
52  4) Primary use will to be used in a Dual classifier setup along with a Support Vector Machine(SVM)(libSVM)
53  where both classifiers agree on the prediction will the label be returned otherwise the label
54  "UnKnown" will be returned. User will have option to have the common part of the prediction in the
55  class hierarchy returned instead.
56 
57 
58  Changes made by Steven Eschrich <eschrich@csee.usf.edu>
59  Fall, 2001
60 
61  - The code was heavily modified in the I/O portion to follow
62  C4.5 standard data formats (although currently only real-valued
63  attributes are supported). It parses the .names file for classes
64  (1 of N representation) and feature values (ignore or real). The
65  logic simply treats all non-ignore fields as real so integer labels
66  are treated as continuous.
67 
68  - All network parameters can be passed via command line arguments,
69  try cascor -h for details. Briefly,
70  -i # input epochs
71  -o # output epochs
72  -r # number of new units to add
73  -P make predictions. generates a filestem.pred file with class
74  names as predictions (from a .test file).
75  -N normalize features to 0-1
76  -f filestem Filestem for data
77  -R use training set (re-substitution)
78 
79  All other parameters that could be specified in the .net file are
80  specified as
81  -O option=value (e.g. -O UseCache=true)
82 
83  - Defaults are used for parameters, in case someone starts it without
84  any...
85 
86  - Support for index files (-I option). That is, the following files
87  are supposed to exist
88  filestem.data
89  filestem.idata
90  filestem.itest (-P only)
91  filestem.names
92 
93  The .idata and .itest are text files with the index number of the record
94  in .data desired. The .data file **must** be in fixed-width format, each
95  line padded to the same length with spaces.
96 */
97 /****************************************************************************/
98 /* C implementation of the Cascade-Correlation learning algorithm. */
99 /* */
100 /* Written by: R. Scott Crowder, III */
101 /* School of Computer Science */
102 /* Carnegie Mellon University */
103 /* Pittsburgh, PA 15213-3890 */
104 /* */
105 /* Phone: (412) 268-8139 */
106 /* Internet: rsc@cs.cmu.edu */
107 /* */
108 /* */
109 /* This code has been placed in the public domain by the author. As a */
110 /* matter of simple courtesy, anyone using or adapting this code is */
111 /* expected to acknowledge the source. The author would like to hear */
112 /* about any attempts to use this system, successful or not. */
113 /* */
114 /* This code is a port to C from the original Common Lisp implementation */
115 /* written by Scott E. Fahlman. (Version dated June 1 1990.) */
116 /* */
117 /* For an explanation of this algorithm and some results, see "The */
118 /* Cascade-Correlation Learning Architecture" by Scott E. Fahlman and */
119 /* Christian Lebiere in D. S. Touretzky (ed.), "Advances in Neural */
120 /* Information Processing Systems 2", Morgan Kaufmann, 1990. A somewhat */
121 /* longer version is available as CMU Computer Science Tech Report */
122 /* CMU-CS-90-100. Instructions for Ftp'ing this report are given at the */
123 /* end of this file. */
124 /* */
125 /* An example of the network set up file is provided at the bottom of */
126 /* this file. */
127 /* */
128 /* This code has been successfully compiled on the following machines. */
129 /* */
130 /* DEC Station 3100 using the MIPS compiler version 1.31 */
131 /* Sun 4 using the gcc compiler version 1.23 */
132 /* IBM PC-RT using the cc compiler */
133 /* IBM RS6000 (Model 520) using the xlc compiler */
134 /* 386 machine using the Turbo C 2.0 compiler */
135 /* The implementation compiles with the ANSI standard. Some machine */
136 /* specific preprocessor commands are required. It is assumed that your */
137 /* system will provide the required preprocessor arguments. */
138 /* */
139 /****************************************************************************/
140 /* Change Log */
141 /****************************************************************************/
142 /* */
143 /* Changes from Release 1 dated Jun-12-90 to Version 1.14 Jul-18-90 */
144 /* */
145 /* bug fix in TYPE_CONVERT Thanks to Michael Witbrock for the 1st report */
146 /* bug fix in BUILD_NET Thanks to Michael Witbrock for the 1st report */
147 /* bug fix in GET_ARRAY_ELEMENT Thanks to Ken Lang */
148 /* bug fix in COMPUTE_CORRELATIONS Thanks to Eric Melz */
149 /* bug fix in ADJUST_CORRELATIONS Thanks to Chris Lebiere */
150 /* bug fix in COMPUTE_SLOPES Thanks to Chris Lebiere */
151 /* removed 2nd call to INIT_GLOBALS Thanks to Dimitris Michailidis */
152 /* Added UnitType ASYMSIGMOID for users who like sigmoids to go from 0-1 */
153 /* all learning utility functions changed with this addition. */
154 /* Added command line argument option type 'cascor1 help' for usage info. */
155 /* Added .net file and on-the-fly parameter adjustment code. See new */
156 /* samples files at the end of this listing for examples. Functions */
157 /* main and GET_NETWORK_CONFIGURATION have changed completely. */
158 /* GET_USER_INPUT replaced by Y_OR_N_P */
159 //* <signal.h> included to support on-the-fly parameter updating */
160 /****************************************************************************/
161 /* */
162 /* Changes from Version 1.15 Jul-18-90 to 1.16 Oct-24-90 */
163 /* */
164 /* bug fix in BUILD_NETWORK, INSTALL_NEW_UNIT, and TRAIN to allow */
165 /* NTestPatterns > NTrainingPatterns. Thanks to William Stevenson */
166 /****************************************************************************/
167 /* */
168 /* Changes from Version 1.16 Oct-24-90 to 1.17 Nov-12-90 */
169 /****************************************************************************/
170 /* bug fix in TRAIN line 1662 change NtrainingPatterns to NTrainingPatterns */
171 /* Thanks to Merrill Flood for pointing out the problem. */
172 /****************************************************************************/
173 /* */
174 /* Changes from Version 1.17 Nov-12-90 to 1.30 Jan-23-91 */
175 /****************************************************************************/
176 /* Added code to allow user to save the weights into and load weights */
177 /* from external files. */
178 /* Added code to allow saving of .net files to save any changes to */
179 /* parameters made during interactive learning trials. */
180 /* Added an alternative main routine that can be used to calculate */
181 /* predictions using a previously saved set of weights. To activate */
182 /* this feature compile the code with the symbol PREDICT_ONLY defined. */
183 /* Added code to allow '# comment' lines in the training or test sets. */
184 /* Added optional code to calculate the number of multiply-accumulates */
185 /* used during training. This is useful for comparing */
186 /* Cascade-correlation to other learning algorithms in a machine */
187 /* independent manner. To activate this feature define the symbol */
188 /* CONNX at compile time. */
189 /* Added code to calculate the Lapedes and Faber Index. Useful for */
190 /* problems with real-valued outputs. */
191 /* Added UnitType VARSIGMOID which can have arbitrary output range */
192 /* defined by SigmoidMin and SigmoidMax. Thanks to Dimitris */
193 /* Michailidis. */
194 /* Added code to allow the training and test data to be read from */
195 /* separate files. Thanks to Carlos Puchol. */
196 /* Added code to save SumError for each output instead of combining it */
197 /* together for all outputs. This change helps for multiple output */
198 /* problems. Thanks to Scott Fahlman. */
199 /* Code added to allow specification of a NonRandomSeed for the random */
200 /* number generator. Thanks to Dimitris Michailidis. */
201 /* Removed useless setting of Ninputs and Noutputs from BUILD_NET. Thanks */
202 /* to Dimitris Michailidis. */
203 /****************************************************************************/
204 /* */
205 /* Changes from Version 1.30 Jan-23-91 to 1.31 Jan-25-91 */
206 /* fixed typo. include <string.h> not <sting.h> thanks to Peter Hancock. */
207 /* */
208 /* Changes from Version 1.31 Jan-25-91 to 1.32 Mar-21-91 */
209 /* BUG FIX in INIT_NET. Thanks to Boris Gokhman */
210 /* BUG FIX in TEST_EPOCH. Thanks to Boris Gokhman */
211 /* */
212 /* Changes from Version 1.32 Mar-21-91 to 1.33 Apr-16-92 */
213 /* Prototype correction for strtok. Thanks to Brian Ripley */
214 /****************************************************************************/
215 
216 
217 
218 // Forward Declarations.
220 
221 
222 
223 const char* _(const char* str)
224 {
225  return str;
226 }
227 
228 
229 
230 
231 UsfCasCor::UsfCasCor ():
232 
233  //***************************************************************
234  //* usfcascor *
235  //***************************************************************
236  in_limit (500),
237  out_limit (500),
238  number_of_trials (1),
239  number_of_rounds (-1),
240  normalization_method (0),
241  my_mpi_rank (0),
242 
243  //*********************************************************************
244  // globals.c *
245  //*********************************************************************
246  number_of_classes (-1),
247  feature_type (NULL),
248 
249  the_random_seed (0),
250 
251  load_weights (false),
252 
253  UnitType (SIGMOID),
254  OutputType (SIGMOID),
255  SigmoidMax (0.5f),
256  SigmoidMin (-0.5f),
257  WeightRange (1.0f),
258  SigmoidPrimeOffset (0.1f),
259  WeightMultiplier (1.0f),
260  OutputMu (2.0f),
261  OutputShrinkFactor (0.0f),
262  OutputEpsilon (0.35f),
263  OutputDecay (0.0001f),
264  OutputPatience (8),
265  OutputChangeThreshold (0.01f),
266  InputMu (2.0f),
267  InputShrinkFactor (0.0f),
268  InputEpsilon (1.0f),
269  InputDecay (0.0f),
270  InputPatience (8),
271  InputChangeThreshold (0.03f),
272 
273 
274  /* Variables related to error and correlation. */
275  TrueError (0.0f),
276  ScoreThreshold (0.35f),
277  ErrorBits (0),
278  SumErrors (NULL),
279  DummySumErrors (NULL),
280  SumSqError (0.0f),
281  BestCandidateScore (0.0f),
282  BestCandidate (0),
283 
284 
285  /* These variables and switches control the simulation and display. */
286  UseCache (false),
287  Epoch (0),
288  Graphics (false),
289  NonRandomSeed (false),
290  Test (true),
291  SinglePass (false),
292  SingleEpoch (false),
293  Step (false),
294  Trial (0),
295 
296  /* The sets of training inputs and outputs. */
297  NTrainingPatterns (0),
298  NTestPatterns (0),
299  TrainingInputs (NULL),
300  TrainingOutputs (NULL),
301  Goal (NULL),
302 
303  example_weight (NULL),
304 
305  /* For some benchmarks there is a separate set of values used for testing */
306  /* the network's ability to generalize. These values are not used during */
307  /* training. */
308  TestInputs (NULL),
309  TestOutputs (NULL),
310 
311  MaxUnits (2000),
312 
313  Nunits (0),
314  Ninputs (0),
315  Noutputs (0),
316  Ncandidates (8),
317  MaxCases (0),
318  Ncases (0),
319  FirstCase (0),
320 
321  /***************************************************************************/
322  /* The following vectors hold values related to hidden units in the active */
323  /* net and their input weights. */
324  /***************************************************************************/
325  Values (NULL),
326  ValuesCache (NULL),
327  ExtraValues (NULL),
328  Nconnections (NULL),
329  Connections (NULL),
330  Weights (NULL),
331 
332  /***************************************************************************/
333  /* The following arrays of arrays hold values for the outputs of the active*/
334  /* network and the output-side weights. */
335  /***************************************************************************/
336  Outputs (NULL),
337  Errors (NULL),
338  ErrorsCache (NULL),
339  ExtraErrors (NULL),
340  OutputWeights (NULL),
341  OutputDeltas (NULL),
342  OutputSlopes (NULL),
343  OutputPrevSlopes (NULL),
344 
345  /***************************************************************************/
346  /* The following arrays have one entry for each candidate unit in the */
347  /* pool of trainees. */
348  /***************************************************************************/
349  CandValues (NULL),
350  CandSumValues (NULL),
351  CandCor (NULL),
352  CandPrevCor (NULL),
353  CandWeights (NULL),
354  CandDeltas (NULL),
355  CandSlopes (NULL),
356  CandPrevSlopes (NULL),
357 
358  /***************************************************************************/
359  /* This saves memory if each candidate unit receives a connection from */
360  /* each existing unit and input. That's always true at present, but may */
361  /* not be in future. */
362  /***************************************************************************/
363  AllConnections (NULL),
364 
365  /***************************************************************************/
366  /* ErrorIndex specific globals. Not in release Lisp version */
367  /***************************************************************************/
368  NtrainingOutputValues (0),
369  NtestOutputValues (0),
370  TrainingStdDev (1.0f),
371  TestStdDev (1.0f),
372  ErrorIndex (0.0f),
373  ErrorIndexThreshold (0.2f),
374  ErrorMeasure (BITS),
375 
376 
377  /***************************************************************************/
378  /* Save and plot file related variables */
379  /***************************************************************************/
380  WeightFile (NULL),
381  InterruptPending (false),
382  classes (NULL),
383  selectedFeatures (NULL)
384 
385 {
386 }
387 
388 
389 
390 
391 
392 UsfCasCor::~UsfCasCor ()
393 {
394  delete classes; classes = NULL;
395  delete selectedFeatures; selectedFeatures = NULL;
396  CleanUpMemory ();
397 }
398 
399 
400 template<typename T>
401 void UsfCasCor::Delete2DArray (T** &A,
402  kkuint32 numRows
403  )
404 {
405  if (!A)
406  return;
407 
408  for (kkuint32 x = 0; x < numRows; ++x)
409  {
410  delete A[x];
411  A[x] = NULL;
412  }
413 
414  delete A;
415  A = NULL;
416 } /* Delete2DArray */
417 
418 
419 void UsfCasCor::CleanUpMemory ()
420 {
421  delete ExtraValues; ExtraValues = NULL;
422  delete Nconnections; Nconnections = NULL;
423  delete Connections; Connections = NULL;
424 
425  Delete2DArray (Weights, MaxUnits);
426 
427  delete ExtraErrors; ExtraErrors = NULL;
428  delete SumErrors; SumErrors = NULL;
429  delete DummySumErrors; DummySumErrors = NULL;
430 
431  delete Outputs; Outputs = NULL;
432 
433  Delete2DArray (OutputWeights, Noutputs);
434  Delete2DArray (OutputDeltas, Noutputs);
435  Delete2DArray (OutputSlopes, Noutputs);
436  Delete2DArray (OutputPrevSlopes, Noutputs);
437 
438  delete CandValues; CandValues = NULL;
439  delete CandSumValues; CandSumValues = NULL;
440 
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);
453 
454  delete feature_type; feature_type = NULL;
455  delete AllConnections; AllConnections = NULL;
456 } /* CleanUpMemory */
457 
458 
459 
460 
461 
462 
463 
464 
465 kkint32 UsfCasCor::MemoryConsumedEstimated () const
466 {
467  kkint32 memoryConsumedEstimated = sizeof (*this);
468 
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);
474 
475  if (AllConnections) memoryConsumedEstimated += MaxUnits * sizeof (int);
476  if (Nconnections) memoryConsumedEstimated += MaxUnits * sizeof (int);
477  if (Connections) memoryConsumedEstimated += MaxUnits * sizeof (int*);
478 
479  if (Nconnections)
480  {
481  memoryConsumedEstimated += MaxUnits * sizeof (float*);
482  for (int x = 0; x < MaxUnits; ++x)
483  memoryConsumedEstimated += Nconnections[x] * sizeof (float);
484  }
485 
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);
491 
492 
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);
497 
498  if (ExtraErrors) memoryConsumedEstimated += Noutputs * sizeof (float);
499  if (CandValues) memoryConsumedEstimated += Noutputs * sizeof (float);
500  if (CandSumValues) memoryConsumedEstimated += Noutputs * sizeof (float);
501 
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);
508 
509  return memoryConsumedEstimated;
510 } /* MemoryConsumedEstimated */
511 
512 
513 
514 
515 /* Administrative variables */
516 const char* UsfCasCor::version="5.0";
517 const char* UsfCasCor::release_date="07-07-2012";
518 const char* UsfCasCor::progname="UsfCasCor";
519 
520 
521 
522 MLClassPtr UsfCasCor::PredictClass (FeatureVectorPtr example)
523 {
524  MLClassPtr predictedClass = NULL;
525  _load_test_data (example);
526 
527  float totalFeature = example->TotalOfFeatureData ();
528 
529  /* Global's must be saved from the last training phase. If they are not */
530  /* saved then the next unit will be training to correlate with the test */
531  /* set error. */
532  Boolean old_UC = UseCache; /* temporarily turn off cache */
533  float old_ST = ScoreThreshold; /* save global */
534  float old_TE = TrueError; /* save global */
535  float *old_SE = SumErrors; /* save global */
536  float old_SSE = SumSqError; /* save global */
537 
538  //ScoreThreshold = test_threshold;
539  UseCache = false;
540 
541  Values = ExtraValues;
542  Errors = ExtraErrors;
543 
544  int i = 0;
545  {
546  Goal = TestOutputs[i];
547  FULL_FORWARD_PASS (TestInputs[i]);
548 
549  /* Find max. output (predicted class) */
550  int max_pred = 0;
551  for (int j = 0; j < Noutputs; j++)
552  {
553  if (Outputs[max_pred] < Outputs[j])
554  max_pred = j;
555  }
556 
557  if (max_pred < classes->QueueSize ())
558  predictedClass = classes->IdxToPtr (max_pred);
559  }
560 
561  /* restore globals */
562  UseCache = old_UC;
563  ScoreThreshold = old_ST;
564  TrueError = old_TE;
565  SumErrors = old_SE;
566  SumSqError = old_SSE;
567 
568  return predictedClass;
569 } /* PredictClass */
570 
571 
572 
573 
574 void UsfCasCor::PredictConfidences (FeatureVectorPtr example,
575  MLClassPtr knownClass,
576  MLClassPtr& predClass1,
577  float& predClass1Prob,
578  MLClassPtr& predClass2,
579  float& predClass2Prob,
580  float& knownClassProb,
581  const MLClassList& classOrder, /**< Dictates the order in which 'probabilities' will be populated. */
582  VectorFloat& probabilities
583  )
584 {
585  float totalFeature = example->TotalOfFeatureData ();
586  _load_test_data (example);
587 
588  /* Global's must be saved from the last training phase. If they are not */
589  /* saved then the next unit will be training to correlate with the test */
590  /* set error. */
591  Boolean old_UC = UseCache; /* temporarily turn off cache */
592  float old_ST = ScoreThreshold; /* save global */
593  float old_TE = TrueError; /* save global */
594  float* old_SE = SumErrors; /* save global */
595  float old_SSE = SumSqError; /* save global */
596 
597  //ScoreThreshold = test_threshold;
598  UseCache = false;
599 
600  Values = ExtraValues;
601  Errors = ExtraErrors;
602 
603  /* Zero some accumulators. */
604  TrueError = 0.0;
605  SumErrors = DummySumErrors;
606  SumSqError = 0.0;
607 
608  float totalDelta = 0.0f;
609  predClass1 = NULL;
610  predClass2 = NULL;
611  predClass1Prob = -9999.99f;
612  predClass2Prob = -9999.99f;
613 
614  int i = 0;
615  {
616  Goal = TestOutputs[i];
617  FULL_FORWARD_PASS (TestInputs[i]);
618 
619  /* Find max. output (predicted class) */
620 
621  for (int j = 0; j < Noutputs; j++)
622  totalDelta += (Outputs[j] - SigmoidMin);
623  }
624 
625  probabilities.clear ();
626  for (int j = 0; j < Noutputs; j++)
627  {
628  MLClassPtr ic = classes->IdxToPtr (j);
629 
630  float prob = (Outputs[j] - SigmoidMin) / totalDelta;
631 
632  if (ic == knownClass)
633  knownClassProb = prob;
634 
635  if (prob > predClass1Prob)
636  {
637  predClass2 = predClass1;
638  predClass2Prob = predClass1Prob;
639  predClass1 = ic;
640  predClass1Prob = prob;
641  }
642  else if (prob > predClass2Prob)
643  {
644  predClass2 = ic;
645  predClass2Prob = prob;
646  }
647  }
648 
649  probabilities.clear ();
650  MLClassList::const_iterator idx;
651  for (idx = classOrder.begin (); idx != classOrder.end (); ++idx)
652  {
653  MLClassPtr ic = *idx;
654  int j = classes->PtrToIdx (ic);
655  if (j >= 0)
656  {
657  float prob = (Outputs[j] - SigmoidMin) / totalDelta;
658  probabilities.push_back (prob);
659  }
660  else
661  {
662  probabilities.push_back (0.0f);
663  }
664  }
665 
666  /* restore global's */
667  UseCache = old_UC;
668  ScoreThreshold = old_ST;
669  TrueError = old_TE;
670  SumErrors = old_SE;
671  SumSqError = old_SSE;
672 
673  return;
674 } /* PredictConfidences */
675 
676 
677 
678 
679 
680 
681 ClassProbListPtr UsfCasCor::PredictClassConfidences (FeatureVectorPtr example)
682 {
683  float totalFeature = example->TotalOfFeatureData ();
684  _load_test_data (example);
685 
686  /* Global's must be saved from the last training phase. If they are not */
687  /* saved then the next unit will be training to correlate with the test */
688  /* set error. */
689  Boolean old_UC = UseCache; /* temporarily turn off cache */
690  float old_ST = ScoreThreshold; /* save global */
691  float old_TE = TrueError; /* save global */
692  float* old_SE = SumErrors; /* save global */
693  float old_SSE = SumSqError; /* save global */
694 
695  //ScoreThreshold = test_threshold;
696  UseCache = false;
697 
698  Values = ExtraValues;
699  Errors = ExtraErrors;
700 
701  /* Zero some accumulators. */
702  TrueError = 0.0;
703  SumErrors = DummySumErrors;
704  SumSqError = 0.0;
705 
706  float totalDelta = 0.0f;
707  int i = 0;
708  {
709  Goal = TestOutputs[i];
710  FULL_FORWARD_PASS (TestInputs[i]);
711  for (int j = 0; j < Noutputs; j++)
712  totalDelta += (Outputs[j] - SigmoidMin);
713  }
714 
715  ClassProbListPtr results = new ClassProbList (true);
716 
717  if (totalDelta == 0.0f)
718  {
719  float p = 1.0f / Noutputs;
720  for (int j = 0; j < Noutputs; j++)
721  {
722  MLClassPtr ic = classes->IdxToPtr (j);
723  results->PushOnBack (new ClassProb (ic, p, 0.0f));
724  }
725  }
726  else
727  {
728  for (int j = 0; j < Noutputs; j++)
729  {
730  MLClassPtr ic = classes->IdxToPtr (j);
731  float prob = (Outputs[j] - SigmoidMin) / totalDelta;
732  results->PushOnBack (new ClassProb (ic, prob, 0.0f));
733  }
734  }
735 
736  /* restore globals */
737  UseCache = old_UC;
738  ScoreThreshold = old_ST;
739  TrueError = old_TE;
740  SumErrors = old_SE;
741  SumSqError = old_SSE;
742 
743  return results;
744 } /* PredictClassConfidences */
745 
746 
747 
748 
749 
750 void UsfCasCor::TrainNewClassifier (kkint32 _in_limit,
751  kkint32 _out_limit,
752  kkint32 _number_of_rounds,
753  kkint32 _number_of_trials,
754  kkint64 _the_random_seed,
755  bool _useCache,
756  FeatureVectorListPtr _trainData,
757  FeatureNumListConstPtr _selectedFeatures,
758  VolConstBool& _cancelFlag,
759  RunLog& _log
760  )
761 {
762  _log.Level (10) << "Cascade Correlation: Version[" << version << "]" << endl;
763 
764  if (_selectedFeatures)
765  selectedFeatures = new FeatureNumList (*_selectedFeatures);
766  else
767  selectedFeatures = new FeatureNumList (_trainData->FileDesc ());
768 
769 
770  FeatureVectorListPtr filteredTrainData = FilterOutExtremeExamples (_trainData);
771 
772  delete classes;
773  classes = filteredTrainData->ExtractListOfClasses ();
774  classes->SortByName ();
775 
776  INITIALIZE_GLOBALS ();
777 
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;
783  if (_useCache)
784  UseCache = true;
785  else
786  UseCache = false;
787 
788  /* First, load the data and configuration */
789  setup_network (filteredTrainData, _log);
790 
791  train_network (_cancelFlag, _log);
792 
793  delete filteredTrainData;
794 } /* TrainNewClassifier */
795 
796 
797 
798 
799 
800 
801 /**
802  *@brief Will create a list that excludes samples that have extreme values that can trip up the Neural net.
803  */
804 FeatureVectorListPtr UsfCasCor::FilterOutExtremeExamples (FeatureVectorListPtr trainExamples)
805 {
806  // At this point the training data should be normalized.
807 
808  kkint16 numSelFeatures = selectedFeatures->NumOfFeatures ();
809  const kkuint16* selFeatures = selectedFeatures->FeatureNums ();
810 
811  FeatureVectorListPtr result = new FeatureVectorList (trainExamples->FileDesc (), false);
812  FeatureVectorList::iterator idx;
813  for (idx = trainExamples->begin (); idx != trainExamples->end (); ++idx)
814  {
815  bool extremeValuesFound = false;
816  FeatureVectorPtr fv = *idx;
817 
818  for (kkint32 x = 0; x < numSelFeatures; ++x)
819  {
820  if (fabs (fv->FeatureData (selFeatures[x])) > 1000.0)
821  {
822  extremeValuesFound = true;
823  break;
824  }
825  }
826 
827  if (!extremeValuesFound)
828  result->PushOnBack (fv);
829  }
830 
831  return result;
832 } /* FilterOutExtremeExamples */
833 
834 
835 
836 
837 /*
838  * Get and initialize a network.
839  */
840 void UsfCasCor::setup_network (FeatureVectorListPtr trainExamples,
841  RunLog& log
842  )
843 {
844  /*
845  There are some required variables, like NInputs,etc
846  that can be taken from the training/testing files.
847  */
848  load_namesfile (trainExamples, selectedFeatures);
849 
850 
851  /* At this point, it looks like the MaxUnits parameter is
852  simply the sum of NInputs+1 and the max. number of units
853  to add. Set this manually, since it doesn't seem to be
854  set elsewhere.
855  */
856 
857 
858  if (number_of_rounds == -1)
859  number_of_rounds=15;
860 
861  //(Ninputs+Noutputs)/2;
862  MaxUnits = Ninputs + 1 + number_of_rounds;
863 
864  /* Once all arguments have been read and what parameters we
865  have, we have -- then, build the network and load the data.
866  */
867  allocate_network (log);
868  load_data (trainExamples, log);
869 
870  /* Randomization. If not specified on command line and NonRandomSeed
871  is not true then seed with time (truly random) */
872  if (NonRandomSeed)
873  the_random_seed = 1;
874 
875  if (the_random_seed <= 0)
876  the_random_seed = time(NULL);
877 
878  the_random_seed += GetProcessId () + my_mpi_rank;
879 
880  SRand48 (the_random_seed);
881  log.Level (10) << "Starting seed " << ((NonRandomSeed)?"fixed":"random") << " at " << the_random_seed << endl;
882 
883  /* Initialize the network variables */
884  initialize_network ();
885 
886  return;
887 } /* setup_network */
888 
889 
890 
891 
892 void UsfCasCor::train_network (VolConstBool& cancelFlag,
893  RunLog& log
894  )
895 {
896  int nhidden; /* number of hidden units used in run */
897  int vics, defs, i;
898  long total_epochs, total_units, total_trials;
899  long min_units, max_units, min_epochs, max_epochs;
900 
901  /* initialize testing parms */
902  total_epochs = 0;
903  total_units = 0;
904  min_units = INT_MAX;
905  min_epochs = INT_MAX;
906  max_units = 0;
907  max_epochs = 0;
908  total_trials = 0;
909  vics = 0;
910  defs = 0;
911 
912  /* Start the main processing loop */
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 << "]."
918  << endl;
919 
920  for (i = 0; (i < number_of_trials) && (!cancelFlag); i++)
921  {
922  Trial = i + 1;
923 
924  if (number_of_trials > 1)
925  log.Level (10) << "train_network Trial " << Trial << endl;
926 
927  switch (TRAIN (out_limit, in_limit, number_of_rounds, cancelFlag, log))
928  {
929  case WIN:
930  vics++;
931  break;
932 
933  case LOSE:
934  defs++;
935  break;
936  }
937 
938  /* how did we do? */
939  if (Test)
940  TEST_EPOCH (ScoreThreshold, log);
941 
942 #ifdef CONNX
943  printf (" Connection Crossings: %d\n\n", conx);
944 #endif
945 
946  /* collect trail stats */
947  nhidden = Nunits - Ninputs - 1; /* don't count inputs or bias unit */
948  total_epochs += Epoch;
949  total_units += nhidden;
950  total_trials++;
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;
955  } /* End trial loop */
956 
957  /* print out loop stats */
958  log.Level (10) << endl << "train_network TRAINING STATS" << endl;
959  LIST_PARAMETERS ();
960  log.Level (10) << "Victories: " << vics << ", Defeats: " << defs << endl;
961 
962  log.Level (10) << "Training Epochs - "
963  << "Min: " << min_epochs << " "
964  << "Avg: " << (total_epochs / total_trials) << " "
965  << "Max: " << max_epochs
966  << endl;
967 
968  log.Level (10) << "Hidden Units - "
969  << "Min: " << min_units << " "
970  << "Avg: " << ((float)total_units /total_trials) << " "
971  << "Max: " << max_units
972  << endl;
973 
974  return;
975 } /* train_network */
976 
977 
978 
979 
980 /* Create the network data structures, given the number of input and output
981  * units. Get the MaxUnits value from a variable.
982  */
983 void UsfCasCor::allocate_network (RunLog& log)
984 {
985  int i;
986 /***************/
987 
988  if (NTrainingPatterns > NTestPatterns)
989  MaxCases = NTrainingPatterns;
990  else
991  MaxCases = NTestPatterns;
992 
993  Ncases = NTrainingPatterns;
994  FirstCase = 0;
995  Nunits = 1 + Ninputs;
996 
997 
998  /* setup for ErrorIndex */
999  NtrainingOutputValues = Noutputs * NTrainingPatterns;
1000  NtestOutputValues = Noutputs * NTestPatterns;
1001 
1002  if (Nunits > MaxUnits)
1003  {
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
1007  << endl;
1008 
1009  MaxUnits = Nunits + 1;
1010  }
1011 
1012 
1013  /* allocate memory for outer arrays */
1014  ExtraValues = new float[MaxUnits];
1015  Values = ExtraValues;
1016 
1017  Nconnections = new int[MaxUnits];
1018  Connections = new int*[MaxUnits];
1019  Weights = new float*[MaxUnits];
1020 
1021  ExtraErrors = new float[Noutputs];
1022  SumErrors = new float[Noutputs];
1023  DummySumErrors = new float[Noutputs];
1024  Errors = ExtraErrors;
1025 
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];
1031 
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];
1040 
1041  TrainingInputs = new float*[NTrainingPatterns];
1042  TrainingOutputs = new float*[NTrainingPatterns];
1043 
1044  if (NTestPatterns)
1045  {
1046  TestInputs = new float*[NTestPatterns];
1047  TestOutputs = new float*[NTestPatterns];
1048  }
1049 
1050  else
1051  { /* no test patterns so just point at training set */
1052  TestInputs = TrainingInputs;
1053  TestOutputs = TrainingOutputs;
1054  }
1055 
1056  /* Only create the caches if UseCache is on -- may not always have room. */
1057  if (UseCache)
1058  {
1059  ValuesCache = new float*[MaxCases];
1060  ErrorsCache = new float*[MaxCases];
1061  for (i = 0; i < MaxCases; i++)
1062  {
1063  ValuesCache[i] = new float[MaxUnits];
1064  ErrorsCache[i] = new float[Noutputs];
1065  }
1066  }
1067 
1068  /* Allocate per unit data arrays */
1069  for (i = 0; i < Noutputs; i++)
1070  {
1071  OutputWeights [i] = new float[MaxUnits];
1072  OutputDeltas [i] = new float[MaxUnits];
1073  OutputSlopes [i] = new float[MaxUnits];
1074  OutputPrevSlopes[i] = new float[MaxUnits];
1075  }
1076 
1077  for (i = 0; i < Ncandidates; i++)
1078  {
1079  CandCor[i] = new float[Noutputs];
1080  CandPrevCor[i] = new float[Noutputs];
1081 
1082  CandWeights[i] = new float[MaxUnits];
1083  CandDeltas[i] = new float[MaxUnits];
1084  CandSlopes[i] = new float[MaxUnits];
1085  CandPrevSlopes[i] = new float[MaxUnits];
1086  }
1087 
1088  /* Allocate per case data arrays */
1089  for (i = 0; i < NTrainingPatterns; i++)
1090  {
1091  TrainingInputs[i] = new float[Ninputs];
1092  TrainingOutputs[i] = new float[Noutputs];
1093  }
1094 
1095  for (i = 0; i < NTestPatterns; i++)
1096  {
1097  TestInputs[i] = new float[Ninputs];
1098  TestOutputs[i] = new float[Noutputs];
1099  }
1100 
1101  /* Allocate generic connection vector */
1102  AllConnections = new int[MaxUnits];
1103 
1104  return;
1105 } /* allocate_network */
1106 
1107 
1108 
1109 
1110 
1111 //****************************************************************
1112 //* 'parms.c' *
1113 //****************************************************************
1114 
1115 
1116 #define EOL '\0'
1117 
1118 
1119 
1120 const KKStr UsfCasCor::type_strings[]={"SIGMOID","GAUSSIAN", "LINEAR","ASYMSIGMOID","VARSIGMOID","WIN","STAGNANT","TIMEOUT","LOSE","BITS","INDEX","Bad Type"};
1121 
1122 
1123 /* Input of the type variables and return a string showing its value. This
1124  * is only used as a output routine for the user's convenience.
1125  */
1126 const KKStr& UsfCasCor::type_to_string (int var) const
1127 {
1128  switch (var)
1129  {
1130  case SIGMOID: return(type_strings[0]);
1131  case GAUSSIAN: return(type_strings[1]);
1132  case LINEAR: return(type_strings[2]);
1133  case ASYMSIGMOID: return(type_strings[3]);
1134  case VARSIGMOID: return(type_strings[4]);
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]);
1141 
1142  default: return(type_strings[11]);
1143  }
1144 } /* type_to_string */
1145 
1146 
1147 
1148 
1149 
1150 
1151 int UsfCasCor::string_to_type (const KKStr& s)
1152 {
1153  if (s.EqualIgnoreCase (type_strings[0]))
1154  return SIGMOID;
1155 
1156  else if (s.EqualIgnoreCase (type_strings[1]))
1157  return GAUSSIAN;
1158 
1159  else if (s.EqualIgnoreCase (type_strings[2]))
1160  return LINEAR;
1161 
1162  else if (s.EqualIgnoreCase (type_strings[3]))
1163  return ASYMSIGMOID;
1164 
1165  else if (s.EqualIgnoreCase (type_strings[4]))
1166  return VARSIGMOID;
1167 
1168  else if (s.EqualIgnoreCase (type_strings[5]))
1169  return WIN;
1170 
1171  else if (s.EqualIgnoreCase (type_strings[6]))
1172  return STAGNANT;
1173 
1174  else if (s.EqualIgnoreCase (type_strings[7]))
1175  return TIMEOUT;
1176 
1177  else if (s.EqualIgnoreCase (type_strings[8]))
1178  return LOSE;
1179 
1180  else if (s.EqualIgnoreCase (type_strings[9]))
1181  return BITS;
1182 
1183  else if (s.EqualIgnoreCase (type_strings[10]))
1184  return INDEX;
1185 
1186  return -1;
1187 } /* string_to_type */
1188 
1189 
1190 
1191 
1192 char const * UsfCasCor::boolean_to_string (bool var) const
1193 {
1194  if (var)
1195  return "true";
1196  else
1197  return "false";
1198 } /* boolean_to_string */
1199 
1200 
1201 
1202 
1203 Boolean UsfCasCor::string_to_boolean (const char* s)
1204 {
1205  if ((STRICMP (s, "true") == 0) ||
1206  (STRICMP (s, "T") == 0) ||
1207  (STRICMP (s, "yes") == 0) ||
1208  (STRICMP (s, "on") == 0)
1209  )
1210  return true;
1211  else
1212  return false;
1213 }
1214 
1215 
1216 
1217 
1218 
1219 /* Convert '\0' terminated sting to all lower case characters. This routine
1220  * is destructive.
1221  */
1222 void UsfCasCor::strdncase (char *s)
1223 {
1224  int i;
1225  for (i = 0; s[i] != EOL; i++)
1226  {
1227  if (isupper(s[i]))
1228  s[i] = tolower(s[i]); /* tolower only guaranteed on upper case */
1229  else
1230  s[i] = s[i];
1231  }
1232 }
1233 
1234 
1235 
1236 
1237 
1238 /**********************************************************
1239  Functions needed only in this file.
1240 ***********************************************************/
1241 
1242 int UsfCasCor::_type_convert (char *input)
1243 {
1244  strdncase(input);
1245 
1246  if (!strcmp (input,"true"))
1247  return (1);
1248 
1249  else if (!strcmp (input, "1")) /* allow backward compatible input */
1250  return (1);
1251 
1252  else if (!strcmp (input, "false"))
1253  return (0);
1254 
1255  else if (!strcmp(input, "0")) /* allow backward compatible input */
1256  return (0);
1257 
1258  else if (!strcmp (input, "sigmoid"))
1259  return (SIGMOID);
1260 
1261  else if (!strcmp (input, "gaussian"))
1262  return (GAUSSIAN);
1263 
1264  else if (!strcmp (input, "linear"))
1265  return (LINEAR);
1266 
1267  else if (!strcmp (input, "asymsigmoid"))
1268  return (ASYMSIGMOID);
1269 
1270  else if (!strcmp (input, "varsigmoid"))
1271  return (VARSIGMOID);
1272 
1273  else if (!strcmp (input, "bits"))
1274  return (BITS);
1275 
1276  else if (!strcmp (input, "index"))
1277  return (INDEX);
1278 
1279  else
1280  return -1;
1281 } /* _type_convert */
1282 
1283 
1284 
1285 Boolean UsfCasCor::_boolean_convert (char *input)
1286 {
1287  strdncase (input);
1288  if (!strcmp (input, "true") ||
1289  !strcmp (input, "1") ||
1290  !strcmp (input, "t")
1291  )
1292  return true;
1293 
1294  if (!strcmp (input, "false") || !strcmp(input,"0"))
1295  return false;
1296 
1297  return false;
1298 } /* _boolean_convert */
1299 
1300 
1301 //**********************************************************************************
1302 //* util.c *
1303 //**********************************************************************************
1304 
1305 
1306 
1307 float UsfCasCor::random_weight ()
1308 {
1309  return ( (float) (WeightRange * (LRand48 () % 1000 / 500.0)) - WeightRange);
1310 }
1311 
1312 
1313 
1314 //**********************************************************************************
1315 //* netio.c *
1316 //**********************************************************************************
1317 
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"
1326 
1327 
1328 
1329 
1330 //******************************************************************************************
1331 //* load_namesfile.c *
1332 //******************************************************************************************
1333 void UsfCasCor::load_namesfile (FeatureVectorListPtr trainExamples,
1334  FeatureNumListConstPtr selectedFeatures
1335  )
1336 {
1337  /* First, ensure the necessary variables are reset */
1338  NTrainingPatterns = -1;
1339  NTestPatterns = -1;
1340 
1341  if (!classes)
1342  classes = trainExamples->ExtractListOfClasses ();
1343  classes->SortByName ();
1344  number_of_classes = classes->QueueSize ();
1345 
1346  NTrainingPatterns = trainExamples->QueueSize ();
1347 
1348  NTestPatterns = 1; // We will be testing one example at a time.
1349 
1350  kkint32 feature_count = selectedFeatures->NumOfFeatures ();
1351 
1352  delete feature_type;
1353  feature_type = new int[feature_count];
1354  for (int i = 0; i < feature_count; ++i)
1355  {
1356  feature_type[i] = REAL;
1357  }
1358 
1359  Noutputs = number_of_classes;
1360 
1361  Ninputs = feature_count;
1362 
1363  return;
1364 } /* load_namesfile */
1365 
1366 
1367 
1368 
1369 /***********************************************************************/
1370 /* learn.c */
1371 /***********************************************************************/
1372 
1373 
1374 /*
1375  * Given the sum of weighted inputs, compute the unit's activation value.
1376  * Defined unit types are SIGMOID, VARSIGMOID, and GAUSSIAN.
1377  */
1378 float UsfCasCor::ACTIVATION (float sum)
1379 {
1380  float temp;
1381 
1382  switch (UnitType)
1383  {
1384  case SIGMOID:
1385  /* Sigmoid function in range -0.5 to 0.5. */
1386  if (sum < -15.0)
1387  return(-0.5f);
1388 
1389  else if (sum > 15.0)
1390  return(0.5f);
1391 
1392  else
1393  return (1.0f / (1.0f + exp (-sum)) - 0.5f);
1394 
1395  case GAUSSIAN:
1396  /* Gaussian activation function in range 0.0 to 1.0. */
1397  temp = -0.5f * sum * sum;
1398  if (temp < -75.0f)
1399  return (0.0f);
1400  else
1401  return (exp (temp));
1402 
1403  case ASYMSIGMOID:
1404  /* asymmetrical sigmoid function in range 0.0 to 1.0. */
1405  if (sum < -15.0f)
1406  return (0.0f);
1407  else if (sum > 15.0f)
1408  return (1.0f);
1409  else
1410  return (1.0f / (1.0f + exp(-sum)));
1411 
1412 
1413  case VARSIGMOID:
1414  /* Sigmoid function in range SigmoidMin to SigmoidMax. */
1415  if (sum < -15.0)
1416  return (SigmoidMin);
1417 
1418  else if (sum > 15.0f)
1419  return (SigmoidMax);
1420 
1421  else
1422  return ((SigmoidMax - SigmoidMin)/ (1.0f + exp(-sum)) + SigmoidMin);
1423  }
1424  return -1;
1425 } /* ACTIVATION */
1426 
1427 
1428 
1429 
1430 /*
1431  * Given the unit's activation value and sum of weighted inputs, compute
1432  * the derivative of the activation with respect to the sum. Defined unit
1433  * types are SIGMOID, VARSIGMOID, and GAUSSIAN.
1434  *
1435  * Note: do not use sigmoid prime offset here, as it confuses the
1436  * correlation machinery. But do use it in output-prime.
1437  *
1438  */
1439 float UsfCasCor::ACTIVATION_PRIME (float value,
1440  float sum
1441  )
1442 {
1443  switch(UnitType)
1444  {
1445  case SIGMOID:
1446  /* Symmetrical sigmoid function. */
1447  return (0.25f - value * value);
1448 
1449  case GAUSSIAN:
1450  /* Gaussian activation function. */
1451  return (sum * (- value));
1452 
1453  case ASYMSIGMOID:
1454  /* asymmetrical sigmoid function in range 0.0 to 1.0. */
1455  return (value * (1.0f - value));
1456 
1457  case VARSIGMOID:
1458  /* Sigmoid function with range SigmoidMin to SigmoidMax. */
1459  return ((value - SigmoidMin) * (1.0f - (value - SigmoidMin) /
1460  (SigmoidMax - SigmoidMin)));
1461 
1462  }
1463  return -1.0f;
1464 } /* ACTIVATION_PRIME */
1465 
1466 
1467 
1468 
1469 /* Compute the value of an output, given the weighted sum of incoming values.
1470  * Defined output types are SIGMOID, ASYMSIGMOID, and LINEAR.
1471  */
1472 float UsfCasCor::OUTPUT_FUNCTION (float sum)
1473 {
1474  switch (OutputType)
1475  {
1476  case SIGMOID:
1477  /* Symmetrical sigmoid function, used for binary functions. */
1478  if (sum < -15.0)
1479  return (-0.5f);
1480 
1481  else if (sum > 15.0f)
1482  return (0.5f);
1483 
1484  else
1485  return (1.0f / (1.0f + exp (-sum)) - 0.5f);
1486 
1487  case LINEAR:
1488  /* Linear output function, used for continuous functions. */
1489  return (sum);
1490 
1491  case ASYMSIGMOID:
1492  /* asymmetrical sigmoid function in range 0.0 to 1.0. */
1493  if (sum < -15.0f)
1494  return (0.0f);
1495 
1496  else if (sum > 15.0f)
1497  return (1.0f);
1498 
1499  else
1500  return (1.0f / (1.0f + exp (-sum)));
1501 
1502  case VARSIGMOID:
1503  /* Sigmoid function in range SigmoidMin to SigmoidMax. */
1504  if (sum < -15.0f)
1505  return (SigmoidMin);
1506 
1507  else if (sum > 15.0f)
1508  return(SigmoidMax);
1509 
1510  else
1511  return ((SigmoidMax - SigmoidMin) / (1.0f + exp (-sum)) + SigmoidMin);
1512  }
1513  return -1.0f;
1514 } /* OUTPUT_FUNCTION */
1515 
1516 
1517 
1518 
1519 /* Compute the value of an output, given the weighted sum of incoming values.
1520  * Defined output types are SIGMOID, ASYMSIGMOID, and LINEAR.
1521  *
1522  * Sigmoid_Prime_Offset used to keep the back-prop error value from going to
1523  * zero.
1524  */
1525 float UsfCasCor::OUTPUT_PRIME (float output)
1526 {
1527  switch(OutputType)
1528  {
1529  case SIGMOID:
1530  /* Symmetrical sigmoid function, used for binary functions. */
1531  return (SigmoidPrimeOffset + 0.25f - output * output);
1532 
1533  case LINEAR:
1534  /* Linear output function, used for continuous functions. */
1535  return (1.0);
1536 
1537  case ASYMSIGMOID:
1538  /* asymmetrical sigmoid function in range 0.0 to 1.0. */
1539  return (SigmoidPrimeOffset + output * (1.0f - output));
1540 
1541  case VARSIGMOID:
1542  /* Sigmoid function with range SigmoidMin to SigmoidMax. */
1543  return (SigmoidPrimeOffset +
1544  (output - SigmoidMin) * (1.0f - (output - SigmoidMin) /
1545  (SigmoidMax - SigmoidMin)));
1546  }
1547 
1548  return -1.0f;
1549 } /* OUTPUT_PRIME */
1550 
1551 
1552 
1553 
1554 /* The basic routine for doing quickprop-style update of weights, given a
1555  * pair of slopes and a delta.
1556  *
1557  * Given arrays holding weights, deltas, slopes, and previous slopes,
1558  * and an index i, update weight[i] and delta[i] appropriately. Move
1559  * slope[i] to prev[i] and zero out slope[i]. Add weight decay term to
1560  * each slope before doing the update.
1561  */
1562 void UsfCasCor::QUICKPROP_UPDATE (int i,
1563  float weights[],
1564  float deltas[],
1565  float slopes[],
1566  float prevs[],
1567  float epsilon,
1568  float decay,
1569  float mu,
1570  float shrink_factor
1571  )
1572 {
1573  float w,d,s,p, next_step;
1574  /********/
1575 
1576  w = weights[i];
1577  d = deltas[i];
1578  s = slopes[i] + decay * w;
1579  p = prevs[i];
1580  next_step = 0.0f;
1581 
1582  /* The step must always be in direction opposite to the slope. */
1583 
1584  if(d < 0.0f){
1585  /* If last step was negative... */
1586  if(s > 0.0f)
1587  /* Add in linear term if current slope is still positive.*/
1588  next_step -= epsilon * s;
1589  /*If current slope is close to or larger than prev slope... */
1590  if(s >= (shrink_factor*p))
1591  next_step += mu * d; /* Take maximum size negative step. */
1592  else
1593  next_step += d * s / (p - s); /* Else, use quadratic estimate. */
1594  }
1595  else if(d > 0.0f){
1596  /* If last step was positive... */
1597  if(s < 0.0f)
1598  /* Add in linear term if current slope is still negative.*/
1599  next_step -= epsilon * s;
1600  /* If current slope is close to or more neg than prev slope... */
1601  if(s <= (shrink_factor*p))
1602  next_step += mu * d; /* Take maximum size negative step. */
1603  else
1604  next_step += d * s / (p - s); /* Else, use quadratic estimate. */
1605  }
1606  else
1607  /* Last step was zero, so use only linear term. */
1608  next_step -= epsilon * s;
1609 
1610  /* update global data arrays */
1611  deltas[i] = next_step;
1612  weights[i] = w + next_step;
1613  prevs[i] = s;
1614  slopes[i] = 0.0;
1615 } /* QUICKPROP_UPDATE */
1616 
1617 
1618 
1619 
1620 /* Set up all the inputs from the INPUT vector as the first few entries in
1621  in the values vector.
1622 */
1623 void UsfCasCor::SETUP_INPUTS (float inputs[])
1624 {
1625  int i;
1626  /*********/
1627 
1628  Values[0] = 1.0; /* bias unit */
1629  for(i=0; i < Ninputs; i++)
1630  Values[i+1] = inputs[i];
1631 }
1632 
1633 
1634 
1635 
1636 /* Assume the values vector has been set up. Just compute the output
1637  values.
1638 */
1639 void UsfCasCor::OUTPUT_FORWARD_PASS ()
1640 {
1641  int i,j;
1642  float sum;
1643  float *ow;
1644 /********/
1645 
1646  for (j = 0; j < Noutputs; j++)
1647  {
1648  sum = 0.0;
1649  ow = OutputWeights[j];
1650 
1651  for(i=0; i<Nunits; i++)
1652  sum += Values[i] * ow[i];
1653 
1654 #ifdef CONNX
1655  conx += Nunits;
1656 #endif
1657 
1658  Outputs[j] = OUTPUT_FUNCTION(sum);
1659  }
1660 } /* OUTPUT_FORWARD_PASS */
1661 
1662 
1663 
1664 
1665 /* Assume that values vector has been set up for units with index less
1666  than J. Compute and record the value for unit J.
1667 */
1668 void UsfCasCor::COMPUTE_UNIT_VALUE (int j)
1669 {
1670  int i;
1671  int *c; /* pointer to unit's connections array */
1672  float *w, /* pointer to unit's weights array*/
1673  sum = 0.0;
1674 /********/
1675 
1676  c = Connections[j];
1677  w = Weights[j];
1678 
1679  for (i = 0; i < Nconnections[j]; i++)
1680  sum += Values[c[i]] * w[i];
1681 
1682 #ifdef CONNX
1683  conx += Nconnections[j];
1684 #endif
1685 
1686  Values[j] = ACTIVATION (sum);
1687 } /* COMPUTE_UNIT_VALUE */
1688 
1689 
1690 
1691 
1692 /* Set up the inputs from the INPUT vector, then propagate activation values
1693  forward through all hidden units and output units.
1694 */
1695 void UsfCasCor::FULL_FORWARD_PASS (float input[])
1696 {
1697  int j;
1698  /********/
1699 
1700  SETUP_INPUTS (input);
1701 
1702  /* Unit values must be calculated in order because the activations */
1703  /* cascade down through the hidden layers */
1704 
1705  for (j = 1 + Ninputs; j < Nunits; j++) /* For each hidden unit J, compute the */
1706  COMPUTE_UNIT_VALUE (j); /* activation value. */
1707 
1708  OUTPUT_FORWARD_PASS (); /* Now compute outputs. */
1709 } /* FULL_FORWARD_PASS */
1710 
1711 
1712 
1713 
1714 /* Goal is a vector of desired values for the output units. Compute and
1715  * record the output errors for the current training case. Record error
1716  * values and related statistics. If output_slopesp is TRUE, then use errors
1717  * to compute slopes for output weights. If statsp is TRUE, accumulate error
1718  * statistics.
1719  */
1720 void UsfCasCor::COMPUTE_ERRORS (float goal[],
1721  Boolean output_slopesp,
1722  Boolean statsp,
1723  int xw
1724  )
1725 {
1726  int i;
1727  int j;
1728  float out = 0.0f;
1729  float dif = 0.0f;
1730  float err_prime = 0.0f;
1731  float* os = NULL; /* pointer to unit's output slopes array */
1732 
1733  int goal_winner;
1734  int output_winner;
1735 
1736  goal_winner = 0;
1737  output_winner = 0;
1738 
1739  for (i = 1; i < Noutputs; i++)
1740  {
1741  if ( Outputs[output_winner] < Outputs[i])
1742  output_winner=i;
1743 
1744  if ( goal[goal_winner] < goal[i] )
1745  goal_winner=i;
1746  }
1747 
1748  if (goal_winner != output_winner)
1749  ErrorMisclassifications++;
1750 
1751  for (j = 0; j < Noutputs; j++)
1752  {
1753  out = Outputs[j];
1754  dif = out - goal[j];
1755  if (load_weights && xw >= 0 && example_weight[xw] != 1.0 )
1756  dif *= example_weight[xw];
1757 
1758  err_prime = dif * OUTPUT_PRIME(out);
1759  os = OutputSlopes[j];
1760 
1761  Errors[j] = err_prime;
1762 
1763  if (statsp)
1764  {
1765  if (fabs(dif) > ScoreThreshold)
1766  ErrorBits++;
1767  TrueError += dif * dif;
1768  SumErrors[j] += err_prime;
1769  SumSqError += err_prime * err_prime;
1770  }
1771 
1772  if (output_slopesp)
1773  {
1774  for (i = 0; i < Nunits; i++)
1775  os[i] += err_prime * Values[i];
1776  }
1777  } /* end for unit j */
1778 
1779 } /* COMPUTE_ERRORS */
1780 
1781 
1782 
1783 
1784 /* Update the output weights, using the pre-computed slopes, prev-slopes,
1785  * and delta values.
1786  */
1787 void UsfCasCor::UPDATE_OUTPUT_WEIGHTS ()
1788 {
1789  int i,j;
1790  float eps; /* epsilon scaled by fan-in */
1791 /********/
1792 
1793  eps = OutputEpsilon / Ncases;
1794 
1795  for (j = 0; j < Noutputs; j++)
1796  for (i = 0; i < Nunits; i++)
1797  QUICKPROP_UPDATE (i,
1798  OutputWeights[j],
1799  OutputDeltas[j],
1800  OutputSlopes[j],
1801  OutputPrevSlopes[j],
1802  eps,
1803  OutputDecay,
1804  OutputMu,
1805  OutputShrinkFactor
1806  );
1807 
1808 }
1809 
1810 
1811 
1812 
1813 /***********************************************************************/
1814 /* */
1815 /* The outer loops for training output weights. */
1816 /* */
1817 /***********************************************************************/
1818 
1819 
1820 /* Perform forward propagation once for each set of weights in the
1821  * training vectors, computing errors and slopes. Then update the output
1822  * weights.
1823  */
1824 void UsfCasCor::TRAIN_OUTPUTS_EPOCH ()
1825 {
1826  int i;
1827 /********/
1828 
1829  /* zero error accumulators */
1830  ErrorBits = 0;
1831  TrueError = 0.0;
1832  ErrorMisclassifications = 0;
1833  for (i = 0; i < Noutputs; i++)
1834  {
1835  SumErrors[i] = 0.0;
1836  }
1837  SumSqError = 0.0;
1838 
1839  /* User may have changed mu between epochs, so fix shrink-factor. */
1840  OutputShrinkFactor = OutputMu / (1.0f + OutputMu);
1841 
1842  for (i= FirstCase; i < (FirstCase+Ncases); i++)
1843  {
1844  Goal = TrainingOutputs[i];
1845 
1846  if (UseCache)
1847  {
1848  Values = ValuesCache[i];
1849  Errors = ErrorsCache[i];
1850  OUTPUT_FORWARD_PASS();
1851  }
1852  else
1853  {
1854  Values = ExtraValues;
1855  Errors = ExtraErrors;
1856  FULL_FORWARD_PASS(TrainingInputs[i]);
1857  }
1858  COMPUTE_ERRORS (Goal, true, true, i);
1859  }
1860 
1861  switch (ErrorMeasure)
1862  {
1863  case BITS:
1864  /* Do not change weights or count epoch if this run was a winner. */
1865  if (ErrorBits > 0)
1866  {
1867  UPDATE_OUTPUT_WEIGHTS();
1868  Epoch++;
1869  }
1870  break;
1871 
1872  case INDEX:
1873  /* Compute index and don't change weights if we have a winner. */
1874  ErrorIndex = ERROR_INDEX(TrainingStdDev, NtrainingOutputValues);
1875  if (ErrorIndex > ErrorIndexThreshold)
1876  {
1877  UPDATE_OUTPUT_WEIGHTS();
1878  Epoch++;
1879  }
1880  break;
1881  }
1882 
1883 } /* TRAIN_OUTPUTS_EPOCH */
1884 
1885 
1886 
1887 
1888 
1889 /* Train the output weights. If we exhaust max_epochs, stop with value
1890  * TIMEOUT. If there are zero error bits, stop with value WIN. Else,
1891  * keep going until the true error has changed by a significant amount,
1892  * and then until it does not change significantly for Patience epochs.
1893  * Then return STAGNANT. If Patience is zero, we do not stop until victory
1894  * or until max_epochs is used up.
1895  */
1896 
1897 int UsfCasCor::TRAIN_OUTPUTS (int max_epochs,
1898  VolConstBool& cancelFlag
1899  )
1900 {
1901  int i;
1902  int retval = TIMEOUT; /* will be reset within loop for other conditions */
1903  float last_error = 0.0;
1904  int quit_epoch = Epoch + OutputPatience;
1905  Boolean first_time = true;
1906 /********/
1907 
1908  for(i = 0; (i < max_epochs) && (!cancelFlag); ++i)
1909  {
1910  TRAIN_OUTPUTS_EPOCH ();
1911 
1912  if ((ErrorMeasure == BITS) && (ErrorBits == 0))
1913  {
1914  retval = WIN;
1915  break;
1916  }
1917 
1918  else if ((ErrorMeasure == INDEX) && (ErrorIndex <= ErrorIndexThreshold))
1919  {
1920  retval = WIN;
1921  break;
1922  }
1923 
1924  else if (OutputPatience == 0)
1925  continue; /* continue training until victory */
1926 
1927  else if (first_time)
1928  {
1929  first_time = false;
1930  last_error = TrueError;
1931  }
1932 
1933  else if (fabs(TrueError - last_error) > (last_error * OutputChangeThreshold))
1934  {
1935  /* still getting better */
1936  last_error = TrueError;
1937  quit_epoch = Epoch + OutputPatience;
1938  }
1939 
1940  else if (Epoch >= quit_epoch)
1941  {
1942  /* haven't gotten better for a while */
1943  retval = STAGNANT;
1944  break;
1945  }
1946  }
1947 
1948  /* tell user about the output weights of new unit */
1949  /*for(o=0; o<Noutputs; o++){
1950  fprintf(stderr," Output %d Weights: ", o);
1951  for(i=0; i<Nunits; i++)
1952  fprintf(stderr,"%6f ", OutputWeights[o][i]);
1953  fprintf(stderr,"\n");
1954  }
1955  */
1956 
1957  /* return result, will be TIMEOUT unless reset in loop */
1958  return(retval);
1959 } /* TRAIN_OUTPUTS */
1960 
1961 
1962 
1963 
1964 
1965 
1966 /***********************************************************************/
1967 /* */
1968 /* Machinery for Training and selecting candidate units. */
1969 /* */
1970 /***********************************************************************/
1971 
1972 /* Give new random weights to all of the candidate units. Zero the other
1973  * candidate-unit statistics.
1974  */
1975 void UsfCasCor::INIT_CANDIDATES ()
1976 {
1977  int i,j,o;
1978 /********/
1979 
1980  for (i = 0; i < Ncandidates; i++)
1981  {
1982  CandValues[i] = 0.0;
1983  CandSumValues[i] = 0.0;
1984 
1985  for (j = 0; j < Nunits; j++)
1986  {
1987  CandWeights[i][j] = random_weight();
1988  CandDeltas[i][j] = 0.0;
1989  CandSlopes[i][j] = 0.0;
1990  CandPrevSlopes[i][j] = 0.0;
1991  }
1992 
1993  for(o=0; o<Noutputs; o++)
1994  {
1995  CandCor[i][o] = 0.0;
1996  CandPrevCor[i][o] = 0.0;
1997  }
1998  }
1999 } /* INIT_CANDIDATES */
2000 
2001 
2002 
2003 
2004 /* Add the candidate-unit with the best correlation score to the active
2005  * network. Then reinitialize the candidate pool.
2006  */
2007 void UsfCasCor::INSTALL_NEW_UNIT (RunLog& log)
2008 {
2009  int i,o;
2010  float wm; /* temporary weight multiplier */
2011  float *w; /* temporary weight array */
2012  float *cw;
2013 /********/
2014 
2015  if (Nunits >= MaxUnits)
2016  {
2017  log.Level (-1) << endl
2018  << "UsfCasCor::INSTALL_NEW_UNIT ***ERROR*** Can not add more units; limit of MaxUnits[" << MaxUnits << "] has been reached." << endl
2019  << endl;
2020  return;
2021  }
2022 
2023  Nconnections[Nunits] = Nunits;
2024  Connections[Nunits] = AllConnections;
2025  /* Set up the weight vector for the new unit. */
2026  w = new float[Nunits];
2027  cw = CandWeights[BestCandidate];
2028  for (i = 0; i < Nunits; i++)
2029  w[i] = cw[i];
2030  Weights[Nunits] = w;
2031 
2032  /* Tell user about the new unit. */
2033  //for(i=0; i<Nunits; i++)
2034  // fprintf(stderr,"%6f ", Weights[Nunits][i]);
2035  //fprintf(stderr,"\n");
2036 
2037  /* Fix up output weights for candidate unit. Use minus the */
2038  /* correlation times the WeightMultiplier as an initial guess. */
2039 
2040  if (ErrorMeasure == BITS)
2041  wm = WeightMultiplier;
2042  else /* ErrorMeasure == INDEX */
2043  wm = WeightMultiplier / (float)Nunits;
2044 
2045  for (o = 0; o < Noutputs; o++)
2046  OutputWeights[o][Nunits] = -CandPrevCor[BestCandidate][o] * wm;
2047 
2048  /* If using cache, run an epoch to compute this unit's values. */
2049  if (UseCache)
2050  for (i = 0; i < NTrainingPatterns; i++)
2051  {
2052  Values = ValuesCache[i];
2053  COMPUTE_UNIT_VALUE(Nunits);
2054  }
2055 
2056  /* Reinitialize candidate units with random weights. */
2057  Nunits++;
2058  INIT_CANDIDATES();
2059 } /* INSTALL_NEW_UNIT*/
2060 
2061 
2062 
2063 
2064 
2065 
2066 
2067 /* Note: Ideally, after each adjustment of the candidate weights, we would */
2068 /* run two epochs. The first would just determine the correlations */
2069 /* between the candidate unit outputs and the residual error. Then, in a */
2070 /* second pass, we would adjust each candidate's input weights so as to */
2071 /* maximize the absolute value of the correlation. We need to know the */
2072 /* direction to tune the input weights. */
2073 /* */
2074 /* Since this ideal method doubles the number of epochs required for */
2075 /* training candidates, we cheat slightly and use the correlation values */
2076 /* computed BEFORE the most recent weight update. This combines the two */
2077 /* epochs, saving us almost a factor of two. To bootstrap the process, we */
2078 /* begin with a single epoch that computes only the correlation. */
2079 /* */
2080 /* Since we look only at the sign of the correlation after the first ideal */
2081 /* epoch and since that sign should change very infrequently, this probably */
2082 /* is OK. But keep a lookout for pathological situations in which this */
2083 /* might cause oscillation. */
2084 
2085 /* For the current training pattern, compute the value of each candidate
2086  * unit and begin to compute the correlation between that unit's value and
2087  * the error at each output. We have already done a forward-prop and
2088  * computed the error values for active units.
2089  */
2090 void UsfCasCor::COMPUTE_CORRELATIONS ()
2091 {
2092  int i,o,u;
2093  float sum=0.0;
2094  float v=0.0;
2095 /*********/
2096 
2097  for(u=0; u<Ncandidates; u++){
2098  sum = 0.0;
2099  v = 0.0;
2100  /* Determine activation value of each candidate unit. */
2101  for(i=0; i<Nunits; i++)
2102  sum += CandWeights[u][i] * Values[i];
2103 #ifdef CONNX
2104  conx += Nunits;
2105 #endif
2106  v = ACTIVATION(sum);
2107  CandValues[u] = v;
2108  CandSumValues[u] += v;
2109  /* Accumulate value of each unit times error at each output. */
2110  for(o=0; o<Noutputs; o++)
2111  CandCor[u][o] += v * Errors[o];
2112  }
2113 } /* COMPUTE_CORRELATIONS */
2114 
2115 
2116 
2117 
2118 
2119 /* NORMALIZE each accumulated correlation value, and stuff the normalized
2120  * form into the CandPrevCor data structure. Then zero CandCor to
2121  * prepare for the next round. Note the unit with the best total
2122  * correlation score.
2123  */
2124 void UsfCasCor::ADJUST_CORRELATIONS ()
2125 {
2126  int o,u;
2127  float cor, score;
2128  float *cc, *cpc;
2129  float avg_value;
2130 /*********/
2131 
2132  BestCandidate = 0;
2133  BestCandidateScore = 0.0;
2134  for(u=0; u<Ncandidates; u++)
2135  {
2136  avg_value = CandSumValues[u] / Ncases;
2137  cor = 0.0;
2138  score = 0.0;
2139  cc = CandCor[u];
2140  cpc = CandPrevCor[u];
2141  for(o=0; o<Noutputs; o++)
2142  {
2143  cor = (cc[o] - avg_value * SumErrors[o]) / SumSqError;
2144  cpc[o] = cor;
2145  cc[o] = 0.0;
2146  score += fabs(cor);
2147  }
2148 
2149  /* zero CandSumValues for next epoch */
2150  CandSumValues[u] = 0.0;
2151  /* Keep track of the candidate with the best overall correlation. */
2152  if(score > BestCandidateScore){
2153  BestCandidateScore = score;
2154  BestCandidate = u;
2155  }
2156  }
2157 } /* ADJUST_CORRELATIONS */
2158 
2159 
2160 
2161 
2162 
2163 /* After the correlations have been computed, we do a second pass over
2164  * the training set and adjust the input weights of all candidate units.
2165  */
2166 void UsfCasCor::COMPUTE_SLOPES ()
2167 {
2168  int i,o,u;
2169  float sum, value, actprime, direction, error, change;
2170 /*********/
2171 
2172  for (u=0; u<Ncandidates; u++)
2173  {
2174  sum = 0.0;
2175  value = 0.0;
2176  actprime = 0.0;
2177  direction = 0.0;
2178  change = 0.0;
2179  /* Forward pass through each candidate unit to compute activation-prime. */
2180  for(i=0; i<Nunits; i++)
2181  sum += CandWeights[u][i] * Values[i];
2182 #ifdef CONNX
2183  conx += Nunits;
2184 #endif
2185  value = ACTIVATION(sum);
2186  actprime = ACTIVATION_PRIME(value, sum);
2187  CandSumValues[u] += value;
2188  /* Now try to adjust the inputs so as to maximize the absolute value */
2189  /* of the correlation. */
2190  for(o=0; o<Noutputs; o++){
2191  error = Errors[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;
2195  }
2196  for(i=0; i<Nunits; i++)
2197  CandSlopes[u][i] += change * Values[i];
2198  }
2199 } /* COMPUTE_SLOPES */
2200 
2201 
2202 
2203 
2204 /* Update the input weights, using the pre-computed slopes, prev-slopes,
2205  * and delta values.
2206  */
2207 void UsfCasCor::UPDATE_INPUT_WEIGHTS ()
2208 {
2209  int i,u;
2210  float eps;
2211  float *cw, *cd, *cs, *cp;
2212 /*********/
2213 
2214  eps = InputEpsilon / (float)(Ncases * Nunits);
2215  for(u=0; u<Ncandidates; u++)
2216  {
2217  cw = CandWeights[u];
2218  cd = CandDeltas[u];
2219  cs = CandSlopes[u];
2220  cp = CandPrevSlopes[u];
2221  for(i=0; i<Nunits; i++)
2222  QUICKPROP_UPDATE(i, cw, cd, cs, cp, eps, InputDecay, InputMu,
2223  InputShrinkFactor);
2224  }
2225 } /* UPDATE_INPUT_WEIGHTS */
2226 
2227 
2228 
2229 
2230 /* For each training pattern, perform a forward pass and compute correlations.
2231  * Then perform a second forward pass and compute input slopes for the
2232  * candidate units. Finally, use quickprop update to adjust the input weights.
2233  */
2234 
2235 void UsfCasCor::TRAIN_INPUTS_EPOCH ()
2236 {
2237  int i;
2238 /********/
2239 
2240  for(i=FirstCase; i<(Ncases+FirstCase); i++)
2241  {
2242  Goal = TrainingOutputs[i];
2243  if(UseCache){
2244  Values = ValuesCache[i];
2245  Errors = ErrorsCache[i];
2246  }
2247  else {
2248  Values = ExtraValues;
2249  Errors = ExtraErrors;
2250  FULL_FORWARD_PASS(TrainingInputs[i]);
2251  COMPUTE_ERRORS (Goal, false, false, i);
2252  }
2253  COMPUTE_SLOPES();
2254  }
2255  /* User may have changed mu between epochs, so fix shrink-factor.*/
2256  InputShrinkFactor = InputMu / (1.0f + InputMu);
2257 
2258  /* Now tweak the candidate unit input weights. */
2259  UPDATE_INPUT_WEIGHTS();
2260 
2261  /* Fix up the correlation values for the next epoch.*/
2262  ADJUST_CORRELATIONS();
2263  Epoch++;
2264 }
2265 
2266 
2267 
2268 
2269 /* Do an epoch through all active training patterns just to compute the
2270  * correlations. After this one pass, we will update the correlations as we
2271  * train.
2272  */
2273 void UsfCasCor::CORRELATIONS_EPOCH ()
2274 {
2275  int i;
2276 /********/
2277 
2278  for (i=FirstCase; i<(Ncases+FirstCase); i++)
2279  {
2280  Goal = TrainingOutputs[i];
2281  if (UseCache)
2282  {
2283  Values = ValuesCache[i];
2284  Errors = ErrorsCache[i];
2285  }
2286  else
2287  {
2288  Values = ExtraValues;
2289  Errors = ExtraErrors;
2290  FULL_FORWARD_PASS(TrainingInputs[i]);
2291  COMPUTE_ERRORS(Goal, false, false, i);
2292  }
2293  COMPUTE_CORRELATIONS();
2294  }
2295  /* Fix up the correlation values for the next epoch. */
2296  ADJUST_CORRELATIONS();
2297  Epoch++;
2298 } /* CORRELATIONS_EPOCH */
2299 
2300 
2301 
2302 
2303 /* Train the input weights of all candidates. If we exhaust max_epochs,
2304  * stop with value TIMEOUT. Else, keep going until the best candidate unit's
2305  * score has changed by a significant amount, and then
2306  * until it does not change significantly for Patience epochs. Then return
2307  * STAGNANT. If Patience is zero, we do not stop until victory or until
2308  * max_epochs is used up.
2309  */
2310 int UsfCasCor::TRAIN_INPUTS (int max_epochs)
2311 {
2312  int i;
2313  float last_score = 0.0;
2314  int quit = max_epochs;
2315  Boolean first_time = true;
2316 /**********/
2317 
2318  for(i=0; i<Noutputs; i++) /* Convert to the average error for use in */
2319  SumErrors[i] /= Ncases; /* calculation of the correlation. */
2320 
2321  CORRELATIONS_EPOCH();
2322 
2323  for(i=0; i<max_epochs; i++){
2324  TRAIN_INPUTS_EPOCH();
2325 
2326  if(InputPatience == 0)
2327  continue; /* continue training until victory */
2328  else if(first_time){
2329  first_time = false;
2330  last_score = BestCandidateScore;
2331  }
2332  else if(fabs(BestCandidateScore - last_score) > /* still getting better */
2333  (last_score * InputChangeThreshold)){
2334  last_score = BestCandidateScore;
2335  quit = i + InputPatience;
2336  }
2337  else if(i >= quit) /* haven't gotten better for a while */
2338  return(STAGNANT);
2339  }
2340 
2341  /* didn't return within the loop, so must have run out of time. */
2342  return(TIMEOUT);
2343 
2344 } /* TRAIN_INPUTS */
2345 
2346 
2347 
2348 
2349 
2350 /**********************************************************************/
2351 /* */
2352 /* The outer loop routines */
2353 /* */
2354 /**********************************************************************/
2355 
2356 
2357 void UsfCasCor::LIST_PARAMETERS ()
2358 {
2359 #ifdef __STDC__ /* does is compiler conform to the standard? */
2360  //fprintf(stderr,"\nCascor.c Version: %5.2f %s Compiled: %s %s\n",
2361  //VERSION, REL_DATE, __DATE__, __TIME__);
2362 #else
2363  //fprintf(stderr,"\nCascor.c Version: %5.2f %s\n", VERSION, REL_DATE);
2364 #endif
2365  /*fprintf(stderr,"Trial Number %d Parameters\n", Trial);
2366  fprintf(stderr,"SigOff %4.2f, WtRng %4.2f, WtMul %4.2f\n",
2367  SigmoidPrimeOffset, WeightRange, WeightMultiplier);
2368  fprintf(stderr,"OMu %4.2f, OEps %4.2f, ODcy %7.5f, OPat %d, OChange %4.2f\n",
2369  OutputMu, OutputEpsilon, OutputDecay, OutputPatience,
2370  OutputChangeThreshold);
2371  fprintf(stderr,"IMu %4.2f, IEps %4.2f, IDcy %7.5f, IPat %d, IChange %4.2f\n",
2372  InputMu, InputEpsilon, InputDecay, InputPatience,
2373  InputChangeThreshold);
2374  fprintf(stderr,"Utype: %s, Otype: %s, Pool %d\n",
2375  TYPE_STRING(UnitType), TYPE_STRING(OutputType), Ncandidates);
2376  fprintf(stderr,"ErrMeas: %s, ErrThres: %5.3f\n",
2377  TYPE_STRING(ErrorMeasure), ErrorIndexThreshold);
2378 */
2379 }
2380 
2381 
2382 
2383 /* Train the output weights until stagnation or victory is reached. Then
2384  * train the input weights to stagnation or victory. Then install the best
2385  * candidate unit and repeat. OUTLIMIT and INLIMIT are upper limits on the
2386  * number of cycles in the output and input phases. ROUNDS is an upper
2387  * limit on the number of unit-installation cycles.
2388  */
2389 int UsfCasCor::TRAIN (int outlimit,
2390  int inlimit,
2391  int rounds,
2392  VolConstBool& cancelFlag,
2393  RunLog& log
2394  )
2395 {
2396  int i,r;
2397 
2398 /***********/
2399 
2400  initialize_network();
2401  LIST_PARAMETERS();
2402 
2403  if (UseCache)
2404  for(i=0; i<NTrainingPatterns; i++)
2405  {
2406  Values = ValuesCache[i];
2407  SETUP_INPUTS(TrainingInputs[i]);
2408  }
2409 
2410  for (r = 0; (r < rounds) && (!cancelFlag); r++)
2411  {
2412  log.Level (10) << "TRAIN Round " << r << endl;
2413  switch (TRAIN_OUTPUTS (outlimit, cancelFlag))
2414  {
2415  case WIN:
2416  LIST_PARAMETERS();
2417  log.Level (10) << "Victory at "
2418  << Epoch << "epochs, "
2419  << Nunits << " units, "
2420  << (Nunits - Ninputs - 1) << " hidden, "
2421  << TrueError << " Error "
2422  << ErrorIndex << " EI"
2423  << endl;
2424  return(WIN);
2425 
2426  case TIMEOUT:
2427  log.Level (10) << "TRAIN -Output: Epoch " << Epoch << " Timeout" << endl
2428  << " train:" << PRINT_SUMMARY (NTrainingPatterns) << endl;
2429  break;
2430 
2431  case STAGNANT:
2432  log.Level (10) << "TRAIN +Output: Epoch " << Epoch << " Timeout" << endl
2433  << " train:" << PRINT_SUMMARY (NTrainingPatterns) << endl;
2434  break;
2435 
2436  default:
2437  log.Level (10) << "Bad return from TRAIN_OUTPUTS" << endl;
2438  break;
2439  }
2440 
2441  /* DumpWeightsFileforROundx */
2442  if (Test) TEST_EPOCH(0.49, log); /* how are we doing? */
2443 
2444  switch (TRAIN_INPUTS (inlimit))
2445  {
2446  case TIMEOUT:
2447  log.Level (10) << "TRAIN -Input : Epoch " << Epoch << " Timeout (Correlation " << BestCandidateScore << ")" << endl;
2448  break;
2449 
2450  case STAGNANT:
2451  log.Level (10) << "TRAIN -Input : Epoch " << Epoch << " Timeout (Correlation " << BestCandidateScore << ")" << endl;
2452  break;
2453 
2454  default:
2455  log.Level (10) << "TRAIN Bad return from TRAIN_INPUTS" << endl;
2456  break;
2457  }
2458 
2459  INSTALL_NEW_UNIT (log);
2460  log.Level (10) << "ADDED UNIT: " << (r + 1) << endl;
2461  }
2462 
2463  LIST_PARAMETERS ();
2464 
2465  switch (TRAIN_OUTPUTS (outlimit, cancelFlag))
2466  {
2467  case WIN:
2468  log.Level (10) << "TRAIN Victory at " << Epoch << " epochs, " << Nunits << " units, " << (Nunits - Ninputs - 1) << " hidden, Error " << TrueError << " EI " << ErrorIndex << endl;
2469  return(WIN);
2470 
2471  case TIMEOUT:
2472  case STAGNANT:
2473  log.Level (10) << "TRAIN Defeat at " << Nunits << " units " << PRINT_SUMMARY (NTrainingPatterns) << endl;
2474  return(LOSE);
2475 
2476  default:
2477  log.Level (10) << "TRAIN Bad return from TRAIN_OUTPUTS" << endl;
2478  break;
2479  }
2480 
2481  return -1;
2482 } /* TRAIN */
2483 
2484 
2485 
2486 
2487 
2488 
2489 /* Perform forward propagation once for each set of weights in the
2490  * testing vectors, computing errors. Do not change any weights.
2491  */
2492 void UsfCasCor::TEST_EPOCH (double test_threshold,
2493  RunLog& log
2494  )
2495 {
2496  int i;
2497 
2498  /* Globals must be saved from the last training phase. If they are not */
2499  /* saved then the next unit will be training to correlate with the test */
2500  /* set error. */
2501  Boolean old_UC = UseCache; /* temporarily turn off cache */
2502  float old_ST = ScoreThreshold; /* save global */
2503  float old_TE = TrueError; /* save global */
2504  float *old_SE = SumErrors; /* save global */
2505  float old_SSE = SumSqError; /* save global */
2506 
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;
2509 
2510  ScoreThreshold = (float)test_threshold;
2511  UseCache = false;
2512 
2513  Values = ExtraValues;
2514  Errors = ExtraErrors;
2515  /* If no separate test inputs, use training inputs. */
2516  //if (NTestPatterns == 0)
2517  //{
2518  // TestInputs = TrainingInputs;
2519  // TestOutputs = TrainingOutputs;
2520  // NTestPatterns = NTrainingPatterns;
2521  //}
2522 
2523  /* Zero some accumulators. */
2524  ErrorBits = 0;
2525  ErrorMisclassifications=0;
2526  TrueError = 0.0;
2527  SumErrors = DummySumErrors;
2528  SumSqError = 0.0;
2529 
2530  /* Now run all test patterns and report the results. */
2531  for (i = 0; i < NTrainingPatterns; ++i)
2532  {
2533  Goal = TrainingOutputs[i];
2534  FULL_FORWARD_PASS (TrainingInputs[i]);
2535  COMPUTE_ERRORS (Goal, false, true, -1);
2536  }
2537 
2538  if (ErrorMeasure == INDEX)
2539  ErrorIndex = ERROR_INDEX (TestStdDev, NtestOutputValues);
2540 
2541  log.Level (10) << "TEST_EPOCH test :" << PRINT_SUMMARY (NTrainingPatterns) << endl;
2542 
2543  /* restore globals */
2544  UseCache = old_UC;
2545  ScoreThreshold = old_ST;
2546  TrueError = old_TE;
2547  SumErrors = old_SE;
2548  SumSqError = old_SSE;
2549 } /* TEST_EPOCH */
2550 
2551 
2552 
2553 
2554 /* print out the things interesting after a pass.
2555  */
2556 void UsfCasCor::OUT_PASS_OUTPUT ()
2557 {
2558  int i;
2559 
2560  fprintf (stderr," Outputs: ");
2561  for (i = 0; i < Noutputs; i++)
2562  fprintf(stderr,"%6.4f ", Outputs[i]);
2563 
2564  fprintf (stderr,"\n Errors: ");
2565  for (i = 0; i < Noutputs; i++)
2566  fprintf(stderr,"%6.4f ", Errors[i]);
2567 
2568  fprintf(stderr,"\n Values: ");
2569  for(i=0;i<Nunits;i++)
2570  fprintf(stderr,"%6.4f ", Values[i]);
2571 
2572  fprintf(stderr,"\n\n");
2573 } /* OUT_PASS_OUTPUT */
2574 
2575 
2576 
2577 /* Print the summary statistics based on the value of ErrorMeasure.
2578  */
2579 KKStr UsfCasCor::PRINT_SUMMARY (int n)
2580 {
2581  KKStr result (20);
2582  switch (ErrorMeasure)
2583  {
2584  case BITS:
2585  result << "errbits " << ErrorBits << "\t" << "error " << TrueError;
2586  break;
2587 
2588  case INDEX:
2589  result << " ErrorIndex " << ErrorIndex << ", TrueError " << TrueError;
2590  break;
2591  }
2592 
2593  double acc = 100.0 - (100.0 * ErrorMisclassifications) / n;
2594  result << " accuracy "
2595  << StrFormatDouble (acc, "##0.00") << "% "
2596  << ErrorMisclassifications << "/" << n;
2597  return result;
2598 } /* PRINT_SUMMARY */
2599 
2600 
2601 
2602 
2603 /* Set up the network for a learning problem. Clean up all the data
2604  * structures. Initialize the output weights to random values controlled by
2605  * WeightRange.
2606  */
2607 void UsfCasCor::initialize_network ()
2608 {
2609  int i,j;
2610 /**********/
2611 
2612  /* Set up the AllConnections vector. */
2613  for (i = 0; i < MaxUnits; i++)
2614  AllConnections[i] = i;
2615 
2616  /* Initialize the active unit data structures. */
2617  for (i = 0; i < MaxUnits; i++)
2618  {
2619  ExtraValues [i] = 0.0;
2620  Nconnections [i] = 0;
2621  Connections [i] = NULL;
2622  Weights [i] = NULL;
2623  }
2624 
2625  /* Initialize the per-output data structures. */
2626  for (i = 0; i < Noutputs; i++)
2627  {
2628  Outputs[i] = 0.0;
2629  ExtraErrors[i] = 0.0;
2630  for(j=0; j<MaxUnits; j++)
2631  {
2632  OutputWeights [i][j] = 0.0;
2633  OutputDeltas [i][j] = 0.0;
2634  OutputSlopes [i][j] = 0.0;
2635  OutputPrevSlopes [i][j] = 0.0;
2636  }
2637  /* Set up initial random weights for the input-to-output connections. */
2638  for (j = 0; j < (Ninputs + 1); ++j)
2639  OutputWeights[i][j] = random_weight();
2640  }
2641 
2642  /* Initialize the caches if they are in use. */
2643  if (UseCache)
2644  {
2645  for (j = 0; j < MaxCases; ++j)
2646  {
2647  for (i = 0; i < MaxUnits; ++i)
2648  ValuesCache[j][i] = 0.0;
2649 
2650  for (i = 0; i < Noutputs; ++i)
2651  ErrorsCache[j][i] = 0.0;
2652  }
2653  }
2654 
2655  /* Candidate units get initialized in a separate routine. */
2656  INIT_CANDIDATES ();
2657 
2658  ExtraValues[0] = 1.0; /* bias unit */
2659  Epoch = 0;
2660  Nunits = Ninputs + 1;
2661  ErrorBits = 0;
2662  TrueError = 0.0;
2663 
2664  for (i = 0; i < Noutputs; ++i)
2665  {
2666  SumErrors[i] = 0.0;
2667  DummySumErrors[i] = 0.0;
2668  }
2669 
2670  SumSqError = 0.0;
2671  BestCandidateScore = 0.0;
2672  BestCandidate = 0;
2673 #ifdef CONNX
2674  conx = 0l;
2675 #endif
2676 
2677  if(ErrorMeasure == INDEX){
2678  /* ErrorIndex initialization */
2679  ErrorIndex = 0.0;
2680  TrainingStdDev = STANDARD_DEV(TrainingOutputs, NTrainingPatterns,
2681  NtrainingOutputValues);
2682  if(NTestPatterns)
2683  TestStdDev = STANDARD_DEV(TestOutputs, NTestPatterns,
2684  NtestOutputValues);
2685  }
2686 } /* initialize_network */
2687 
2688 
2689 
2690 
2691 /* Calculate the standard deviation of an entire output set.
2692  */
2693 float UsfCasCor::STANDARD_DEV (float** outputs,
2694  int npatterns,
2695  int nvalues
2696  )
2697 {
2698  int i,j;
2699  float sum_o_sqs = 0.0;
2700  float sum = 0.0;
2701  float cur = 0.0;
2702  float fnum = (float)nvalues;
2703 /**************/
2704 
2705  for(i=0;i<npatterns;i++)
2706  for(j=0;j<Noutputs;j++){
2707  cur = outputs[i][j];
2708  sum += cur;
2709  sum_o_sqs += cur * cur;
2710  }
2711 
2712  return (sqrt((fnum * sum_o_sqs - sum * sum)/
2713  (fnum * (fnum - 1.0f))));
2714 } /* STANDARD_DEV */
2715 
2716 
2717 
2718 
2719 /* ErrorIndex is the rms TrueError normalized by the standard deviation of the
2720  * goal set.
2721  */
2722 float UsfCasCor::ERROR_INDEX (double std_dev,
2723  int num
2724  )
2725 {
2726  return (sqrt( TrueError / (float)num) / (float)std_dev);
2727 }
2728 
2729 
2730 
2731 
2732 //******************************************************************************
2733 // Utility Functions. *
2734 //******************************************************************************
2735 
2736 
2738 {
2739  return osGetProcessId ();
2740 }
2741 
2742 
2743 
2744 
2745 //******************************************************************************
2746 // globals.c *
2747 //******************************************************************************
2748 
2749 
2750 /* Initialize all globals that are not problem dependent. Put this function
2751  * in a separate file to make changing parameters less painful.
2752  */
2753 void UsfCasCor::INITIALIZE_GLOBALS ()
2754 {
2755  OutputShrinkFactor=OutputMu / (1.0f + OutputMu);
2756  InputShrinkFactor=InputMu / (1.0f + InputMu);
2757  //signal(SIGINT, TRAP_CONTROL_C); /* initialize interrupt handler */
2758  //InterruptPending = FALSE;
2759 }
2760 
2761 
2762 
2763 
2764 
2765 
2766 //******************************************************************
2767 //* load_data.c *
2768 //******************************************************************
2769 
2770 
2771 void UsfCasCor::load_data (FeatureVectorListPtr trainExamples,
2772  RunLog& log
2773  )
2774 {
2775  _load_training_data (trainExamples);
2776 
2777  log.Level (10) << "UsfCasCor::load_data Read in [" << NTrainingPatterns << "] training patterns of dimension[" << Ninputs << "]." << endl;
2778 
2779  return;
2780 } /* load_data */
2781 
2782 
2783 
2784 /*****************************************************************
2785  private functions
2786 ******************************************************************/
2787 void UsfCasCor::_load_training_data (FeatureVectorListPtr trainExamples)
2788 {
2789  for (kkint32 i = 0; i < NTrainingPatterns; ++i)
2790  _load_training_example (trainExamples->IdxToPtr (i), i);
2791 } /* _load_training_data */
2792 
2793 
2794 
2795 
2796 /* Build the next NTrainingPattern example from 'example'. */
2797 void UsfCasCor::_load_training_example (FeatureVectorPtr example,
2798  int i
2799  )
2800 {
2801  if (Ninputs != selectedFeatures->NumSelFeatures ())
2802  {
2803  cerr << endl
2804  << "UsfCasCor::_load_training_example ***ERROR*** Ninputs[" << Ninputs
2805  << "] != selectedFeatures->NumSelFeatures ()[" << selectedFeatures->NumSelFeatures () << "]" << endl
2806  << endl;
2807  }
2808 
2809  kkint32 j = 0;
2810 
2811  const float* featureData = example->FeatureData ();
2812  const kkuint16* featureNums = selectedFeatures->FeatureNums ();
2813 
2814  // at this point nInputs should equal selecedFeatures->NumSelFeatures ()
2815  for (j = 0; j < Ninputs; ++j)
2816  TrainingInputs[i][j] = featureData[featureNums[j]];
2817 
2818  kkint32 k = classes->PtrToIdx (example->MLClass ());
2819 
2820  for (j = 0; j < Noutputs; j++)
2821  {
2822  if (j == k)
2823  TrainingOutputs[i][j] = SigmoidMax;
2824  else
2825  TrainingOutputs[i][j] = SigmoidMin;
2826  }
2827 } /* _load_training_example */
2828 
2829 
2830 
2831 
2832 /* Get the test data from a separate file. Open the file
2833  */
2834 void UsfCasCor::_load_test_data (FeatureVectorPtr example)
2835 {
2836  const float* featureData = example->FeatureData ();
2837  const kkuint16* featureNums = selectedFeatures->FeatureNums ();
2838 
2839  // at this point nInputs should equal selecedFeatures->NumSelFeatures ()
2840  for (int j = 0; j < Ninputs; ++j)
2841  TestInputs[0][j] = featureData[featureNums[j]];
2842 
2843  //int k = classes->PtrToIdx (example->MLClass ());
2844  int k = -1;
2845 
2846  for (int j = 0; j < Noutputs; j++)
2847  {
2848  if (j == k)
2849  TestOutputs[0][j] = SigmoidMax;
2850  else
2851  TestOutputs[0][j] = SigmoidMin;
2852  }
2853  return;
2854 
2855 } /* _load_test_data */
2856 
2857 
2858 
2859 
2860 void UsfCasCor::WriteXML (const KKStr& varName,
2861  ostream& o
2862  ) const
2863 {
2864  XmlTag startTag ("UsfCasCor", XmlTag::TagTypes::tagStart);
2865  if (!varName.Empty ())
2866  startTag.AddAtribute ("VarName", varName);
2867  startTag.WriteXML (o);
2868  o << endl;
2869 
2870  XmlElementInt32::WriteXML (number_of_classes, "number_of_classes", o);
2871  XmlElementMLClassNameList::WriteXML (*classes, "Classes", o);
2872  selectedFeatures->WriteXML ("SelectedFeatures", o);
2873 
2874  // kkint32 variables
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);
2903 
2904 
2905  // Float Variables
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);
2928 
2929  XmlElementKKStr::WriteXML (type_to_string (UnitType), "UnitType", o);
2930  XmlElementKKStr::WriteXML (type_to_string (OutputType), "OutputType", o);
2931 
2932  XmlElementBool::WriteXML (load_weights, "load_weights", o);
2933  XmlElementBool::WriteXML (UseCache, "UseCache", o);
2934  XmlElementBool::WriteXML (Graphics, "Graphics", o);
2935  XmlElementBool::WriteXML (NonRandomSeed, "NonRandomSeed", o);
2936  XmlElementBool::WriteXML (Test, "Test", o);
2937  XmlElementBool::WriteXML (SinglePass, "SinglePass", o);
2938  XmlElementBool::WriteXML (SingleEpoch, "SingleEpoch", o);
2939  XmlElementBool::WriteXML (Step, "Step", o);
2940  XmlElementBool::WriteXML (InterruptPending, "InterruptPending", o);
2941 
2942  XmlElementArrayInt32::WriteXML (Ninputs, feature_type, "feature_type", o);
2943 
2944  XmlElementArrayFloat::WriteXML (Noutputs, SumErrors, "SumErrors", o);
2945  XmlElementArrayFloat::WriteXML (Noutputs, DummySumErrors, "DummySumErrors", o);
2946 
2947  if (AllConnections) XmlElementArrayInt32::WriteXML (MaxUnits, AllConnections, "AllConnections", o);
2948  if (Nconnections) XmlElementArrayInt32::WriteXML (MaxUnits, Nconnections, "Nconnections", o);
2949 
2950  {
2951  VectorKKStr connectionsVector;
2952  for (int x = 0; x < MaxUnits; ++x)
2953  {
2954  if (Connections[x] == NULL)
2955  connectionsVector.push_back ("NULL");
2956 
2957  else if (Connections[x] == AllConnections)
2958  connectionsVector.push_back ("AC");
2959 
2960  else
2961  connectionsVector.push_back ("Other");
2962  }
2963  connectionsVector.WriteXML ("Connections", o);
2964  }
2965 
2966  {
2967  XmlElementArrayFloat2DVarying::WriteXML (MaxUnits, Nconnections, Weights, "Weights", o);
2968  }
2969 
2970  if (ExtraValues) XmlElementArrayFloat::WriteXML (MaxUnits, ExtraValues, "ExtraValues", o);
2971  if (Outputs) XmlElementArrayFloat::WriteXML (Noutputs, Outputs, "Outputs", o);
2972 
2973  if (OutputWeights)
2974  XmlElementArrayFloat2D::WriteXML (Noutputs, MaxUnits, OutputWeights, "OutputWeights", o);
2975 
2976  if (ExtraErrors) XmlElementArrayFloat::WriteXML (Noutputs, ExtraErrors, "ExtraErrors", o);
2977 
2978  XmlTag endTag ("UsfCasCor", XmlTag::TagTypes::tagEnd);
2979  endTag.WriteXML (o);
2980  o << endl;
2981 } /* WriteXML */
2982 
2983 
2984 
2985 
2986 void UsfCasCor::ReadXML (XmlStream& s,
2987  XmlTagConstPtr tag,
2988  VolConstBool& cancelFlag,
2989  RunLog& log
2990  )
2991 {
2992  if (Weights)
2993  Delete2DArray (Weights, MaxUnits);
2994 
2995  if (OutputWeights)
2996  Delete2DArray (OutputWeights, Noutputs);
2997 
2998  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
2999  while (t && (!cancelFlag))
3000  {
3001  bool tokenProcessed = false;
3003  {
3004  XmlElementPtr e = dynamic_cast<XmlElementPtr> (t);
3005  tokenProcessed = true;
3006 
3007  const KKStr& varName = e->VarName ();
3008 
3009  kkint32 valueInt32 = e->ToInt32 ();
3010  float valueFloat = e->ToFloat ();
3011 
3012  if (varName.EqualIgnoreCase ("number_of_classes"))
3013  {
3014  number_of_classes = valueInt32;
3015  }
3016 
3017  else if (varName.EqualIgnoreCase ("Classes") || (typeid (*e) == typeid (XmlElementMLClassNameList)))
3018  {
3019  delete classes;
3020  classes = dynamic_cast<XmlElementMLClassNameListPtr> (t)->TakeOwnership ();
3021  }
3022 
3023  else if (varName.EqualIgnoreCase ("SelectedFeatures"))
3024  {
3025  delete selectedFeatures;
3026  selectedFeatures = dynamic_cast<XmlElementFeatureNumListPtr> (t)->TakeOwnership ();
3027  }
3028 
3029  else if (varName.EqualIgnoreCase ("MaxUnits")) MaxUnits = valueInt32;
3030  else if (varName.EqualIgnoreCase ("Ninputs")) Ninputs = valueInt32;
3031  else if (varName.EqualIgnoreCase ("Noutputs")) Noutputs = valueInt32;
3032  else if (varName.EqualIgnoreCase ("Nunits")) Nunits = valueInt32;
3033  else if (varName.EqualIgnoreCase ("Ncandidates")) Ncandidates = valueInt32;
3034  else if (varName.EqualIgnoreCase ("MaxCases")) MaxCases = valueInt32;
3035  else if (varName.EqualIgnoreCase ("Ncases")) Ncases = valueInt32;
3036  else if (varName.EqualIgnoreCase ("FirstCase")) FirstCase = valueInt32;
3037  else if (varName.EqualIgnoreCase ("line_length")) line_length = valueInt32;
3038  else if (varName.EqualIgnoreCase ("the_random_seed")) the_random_seed = valueInt32;
3039  else if (varName.EqualIgnoreCase ("in_limit")) in_limit = valueInt32;
3040  else if (varName.EqualIgnoreCase ("out_limit")) out_limit = valueInt32;
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;
3044  else if (varName.EqualIgnoreCase ("my_mpi_rank")) my_mpi_rank = valueInt32;
3045  else if (varName.EqualIgnoreCase ("NTrainingPatterns")) NTrainingPatterns = valueInt32;
3046  else if (varName.EqualIgnoreCase ("NTestPatterns")) NTestPatterns = valueInt32;
3047  else if (varName.EqualIgnoreCase ("ErrorMisclassifications")) ErrorMisclassifications = valueInt32;
3048  else if (varName.EqualIgnoreCase ("OutputPatience")) OutputPatience = valueInt32;
3049  else if (varName.EqualIgnoreCase ("InputPatience")) InputPatience = valueInt32;
3050  else if (varName.EqualIgnoreCase ("ErrorBits")) ErrorBits = valueInt32;
3051  else if (varName.EqualIgnoreCase ("BestCandidate")) BestCandidate = valueInt32;
3052  else if (varName.EqualIgnoreCase ("Epoch")) Epoch = valueInt32;
3053  else if (varName.EqualIgnoreCase ("Trial")) Trial = 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;
3057  else if (varName.EqualIgnoreCase ("ErrorMeasure")) ErrorMeasure = valueInt32;
3058 
3059  else if (varName.EqualIgnoreCase ("SigmoidMax")) SigmoidMax = valueFloat;
3060  else if (varName.EqualIgnoreCase ("WeightRange")) WeightRange = valueFloat;
3061  else if (varName.EqualIgnoreCase ("SigmoidPrimeOffset")) SigmoidPrimeOffset = valueFloat;
3062  else if (varName.EqualIgnoreCase ("WeightMultiplier")) WeightMultiplier = valueFloat;
3063  else if (varName.EqualIgnoreCase ("OutputMu")) OutputMu = valueFloat;
3064  else if (varName.EqualIgnoreCase ("OutputShrinkFactor")) OutputShrinkFactor = valueFloat;
3065  else if (varName.EqualIgnoreCase ("OutputEpsilon")) OutputEpsilon = valueFloat;
3066  else if (varName.EqualIgnoreCase ("OutputDecay")) OutputDecay = valueFloat;
3067  else if (varName.EqualIgnoreCase ("OutputChangeThreshold")) OutputChangeThreshold = valueFloat;
3068  else if (varName.EqualIgnoreCase ("InputMu")) InputMu = valueFloat;
3069  else if (varName.EqualIgnoreCase ("InputShrinkFactor")) InputShrinkFactor = valueFloat;
3070  else if (varName.EqualIgnoreCase ("InputEpsilon")) InputEpsilon = valueFloat;
3071  else if (varName.EqualIgnoreCase ("InputDecay")) InputDecay = valueFloat;
3072  else if (varName.EqualIgnoreCase ("InputChangeThreshold")) InputChangeThreshold = valueFloat;
3073  else if (varName.EqualIgnoreCase ("TrueError")) TrueError = valueFloat;
3074  else if (varName.EqualIgnoreCase ("ScoreThreshold")) ScoreThreshold = valueFloat;
3075  else if (varName.EqualIgnoreCase ("SumSqError")) SumSqError = valueFloat;
3076  else if (varName.EqualIgnoreCase ("BestCandidateScore")) BestCandidateScore = valueFloat;
3077  else if (varName.EqualIgnoreCase ("TrainingStdDev")) TrainingStdDev = valueFloat;
3078  else if (varName.EqualIgnoreCase ("TestStdDev")) TestStdDev = valueFloat;
3079  else if (varName.EqualIgnoreCase ("ErrorIndex")) ErrorIndex = valueFloat;
3080  else if (varName.EqualIgnoreCase ("ErrorIndexThreshold")) ErrorIndexThreshold = valueFloat;
3081 
3082  else if (varName.EqualIgnoreCase ("UnitType"))
3083  {
3084  UnitType = string_to_type (e->ToKKStr ());
3085  }
3086 
3087  else if (varName.EqualIgnoreCase ("OutputType"))
3088  {
3089  OutputType = string_to_type (e->ToKKStr ());
3090  }
3091 
3092  else if (typeid (*t) == typeid(XmlElementBool))
3093  {
3094  XmlElementBoolPtr b = dynamic_cast<XmlElementBoolPtr> (t);
3095  if (b)
3096  {
3097  bool valueBool = b->Value ();
3098  if (varName.EqualIgnoreCase ("load_weights")) load_weights = valueBool;
3099  else if (varName.EqualIgnoreCase ("UseCache")) UseCache = valueBool;
3100  else if (varName.EqualIgnoreCase ("Graphics")) Graphics = valueBool;
3101  else if (varName.EqualIgnoreCase ("NonRandomSeed")) NonRandomSeed = valueBool;
3102  else if (varName.EqualIgnoreCase ("Test")) Test = valueBool;
3103  else if (varName.EqualIgnoreCase ("SinglePass")) SinglePass = valueBool;
3104  else if (varName.EqualIgnoreCase ("SingleEpoch")) SingleEpoch = valueBool;
3105  else if (varName.EqualIgnoreCase ("Step")) Step = valueBool;
3106  else if (varName.EqualIgnoreCase ("InterruptPending")) InterruptPending = valueBool;
3107  else
3108  tokenProcessed = false;
3109  }
3110  }
3111 
3112  else if (varName.EqualIgnoreCase ("feature_type"))
3113  {
3114  delete feature_type;
3115  feature_type = dynamic_cast<XmlElementArrayInt32Ptr> (t)->TakeOwnership ();
3116  }
3117 
3118  else if ((varName.EqualIgnoreCase ("SumErrors")) && (typeid (*t) == typeid (XmlElementArrayFloat)))
3119  {
3120  delete SumErrors;
3121  SumErrors = dynamic_cast<XmlElementArrayFloatPtr> (t)->TakeOwnership ();
3122  }
3123 
3124  else if (varName.EqualIgnoreCase ("DummySumErrors") && (typeid (*t) == typeid (XmlElementArrayFloat)))
3125  {
3126  delete DummySumErrors;
3127  DummySumErrors = dynamic_cast<XmlElementArrayFloatPtr> (t)->TakeOwnership ();
3128  }
3129 
3130  else if (varName.EqualIgnoreCase ("AllConnections") && (typeid (*t) == typeid (XmlElementArrayInt32)))
3131  {
3132  delete AllConnections;
3133  AllConnections = dynamic_cast<XmlElementArrayInt32Ptr> (t)->TakeOwnership ();
3134  }
3135 
3136  else if (varName.EqualIgnoreCase ("Nconnections") && (typeid (*t) == typeid (XmlElementArrayInt32)))
3137  {
3138  delete Nconnections;
3139  Nconnections = dynamic_cast<XmlElementArrayInt32Ptr> (t)->TakeOwnership ();
3140  }
3141 
3142  else if (varName.EqualIgnoreCase ("Connections") && (typeid (*t) == typeid (XmlElementVectorKKStr)))
3143  {
3144  VectorKKStr* connectionsStr = dynamic_cast<XmlElementVectorKKStrPtr> (t)->TakeOwnership ();
3145  if (connectionsStr)
3146  {
3147  kkuint32 count = connectionsStr->size ();
3148  delete Connections;
3149  Connections = new int*[MaxUnits];
3150 
3151  for (kkuint32 x = 0; x < count; ++x)
3152  {
3153  if ((*connectionsStr)[x] == "NULL")
3154  Connections[x] = NULL;
3155  else if ((*connectionsStr)[x] == "AC")
3156  Connections[x] = AllConnections;
3157  else
3158  Connections[x] = NULL;
3159  }
3160  }
3161  delete connectionsStr;
3162  connectionsStr = NULL;
3163  }
3164 
3165  else if (varName.EqualIgnoreCase ("Weights"))
3166  {
3167  if (typeid (*t) == typeid (XmlElementArrayFloat2DVarying))
3168  {
3169  XmlElementArrayFloat2DVaryingPtr w = dynamic_cast<XmlElementArrayFloat2DVaryingPtr> (t);
3170  if (w->Height () != MaxUnits)
3171  {
3172  log.Level (-1) << endl
3173  << "UsfCasCor::ReadXML ***ERROR*** Height[" << w->Height () << "] of Weights array does not MaxUnits[" << MaxUnits << "]." << endl
3174  << endl;
3175  }
3176  else
3177  {
3178  Weights = w->TakeOwnership ();
3179  }
3180  }
3181  }
3182 
3183  else if ((varName.EqualIgnoreCase ("ExtraValues")) && (typeid (*t) == typeid (XmlElementArrayFloat)))
3184  {
3185  delete ExtraValues;
3186  ExtraValues = dynamic_cast<XmlElementArrayFloatPtr> (t)->TakeOwnership ();
3187  }
3188 
3189  else if ((varName.EqualIgnoreCase ("Outputs")) && (typeid (*t) == typeid (XmlElementArrayFloat)))
3190  {
3191  delete Outputs;
3192  Outputs = dynamic_cast<XmlElementArrayFloatPtr> (t)->TakeOwnership ();
3193  }
3194 
3195  else if ((varName.EqualIgnoreCase ("ExtraErrors")) && (typeid (*t) == typeid (XmlElementArrayFloat)))
3196  {
3197  delete ExtraErrors;
3198  ExtraErrors = dynamic_cast<XmlElementArrayFloatPtr> (t)->TakeOwnership ();
3199  }
3200 
3201  else if ((varName.EqualIgnoreCase ("OutputWeights")) && (typeid (*t) == typeid (XmlElementArrayFloat2D)))
3202  {
3203  XmlElementArrayFloat2DPtr array2D = dynamic_cast<XmlElementArrayFloat2DPtr> (t);
3204  kkuint32 owHeight = array2D->Height ();
3205  kkuint32 owWidth = array2D->Width ();
3206  if ((owHeight != Noutputs) || (owWidth != MaxUnits))
3207  {
3208  log.Level (-1) << endl
3209  << "UsfCasCor::ReadXML ***ERROR*** OutputWeights read Dimensions[" << owHeight << ", " << owWidth << "] not the expected [" << Noutputs << ", " << MaxUnits << "]."
3210  << endl;
3211  }
3212  else
3213  {
3214  delete OutputWeights;
3215  OutputWeights = array2D->TakeOwnership ();
3216  }
3217  }
3218  }
3219 
3220  delete t;
3221  t = s.GetNextToken (cancelFlag, log);
3222  }
3223 
3224  delete t;
3225  t = NULL;
3226 
3227  // We are done reading the XML data; now we must build our dynamic memory structures.
3228  if (!cancelFlag)
3229  {
3230  if (NTestPatterns > 0)
3231  {
3232  TestInputs = new float*[NTestPatterns];
3233  TestOutputs = new float*[NTestPatterns];
3234  for (int z = 0; z < NTestPatterns; ++z)
3235  {
3236  TestInputs[z] = new float[Ninputs];
3237  TestOutputs[z] = new float[Noutputs];
3238  }
3239  }
3240  else
3241  {
3242  TestInputs = TrainingInputs;
3243  TestOutputs = TrainingOutputs;
3244  }
3245 
3246  if (!ExtraValues)
3247  ExtraValues = new float[MaxUnits];
3248 
3249  if (!Values)
3250  Values = ExtraValues;
3251 
3252 
3253  if (UseCache)
3254  {
3255  /* Odd error check. If usecache was specified in file, but not on
3256  * command line, then Cache was not allocated. We look for NULL
3257  * value and allocate storage here.
3258  */
3259  if (ValuesCache == NULL )
3260  {
3261  ValuesCache = new float*[MaxCases]; //(float **)CALLOC(MaxCases, sizeof(float *));
3262  ErrorsCache = new float*[MaxCases];
3263 
3264  for (kkint32 i = 0; i < MaxCases; i++)
3265  {
3266  ValuesCache[i] = new float[MaxUnits];
3267  ErrorsCache[i] = new float[Noutputs];
3268  }
3269  }
3270 
3271  for (kkint32 i = 0; i < NTrainingPatterns; i++)
3272  {
3273  Values = ValuesCache[i];
3274 
3275  /* Unit values must be calculated in order because the activations */
3276  /* cascade down through the hidden layers */
3277  for (kkint32 j = 1 + Ninputs; j < Nunits; j++)
3278  COMPUTE_UNIT_VALUE(j);
3279  }
3280  }
3281  }
3282 
3283 } /* ReadXML */
3284 
3285 
3286 
3287 XmlFactoryMacro(UsfCasCor)
__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
void PushOnBack(FeatureVectorPtr image)
Overloading the PushOnBack function in KKQueue so we can monitor the Version and Sort Order...
kkint32 GetProcessId()
Definition: UsfCasCor.cpp:2737
MLClass * MLClassPtr
Definition: MLClass.h:46
bool EqualIgnoreCase(const char *s2) const
Definition: KKStr.cpp:1257
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 LRand48()
A implementations of the Unix version of rand48 returning a 32 bit integer.
Definition: KKBaseTypes.cpp:36
const float * FeatureData() const
Returns as a pointer to the feature data itself.
virtual KKStr ToKKStr() const
Definition: XmlStream.h:314
float FeatureData(kkint32 featureNum) const
bool Value() const
Definition: XmlStream.cpp:1028
Keeps track of selected features.
FeatureNumList(FileDescPtr _fileDesc)
virtual float ToFloat() const
Definition: XmlStream.h:316
virtual void PushOnBack(ClassProbPtr cp)
Definition: ClassProb.cpp:212
XmlElementBool * XmlElementBoolPtr
Definition: XmlStream.h:524
MLClassListPtr ExtractListOfClasses() const
#define REAL
Definition: UsfCasCor.h:239
#define GAUSSIAN
Definition: UsfCasCor.h:220
unsigned __int16 kkuint16
16 bit unsigned integer.
Definition: KKBaseTypes.h:86
static void WriteXML(const MLClassList &mlClassList, const KKStr &varName, std::ostream &o)
Definition: MLClass.cpp:1499
const FileDescPtr FileDesc() const
#define BITS
Definition: UsfCasCor.h:230
kkint32 STRICMP(const char *left, const char *right)
Definition: KKStr.cpp:92
#define VARSIGMOID
Definition: UsfCasCor.h:223
static void WriteXML(const bool b, const KKStr &varName, std::ostream &o)
Definition: XmlStream.cpp:1035
bool EqualIgnoreCase(const KKStr &s2) const
Definition: KKStr.cpp:1250
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
FeatureNumList(const FeatureNumList &featureNumList)
Copy constructor.
__int64 kkint64
Definition: KKBaseTypes.h:90
FeatureVectorList(FileDescPtr _fileDesc, bool _owner)
Will create a new empty list of FeatureVector&#39;s.
#define LINEAR
Definition: UsfCasCor.h:221
Container class for FeatureVector derived objects.
virtual kkint32 ToInt32() const
Definition: XmlStream.h:317
#define ASYMSIGMOID
Definition: UsfCasCor.h:222
KKTHread * KKTHreadPtr
#define EOL
Definition: UsfCasCor.cpp:1116
XmlElement * XmlElementPtr
Definition: XmlStream.h:21
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Definition: XmlStream.cpp:602
kkint32 NumOfFeatures() const
Used to record probability for a specified class; and a list of classes.
Definition: ClassProb.h:25
kkint32 osGetProcessId()
bool Empty() const
Definition: KKStr.h:241
#define INDEX
Definition: UsfCasCor.h:231
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
std::vector< float > VectorFloat
Definition: KKBaseTypes.h:149
MLClassListPtr TakeOwnership()
Definition: MLClass.cpp:1491
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
Definition: XmlStream.h:46
static void WriteXML(kkuint32 height, const kkint32 *widths, float **const mat, const KKStr &varName, std::ostream &o)
Definition: XmlStream.cpp:1374
ClassProbList(bool owner)
Definition: ClassProb.cpp:48
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
#define TIMEOUT
Definition: UsfCasCor.h:227
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: KKStr.cpp:5319
virtual const KKStr & VarName() const
Definition: XmlStream.cpp:794
float TotalOfFeatureData() const
Returns the total of all Feature Attributes for this feature vector.
#define WIN
Definition: UsfCasCor.h:225
#define LOSE
Definition: UsfCasCor.h:228
kkint32 NumSelFeatures() const
KKStr StrFormatDouble(double val, const char *mask)
Definition: KKStr.cpp:4819
#define STAGNANT
Definition: UsfCasCor.h:226
void WriteXML(std::ostream &o)
Definition: XmlStream.cpp:723
FeatureNumListConst * FeatureNumListConstPtr
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)
void SRand48(kkint64 _seed)
Seeds the Lrand48 functions with the parameters passed to it.
Definition: KKBaseTypes.cpp:49
virtual TokenTypes TokenType()=0
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
ClassProb(MLClassPtr _classLabel, double _probability, float _votes)
Definition: ClassProb.cpp:22
Maintains a list of MLClass instances.
Definition: MLClass.h:233
const char * _(const char *str)
Definition: UsfCasCor.cpp:223
Will only write the ClassName rather than complete MLClass instances.
Definition: MLClass.h:580
#define INT_MAX
Adapted by Kurt Kramer be a &#39;class&#39; definition so as to make it more usable in th ePices software wor...
Definition: UsfCasCor.h:186
#define XmlFactoryMacro(NameOfClass)
Definition: XmlStream.h:688
void WriteXML(const KKStr &varName, std::ostream &o) const
const kkuint16 * FeatureNums() const
#define SIGMOID
Definition: UsfCasCor.h:219
bool Boolean
Definition: UsfCasCor.h:190
XmlElementMLClassNameList * XmlElementMLClassNameListPtr
Definition: MLClass.h:603
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163