Пример #1
0
void SOM::train( QMap<QString, QVariant> somParameters, QVector<FeaturePtr> features, bool skipInit_debugOnly )
{

    if( skipInit_debugOnly == false )
        initializeTraining( somParameters, features );

    do
    {
       // Shuffle the list of features to remove bias
        std::random_shuffle( features.begin(), features.end() );

        // Update the SOM with each feature from the globalFeatures list.
        // @todo: consider random sampling for the training if the number of features is high
        foreach( FeaturePtr feature, features )
            update( feature );

    } while( nextEpoch() != false );

}
Пример #2
0
int main( int argc, char  **argv ) {

     char *progname = argv[0];
     FILE *in ,  *out;
     char opt;
     int i;
     double startTime;

     C = 0;
     kernelType =  -1;
     degree = 0;
     sigmaSqr = 0;
     binaryFeature = 0;

     /* Check command line options */
     optarg = NULL;
     while (((opt = getopt(argc, argv, OPTSTRING)) != -1)) {
            switch(opt)  {
            case 't':
            if ( strcmp(optarg, "0") == 0 ) kernelType = 0;
            else if ( strcmp(optarg, "1")  == 0 ) kernelType = 1;
            else if ( strcmp(optarg, "2")  == 0 ) kernelType = 2;
            else {
                   fprintf( stderr, "kernel type is either 0,1 or 2\n");
                   exit(1);
            }
            break;
            case 'c':
            if( sscanf(optarg, "%f", &C) == 0 ) {
                fprintf(stderr,"Expect  a positive number for C.\n");
                exit(1);
            } else
                C = atof(optarg);
              if( C  <= 0 ) {
                 fprintf( stderr, "C has to be > 0\n");
                 exit(1);
              }
              break;
              case 'd':
                if( sscanf(optarg, "%d", &degree) == 0 ) {
                   fprintf( stderr, "Expect degree to be a positive integer.\n");
                   exit(1);
                } else degree = atoi(optarg);
                if ( degree <= 0 ) {
                  fprintf( stderr, "degree has to be a positive integer.\n");
                  exit(1);
                }
                break;
                case 'v':
                if( sscanf(optarg, "%f", &sigmaSqr) == 0 ) {
                    fprintf(stderr,"Expect  a positive number for variance.\n");
                    exit(1);
                } else sigmaSqr = atof(optarg);
                  if( sigmaSqr  <= 0 ) {
                     fprintf( stderr, "variance has to be > 0\n");
                     exit(1);
                  }

                  rbfConstant = 1/(2*sigmaSqr);
                  break;
                  case 'b':
                  if( sscanf(optarg, "%d", &binaryFeature) == 0 ) {
                     fprintf( stderr, "binaryFeature option is either 0 or 1.\n");
                     exit(1);
                  } else binaryFeature = atoi(optarg);
                if ( binaryFeature != 0 && binaryFeature != 1 ) {
                    fprintf( stderr, "binaryFeature option is either 0 or 1.\n");
                    exit(1);
                }
                break;
                case 'h':
                useMsg( progname );
                exit(1);
                break;
                default:
                  useMsg( progname );
                  exit(1);
                  break;
               }
          }

          /* Check all necessary parameters are in */

          if( kernelType ==  -1 ) {
               fprintf( stderr, "Kernel type has not been specified.\n");
               exit(2);
          } else if( kernelType == 1 && degree == 0 ) {
              fprintf( stderr, "Degree has not been specified.\n");
              exit(2);
          } else if( kernelType == 2 && sigmaSqr == 0 ){
                fprintf( stderr, "Variance has not been specified.\n");
                exit(2);
          } else if( C == 0 ) C = DEFAULT;

        /* Check training file and model file */
         printf("INPUT FILE %s\n", argv[argc-2]);
          if (( in = fopen( argv[argc-2], "r") ) == NULL ) {
               fprintf( stderr, "Can't open %s\n",  argv[argc-2] );
               exit(2);
          }

          if (( out = fopen( argv[argc-1], "w") ) == NULL ) {
              fprintf( stderr, "Can't open %s\n",  argv[argc-1] );
              exit(2);
          }

         printf("smo_learn is preparing to learn. . .\n");
         if( ! readFile( in ) ) {
             fprintf( stderr, "Error in initializing. Program exits.\n" );
             exit (1);
         } else fclose( in );

         if( !initializeTraining()) {
             fprintf( stderr, "Error in initializing data structure. Program exits.\n");
             exit(1);
         }

         printf("Start training . . .\n");
         startTime = clock()/CLOCKS_PER_SEC;
         startLearn();
         printf("Training is completed\n");

         /* Print training statistics */
         printf("CPU time is %f secs\n", clock()/CLOCKS_PER_SEC-startTime);
         printf("Writing training results . . .\n");
         writeModel( out );
         fclose( out );
         printf("Finish writing training results.\n");
         printf("no of iteration is %f\n",  iteration);
         printf("threshold b is %f\n", getb());
         if ( kernelType == 0 )
                printf("norm of weight vector is %f\n",  calculateNorm());
         printf("no. of unBound multipliers is %d\n",  unBoundSv );
         printf("no. of bounded multipliers is %d\n",  boundSv );

         /* Free memory */
         free( target );
         free( lambda );
         free( nonZeroFeature );
         free( error );
         free( nonBound );
         free( weight );
         free( unBoundIndex );
         free( nonZeroLambda );
         for( i = 0; i <= numExample; i++ ) {
            free( example[i] );
         }
         free( example );
         free( errorCache );

         return 0;
}