📄 neural.c
字号:
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 () ; exit ( 0 ) ;}/*-------------------------------------------------------------------------------- Static routines--------------------------------------------------------------------------------*///static int ok_to_clear_tset( TrainingSet **tset ){ if (*tset == NULL) return 1 ; if (get_yn ( "Existing training set must be cleared. OK" )) { MEMTEXT ( "NEURAL: delete tset" ) ; delete *tset ; *tset = NULL ; return 1 ; } else return 0 ;}//static int ok_to_clear_weights( Network **network ){ if (*network == NULL) return 1 ; if (get_yn ( "Existing learned weights must be cleared. OK" )) { MEMTEXT ( "NEURAL: delete network" ) ; delete *network ; *network = NULL ; return 1 ; } else return 0 ;}//static int get_yn ( char *msg ){ int key ; printf ( "\n%s? (Y/N):", msg ) ; for (;;) { key = getchar () ; if ((key == 'y') || (key == 'Y'))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -