⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lvq.c

📁 LVQ - Learning Vector Quantization Demonstration Download xlvq Linux executable (128 kb) wlvq.ex
💻 C
📖 第 1 页 / 共 3 页
字号:
    if (i > 0)                  /* start a new output line */      fprintf(file, ",\n%s          ", indent);    fputs("{ [", file);         /* write the center coordinates */    for (k = 0; k < lvq->dim-1; k++)      fprintf(file, "%g, ", p->ctr[k]);    fprintf(file, "%g]", p->ctr[k]);    if (lvq->type & LVQ_SIZE)   /* print the variance (sq. radius) */      fprintf(file, ",\n%s            [%g]", indent, p->var);    fputs(" }", file);          /* terminate the quant. prototype */  }  fputs("};\n", file);          /* terminate the parameter section */  #ifdef LVQ_EXTFN              /* if to compile extended functions */  if (lvq->attmap) fputs("};\n", file);  #endif                        /* terminate the description */  return ferror(file) ? -1 : 0; /* return the write status */}  /* lvq_desc() *//*--------------------------------------------------------------------*/#ifdef LVQ_PARSEstatic int _scales (LVQNET *lvq, SCAN *scan){                               /* --- get the input scalings */  assert(lvq && scan);          /* check the function arguments */  if (lvq->nst) return 0;       /* check whether scalings exist */  if ((sc_token(scan) != T_ID)  /* check whether 'scales' follows */  ||  (strcmp(sc_value(scan), "scales") != 0))    return 0;                   /* if not, abort the function */  lvq->nst = nst_parse(scan, -1);  return (lvq->nst) ? 0 : 1;    /* parse the scaling parameters */}  /* _scales() *//*--------------------------------------------------------------------*/static int _function (LVQNET *lvq, SCAN *scan){                               /* --- parse the function description */  const char *s;                /* buffer for token value */  double     p;                 /* buffer for a parameter */  if ((sc_token(scan) != T_ID)  /* check for a function specification */  ||  (strcmp(sc_value(scan), "function") != 0))    return 0;                   /* if there is none, abort */  GET_TOK();                    /* consume the "function" keyword */  GET_CHR('=');                 /* consume '=' */  if (sc_token(scan) != T_ID) ERR_STR("cauchy");  s = sc_value(scan);           /* check for a function name */  if      (strcmp(s, "cauchy") == 0) lvq->actfn = rf_cauchy;  else if (strcmp(s, "gauss")  == 0) lvq->actfn = rf_gauss;  else ERR_STR("cauchy");       /* decode the function name */  GET_TOK();                    /* and consume it */  GET_CHR('(');                 /* consume '(' */  if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);  p = strtod(sc_value(scan), NULL);  if (p <= 0) ERROR(E_ILLNUM);  /* check for a number and */  lvq->params[0] = p;           /* get the first parameter, */  GET_TOK();                    /* i.e., the distance exponent */  GET_CHR(',');                 /* consume the separating ',' */  if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);  p = strtod(sc_value(scan), NULL);  if (p < 0) ERROR(E_ILLNUM);   /* check for a number and */  lvq->params[1] = p;           /* get the second parameter, */  GET_TOK();                    /* i.e., the scaling factor */  GET_CHR(')');                 /* consume the closing ')' */  GET_CHR(';');                 /* and the ';' */  return 0;                     /* return 'ok' */}  /* _function() *//*--------------------------------------------------------------------*/static int _norm (LVQNET *lvq, SCAN *scan){                               /* --- parse the normalization mode */  const char *s;                /* buffer for token value */  if ((sc_token(scan) != T_ID)  /* check for a function specification */  ||  (strcmp(sc_value(scan), "normmode") != 0))    return 0;                   /* if there is none, abort */  GET_TOK();                    /* consume the "normmode" keyword */  GET_CHR('=');                 /* consume '=' */  if (sc_token(scan) != T_ID) ERR_STR("sum1");  s = sc_value(scan);           /* check for a norm. mode name */  if      (strcmp(s, "none") == 0) lvq->mode = LVQ_NONE;  else if (strcmp(s, "sum1") == 0) lvq->mode = LVQ_SUM1;  else if (strcmp(s, "max1") == 0) lvq->mode = LVQ_MAX1;  else if (strcmp(s, "hard") == 0) lvq->mode = LVQ_HARD;  else ERR_STR("hard");         /* decode the norm. mode name */  GET_TOK();                    /* and consume it */  if (sc_token(scan) == ',') {  /* if a comma follows, */    GET_TOK();                  /* consume the comma ',' */    if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);    lvq->wtarf = strtod(sc_value(scan), NULL);    GET_TOK();                  /* get winner takes all radius factor */  }  GET_CHR(';');                 /* consume the closing ';' */  return 0;                     /* return 'ok' */}  /* _norm() *//*--------------------------------------------------------------------*/static int _params (LVQNET *lvq, SCAN *scan){                               /* --- parse a set of prototypes */  int    i;                     /* loop variables */  int    err = 0;               /* error status */  int    vsz = 0;               /* size of prototype vector */  int    dim = 0;               /* dimension of data space */  QPROT  *p;                    /* to access the prototypes */  double *v = NULL;             /* buffer for reallocation */  if ((sc_token(scan) != T_ID)  /* check for parameters */  ||  (strcmp(sc_value(scan), "params") != 0))    ERR_STR("params");          /* if there are none, abort */  GET_TOK();                    /* consume the "params" keyword */  GET_CHR('=');                 /* consume '=' */  GET_CHR('{');                 /* consume '{' */  while (1) {                   /* prototype read loop */    GET_CHR('{');               /* consume '{' */    if (lvq->cnt >= vsz) {      /* if the prototype vector is full */      vsz += (vsz > BLKSIZE) ? vsz >> 1 : BLKSIZE;      p = (QPROT*)realloc(lvq->qps, vsz *sizeof(QPROT));      if (!p) ERROR(E_NOMEM);   /* allocate a new prototype vector */      lvq->qps = p;             /* and set it */    }    GET_CHR('[');               /* consume opening '[' */    if (lvq->dim <= 0) {        /* if the dimension is yet unknown */      lvq->dim = 0;             /* initialize the dimension */      while (1) {               /* coordinate read loop */        if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);        if (lvq->dim >= dim) {  /* if the coordinate vector is full */          dim = (dim > BLKSIZE) ? dim >> 1 : BLKSIZE;          v = (double*)realloc(lvq->vec, dim *sizeof(double));          if (!v) ERROR(E_NOMEM);          lvq->vec = v;         /* enlarge the coordinate vector */        }                       /* and set the new one */        lvq->vec[lvq->dim++] = strtod(sc_value(scan), NULL);        GET_TOK();              /* get and consume the coordinate */        if (sc_token(scan) != ',') break;        GET_TOK();              /* if at end of coord. list, abort */      }                         /* the loop, otherwise consume ',' */    }    p = lvq->qps +lvq->cnt++;   /* init. a quantization prototype */    if (_qprot(p, lvq->dim) != 0) ERROR(E_NOMEM);    if (dim > 0) {              /* if the dimension was unknown */      vec_copy(p->ctr, lvq->vec, lvq->dim);      dim = 0; }                /* copy the prototype center */    else {                      /* if the dimension was known */      for (i = 0; i < lvq->dim; i++) {        if (i > 0) { GET_CHR(','); }  /* consume ',' */        if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);        p->ctr[i] = strtod(sc_value(scan), NULL);        GET_TOK();              /* traverse the dimensions and */      }                         /* get and consume the coordinates */    }                           /* of the center vector */    GET_CHR(']');               /* consume closing ']' */    if ((lvq->cnt <= 1)         /* if the prototype type is yet */    &&  (sc_token(scan) == ','))/* unknown and there is a comma, */      lvq->type |= LVQ_SIZE;    /* set the prototype size flag */    if (lvq->type & LVQ_SIZE) { /* if to use a prototype size */      GET_CHR(',');             /* consume ',' */      GET_CHR('[');             /* consume opening '[' */      if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);      p->var = strtod(sc_value(scan), NULL);      if (p->var <= 0)             ERROR(E_ILLNUM);      GET_TOK();                /* get and check the squared radius */      GET_CHR(']');             /* consume closing ']' */    }    GET_CHR('}');               /* consume '}' */    if (sc_token(scan) != ',') break;    GET_TOK();                  /* if at end of proto. list, abort */  }                             /* the loop, otherwise consume ',' */  GET_CHR('}');                 /* consume '}' and ';' */  GET_CHR(';');                 /* (end of the parameters section) */  return err;                   /* return the error status */}  /* _params() *//*--------------------------------------------------------------------*/LVQNET* lvq_parse (SCAN *scan, int dim){                               /* --- parse a d.s. network */  int    i, err;                /* loop variable, error status */  double *v;                    /* to resize the numeric vectors */  QPROT  *p;                    /* to traverse the prototypes */  LVQNET *lvq = NULL;           /* created neural network */  assert(scan);                 /* check the function argument */  pa_init(scan);                /* initialize parsing */  lvq = (LVQNET*)malloc(sizeof(LVQNET));  if (!lvq) return NULL;        /* create the base structure */  _init(lvq, dim, 0);           /* and initialize some fields */  err      = _scales(lvq, scan);     /* read the scaling parameters */  if (err && (sc_recover(scan, ';', 0, 0, 0) == T_EOF)) {    lvq_delete(lvq); return NULL; }  err |= i = _function(lvq, scan);   /* read the function description */  if (i   && (sc_recover(scan, ';', 0, 0, 0) == T_EOF)) {    lvq_delete(lvq); return NULL; }  err |= i = _norm(lvq, scan);       /* read the normalization mode */  if (i   && (sc_recover(scan, ';', 0, 0, 0) == T_EOF)) {    lvq_delete(lvq); return NULL; }  err |= i = _params(lvq, scan);     /* read the prototype parameters */  if (i   && (sc_recover(scan, ';', 0, 0, 0) == T_EOF)) {    lvq_delete(lvq); return NULL; }  err |=     _scales(lvq, scan);     /* read the scaling parameters */  if (err) { lvq_delete(lvq); return NULL; }  if (!lvq->nst) lvq->nst = nst_create(lvq->dim);  if (!lvq->nst) { lvq_delete(lvq); return NULL; }  lvq->qps =                    /* try to shrink the proto. vector */  p = (QPROT*) realloc(lvq->qps,    lvq->cnt *sizeof(QPROT));  v = (double*)realloc(lvq->vec, 2 *lvq->dim *sizeof(double));  if (!v) { lvq_delete(lvq); return NULL; }  lvq->vec = v;                 /* create the numeric vectors */  lvq->buf = v +lvq->dim;       /* and organize them */  #ifdef LVQ_EXTFN              /* if extended function set, */  lvq->attset = NULL;           /* clear the attribute set and */  lvq->attmap = NULL;           /* attribute map pointer as */  #endif                        /* an indicator for normal LVQ */  return lvq;                   /* return the created d.s. network */}  /* lvq_parse() *//*--------------------------------------------------------------------*/#ifdef LVQ_EXTFNstatic int _header (SCAN *scan){                               /* --- parse header */  assert(scan);                 /* check the function argument */  if ((sc_token(scan) != T_ID)  ||  ((strcmp(sc_value(scan), "lvqnet") != 0)  &&   (strcmp(sc_value(scan), "clset")  != 0)))    ERR_STR("lvqnet");          /* check for 'lvqnet' */  GET_TOK();                    /* consume 'lvqnet' */  GET_CHR('=');                 /* consume '=' */  GET_CHR('{');                 /* consume '{' */  return 0;                     /* return 'ok' */}  /* _header() *//*--------------------------------------------------------------------*/static int _trailer (SCAN *scan){                               /* --- parse trailer */  assert(scan);                 /* check the function argument */  GET_CHR('}');                 /* consume '}' */  GET_CHR(';');                 /* consume ';' */  return 0;                     /* return 'ok' */}  /* _trailer() *//*--------------------------------------------------------------------*/LVQNET* lvq_parsex (SCAN *scan, ATTSET *attset, int marked){                               /* --- parse a LVQ network */  LVQNET *lvq;                  /* created neural network */  ATTMAP *attmap;               /* created attribute map */  assert(scan && attset);       /* check the function arguments */  pa_init(scan);                /* initialize parsing */  if (_header(scan) != 0)       /* register the error messages */    return NULL;                /* and parse the header */  attmap = am_create(attset, marked, -1);  if (!attmap) return NULL;     /* create an attribute map */  lvq = lvq_parse(scan, am_dim(attmap));  if (!lvq) { am_delete(attmap); return NULL; }  if (_trailer(scan) != 0) {    /* parse the neural network desc. */    lvq_delete(lvq); return NULL; }  lvq->attset = attset;         /* note the attribute set */  lvq->attmap = attmap;         /* and  the attribute map */  return lvq;                   /* return the parsed neural network */}  /* lvq_parsex() */#endif  /* #ifdef LVQ_EXTFN */#endif  /* #ifdef LVQ_PARSE */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -