static void create_snns_unit(void) { struct PosType unit_pos; PLANE *PLANE_element = PLANE_first_element; int x,y,width,height,unit_no,pos,ret; int y_offset=0,relative_y_max=0,absolute_y_max=0,y_begin; int x_max=0,old_x_begin=2,new_x_begin,x_begin; ret = krui_allocateUnits((*PLANE_last_element).width * (*PLANE_last_element).height + (*PLANE_last_element).begin-1); if(ret != 0){ ui_tw_errorMessage(krui_error(ret)); BN_ERROR = 1; } while((! BN_ERROR) && (PLANE_element != NULL)) { height = (*PLANE_element).height; width = (*PLANE_element).width; pos = (*PLANE_element).pos; y_begin = calculate_y_begin(&absolute_y_max,&relative_y_max,&y_offset,height,pos); x_begin = calculate_x_begin(&new_x_begin,&old_x_begin,&x_max,width,pos); for(y=0;y<height;y++){ for(x=0;x<width;x++){ unit_no = krui_createDefaultUnit(); if(unit_no<0) ui_checkError(unit_no); krui_setUnitActFunc(unit_no, "Act_Perceptron"); ret = krui_setUnitTType(unit_no,(*PLANE_element).type+1); if(ret != 0){ ui_tw_errorMessage(krui_error(ret)); BN_ERROR = 1; } unit_pos.x = x_begin + x; unit_pos.y = y_begin + y; krui_setUnitPosition(unit_no,&unit_pos); }/*for*/ }/*for*/ PLANE_element = (*PLANE_element).next; }/*while*/ }
krui_err SnnsCLib::bn_assoz_createNet(int X, int Y) { int i,j,unit_no; struct PosType unit_pos; krui_err ret = KRERR_NO_ERROR; int HUnits= X*Y; int IUnits= X*Y; /* Allocate units */ ret = krui_allocateUnits( IUnits + HUnits ); CHECK_RETURN (ret); /* Create standard input Units. The size of the input layer is X*Y */ for (i = 1; i <= Y; i++) for (j = 1; j <= X; j++) { unit_pos.x = j; unit_no = krui_createDefaultUnit(); if (unit_no < 0) CHECK_RETURN (unit_no); ret = krui_setUnitTType( unit_no, INPUT ); CHECK_RETURN (ret); ret = krui_setUnitActFunc( unit_no, const_cast<char*>("Act_RM") ); CHECK_RETURN (ret); unit_pos.y = i; krui_setUnitPosition( unit_no, &unit_pos ); } /* Create standard hidden Units. The size of the output layer is X*Y */ for (i = 1; i <= Y; i++) for (j = 1; j <= X; j++) { unit_pos.x = X+4+j; unit_no = krui_createDefaultUnit(); if (unit_no < 0) CHECK_RETURN (unit_no); ret = krui_setUnitTType( unit_no, HIDDEN ); CHECK_RETURN (ret); ret = krui_setUnitActFunc( unit_no, const_cast<char*>("Act_RM") ); CHECK_RETURN (ret); unit_pos.y = i; krui_setUnitPosition( unit_no, &unit_pos ); } /* Make connections between input units and hidden units */ for (i = IUnits + 1; i <= IUnits + HUnits; i++) { /* Make hidden unit to current unit */ ret = krui_setCurrentUnit( i ); CHECK_RETURN (ret); /* (backward) connect current (hidden) unit with input unit */ /* set all link weights from the input units to one */ ret = krui_createLink( i-IUnits, 1.0); CHECK_RETURN (ret); /* Make connections from all hidden units */ /* set link weights to zero */ for (j = IUnits + 1; j <= IUnits + HUnits; j++) { if (j != i){ ret = krui_createLink( j, 0.0); CHECK_RETURN (ret); } } } /* set the update function */ ret = krui_setUpdateFunc (const_cast<char*>(ASSOZ_UPDATE_FUNC_NAME)); CHECK_RETURN (ret); /* set the learning function */ ret = krui_setLearnFunc (const_cast<char*>(ASSOZ_LEARN_FUNC_NAME)); CHECK_RETURN (ret); /* set the init function */ ret = krui_setInitialisationFunc (const_cast<char*>(ASSOZ_INIT_FUNC_NAME)); //CHECK_RETURN (ret); return(ret); } /* bn_assoz_createNet */
krui_err SnnsCLib::bn_kohonen_createNet(int X, int Y, int IUnits, int HUnits) { int i,j,unit_no; struct PosType unit_pos; krui_err ret; unit_pos.z = 0; /* Allocate units */ ret = krui_allocateUnits( HUnits + IUnits ); CHECK_RETURN ( ret ); /* Create standard (input) Units */ unit_pos.x = 1; for (i = 1; i <= IUnits; i++) { unit_no = krui_createDefaultUnit(); if (unit_no < 0) CHECK_RETURN( unit_no ); ret = krui_setUnitTType( unit_no, INPUT ); CHECK_RETURN( ret ); unit_pos.y = (IUnits<Y)?i+(Y-IUnits)/2:i; krui_setUnitPosition( unit_no, &unit_pos ); } /* Create standard hidden Units. The size of the feature map is X*Y */ 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) CHECK_RETURN( unit_no ); ret = krui_setUnitTType( unit_no, HIDDEN ); CHECK_RETURN( ret ); unit_pos.y = i; krui_setUnitPosition( unit_no, &unit_pos ); } /* Make connections between input units and hidden units */ /* set all link weights to zero */ for (i = IUnits + 1; i <= IUnits + HUnits; i++) { /* Make hidden unit to current unit */ ret = krui_setCurrentUnit( i ); CHECK_RETURN( ret ); /* (backward) connect current (hidden) unit with input unit */ for (j = 1; j <= IUnits; j++) { ret = krui_createLink( j,0.0); CHECK_RETURN( ret ); } } /* set the update function */ ret = krui_setUpdateFunc (const_cast<char*>(KOHONEN_UPDATE_FUNC_NAME)); CHECK_RETURN( ret ); /* set the learning function */ ret = krui_setLearnFunc (const_cast<char*>(KOHONEN_LEARN_FUNC_NAME)); CHECK_RETURN( ret ); /* set the init function */ ret = krui_setInitialisationFunc (const_cast<char*>(KOHONEN_INIT_FUNC_NAME)); //CHECK_RETURN( ret ); return(ret); } /* bn_kohonen_createNet */
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 */