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

📄 fileio.c

📁 SOM-sd 是现在国外非常流行的一个神经网络的结构自组织特征映射算法
💻 C
📖 第 1 页 / 共 5 页
字号:
      tdim = gptr->tdim;      printf("dim_target=%d\n", tdim);    }    if (FanIn != gptr->FanIn){      FanIn = gptr->FanIn;      printf("indegree=%d\n", FanIn);    }    if (FanOut != gptr->FanOut){      FanOut = gptr->FanOut;      printf("outdegree=%d\n", FanOut);    }    if (gptr->gname != NULL)      fprintf(ofile, "graph:%s\n", gptr->gname);    else      fprintf(ofile, "graph:\n");    for (n = 0; n < gptr->numnodes; n++){      fprintf(ofile, "%d ", gptr->nodes[n]->nnum);      for (i = 0; i < gptr->dimension; i++){	PrintFloat(ofile, gptr->nodes[n]->points[i]);	fprintf(ofile, " ");	  //	fprintf(ofile, "%f ", gptr->nodes[n]->points[i]);      }      fprintf(ofile, "%d", (int)gptr->nodes[n]->depth);            for (i = 0; i < gptr->FanOut; i++){	if (gptr->nodes[n]->children[i] == NULL)	  fprintf(ofile, "- ");	else	  fprintf(ofile, "%d ", (int)((graph->nodes[n]->children[i] - graph->nodes[0])/sizeof(struct Node*)));      }      for (i = 0; i < gptr->FanIn; i++){	if (i < gptr->nodes[n]->numparents)     	  printf("%3d %3d ", gptr->nodes[n]->parents[i]->x, gptr->nodes[n]->parents[i]->y);	else	  fprintf(ofile, " -1  -1 ");      }      fprintf(ofile, "\n");    }  }*/  sync();  /* Ensure data is actually written to disk */}/******************************************************************************Description: Read the Self-Organizing Map data from a given file and store             the data in the structure pointed to by map.Return value: 0 if no errors, or a value greater than zero if there were errors******************************************************************************/int LoadMap(struct Parameters *params){  char *cptr;  /* Pointer to text we read from the file */  char *carg;  /* Pointer used to point at the argument of an parameter */  UNSIGNED num;  struct Map *map;  struct FileInfo *finfo = NULL; /* File structure */  if (params == NULL)    return 0;  map = &params->map;  fprintf(stderr, "Reading codebook entries....");/* Print What is being done*/  memset(map, 0, sizeof(struct Map));    /* Initialize Map structure         */  if (params->inetfile == NULL)          /* Ensure that there is a file name */    AddError("No file name for map-file given.");  else if ((finfo = OpenFile(params->inetfile, "rb")) == NULL) /* open stream*/    AddError("Unable to open file for reading.");  if (CheckErrors()){                    /* Any errors so far?       */    fprintf(stderr, "%46s\n", "[FAILED]");    return CheckErrors();  }  /* Read the header */  cptr = ReadLine(finfo);              /* Read first line of data  */  while(cptr != NULL){    cptr = strnspc(cptr);    num = 0;    num += satou(GetFileOption(cptr, "iteration"),(uint*)&map->iter);    num += satou(GetFileOption(cptr, "dim"), (uint*)&map->dim);    num += satou(GetFileOption(cptr, "xdim"), (uint*)&map->xdim);    num += satou(GetFileOption(cptr, "ydim"), (uint*)&map->ydim);    num += satou(GetFileOption(cptr, "byteorder"),&finfo->byteorder);    num += GetNeighborhoodID(GetFileOption(cptr, "neighborhood"), &map->neighborhood);    num += GetTopologyID(GetFileOption(cptr, "topology"), &map->topology);    if (!strncasecmp(cptr, "Train", 5)){/* If info on train params available */      cptr += 5;  /* Jump over key "Train" */      num += satof(GetFileOption(cptr, "mu1"), &params->mu1);      num += satof(GetFileOption(cptr, "mu2"), &params->mu2);      num += satof(GetFileOption(cptr, "mu3"), &params->mu3);      num += satof(GetFileOption(cptr, "mu4"), &params->mu4);      num += satof(GetFileOption(cptr, "alpha"), &params->alpha);      num += satof(GetFileOption(cptr, "beta"), &params->beta);      num += satou(GetFileOption(cptr, "iter"), (uint*)&params->rlen);      num += satou(GetFileOption(cptr, "radius"), (uint*)&params->radius);      num += satos(GetFileOption(cptr, "data"), &params->datafile);      num += satos(GetFileOption(cptr, "valid"), &params->validfile);      /* Now we look at bit-field values */      if ((carg = GetFileOption(cptr, "supervised")) != NULL &&isdigit((int)*carg)){	params->super = atoi(carg);	num++;      }      if ((carg = GetFileOption(cptr, "alphatype")) != NULL && isdigit((int)*carg)){	params->alphatype = atoi(carg);	num++;      }      if ((carg = GetFileOption(cptr, "kernel")) != NULL && isdigit((int)*carg)){	params->kernel = atoi(carg);	num++;      }      if ((carg = GetFileOption(cptr, "batchmode")) != NULL && *carg != '0' && strncasecmp("no", carg, 2)){	params->batch = 1;	num++;      }      if ((carg = GetFileOption(cptr, "momentum")) != NULL && *carg != '0' && strncasecmp("no", carg, 2)){	params->momentum = 1;	num++;      }    }    else if (!strncmp(cptr, "map", 3)) /* Codebook vectors follow */      break;    if (num == 0 && *cptr != '\0' && *cptr != '#'){      AddError("Unrecognized keyword found in header.");      AddFileInfoOnError(finfo);  /* Add file status info to error message */      break;    }    cptr = ReadLine(finfo);  }  if (CheckErrors())          /* Abort if there were errors in the header  */    return CheckErrors();  /* Verify that header contained useful information */  if (finfo && finfo->byteorder > 0 && finfo->byteorder != LITTLE_ENDIAN && finfo->byteorder != BIG_ENDIAN && finfo->byteorder != PDP_ENDIAN){    fprintf(stderr, ">>%d %d %d\n", finfo->byteorder, LITTLE_ENDIAN, BIG_ENDIAN);    AddError("Invalid byteorder specified in file!");  }  if (map->xdim == 0 || map->ydim == 0)        /* No map dimension specified */    AddError("Map dimension is zero!");  if (map->dim == 0)    AddError("Codebook dimension is zero!"); /* Codebook dimension is zero */  if (cptr == NULL)            /* Obligatory keywork "map" not found   */    AddError("This doesn't seem to be a codebook file.");  if (!CheckErrors())          /* If no errors...                */    ReadCodes(finfo, map);     /* then read the codebook entries */  CloseFile(finfo);            /* clean up */  if (!CheckErrors())          /* If no errors...                */    fprintf(stderr, "%d codes%*s\n", map->xdim * map->ydim, 39-(int)(log10(map->xdim * map->ydim)), "[OK]");  else    fprintf(stderr, "%46s\n", "[FAILED]");  return CheckErrors();}/******************************************************************************Description: Save the Self-Organizing Map data in a given file in a given             format. If format is 0, then write data in binary format,             otherwise save the map in AscII format.Return value: 0 if no errors, or a value greater than zero if there were errors******************************************************************************/int SaveMapInFormat(struct Parameters *params, char *fname, int format){  UNSIGNED i;  UNSIGNED clen;          /* Length of a label */  char *label;            /* Pointer to label  */  struct Map *map;  struct FileInfo *finfo = NULL; /* File structure    */  if (params == NULL)    return 0;  map = &params->map;  fprintf(stderr, "Saving codebook entries....");/* Print what is being done */  if (fname == NULL)                     /* Ensure that there is a file name */    AddError("No file name given to save map.");  else if ((finfo = OpenFile(fname, "wb")) == NULL) /* open output stream    */    AddError("Unable to open file for writing.");  if (CheckErrors()){                            /* Any errors so far?       */    fprintf(stderr, "%47s\n", "[FAILED]");    return CheckErrors();  }  if (format == 0 && FindEndian() == UNKNOWN)/* If binary mode was requested */    format = 1;/* but Endian of this hardware is unknown switch to AscII mode*/  /* Write header */#ifdef PROG_VERSION  fprintf(finfo->fptr, "#Written by: %s\n", PROG_VERSION);#endif  fprintf(finfo->fptr, "\n#Network properties:\n");  fprintf(finfo->fptr, "Iteration=%u\n", map->iter);  fprintf(finfo->fptr, "Dim=%u\n", map->dim);  fprintf(finfo->fptr, "Xdim=%u\n", map->xdim);  fprintf(finfo->fptr, "Ydim=%u\n", map->ydim);  if (format == 0)  /* Write byteorder in binary mode */    fprintf(finfo->fptr, "Byteorder=%d\n", FindEndian());  fprintf(finfo->fptr, "Neighborhood=%s\n", GetNeighborhoodName(map->neighborhood));  fprintf(finfo->fptr, "Topology=%s\n", GetTopologyName(map->topology));  if (params){    fprintf(finfo->fptr, "\n#Training parameters used:\n");    if (params->rlen > 0)      fprintf(finfo->fptr, "TrainIter=%u\n", params->rlen);    if (params->alpha > 0.0)      fprintf(finfo->fptr, "TrainAlpha=%.9f\n", params->alpha);    if (params->beta > 0.0)      fprintf(finfo->fptr, "TrainBeta=%.9f\n", params->beta);    if (params->radius > 0)      fprintf(finfo->fptr, "TrainRadius=%u\n", params->radius);    if (params->datafile != NULL)      fprintf(finfo->fptr, "TrainData=%s\n", params->datafile);    if (params->validfile != NULL)      fprintf(finfo->fptr, "TrainValid=%s\n", params->validfile);    if (params->mu1 > 0.0)      fprintf(finfo->fptr, "Trainmu1=%.9f\n", params->mu1);    if (params->mu2 > 0.0)      fprintf(finfo->fptr, "Trainmu2=%.9f\n", params->mu2);    if (params->mu3 > 0.0)      fprintf(finfo->fptr, "Trainmu3=%.9f\n", params->mu3);    if (params->mu4 > 0.0)      fprintf(finfo->fptr, "Trainmu4=%.9f\n", params->mu4);    if (params->alphatype != 0)      fprintf(finfo->fptr, "TrainAlphatype=%u\n", params->alphatype);    if (params->batch != 0)      fprintf(finfo->fptr, "TrainBatchmode=1\n");    if (params->momentum != 0)      fprintf(finfo->fptr, "TrainMomentum=1\n");    if (params->super != 0)      fprintf(finfo->fptr, "TrainSuper=%u\n", params->super);    if (params->kernel != 0)      fprintf(finfo->fptr, "TrainKernel=1\n");  }  fprintf(finfo->fptr, "\nmap\n");  /* Write the codebooks */  for (i = 0; i < map->xdim * map->ydim; i++){    if (format)      WriteAscIIVector(map->codes[i].points, map->dim, finfo);    else      WriteBinaryVector(map->codes[i].points, map->dim, finfo);    if (CheckErrors() != 0)      break;    /* Write a codebook's label if available */    if ((label = GetLabel(map->codes[i].label)) != NULL){      clen = strlen(GetLabel(map->codes[i].label));      if (format == 0){  /* In binary mode */	if (fwrite(&clen, sizeof(unsigned), 1, finfo->fptr) != 1){	  AddError("Unable to write data. File system full?");	  break;	}	if (fwrite(label, sizeof(char), clen, finfo->fptr) != clen){	  AddError("Unable to write data. File system full?");	  break;	}      }      else{ /* In AscII mode */	if (fprintf(finfo->fptr, "%s\n", label) != clen){	  AddError("Unable to write data. File system full?");	  break;	}      }    }    else{ /* No label available */      if (format == 0){  /* In binary mode */	clen = 0;	if (fwrite(&clen, sizeof(unsigned), 1, finfo->fptr) != 1){	  AddError("Unable to write data. File system full?");	  break;	}      }      else{  /* In AscII mode */	fputc('\n', finfo->fptr);      }    }  }  CloseFile(finfo);            /* clean up and return */  if (!CheckErrors())          /* If no errors...                */    fprintf(stderr, "%47s\n", "[OK]");  else    fprintf(stderr, "%47s\n", "[FAILED]");  sync();  /* Ensure data is actually written to disk */  return CheckErrors();}/******************************************************************************Description: Save the Self-Organizing Map data to a given file in AscII format.Return value: 0 if no errors, or a value greater than zero if there were errors******************************************************************************/int SaveMapAscII(struct Parameters *params){  if (params->onetfile == NULL){         /* Ensure that there is a file name */    AddError("No file name given to save map.");    return 1;  }  return SaveMapInFormat(params, params->onetfile, 1);}/******************************************************************************Description: Save the Self-Organizing Map data to a given file in the             default binary format.Return value: 0 if no errors, or a value greater than zero if there were errors******************************************************************************/int SaveMap(struct Parameters *params){  if (params->onetfile == NULL){         /* Ensure that there is a file name */    AddError("No file name given to save map.");    return 1;  }  return SaveMapInFormat(params, params->onetfile, 0);}/******************************************************************************Description: Save the snapshot of the Self-Organizing Map data to file in the             default binary format.Return value: 0 if no errors, or a value greater than zero if there were errors******************************************************************************/int SaveSnapShot(struct Parameters *params){  if (params->snap.file == NULL){    /* Ensure that there is a file name */    AddError("No file name given to save snapshot.");    return 1;  }  fputc('\r', stderr);  return SaveMapInFormat(params, params->snap.file, 0);}/* End of file */

⌨️ 快捷键说明

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