📄 data.c
字号:
maxdim = gptr->ldim; } if (mindim != maxdim){ fprintf(stderr, "\nData label component requires padding.\n"); } /* Check if child state vector requires padding */ mindim = MAX_UNSIGNED; maxdim = 0; for (gptr = graphs; gptr != NULL; gptr = gptr->next){ if (mindim > gptr->FanOut) mindim = gptr->FanOut; if (maxdim < gptr->FanOut) maxdim = gptr->FanOut; } if (mindim != maxdim){ fprintf(stderr, "\nData child state vector requires padding.\n"); } /* Check if parent state vector requires padding */ mindim = MAX_UNSIGNED; maxdim = 0; for (gptr = graphs; gptr != NULL; gptr = gptr->next){ if (mindim > gptr->FanIn) mindim = gptr->FanIn; if (maxdim < gptr->FanIn) maxdim = gptr->FanIn; } if (mindim != maxdim){ fprintf(stderr, "\nData parent state vector requires padding.\n"); } /* Check if target label component requires padding */ mindim = MAX_UNSIGNED; maxdim = 0; for (gptr = graphs; gptr != NULL; gptr = gptr->next){ if (mindim > gptr->tdim) mindim = gptr->tdim; if (maxdim < gptr->tdim) maxdim = gptr->tdim; } if (mindim != maxdim){ fprintf(stderr, "\nData target vector requires padding.\n"); } fprintf(stderr, "Padding of training/test/validation data is required but not implemented in module data.c, function Padding(). This may cause a segmentation fault, or may produce wrong or unexpected results.\n");}/******************************************************************************Description: Temporary function until undirected graph file format is supportedReturn value: This function does not return any value.******************************************************************************/void ConvertToUndirectedLinks(struct Graph *train){ struct Graph *gptr; struct Node *node, *child; int nnum, childno, cchildno; int maxFanIn; fprintf(stderr, "Converting all links in dataset to undirected links."); for (gptr = train; gptr != NULL; gptr = gptr->next){ maxFanIn = 0; for (nnum = 0; nnum < gptr->numnodes; nnum++){ node = gptr->nodes[nnum]; for (childno = 0; childno < gptr->FanOut; childno++){ child = node->neighbors[childno]; if (child == NULL) continue; /* Add node to list of child's children */ for (cchildno = 0; cchildno < gptr->FanOut; cchildno++){ if (child->neighbors[cchildno] == node) break; else if (child->neighbors[cchildno] == NULL){ child->neighbors[cchildno] = node; break; } } if (cchildno >= gptr->FanOut){ fprintf(stderr, "\nFanOut too small. Cannot convert to undirected links.\n"); exit(0); } /* Add node's child to list of parents */ // node->numparents += 1; /* add node to list of parents */ // node->parents = MyRealloc(node->parents, node->numparents * sizeof(struct Node*)); // node->parents[node->numparents-1] = child; // if (node->numparents > maxFanIn) // maxFanIn = node->numparents; } } /* This is only good if we were to do contextual stuff if (maxFanIn > gptr->FanIn){ IncreaseDimension(gptr, maxFanIn, PARENTSTATES); gptr->FanIn = maxFanIn; } */ } fprintf(stderr, "%22s\n", "[OK]");}/*****************************************************************************Description: Initializes nodes with vector weight values.Return value: The function does not return a value.*****************************************************************************/void SetWeightValues(FLOAT mu1, FLOAT mu2, FLOAT mu3, FLOAT mu4, struct Graph *gptr){ UNSIGNED i, j; for (; gptr != NULL; gptr = gptr->next){ for (i = 0u; i < gptr->numnodes; i++){ if (gptr->nodes[i]->mu == NULL) gptr->nodes[i]->mu = MyMalloc(gptr->dimension * sizeof(FLOAT)); for (j = 0; j < gptr->ldim; j++) gptr->nodes[i]->mu[j] = mu1; for (; j < gptr->ldim + 2*gptr->FanOut; j++) gptr->nodes[i]->mu[j] = mu2; for (; j < gptr->ldim + 2*gptr->FanOut + 2*gptr->FanIn; j++) gptr->nodes[i]->mu[j] = mu3; for (; j < gptr->dimension; j++) gptr->nodes[i]->mu[j] = mu4; } }}/*****************************************************************************Description: Auxilary function used by qsort to sort nodes according to the node's depth. Return value: The function return -1,+1, or 0 if the depth of the dirt node is greater than, smaller than, or equal to the depth of a given second node.*****************************************************************************/int CompareDepth(const void *node1, const void *node2){ if ((*(struct Node**)node1)->depth > (*(struct Node**)node2)->depth) return 1; else if ((*(struct Node**)node1)->depth < (*(struct Node**)node2)->depth) return -1; else return 0;}/*****************************************************************************Description: Auxilary function used by qsort to obtain randomization of items in an array. Abusing qsort for this task works well for relatively small arrays (i.e. arrays with less than 100000 elements).Return value: The function returns a uniformly distributed signed integers.*****************************************************************************/int Random(const void *node1, const void *node2){ return (int)mrand48();}/*****************************************************************************Description: Sort the nodes in each graph accoring to the depth values in ascending order.Return value: The function does not return a value.*****************************************************************************/void SortNodesByDepth(struct Graph *gptr){ for(; gptr != NULL; gptr = gptr->next) qsort(gptr->nodes, gptr->numnodes, sizeof(struct Node*), CompareDepth);}/*****************************************************************************Description: Randomizes the occurence of nodes in each graph. The connectivity remains unaffected. Hence, this randomization merely affects how nodes are processed during training (i.e. in a random order rather than in a bottom-up order as achieved by calling the function SortNodesByDepth().Return value: The function does not return a value.*****************************************************************************/void RandomizeNodeOrder(struct Graph *gptr){ for(; gptr != NULL; gptr = gptr->next) qsort(gptr->nodes, gptr->numnodes, sizeof(struct Node*), Random);}/*****************************************************************************Description: Randomizes the sequence of a given list of graphs.Return value: Pointer to the new first element of the list of graphs.*****************************************************************************/struct Graph *RandomizeGraphOrder(struct Graph *graph){ unsigned rval; int i, num; struct Graph **garray, *gptr; /* Sanity check */ if (graph == NULL) return graph; /* Initialize an auxilliary array for list elements */ for(gptr = graph, num = 0; gptr != NULL; gptr = gptr->next, num++); garray = (struct Graph **)MyMalloc(num * sizeof(struct Graph *)); for(gptr = graph, num = 0; gptr != NULL; gptr = gptr->next, num++) garray[num] = gptr; /* Randomize order of elements in the array */ for (i = 0; i < num; i++){ rval = (unsigned)(drand48() * num / 1.0); gptr = garray[i]; garray[i] = garray[rval]; garray[rval] = gptr; } graph = garray[0]; /* (Re-)link list elements */ for (i = 0; i < num-1; i++) garray[i]->next = garray[i+1]; garray[i]->next = NULL; free(garray); /* Free memory used by auxillary array */ return graph;}/*****************************************************************************Description: This function is called in VQ mode, it allocates memory to hold the index number of winner neurons.Return value: The function does not return a value.*****************************************************************************/void VQInitWinner(struct Graph *graph){ struct Graph *gptr; UNSIGNED n; for (gptr = graph; gptr != NULL; gptr = gptr->next){ for (n = 0; n < gptr->numnodes; n++){ gptr->nodes[n]->winner = -1; /* Initialize with illegal ID value */ } }}/*****************************************************************************Description: This function ensures that all nodes in the dataset are fully initialized. It also performs padding if nodes are of different dimension, or if the requested training mode requires padding.Return value: The function does not return a value.*****************************************************************************/void PrepareData(struct Parameters *param){ int i; struct Graph *GList[3]; /* Datasets will be prepared in the following order */ GList[0] = param->train; GList[1] = param->valid; GList[2] = param->test; Padding(*param); /* Ensure that all nodes are of the same dimension */ for (i = 0; i < 3; i++){ if (GList[i] == NULL) continue; SetWeightValues(param->mu1, param->mu2, param->mu3, param->mu4, GList[i]); if (param->nodeorder == 1) RandomizeNodeOrder(GList[i]); /* Randomize the order of nodes */ else SortNodesByDepth(GList[i]); /* Ensure bottom up processing of nodes */ if (param->graphorder == 1) param->train = RandomizeGraphOrder(GList[i]); if (param->map.topology == TOPOL_VQ) /* In VQ mode only... */ VQInitWinner(GList[i]); }}/*****************************************************************************Description: Free all memory allocated to the list of graphs starting from the pointer graph.Return value: The function does not return a value.*****************************************************************************/void FreeGraphs(struct Graph *graph){ struct Graph *gptr, *prev; struct Node *node; UNSIGNED n; for (gptr = graph; gptr != NULL; ){ if (gptr->gname != NULL) free(gptr->gname); if (gptr->nodes != NULL){ for (n = 0; n < gptr->numnodes; n++){ node = gptr->nodes[n]; if (node == NULL) continue; if (node->points != NULL) free(node->points); if (node->parents != NULL) free(node->parents); if (node->children != NULL) free(node->children); free(node); } free(gptr->nodes); } prev = gptr; gptr = gptr->next; memset(prev, 0, sizeof(struct Graph)); /* Reset the graph */ free(prev); }}/*****************************************************************************Description: Free all memory allocated by the map.Return value: The function does not return a value.*****************************************************************************/void FreeMap(struct Map *map){ UNSIGNED i, noc; if (map->codes != NULL){ noc = map->xdim * map->ydim; for (i = 0; i < noc; i++) if (map->codes[i].points != NULL) free(map->codes[i].points); } memset(map, 0, sizeof(struct Map)); /* Reset the map */}/******************************************************************************Description: Free the memory allocated for the Parameters structureReturn value: This function does not return a value.******************************************************************************/void ClearParameters(struct Parameters* parameters){ if (parameters == NULL) return; if (parameters->inetfile) free(parameters->inetfile); if (parameters->datafile) free(parameters->datafile); if (parameters->validfile) free(parameters->validfile); if (parameters->onetfile) free(parameters->onetfile); if (parameters->snap.command) free(parameters->snap.command); if (parameters->snap.file) free(parameters->snap.file); FreeGraphs(parameters->train); FreeGraphs(parameters->valid); FreeGraphs(parameters->test); FreeMap(¶meters->map); memset(parameters, 0, sizeof(struct Parameters));}/******************************************************************************Description: Free all dynamically allocated memory, and flush error messages.Return value: This function does not return a value.******************************************************************************/void Cleanup(struct Parameters* parameters){ ClearParameters(parameters); /* Free memory used by the parameter struct */ ClearLabels(); /* Clear all data labels */ PrintErrors(); /* Flush all error messages */}/* End of file */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -