accum.c

来自「NIST Handwriting OCR Testbed」· C语言 代码 · 共 578 行 · 第 1/2 页

C
578
字号
    (float)sqrt((double)(err * 2.)), e1, e2, wtd_nr, wtd_nw,    wtd_rpct_min);}/******************************************************************//* accum_free: Frees the local (to this file) buffers.  Call this atend of each run. */voidaccum_free(){  free((char *)r_acc_bc);  free((char *)w_acc_bc);  free((char *)rej_acc_bc);  free((char *)iwtd_rpct_bc);  free((char *)outrej);  free_notnull((char *)confuse_acc.buf);  free_notnull((char *)outlvl.buf);  free_notnull((char *)npats_bc);}/*******************************************************************//* accum_printer: Used by accum_print() to nicely format someinfo and write it to stderr and to the short outfile.Input args:  long_classnames: Long names of the classes.  short_classnames: Short names of the classes.  ntotal: Number of outputs times number of patterns.  wtd_nw: Weighted "number wrong".  wtd_nrej_bc: Weighted "number rejected by class".  confuse_wtd_counts: Confusion matrix of weighted "counts".*/voidaccum_printer(long_classnames, short_classnames, ntotal, wtd_nw,  wtd_nrej_bc, confuse_wtd_counts)char **long_classnames, **short_classnames;int ntotal, wtd_nw, *wtd_nrej_bc;TDA_INT *confuse_wtd_counts;{  char str[200];  int j, k, isum;  sprintf(str, "\n oklvl %.2f\n # Highest two outputs (mean) %.3f \%.3f; mean diff %.3f\n", oklvl_loc, sum1, sum2, sum1 - sum2);  fsaso(str);#ifdef KEYS_NAMES_TABLE  fsaso("   key  name\n");  for(j = 0; j < nouts_loc; j++) {    sprintf(str, "   %s   %s\n", short_classnames[j],      long_classnames[j]);    fsaso(str);  }#endif  fsaso(" #  key:   ");  for(j = 0; j < nouts_loc; j++) {    sprintf(str, "  %s", short_classnames[j]);    fsaso(str);  }  fsaso("\n");  fsaso(" #  row: correct, column: actual\n");  for(j = 0; j < nouts_loc; j++) {    sprintf(str, " #      %s:", short_classnames[j]);    fsaso(str);    for(k = 0; k < nouts_loc; k++) {      sprintf(str, " %3d", e(*confuse_wtd_counts, k, j));      fsaso(str);    }    fsaso("\n");  }  fsaso(" #  unknown\n #    * ");  for(j = 0; j < nouts_loc; j++) {    sprintf(str, " %3d", wtd_nrej_bc[j]);    fsaso(str);  }  fsaso("\n");  /* row sums: percent of each class correctly identified */  fsaso("\n percent of true IDs correctly identified (rows)\n\        ");  for(j = 0; j < nouts_loc; j++) {    /* The following line computes percentages which, if priors is    allsame, should come out the same as (or very close to) the    percentages produced by the non-weighting version, i.e. in which    the "counts" used as the basis for the calculations really are    just counts; but, it may be that it would make more sense _not_    to subtract wtd_nrej_bc[j] from npats_bc[j]. */    sprintf(str, " %3d", round(100. * (float)e(*confuse_wtd_counts, j,      j) / (float)max(1, npats_bc[j] - wtd_nrej_bc[j])));    fsaso(str);  }  fsaso("\n");  /* [Do the "column sum" numbers as computed here make sense for the  _weighted_ version?  Because of the particular weighted method used  to compute confuse_wtd_counts, the sum of its i'th column might  _not_ be the number of examples whose hyp class is i.] */  /* column sums: percent of each predicted class correctly  identified */  fsaso("percent of predicted IDs correctly identified (cols)\n\        ");  for(k = 0; k < nouts_loc; k++) {    for(j = isum = 0; j < nouts_loc; j++)      isum += e(*confuse_wtd_counts, k, j);    sprintf(str, " %3d", round(100. * (float)e(*confuse_wtd_counts, k,      k) / (float)max(1, isum)));    fsaso(str);  }  fsaso("\n\n #  mean highest activation level\n #  row: \correct, column: actual\n #  key:   ");  for(j = 0; j < nouts_loc; j++) {    sprintf(str, "  %s", short_classnames[j]);    fsaso(str);  }  fsaso("\n");  for(j = 0; j < nouts_loc; j++) {    sprintf(str, " #      %s:", short_classnames[j]);    fsaso(str);    for(k = 0; k < nouts_loc; k++) {      sprintf(str, " %3d", round(e(outlvl, k, j)));      fsaso(str);    }    fsaso("\n");  }  fsaso(" #  unknown\n #    * ");  for(k = 0; k < nouts_loc; k++) {    sprintf(str, " %3d", round(outrej[k]));    fsaso(str);  }  sprintf(str, "\n\n Histogram of errors, from 2^(-%d) to 1\n",    ERRS_NERRS_DIM - 1);  fsaso(str);  for(j = 0; j < ERRS_NERRS_DIM; j++) {    sprintf(str, " %6d", nerrs[j]);    fsaso(str);  }  fsaso("\n");  for(j = 0; j < ERRS_NERRS_DIM; j++) {    sprintf(str, " %6.1f", 100. * (float)nerrs[j] / (float)ntotal);    fsaso(str);  }  fsaso("%\n \n");}/*******************************************************************//* accum_sumout: Writes some summary output to stderr, to the shortoutfile, and possibly to the "NN.PROGRESS" file.Input args:  purpose: CLASSIFIER or FITTER.  npats: Number of patterns.  iter: Current iteration.  c: A character to be used at the beginning of the first summary    line: should be either ' ' (blank), or 'F', meaning "final".    'F' is suitable if the summary line results from a final sending    of the patterns through the net during a training run.  w: Network weights.  rmserr: Root-mean-square error.  e1: Main part of error, basically.  e2: Mean squared weight.Output args:  wtd_nr: Weighted "number right".  wtd_nw: Weighted "number wrong".  wtd_rpct_min: Minimum, across classes, of weighted    "right-percentage by class".*/voidaccum_sumout(purpose, npats, iter, c, w, rmserr, e1, e2,  wtd_nr, wtd_nw, wtd_rpct_min)int npats, iter, *wtd_nr, *wtd_nw;char purpose, c;float w[], rmserr, e1, e2, *wtd_rpct_min;{  FILE *fp_nnp;  char str[200];  int i, wtd_nrej;  float a, denom, r_acc, w_acc, rej_acc, wtd_nr_pct,    wtd_nw_pct, wtd_nrej_pct;  void accum_yow();  /* Compute weighted "right-percentages by class" and their minimum,  and accumulate the accumulators across classes for next part */  for(i = 0, *wtd_rpct_min = 100., r_acc = w_acc = rej_acc = 0.;    i < nouts_loc; i++) {    denom = r_acc_bc[i] + w_acc_bc[i] + rej_acc_bc[i];    if(denom == 0.)      denom = 1.;    iwtd_rpct_bc[i] = round(a = 100. * r_acc_bc[i] / denom);    if(a < *wtd_rpct_min)      *wtd_rpct_min = a;    r_acc += r_acc_bc[i];    w_acc += w_acc_bc[i];    rej_acc += rej_acc_bc[i];  }  /* Compute (across-classes) weighted "numbers right, wrong, and  rejected" and the corresponding percentages */  denom = r_acc + w_acc + rej_acc;  if(denom == 0.)    denom = 1.;  *wtd_nr = round(npats * r_acc / denom);  wtd_nr_pct = 100. * (float)(*wtd_nr) / (float)npats;  *wtd_nw = round(npats * w_acc / denom);  wtd_nw_pct = 100. * (float)(*wtd_nw) / (float)npats;  wtd_nrej = npats - *wtd_nr - *wtd_nw;  wtd_nrej_pct = 100. * (float)wtd_nrej / (float)npats;  if(iter >= 0) {    if((fp_nnp = fopen("NN.PROGRESS", "wb")) == (FILE *)NULL)      fatalerr("accum_sumout (accum.c)", "fopen of NN.PROGRESS \for writing failed", NULL);    if(iter == 0 || c == 'F') {      if(purpose == CLASSIFIER)	accum_yow(fp_nnp, "     Iter   Err (   Ep    Ew)     OK\    UNK     NG      OK   UNK    NG\n");      else	accum_yow(fp_nnp, "     Iter   Err (   Ep    Ew)\n");    }    if(purpose == CLASSIFIER) {      sprintf(str, "  %c %5d %5.3f (%5.3f %5.3f) %6d %6d %6d\ = %5.1f %5.1f %5.1f %%\n", c, iter, rmserr, e1, e2, *wtd_nr,        wtd_nrej, *wtd_nw, wtd_nr_pct, wtd_nrej_pct, wtd_nw_pct);      accum_yow(fp_nnp, str);      sprintf(str, " %5.1f  ", *wtd_rpct_min);      accum_yow(fp_nnp, str);      for(i = 0; i < nouts_loc; i++) {	sprintf(str, " %2d", iwtd_rpct_bc[i]);	accum_yow(fp_nnp, str);      }      accum_yow(fp_nnp, "\n");    }    else {      sprintf(str, "  %c %5d %5.3f (%5.3f %5.3f)\n", c, iter,        rmserr, e1, e2);      accum_yow(fp_nnp, str);    }    fclose(fp_nnp);  }  else { /* (iter < 0) */    if(purpose == CLASSIFIER) {      fsaso("           Err (   Ep    Ew)     OK\    UNK     NG      OK   UNK    NG\n");      sprintf(str, "     Test %5.3f (%5.3f %5.3f) %6d %6d %6d\ = %5.1f %5.1f %5.1f %%\n", rmserr, e1, e2, *wtd_nr, wtd_nrej,        *wtd_nw, wtd_nr_pct, wtd_nrej_pct, wtd_nw_pct);      fsaso(str);      sprintf(str, " %5.1f  ", *wtd_rpct_min);      fsaso(str);      for(i = 0; i < nouts_loc; i++) {	sprintf(str, " %2d", iwtd_rpct_bc[i]);	fsaso(str);      }      fsaso("\n");    }    else {      fsaso("           Err (   Ep    Ew)\n");      sprintf(str, "     Test %5.3f (%5.3f %5.3f)\n", rmserr, e1, e2);      fsaso(str);    }  }}/*******************************************************************//* accum_yow: Causes str to be fputs'd, with subsequent fflushing, tostderr, to the short outfile, and to stream fp. */voidaccum_yow(fp, str)FILE *fp;char str[];{  fsaso(str);  fputs(str, fp);  fflush(fp);}/*******************************************************************/

⌨️ 快捷键说明

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