//******************************************************************* // WinMain - Neural main // // parameters: // hInstance - The instance of this instance of this // application. // hPrevInstance - The instance of the previous instance // of this application. This will be 0 // if this is the first instance. // lpszCmdLine - A long pointer to the command line that // started this application. // cmdShow - Indicates how the window is to be shown // initially. ie. SW_SHOWNORMAL, SW_HIDE, // SW_MIMIMIZE. // // returns: // wParam from last message. // //******************************************************************* int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int cmdShow) { /* Declarations of local variables */ int control_file_number = -1 ; // Stack pointer for control files FILE *control_files[MAX_CONTROL_FILES] ; // This is the stack char *control_line ; // User's commands here char *command, *rest ; // Pointers to its command and parameter parts int n_command, n_rest ; // Lengths of those parts int net_model = -1 ; // Network model (see NETMOD_? in CONST.H) int out_model = -1 ; // Output model (see OUTMOD_? in CONST.H) int n_inputs = -1 ; // Number of input neurons int n_outputs = -1 ; // Number of output neurons int n_hidden1 = -1 ; // Number of hidden layer one neurons int n_hidden2 = -1 ; // Ditto layer 2 (0 if just one hidden layer) TrainingSet *tset = NULL ; // Training set here Network *network = NULL ; // Network here struct LearnParams learn_params ; // General learning parameters struct AnnealParams anneal_params ; // Simulated annealing parameters struct GenInitParams geninit_params ; // Genetic initialization parameters struct KohParams koh_params ; // Kohonen parameters int classif_output = -1 ; // Current class (0=reject) for classif training char out_file[80] = "" ; // File for EXECUTE output float threshold ; // CLASSIFY confusion reject cutoff char resp_file[80]=""; // file for initializing output neuron's name char train_file[80]=""; /* Miscellaneous variables */ int i, n, m ; float p ; char *msg ; FILE *fp ; unsigned long me,mc; char *fname; char *control; #if VERSION_16_BIT if (sizeof(int) > 2) { printf ( "\nRecompile with VERSION_16_BIT set to 0 in CONST.H" ) ; exit ( 1 ) ; } #else if (sizeof(int) < 4) { printf ( "\nRecompile with VERSION_16_BIT set to 1 in CONST.H" ) ; exit ( 1 ) ; } #endif printf ( "\nNEURAL SYSTEM - Program to train and test neural networks" ) ; if (argc>1) { strcpy(fname,argv[1]); } /* Process command line parameters */ mem_name[0] = 0 ; // Default is no memory allocation file /* if (strlen ( mem_name )) { strcat ( mem_name , ":mem.log" ) ; fp = fopen ( mem_name , "wt" ) ; if (fp == NULL) { printf ( "\nCannot open debugging file %s", mem_name ) ; exit ( 1 ) ; } fclose ( fp ) ; mem_log = 1 ; } else mem_log = 0 ; */ mem_log = 0 ; mem_used = 0 ; /* Initialize defaults */ learn_params.init = -1 ; learn_params.quit_err = 0.0 ; learn_params.retries = 32767 ; anneal_params.temps0 = 3 ; anneal_params.temps = 4 ; anneal_params.iters0 = 50 ; anneal_params.iters = 20 ; anneal_params.setback0 = 50 ; anneal_params.setback = 20 ; anneal_params.start0 = 3.0 ; anneal_params.start = 4.0 ; anneal_params.stop0 = 1.0 ; anneal_params.stop = 0.02 ; geninit_params.pool = 50 ; geninit_params.gens = 3 ; geninit_params.climb = 0 ; geninit_params.overinit = 1.5 ; geninit_params.pcross = 0.8 ; geninit_params.pmutate = 0.0001 ; koh_params.normalization = 0 ; // 0=multiplicative, 1=Z koh_params.learn_method = 1 ; // 0=additive, 1=subtractive koh_params.rate = 0.4 ; // learning rate koh_params.reduction = 0.99 ; // learning rate reduction learn_params.ap = &anneal_params ; learn_params.gp = &geninit_params ; learn_params.kp = &koh_params ; act_func_init () ; // Initialize interpolation table for activation function MEMTEXT ( "NEURAL: control_line, msg" ) ; if (((control_line = (char *) MALLOC ( CONTROL_LINE_LENGTH+1 )) == NULL) || ((msg = (char *) MALLOC ( CONTROL_LINE_LENGTH+1 )) == NULL)) { printf ( "\nInsufficient memory" ) ; exit ( 1 ) ; } /* Main loop processes all commands */ for (;;) { if (argv[1]) { strcpy(control_line,"CONTROL:"); strcat(control_line,fname); //printf("%s\n",control_line); argv[1]=NULL; } else get_control_line ( control_line , &control_file_number, control_files ) ; split_control_line ( control_line , &command , &n_command , &rest , &n_rest ) ; if (! n_command) { if (n_rest) { sprintf ( msg , "No colon after command: %s", rest ) ; error_message ( msg ) ; } continue ; } sprintf ( msg , "%s : %s", command, rest ) ; normal_message ( msg ) ; /* Act on the command */ if (! strcmp ( command , "QUIT" )) break ; if (! strcmp ( command , "CONTROL" )) { stack_control_file (rest, &control_file_number, control_files) ; continue ; } if (! strcmp ( command , "NETWORK MODEL" )) { // Multi layer network if (! strcmp ( rest , "LAYER" )) n = NETMOD_LAYER ; // Kohonen network else if (! strcmp ( rest , "KOHONEN" )) n = NETMOD_KOH ; // Hopfield network else if (! strcmp ( rest , "HOPFIELD" )) n = NETMOD_HOP ; // Bidirectionnal associative memory network else if (! strcmp ( rest , "BAM" )) n = NETMOD_BAM ; else { sprintf ( msg , "Illegal NETWORK MODEL: %s", rest ) ; error_message ( msg ) ; continue ; } if (net_model == n) continue ; if (ok_to_clear_weights( &network )) { net_model = n ; learn_params.init = -1 ; } else warning_message ( "Command aborted" ) ; continue ; } if (! strcmp ( command , "OUTPUT MODEL" )) { if (! strcmp ( rest , "CLASSIFY" )) n = OUTMOD_CLASSIFY ; else if (! strcmp ( rest , "AUTO" )) n = OUTMOD_AUTO ; else if (! strcmp ( rest , "GENERAL" )) n = OUTMOD_GENERAL ; else { sprintf ( msg , "Illegal OUTPUT MODEL: %s", rest ) ; error_message ( msg ) ; continue ; } if (out_model == n) continue ; if ((ok_to_clear_tset( &tset )) && (ok_to_clear_weights( &network))) out_model = n ; else warning_message ( "Command aborted" ) ; continue ; } if (! strcmp ( command , "N INPUTS" )) { m = sscanf ( rest , "%d" , &n ) ; if ((m <= 0) || (n <= 0) || (n > MAX_INPUTS)) { sprintf ( msg , "Illegal N INPUTS: %s", rest ) ; error_message ( msg ) ; continue ; } if (n_inputs == n) continue ; if ((ok_to_clear_tset( &tset)) && (ok_to_clear_weights(&network))) n_inputs = n ; else warning_message ( "Command aborted" ) ; continue ; } if (! strcmp ( command , "N OUTPUTS" )) { m = sscanf ( rest , "%d" , &n ) ; if ((m <= 0) || (n <= 0) || (n > MAX_OUTPUTS)) { sprintf ( msg , "Illegal N OUTPUTS: %s", rest ) ; error_message ( msg ) ; continue ; } if (n_outputs == n) continue ; if ((ok_to_clear_tset( &tset)) && (ok_to_clear_weights(&network))) n_outputs = n ; else warning_message ( "Command aborted" ) ; continue ; } if (! strcmp ( command , "N HIDDEN1" )) { m = sscanf ( rest , "%d" , &n ) ; if ((m <= 0) || (n < 0) || (n > MAX_HIDDEN)) { sprintf ( msg , "Illegal N HIDDEN1: %s", rest ) ; error_message ( msg ) ; continue ; } if (n_hidden1 == n) continue ; if (ok_to_clear_weights( &network )) n_hidden1 = n ; else warning_message ( "Command aborted" ) ; continue ; } if (! strcmp ( command , "N HIDDEN2" )) { m = sscanf ( rest , "%d" , &n ) ; if ((m <= 0) || (n < 0) || (n > MAX_HIDDEN)) { sprintf ( msg , "Illegal N HIDDEN2: %s", rest ) ; error_message ( msg ) ; continue ; } if (n && ! n_hidden1) { error_message ( "N HIDDEN2 must be 0 if N HIDDEN1 IS 0." ) ; continue ; } if (n_hidden2 == n) continue ; if (ok_to_clear_weights( &network )) n_hidden2 = n ; else warning_message ( "Command aborted" ) ; continue ; } if (! strcmp ( command , "TRAIN" )) { if ((out_model == OUTMOD_AUTO) && (n_outputs != n_inputs)) { warning_message ( "Setting N OUTPUTS = N INPUTS" ) ; n_outputs = n_inputs ; } if (out_model <= 0) error_message ( "TRAIN used before OUTPUT MODEL set." ) ; else if (n_inputs <= 0) error_message ( "TRAIN used before N INPUTS set." ) ; else if (n_outputs <= 0) error_message ( "TRAIN used before N OUTPUTS set." ) ; else if ((net_model == NETMOD_HOP) && (n_inputs != n_outputs)) error_message("HOPFIELD netowork requires INPUTS = OUTPUTS."); else if ((net_model == NETMOD_BAM) && (out_model != OUTMOD_GENERAL)) error_message("BAM network requires AUTO output mode."); else if ((net_model == NETMOD_HOP) && (out_model != OUTMOD_AUTO)) error_message("HOFIELD network requires AUTO output mode."); else if ((net_model != NETMOD_KOH) && (out_model == OUTMOD_CLASSIFY) && (classif_output < 0)) error_message( "CLASSIFY output mode but CLASSIFY OUTPUT not set."); else if ((net_model == NETMOD_KOH) && (out_model != OUTMOD_CLASSIFY)) error_message( "KOHONEN network requires CLASSIFY output mode."); else { if (tset == NULL) { MEMTEXT ( "NEURAL: new tset" ) ; tset = new TrainingSet ( out_model , n_inputs , n_outputs ) ; } tset->train ( rest , classif_output ) ; strcpy(train_file,rest); } continue ; } if (check_anneal ( command , rest , &anneal_params )) continue ; if (check_genetic ( command , rest , &geninit_params )) continue ; if (check_kohonen ( command , rest , &koh_params , &network )) continue ; if (check_learn_params ( command , rest , &learn_params , net_model )) continue ; if (! strcmp ( command , "LEARN" )) { if ((tset == NULL) || (tset->ntrain == 0)) { error_message ( "Cannot LEARN; No training set exists." ) ; continue ; } if ((net_model == NETMOD_KOH) && (out_model != OUTMOD_CLASSIFY)) { error_message( "KOHONEN network requires CLASSIFY output mode."); continue ; } if (learn_params.init < 0) { error_message( "Initialization method not set."); continue ; } if (network == NULL) { if (net_model == NETMOD_LAYER) { if (n_hidden1 < 0) { error_message ( "LEARN used before N HIDDEN1 set." ) ; continue ; } else if (n_hidden2 < 0) { error_message ( "LEARN used before N HIDDEN2 set." ) ; continue ; } else { MEMTEXT ( "NEURAL: new LayerNet" ) ; network = new LayerNet ( out_model , n_inputs , n_hidden1 , n_hidden2 , n_outputs , 1 , 1 ) ; } } else if (net_model == NETMOD_KOH) { MEMTEXT ( "NEURAL: new KohNet" ) ; network = new KohNet ( n_inputs , n_outputs , &koh_params , 1 , 1 ) ; } else if (net_model == NETMOD_HOP) { MEMTEXT ( "NEURAL: new HopNet" ); network = new HopNet (n_inputs,n_outputs, 1,1); } else if (net_model == NETMOD_BAM) { MEMTEXT ("NEURAL: new BamNet"); network = new LayerNet ( out_model , n_inputs , n_hidden1 , n_hidden2 , n_outputs , 1 , 1 ) ; } } if ((network == NULL) || (! network->ok)) { // Malloc failure? memory_message ( "to create network." ) ; if (network != NULL) { delete network ; network = NULL ; } continue ; } normal_message("Learning...\n"); network->learn ( tset , &learn_params ) ; normal_message("End of Learning\n"); if (network->neterr > 0.999999) { // Indicates massive failure MEMTEXT ( "NEURAL: learn failure delete network" ) ; delete network ; network = NULL ; } else { sprintf ( msg , "Final error = %.4lf%% of max possible", 100.0 * network->neterr ) ; normal_message ( msg ) ; } continue ; } if (! strcmp ( command , "SAVE WEIGHTS" )) { if (network == NULL) error_message ( "There are no learned weights to save." ) ; else wt_save ( network , net_model , 0 , rest ) ; continue ; } if (! strcmp ( command , "RESTORE WEIGHTS" )) { if (network != NULL) { MEMTEXT ( "NEURAL: delete network for restore" ) ; delete network ; network = NULL ; } network = wt_restore ( rest , &net_model ) ; if (network == NULL) continue ; if (tset != NULL) { if ((tset->nin != network->nin) || (tset->nout != network->nout) || (tset->outmod != network->outmod)) { error_message ( "Network conflicts with existing training set."); continue ; } } out_model = network->outmod ; n_inputs = network->nin ; n_outputs = network->nout ; if (net_model == NETMOD_LAYER) { n_hidden1 = ((LayerNet*) network)->nhid1 ; n_hidden2 = ((LayerNet*) network)->nhid2 ; } if (net_model == NETMOD_KOH) koh_params.normalization = ((KohNet *) network)->normalization ; learn_params.init = -1 ; continue ; } if (! strcmp ( command , "CLEAR TRAINING" )) { if (tset != NULL) { MEMTEXT ( "NEURAL: delete tset" ) ; delete tset ; tset = NULL ; } continue ; } if (! strcmp ( command , "CLEAR WEIGHTS" )) { if (network != NULL) { MEMTEXT ( "NEURAL: delete network" ) ; delete network ; network = NULL ; } continue ; } if (! strcmp ( command , "CLASSIFY OUTPUT" )) { if (net_model == NETMOD_KOH) { error_message ( "Cannot specify output for KOHONEN model." ) ; continue ; } if (n_outputs < 0) { error_message ( "CLASSIFY OUTPUT used before N OUTPUTS set." ) ; continue ; } if (out_model != OUTMOD_CLASSIFY) { error_message ( "CLASSIFY OUTPUT only valid when OUTPUT MODEL:CLASSIFY" ) ; continue ; } m = sscanf ( rest , "%d" , &n ) ; if ((m <= 0) || (n < 0)) { sprintf ( msg , "Illegal CLASSIFY OUTPUT: %s", rest ) ; error_message ( msg ) ; } else if (n > n_outputs) { sprintf ( msg , "CLASSIFY OUTPUT (%d) exceeds N OUTPUTS (%d)", n, n_outputs ) ; error_message ( msg ) ; } else classif_output = n ; continue ; } if (! strcmp ( command , "OUTPUT FILE" )) { strcpy ( out_file , rest ) ; continue ; } if (! strcmp ( command , "EXECUTE" )) { if (network == NULL) error_message ( "There is no trained network" ) ; else { network->execute_from_file ( rest , out_file) ; continue ; } } if (! strcmp ( command , "TEST NETWORK" )) { if (network == NULL) error_message ( "There is no trained network" ) ; else { network->test_from_file ( rest ,out_file,net_model) ; continue ; } } if (! strcmp ( command , "CLASSIFY" )) { if (network == NULL) error_message ( "There is no trained network" ) ; else if (out_model != OUTMOD_CLASSIFY) error_message ( "CLASSIFY valid only in CLASSIFY output mode" ) ; else network->classify_from_file ( rest , threshold ) ; continue ; } if (! strcmp ( command , "RESET CONFUSION" )) { if (network == NULL) error_message ( "There is no trained network" ) ; else network->reset_confusion () ; continue ; } if (! strcmp ( command , "CONFUSION THRESHOLD" )) { p = atof ( rest ) ; if ((p < 0.0) || (p > 100.0)) { sprintf ( msg , "Illegal CONFUSION THRESHOLD: %s", rest ) ; error_message ( msg ) ; } else threshold = p / 100.0 ; continue ; } if (! strcmp ( command , "SHOW CONFUSION" )) { if (network == NULL) error_message ( "There is no trained network" ) ; else if (out_model != OUTMOD_CLASSIFY) error_message ( "CONFUSION valid only in CLASSIFY output mode" ) ; else network->show_confusion () ; continue ; } if (! strcmp ( command , "SAVE CONFUSION" )) { if (network == NULL) error_message ( "There is no trained network" ) ; else if (out_model != OUTMOD_CLASSIFY) error_message ( "CONFUSION valid only in CLASSIFY output mode" ) ; else network->save_confusion ( rest ) ; continue ; } sprintf ( msg , "Unknown command: %s", command ) ; error_message ( msg ) ; } // Endless command loop MEMTEXT ( "NEURAL: control_line, msg" ) ; FREE ( control_line ) ; FREE ( msg ) ; MEMCLOSE () ; exit ( 0 ) ; }
int main ( int argc , // Number of command line arguments (includes prog name) char *argv[] // Arguments (prog name is argv[0]) ) { /* Declarations of local variables */ /* User's command control line related variables are here. Control_file_number and control_files permit nesting of 'CONTROL' commands. If control_file_number equals -1, control commands are read from stdin. Otherwise they are read from that file in FILE *control_files. Up to MAX_CONTROL_FILES can be stacked. */ int control_file_number = -1 ; // Stack pointer for control files FILE *control_files[MAX_CONTROL_FILES] ; // This is the stack char *control_line ; // User's commands here char *command, *rest ; // Pointers to its command and parameter parts int n_command, n_rest ; // Lengths of those parts /* These are network parameters which may be set by the user via commands. They are initialized to defaults which indicate that the user has not yet set them. As they are set, their current values are placed here. When learning is done for a network, their values are copied from here into the network object. When a network is read, the object's values are copied from it to here. Otherwise, these variables are not used; the values in the network object itself are used. The only purpose of these variables is to keep track of current values. */ int net_model = -1 ; // Network model (see NETMOD_? in CONST.H) int out_model = -1 ; // Output model (see OUTMOD_? in CONST.H) int n_inputs = -1 ; // Number of input neurons int n_outputs = -1 ; // Number of output neurons int n_hidden1 = -1 ; // Number of hidden layer one neurons int n_hidden2 = -1 ; // Ditto layer 2 (0 if just one hidden layer) TrainingSet *tset = NULL ; // Training set here Network *network = NULL ; // Network here struct LearnParams learn_params ; // General learning parameters struct AnnealParams anneal_params ; // Simulated annealing parameters struct GenInitParams geninit_params ; // Genetic initialization parameters struct KohParams koh_params ; // Kohonen parameters int classif_output = -1 ; // Current class (0=reject) for classif training char out_file[80] = "" ; // File for EXECUTE output double threshold ; // CLASSIFY confusion reject cutoff /* Miscellaneous variables */ int i, n, m ; double p ; char *msg ; FILE *fp ; /* -------------------------------------------------------------------------------- Program starts here. Verify that a careless user didn't fail to set the integer size correctly when compiling. -------------------------------------------------------------------------------- */ #if VERSION_16_BIT if (sizeof(int) > 2) { printf ( "\nRecompile with VERSION_16_BIT set to 0 in CONST.H" ) ; exit ( 1 ) ; } #else if (sizeof(int) < 4) { printf ( "\nRecompile with VERSION_16_BIT set to 1 in CONST.H" ) ; exit ( 1 ) ; } #endif printf ( "\nNEURAL - Program to train and test neural networks" ) ; printf("\nCopyright (c) 1993 by Academic Press, Inc."); printf("\nAll rights reserved. Permission is hereby granted, until further notice,"); printf("\nto make copies of this diskette, which are not for resale, provided these"); printf("\ncopies are made from this master diskette only, and provided that the"); printf("\nfollowing copyright notice appears on the diskette label:"); printf("\n(c) 1993 by Academic Press, Inc."); printf("\nExcept as previously stated, no part of the computer program embodied in"); printf("\nthis diskette may be reproduced or transmitted in any form or by any means,"); printf("\nelectronic or mechanical, including input into storage in any information"); printf("\nsystem for resale, without permission in writing from the publisher."); printf("\nProduced in the United States of America."); printf("\nISBN 0-12-479041-0"); /* Process command line parameters */ mem_name[0] = 0 ; // Default is no memory allocation file for (i=1 ; i<argc ; i++) { // Process all command line args str_to_upr ( argv[i] ) ; // Easier if all upper case if (! strcmp ( argv[i] , "/DEBUG" )) { sscanf ( argv[++i] , "%s" , mem_name ) ; if ((strlen ( mem_name ) > 1) || ! isalpha ( mem_name[0] )) { printf ( "\nIllegal DEBUG drive (%s); must be 1 letter." ) ; exit ( 1 ) ; } continue ; } printf ( "\nIllegal command line parameter (%s)", argv[i] ) ; exit ( 1 ) ; } /* Initialize memory allocation debugging */ if (strlen ( mem_name )) { strcat ( mem_name , ":mem.log" ) ; fp = fopen ( mem_name , "wt" ) ; if (fp == NULL) { printf ( "\nCannot open debugging file %s", mem_name ) ; exit ( 1 ) ; } fclose ( fp ) ; mem_log = 1 ; } else mem_log = 0 ; mem_used = 0 ; /* Initialize defaults */ learn_params.init = -1 ; learn_params.quit_err = 0.0 ; learn_params.retries = 32767 ; anneal_params.temps0 = 3 ; anneal_params.temps = 4 ; anneal_params.iters0 = 50 ; anneal_params.iters = 20 ; anneal_params.setback0 = 50 ; anneal_params.setback = 20 ; anneal_params.start0 = 3.0 ; anneal_params.start = 4.0 ; anneal_params.stop0 = 1.0 ; anneal_params.stop = 0.02 ; geninit_params.pool = 50 ; geninit_params.gens = 3 ; geninit_params.climb = 0 ; geninit_params.overinit = 1.5 ; geninit_params.pcross = 0.8 ; geninit_params.pmutate = 0.0001 ; koh_params.normalization = 0 ; // 0=multiplicative, 1=Z koh_params.learn_method = 1 ; // 0=additive, 1=subtractive koh_params.rate = 0.4 ; // learning rate koh_params.reduction = 0.99 ; // learning rate reduction learn_params.ap = &anneal_params ; learn_params.gp = &geninit_params ; learn_params.kp = &koh_params ; act_func_init () ; // Initialize interpolation table for activation function MEMTEXT ( "NEURAL: control_line, msg" ) ; if (((control_line = (char *) MALLOC ( CONTROL_LINE_LENGTH+1 )) == NULL) || ((msg = (char *) MALLOC ( CONTROL_LINE_LENGTH+1 )) == NULL)) { printf ( "\nInsufficient memory" ) ; exit ( 1 ) ; } /* Main loop processes all commands */ for (;;) { get_control_line ( control_line , &control_file_number, control_files ) ; split_control_line ( control_line , &command , &n_command , &rest , &n_rest ) ; if (! n_command) { if (n_rest) { sprintf ( msg , "No colon after command: %s", rest ) ; error_message ( msg ) ; } continue ; } sprintf ( msg , "%s : %s", command, rest ) ; normal_message ( msg ) ; /* Act on the command */ if (! strcmp ( command , "QUIT" )) break ; if (! strcmp ( command , "CONTROL" )) { stack_control_file ( rest , &control_file_number , control_files ) ; continue ; } if (! strcmp ( command , "NETWORK MODEL" )) { if (! strcmp ( rest , "LAYER" )) n = NETMOD_LAYER ; else if (! strcmp ( rest , "KOHONEN" )) n = NETMOD_KOH ; else { sprintf ( msg , "Illegal NETWORK MODEL: %s", rest ) ; error_message ( msg ) ; continue ; } if (net_model == n) continue ; if (ok_to_clear_weights( &network )) { net_model = n ; learn_params.init = -1 ; } else warning_message ( "Command aborted" ) ; continue ; } if (! strcmp ( command , "OUTPUT MODEL" )) { if (! strcmp ( rest , "CLASSIFY" )) n = OUTMOD_CLASSIFY ; else if (! strcmp ( rest , "AUTO" )) n = OUTMOD_AUTO ; else if (! strcmp ( rest , "GENERAL" )) n = OUTMOD_GENERAL ; else { sprintf ( msg , "Illegal OUTPUT MODEL: %s", rest ) ; error_message ( msg ) ; continue ; } if (out_model == n) continue ; if ((ok_to_clear_tset( &tset )) && (ok_to_clear_weights( &network))) out_model = n ; else warning_message ( "Command aborted" ) ; continue ; } if (! strcmp ( command , "N INPUTS" )) { m = sscanf ( rest , "%d" , &n ) ; if ((m <= 0) || (n <= 0) || (n > MAX_INPUTS)) { sprintf ( msg , "Illegal N INPUTS: %s", rest ) ; error_message ( msg ) ; continue ; } if (n_inputs == n) continue ; if ((ok_to_clear_tset( &tset)) && (ok_to_clear_weights(&network))) n_inputs = n ; else warning_message ( "Command aborted" ) ; continue ; } if (! strcmp ( command , "N OUTPUTS" )) { m = sscanf ( rest , "%d" , &n ) ; if ((m <= 0) || (n <= 0) || (n > MAX_OUTPUTS)) { sprintf ( msg , "Illegal N OUTPUTS: %s", rest ) ; error_message ( msg ) ; continue ; } if (n_outputs == n) continue ; if ((ok_to_clear_tset( &tset)) && (ok_to_clear_weights(&network))) n_outputs = n ; else warning_message ( "Command aborted" ) ; continue ; } if (! strcmp ( command , "N HIDDEN1" )) { m = sscanf ( rest , "%d" , &n ) ; if ((m <= 0) || (n < 0) || (n > MAX_HIDDEN)) { sprintf ( msg , "Illegal N HIDDEN1: %s", rest ) ; error_message ( msg ) ; continue ; } if (n_hidden1 == n) continue ; if (ok_to_clear_weights( &network )) n_hidden1 = n ; else warning_message ( "Command aborted" ) ; continue ; } if (! strcmp ( command , "N HIDDEN2" )) { m = sscanf ( rest , "%d" , &n ) ; if ((m <= 0) || (n < 0) || (n > MAX_HIDDEN)) { sprintf ( msg , "Illegal N HIDDEN2: %s", rest ) ; error_message ( msg ) ; continue ; } if (n && ! n_hidden1) { error_message ( "N HIDDEN2 must be 0 if N HIDDEN1 IS 0." ) ; continue ; } if (n_hidden2 == n) continue ; if (ok_to_clear_weights( &network )) n_hidden2 = n ; else warning_message ( "Command aborted" ) ; continue ; } if (! strcmp ( command , "TRAIN" )) { if ((out_model == OUTMOD_AUTO) && (n_outputs != n_inputs)) { warning_message ( "Setting N OUTPUTS = N INPUTS" ) ; n_outputs = n_inputs ; } if (out_model <= 0) error_message ( "TRAIN used before OUTPUT MODEL set." ) ; else if (n_inputs <= 0) error_message ( "TRAIN used before N INPUTS set." ) ; else if (n_outputs <= 0) error_message ( "TRAIN used before N OUTPUTS set." ) ; else if ((net_model != NETMOD_KOH) && (out_model == OUTMOD_CLASSIFY) && (classif_output < 0)) error_message( "CLASSIFY output mode but CLASSIFY OUTPUT not set."); else if ((net_model == NETMOD_KOH) && (out_model != OUTMOD_CLASSIFY)) error_message( "KOHONEN network requires CLASSIFY output mode."); else { if (tset == NULL) { MEMTEXT ( "NEURAL: new tset" ) ; tset = new TrainingSet ( out_model , n_inputs , n_outputs ) ; } tset->train ( rest , classif_output ) ; } continue ; } if (check_anneal ( command , rest , &anneal_params )) continue ; if (check_genetic ( command , rest , &geninit_params )) continue ; if (check_kohonen ( command , rest , &koh_params , &network )) continue ; if (check_learn_params ( command , rest , &learn_params , net_model )) continue ; if (! strcmp ( command , "LEARN" )) { if ((tset == NULL) || (tset->ntrain == 0)) { error_message ( "Cannot LEARN; No training set exists." ) ; continue ; } if ((net_model == NETMOD_KOH) && (out_model != OUTMOD_CLASSIFY)) { error_message( "KOHONEN network requires CLASSIFY output mode."); continue ; } if (learn_params.init < 0) { error_message( "Initialization method not set."); continue ; } if (network == NULL) { if (net_model == NETMOD_LAYER) { if (n_hidden1 < 0) { error_message ( "LEARN used before N HIDDEN1 set." ) ; continue ; } else if (n_hidden2 < 0) { error_message ( "LEARN used before N HIDDEN2 set." ) ; continue ; } else { MEMTEXT ( "NEURAL: new LayerNet" ) ; network = new LayerNet ( out_model , n_inputs , n_hidden1 , n_hidden2 , n_outputs , 1 , 1 ) ; } } else if (net_model == NETMOD_KOH) { MEMTEXT ( "NEURAL: new KohNet" ) ; network = new KohNet ( n_inputs , n_outputs , &koh_params , 1 , 1 ) ; } } if ((network == NULL) || (! network->ok)) { // Malloc failure? memory_message ( "to create network." ) ; if (network != NULL) { delete network ; network = NULL ; } continue ; } network->learn ( tset , &learn_params ) ; if (network->neterr > 0.999999) { // Indicates massive failure MEMTEXT ( "NEURAL: learn failure delete network" ) ; delete network ; network = NULL ; } else { sprintf ( msg , "Final error = %.4lf%% of max possible", 100.0 * network->neterr ) ; normal_message ( msg ) ; } continue ; } if (! strcmp ( command , "SAVE WEIGHTS" )) { if (network == NULL) error_message ( "There are no learned weights to save." ) ; else wt_save ( network , net_model , 0 , rest ) ; continue ; } if (! strcmp ( command , "RESTORE WEIGHTS" )) { if (network != NULL) { MEMTEXT ( "NEURAL: delete network for restore" ) ; delete network ; network = NULL ; } network = wt_restore ( rest , &net_model ) ; if (network == NULL) continue ; if (tset != NULL) { if ((tset->nin != network->nin) || (tset->nout != network->nout) || (tset->outmod != network->outmod)) { error_message ( "Network conflicts with existing training set."); continue ; } } out_model = network->outmod ; n_inputs = network->nin ; n_outputs = network->nout ; if (net_model == NETMOD_LAYER) { n_hidden1 = ((LayerNet*) network)->nhid1 ; n_hidden2 = ((LayerNet*) network)->nhid2 ; } if (net_model == NETMOD_KOH) koh_params.normalization = ((KohNet *) network)->normalization ; learn_params.init = -1 ; continue ; } if (! strcmp ( command , "CLEAR TRAINING" )) { if (tset != NULL) { MEMTEXT ( "NEURAL: delete tset" ) ; delete tset ; tset = NULL ; } continue ; } if (! strcmp ( command , "CLEAR WEIGHTS" )) { if (network != NULL) { MEMTEXT ( "NEURAL: delete network" ) ; delete network ; network = NULL ; } continue ; } if (! strcmp ( command , "CLASSIFY OUTPUT" )) { if (net_model == NETMOD_KOH) { error_message ( "Cannot specify output for KOHONEN model." ) ; continue ; } if (n_outputs < 0) { error_message ( "CLASSIFY OUTPUT used before N OUTPUTS set." ) ; continue ; } if (out_model != OUTMOD_CLASSIFY) { error_message ( "CLASSIFY OUTPUT only valid when OUTPUT MODEL:CLASSIFY" ) ; continue ; } m = sscanf ( rest , "%d" , &n ) ; if ((m <= 0) || (n < 0)) { sprintf ( msg , "Illegal CLASSIFY OUTPUT: %s", rest ) ; error_message ( msg ) ; } else if (n > n_outputs) { sprintf ( msg , "CLASSIFY OUTPUT (%d) exceeds N OUTPUTS (%d)", n, n_outputs ) ; error_message ( msg ) ; } else classif_output = n ; continue ; } if (! strcmp ( command , "OUTPUT FILE" )) { strcpy ( out_file , rest ) ; continue ; } if (! strcmp ( command , "EXECUTE" )) { if (network == NULL) error_message ( "There is no trained network" ) ; else network->execute_from_file ( rest , out_file ) ; continue ; } if (! strcmp ( command , "CLASSIFY" )) { if (network == NULL) error_message ( "There is no trained network" ) ; else if (out_model != OUTMOD_CLASSIFY) error_message ( "CLASSIFY valid only in CLASSIFY output mode" ) ; else network->classify_from_file ( rest , threshold ) ; continue ; } if (! strcmp ( command , "RESET CONFUSION" )) { if (network == NULL) error_message ( "There is no trained network" ) ; else network->reset_confusion () ; continue ; } if (! strcmp ( command , "CONFUSION THRESHOLD" )) { p = atof ( rest ) ; if ((p < 0.0) || (p > 100.0)) { sprintf ( msg , "Illegal CONFUSION THRESHOLD: %s", rest ) ; error_message ( msg ) ; } else threshold = p / 100.0 ; continue ; } if (! strcmp ( command , "SHOW CONFUSION" )) { if (network == NULL) error_message ( "There is no trained network" ) ; else if (out_model != OUTMOD_CLASSIFY) error_message ( "CONFUSION valid only in CLASSIFY output mode" ) ; else network->show_confusion () ; continue ; } if (! strcmp ( command , "SAVE CONFUSION" )) { if (network == NULL) error_message ( "There is no trained network" ) ; else if (out_model != OUTMOD_CLASSIFY) error_message ( "CONFUSION valid only in CLASSIFY output mode" ) ; else network->save_confusion ( rest ) ; continue ; } sprintf ( msg , "Unknown command: %s", command ) ; error_message ( msg ) ; } // Endless command loop MEMTEXT ( "NEURAL: control_line, msg" ) ; FREE ( control_line ) ; FREE ( msg ) ; MEMCLOSE () ; return 0 ; }