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

📄 testsom.c

📁 SOM-sd 是现在国外非常流行的一个神经网络的结构自组织特征映射算法
💻 C
📖 第 1 页 / 共 5 页
字号:
      fprintf(ofile, "4 0 0 50 0 0 %d 0.0000 4 105 645 ", fontsize);      fprintf(ofile, "%d %d %s\\001\n", xynode->x*20 + 200+30, xynode->y*20-200+spacing, buffer);      */      /* Print node and its links */      fprintf(ofile, "1 4 0 2 0 7 50 0 -1 0.000 1 0.0000 ");      fprintf(ofile, "%d %d %d %d %d %d %d %d\n", xynode->x, xynode->y, (int)scale, (int)scale, xynode->x-201, xynode->y, xynode->x+201, xynode->y);      for (c = 0; c < gptr->FanOut && node->children[c] != NULL; c++){	cd = d-1;	for(cn = 0; gmatrix[cd][cn].node != NULL && gmatrix[cd][cn].node != node->children[c] && cn < width; cn++);	if (gmatrix[cd][cn].node == NULL){	  fprintf(stderr, "Child not found %d,%d\n", node->nnum, node->depth);	  continue;	}	child = &gmatrix[cd][cn];	fprintf(ofile, "2 1 0 2 0 7 50 0 -1 0.000 0 0 -1 1 0 2\n");	fprintf(ofile, "0 0 1.00 60.00 120.00\n"); //if arrow mode is on      	fprintf(ofile, "%d %d %d %d\n", xynode->x, xynode->y+(int)scale, child->x, child->y-(int)scale);      }    }  }  for (i = 0; i < height; i++)    free(gmatrix[i]);  free(gmatrix);}//Compute activation of every node of the map, and the maximum activation of any node on the map. Result is stored in vmap.activation[y][x], and vmap.maxstruct VMap GetHits(int xdim, int ydim, struct Graph *graph, int mode){  int n, N, nN, hits, x, y;  struct VMap vmap = {0};  struct Graph *gptr;  struct Node *node;  fprintf(stderr, "t3\n");  vmap.activation = (UNSIGNED**)MyMalloc(ydim * sizeof(UNSIGNED*));  for (n = 0; n < ydim; n++)    vmap.activation[n] = (UNSIGNED*)MyCalloc(xdim, sizeof(FLOAT));  hits = 0;  vmap.max = 0;  N = 0;  nN = 0;  for(gptr = graph; gptr != NULL; gptr = gptr->next){    nN += gptr->numnodes;    for (n = 0; n < gptr->numnodes; n++){      node = gptr->nodes[n];      if (mode & ROOT && IsRoot(node)){	N++;	vmap.activation[node->y][node->x] += 1;	if (vmap.activation[node->y][node->x] > vmap.max)	  vmap.max = vmap.activation[node->y][node->x];	continue;      }      else if (mode & LEAF && IsLeaf(node)){	vmap.activation[node->y][node->x] += 1;	if (vmap.activation[node->y][node->x] > vmap.max)	  vmap.max = vmap.activation[node->y][node->x];	continue;      }      else if (mode & INTERMEDIATE && IsIntermediate(node)){	vmap.activation[node->y][node->x] += 1;	if (vmap.activation[node->y][node->x] > vmap.max)	  vmap.max = vmap.activation[node->y][node->x];	continue;      }    }  }  for (y = 0; y < ydim; y++){    for (x = 0; x < xdim; x++){      if (vmap.activation[y][x] != 0)	hits++;    }  }  if (!(mode & QUIET)){    fprintf(stdout, "Neurons activated: %d\n", hits);    fprintf(stdout, "Compression ratio: %f (root nodes only)\n",(float)N/hits);    fflush(stdout);  }  return vmap;}//plot "x" u 1:2:3:4 w e, "x" u 1:2 w l lt 2, "x" u 1:3 w l lt 2, "x" u 1:4 w l lt 2//plot "x" u 1:5:6:7 w e, "x" u 1:5 w l lt 2, "x" u 1:6 w l lt 2, "x" u 1:7 w l lt 2//plot "x" u 1:8:9:10 w e, "x" u 1:8 w l lt 2, "x" u 1:9 w l lt 2, "x" u 1:10 w l lt 2//plot "x" u 1:11:12:13 w e, "x" u 1:11 w l lt 2, "x" u 1:12 w l lt 2, "x" u 1:13 w l lt 2void GetClusterID(struct Map map, struct Graph *graph, struct VMap *vmap){  UNSIGNED *frequency;  int numlabels;  int n, max, id, x, y;  struct Node *node;  struct Graph *gptr;  int xdim, ydim;  int noactive = 0;  xdim = map.xdim;  ydim = map.ydim;  vmap->winnerclass = (UNSIGNED**)MyMalloc(ydim * sizeof(UNSIGNED*));  vmap->classes = (UNSIGNED***)MyMalloc(ydim * sizeof(UNSIGNED**));  for (n = 0; n < ydim; n++){    vmap->winnerclass[n] = (UNSIGNED*)MyCalloc(xdim, sizeof(UNSIGNED));    vmap->classes[n] = (UNSIGNED**)MyCalloc(xdim, sizeof(UNSIGNED*));  }  if (graph == NULL)    return;  numlabels = GetNumLabels();  vmap->numclasses = numlabels;  for (y = 0; y < ydim; y++){    for (x = 0; x < xdim; x++){      if (vmap->activation[y][x] == 0)	continue;      frequency = MyCalloc(numlabels, sizeof(UNSIGNED));      for(gptr = graph; gptr != NULL; gptr = gptr->next){	for (n = 0; n < gptr->numnodes; n++){	  node = gptr->nodes[n];	  if (node->x == x && node->y == y)	    if (GetLabel(node->label) != NULL)	      	      if (strcmp(GetLabel(node->label), "*"))		frequency[node->label-1]++;	  	}      }      id = -1;      max = 0;      for (n = 0; n < numlabels; n++){	if (frequency[n] > max){	  max = frequency[n];	  id = n;	}      }      if (id >= 0){	vmap->winnerclass[y][x] = id+1;	vmap->classes[y][x] = frequency;      }      else{	noactive++;	if (noactive < 10){	  for (n = 1; n <= numlabels; n++)	    fprintf(stderr, "%s->%d ", GetLabel(n), frequency[n-1]);	  fprintf(stderr, "\n");	}	free(frequency);      }    }  }  if (noactive)    fprintf(stderr, "There were %d activated neurons without label\n", noactive);}/*****************************************************************************Description:Return value:*****************************************************************************/void MapNodeLabel(struct Map map, struct Graph *graph){  UNSIGNED n, cnt;  UNSIGNED x, y;  UNSIGNED *flags;  struct Node *node;  struct Graph *gptr;  if (graph == NULL)    return;  if (GetNumLabels() < 1)    return;  flags = (UNSIGNED*)MyMalloc(GetNumLabels() * sizeof(UNSIGNED));  /* initialize node location */  if (KstepEnabled)    K_Step_Approximation(&map, graph, 1);  else    GetNodeCoordinates(&map, graph);  cnt = 0;  for(gptr = graph; gptr != NULL; gptr = gptr->next)    for (n = 0; n < gptr->numnodes; n++)      if (IsRoot(gptr->nodes[n]))	cnt++;  fprintf(stderr, "%d roots\n", cnt);  for (y = 0; y < map.ydim; y++){    for (x = 0; x < map.xdim; x++){      cnt = 0;      memset(flags, 0, GetNumLabels() * sizeof(UNSIGNED));      for(gptr = graph; gptr != NULL; gptr = gptr->next){	for (n = 0; n < gptr->numnodes; n++){	  node = gptr->nodes[n];	  if (node->x == x && node->y == y && IsRoot(node) && GetLabel(node->label) != NULL){	    flags[node->label-1]++;	    cnt = 1;	  }	}      }      if (cnt > 0){	printf("%d %d", x, y);	for (n = 0; n < GetNumLabels(); n++){	  printf(" %d", flags[n]);	}	printf("\n");      }    }  }  free(flags);}/*****************************************************************************Description:Return value:*****************************************************************************/void MapNode(struct Map map, struct Graph *graph){  UNSIGNED n;  UNSIGNED x, y;  struct Node *node;  struct Graph *gptr;  if (graph == NULL)    return;  if (GetNumLabels() < 1)    return;  /* initialize node location */  if (KstepEnabled)    K_Step_Approximation(&map, graph, 1);  else    GetNodeCoordinates(&map, graph);  for (y = 0; y < map.ydim; y++){    for (x = 0; x < map.xdim; x++){      for(gptr = graph; gptr != NULL; gptr = gptr->next){	for (n = 0; n < gptr->numnodes; n++){	  node = gptr->nodes[n];	  if (node->x == x && node->y == y){	    fprintf(stdout, "%d %d: %d %d\n", x, y, gptr->gnum, node->nnum);	  }	}      }    }  }}/*****************************************************************************Description: In multi-layer contextual modeReturn value:*****************************************************************************/void ClassifyAndWriteDatafileWithParents(struct Map map, struct Graph *gptr){  UNSIGNED i, n;  UNSIGNED ldim, tdim, FanIn, FanOut;  UNSIGNED soff, coff, poff;  struct Node *node;  if (gptr == NULL)    return;  /* initialize node location */  if (KstepEnabled)    K_Step_Approximation(&map, gptr, 1);  else    GetNodeCoordinates(&map, gptr);  ldim = INT_MAX;  /* Initialize graph properties with illegal values to  */  tdim = INT_MAX;  /* enforce the writing of a data header for the first  */  FanIn = INT_MAX; /* graph. Any successife graph will have an own header */  FanOut = INT_MAX;/* if a property differs from an earlier graph.        */  printf("format=nodenumber,nodelabel,childstate,parentstate,target,depth,links,label\n");  for(; gptr != NULL; gptr = gptr->next){    /* Compute max indegree of graph */    for (n = 0; n < gptr->numnodes; n++){      if (gptr->FanIn < gptr->nodes[n]->numparents)	gptr->FanIn = gptr->nodes[n]->numparents;    }    if (ldim != gptr->ldim){      ldim = gptr->ldim;      printf("dim_label=%d\n", ldim);    }    if (tdim != gptr->tdim){      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)      printf("graph:%s\n", gptr->gname);    else      printf("graph\n");    for (n = 0; n < gptr->numnodes; n++){      node = gptr->nodes[n];      soff = gptr->ldim + 2*gptr->FanOut;      poff = soff + 2*node->numparents;      coff = poff + gptr->tdim;      printf("%u ", node->nnum);  /* Print node number */      for (i = 0; i < gptr->ldim; i++){	if (node->points[i] == (int)node->points[i])	  printf("%3d ", (int)node->points[i]); /* Print node label   */	else	  printf("%f ", node->points[i]); /* Print node label   */      }      for (; i < soff; i++){	if (node->points[i] == (int)node->points[i])	  printf("%3d ", (int)node->points[i]); /* Print child state  */	else	  printf("%f ", node->points[i]); /* Print child state  */      }      for (i = 0; i < gptr->FanIn; i++){	if (i < node->numparents)       /* Print parent state */	  printf("%3d %3d ", node->parents[i]->x, node->parents[i]->y);	else	  printf(" -1  -1 ");      }      for (i = poff; i < coff; i++)	printf("%f ", node->points[i]); /* Print target vector */      printf("%u ", node->depth);       /* Print node depth */            for (i = 0; i < gptr->FanOut; i++){  /* Print links */	if (node->children[i] == NULL)	  printf("- ");	else	  printf("%u ", node->children[i]->nnum);      }      if (GetLabel(node->label) != NULL) /* Print node label */	printf("%s\n", GetLabel(node->label));      else	printf("\n");    }  }}/*****************************************************************************Description: In single-layer contextual modeReturn value:*****************************************************************************/void ClassifyAndWriteDatafile(struct Map map, struct Graph *gptr){  UNSIGNED i, n;  UNSIGNED ldim, tdim, FanIn, FanOut;  UNSIGNED soff, coff, poff;  struct Node *node;  if (gptr == NULL)    return;  /* initialize node location */  if (KstepEnabled)    K_Step_Approximation(&map, gptr, 1);  else    GetNodeCoordinates(&map, gptr);  ldim = INT_MAX;  /* Initialize graph properties with illegal values to  */  tdim = INT_MAX;  /* enforce the writing of a data header for the first  */  FanIn = INT_MAX; /* graph. Any successife graph will have an own header */  FanOut = INT_MAX;/* if a property differs from an earlier graph.        */  printf("format=nodenumber,nodelabel,childstate,parentstate,target,depth,links,label\n");  for(; gptr != NULL; gptr = gptr->next){    /* Compute max indegree of graph */    for (n = 0; n < gptr->numnodes; n++){      if (gptr->FanIn < gptr->nodes[n]->numparents)	gptr->FanIn = gptr->nodes[n]->numparents;    }    if (ldim != gptr->ldim){      ldim = gptr->ldim;      printf("dim_label=%d\n", ldim);    }    if (tdim != gptr->tdim){      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)      printf("graph:%s\n", gptr->gname);    else      printf("graph\n");    for (n = 0; n < gptr->numnodes; n++){      node = gptr->nodes[n];      soff = gptr->ldim + 2*gptr->FanOut;      poff = soff + 2*node->numparents;      coff = poff + gptr->tdim;      printf("%u ", node->nnum);  /* Print node number */      for (i = 0; i < gptr->ldim; i++){	if (node->points[i] == (int)node->points[i])	  printf("%3d ", (int)node->points[i]); /* Print node label   */	else	  printf("%f ", node->points[i]); /* Print node label   */      }      for (; i < soff; i++){	if (node->points[i] == (int)node->points[i])	  printf("%3d ", (int)node->points[i]); /* Print child state  */	else	  printf("%f ", node->points[i]); /* Print child state  */      }      for (i = 0; i < gptr->FanIn; i++){	if (i < node->numparents)       /* Print parent state */	  printf("%3d %3d ", node->parents[i]->x, node->parents[i]->y);	else	  printf(" -1  -1 ");      }      for (i = poff; i < coff; i++)	printf("%f ", node->points[i]); /* Print target vector */      printf("%u ", node->depth);       /* Print node depth */            for (i = 0; i < gptr->FanOut; i++){  /* Print links */	if (node->children[i] == NULL)	  printf("- ");	else	  printf("%u ", node->children[i]->nnum);      }      if (GetLabel(node->label) != NULL) /* Print node label */	printf("%s\n", GetLabel(node->label));      else	printf("\n");    }  }}/******************************************************************************

⌨️ 快捷键说明

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