/************************************************** * snns_saveNet * * Saves a network via the SNNS kernel UI function * krui_saveNet(). **************************************************/ int snns_saveNet(stEval *args, stEval *result, void *instance) { char *filename = STSTRING(&args[0]); char *netname = STSTRING(&args[1]); krui_err errCode; char *errMsg; errCode = krui_saveNet(filename, netname); if(errCode == KRERR_NO_ERROR) { return EC_OK; } else { errMsg = krui_error(errCode); slMessage(0, "breveSNNS: saveNet failed with message %s (error code %d).", errMsg, errCode); return EC_ERROR; } }
void ui_file_saveNet (Widget w, XtPointer button, caddr_t call_data) { Bool performSave = TRUE; char filename[4+MAX_NAME_LENGTH+MAX_NAME_LENGTH]; if (strlen(ui_filenameNET) == 0) { ui_confirmOk("No file specified!"); return; } if (ui_filenameNET[0] == '/') sprintf(filename, "%s%s", ui_filenameNET, ui_getExtension(UI_FILE_NET)); else sprintf(filename, "%s/%s%s", ui_pathname, ui_filenameNET, ui_getExtension(UI_FILE_NET)); if (ui_fileExist(filename, 0)) performSave = ui_confirmYes("This file already exist! Overwrite?"); if (performSave) { ui_printMessage("Saving network ..."); ui_checkError(krui_saveNet(filename, ui_filenameNET)); if (ui_kernelError < 0) ui_confirmOk("Error during saving the network!"); else { char string[40+MAX_NAME_LENGTH+MAX_NAME_LENGTH]; ui_printMessage("Network saved."); strcpy(ui_filenameSLNET, ui_filenameNET); ui_file_updateShellLabels(); sprintf(string,"Network saved on file: %s\n",filename); ui_tw_printMessage(string); } } }
void create_network(char *weight_file) { FILE *fp; float val; /* Allocate units (the user may or may not use this function, there is no need to do this) */ ret = krui_allocateUnits( OUnits + HUnits + IUnits ); errChk( ret ); printf( "\n\nCreate Units now\n" ); /* Create standard (input) Units */ unit_pos.x = 1; for (i = 1; i <= IUnits; i++) { unit_no = krui_createDefaultUnit(); if (unit_no < 0) errChk( unit_no ); ret = krui_setUnitTType( unit_no, INPUT ); errChk( ret ); unit_pos.y = (IUnits<Y)?i+(Y-IUnits)/2:i; krui_setUnitPosition( unit_no, &unit_pos ); } /* Create standard (hidden) Units */ for (i = 1; i <= Y; i++) for (j = 1; j <= X; j++) { unit_pos.x = 4+j; unit_no = krui_createDefaultUnit(); if (unit_no < 0) errChk( unit_no ); ret = krui_setUnitTType( unit_no, HIDDEN ); errChk( ret ); unit_pos.y = i; krui_setUnitPosition( unit_no, &unit_pos ); } /* Create standard (output) Units */ unit_pos.x = 4+X+3; if (OUnits) for (i = 1; i <= OUnits; i++) { unit_no = krui_createDefaultUnit(); if (unit_no < 0) errChk( unit_no ); ret = krui_setUnitTType( unit_no, OUTPUT ); errChk( ret ); unit_pos.y = (OUnits<Y)?i+(Y-OUnits)/2:i; krui_setUnitPosition( unit_no, &unit_pos ); } /* Make Connections now */ /* Make connections between hidden units and output units first ! */ for (i = IUnits + HUnits + 1; i <= IUnits + HUnits + OUnits; i++) { /* Make output unit to current unit */ ret = krui_setCurrentUnit( i ); errChk( ret ); for (j = IUnits + 1; j <= IUnits + HUnits; j++) { /* connect current (output) unit with hidden unit. REMEMBER: The hidden unit #j is the predecessor of the (output) unit #i (it is a backward connection) */ ret = krui_createLink( j, 0 ); errChk( ret ); } } /* Make connections between input units and hidden units and set link weight with datas from output_file */ printf("\nSet link weights now\n"); if((fp=fopen(weight_file,"r"))!=NULL) for (i = IUnits + 1; i <= IUnits + HUnits; i++) { /* Make hidden unit to current unit */ ret = krui_setCurrentUnit( i ); errChk( ret ); for (j = 1; j <= IUnits; j++) { /* (backward) connect current (hidden) unit with input unit */ fscanf(fp,"%s",string); val = atof(string); ret = krui_createLink( j,val); errChk( ret ); } } else{ /* set all link weights to zero */ for (i = IUnits + 1; i <= IUnits + HUnits; i++) { /* Make hidden unit to current unit */ ret = krui_setCurrentUnit( i ); errChk( ret ); for (j = 1; j <= IUnits; j++) {/* (backward) connect current (hidden) unit with input unit */ ret = krui_createLink( j,0); errChk( ret ); } } printf("\nWeight file %s could not be opened!\n",weight_file); printf("All weights have been set to zero!\n"); } fclose(fp); /* set the update function */ ret = krui_setUpdateFunc (KOHONEN_UPDATE_FUNC_NAME); errChk( ret ); /* set the learning function */ ret = krui_setLearnFunc (KOHONEN_LEARN_FUNC_NAME); errChk( ret ); /* set the init function */ ret = krui_setInitialisationFunc (KOHONEN_INIT_FUNC_NAME); errChk( ret ); printf("\nEnter Filename of the Network to save: "); scanf("%s", name); strcat(name,".net"); printf("Save Network\n"); /* save the network */ ret = krui_saveNet( name, NULL ); errChk( ret ); printf( "\nCreate Patterns now\n" ); } /* end of create_network */