📄 testsomnew.c
字号:
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++; } } 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: 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"); } }}/******************************************************************************Description: Return value: ******************************************************************************/void MapGraph(struct Parameters parameters, int x, int y){ int n; FLOAT qerr; struct Graph *graph; struct Node *node; struct Map *map; struct Winner winner; struct Hits{ int gnum; int nnum; FLOAT err; } **hits; if (parameters.train == NULL) return; map = ¶meters.map; /* Find the winners for all nodes */ if (KstepEnabled) qerr = K_Step_Approximation(map, parameters.train, 1); else qerr = GetNodeCoordinates(map, parameters.train); fprintf(stderr, "Qerror:%f\n", qerr); if (x >= 0 && y >= 0){ for (graph = parameters.train; graph != NULL; graph = graph->next){ for (n = 0; n < graph->numnodes; n++){ node = graph->nodes[n]; if (x == node->x && y == node->y){ printf("%d %d %d %d\n", x, y, graph->gnum, node->nnum); fprintf(stderr, "%s\n", graph->gname); } } } } else{ hits = (struct Hits**)MyMalloc(map->ydim * sizeof(struct Hits*)); for (y = 0; y < map->ydim; y++) hits[y] = (struct Hits*)MyCalloc(map->xdim, sizeof(struct Hits)); for (y = 0; y < map->ydim; y++) for (x = 0; x < map->xdim; x++){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -