📄 nn_util.c
字号:
/********************************************************************** NN_train_batch_systematic() synopsis: Train network using back-propagation. parameters: network_t *network int num_epochs return: none last updated: 25 Feb 2002 **********************************************************************/void NN_train_batch_systematic(network_t *network, const int num_epochs) { int i, n; for (i=0; i<num_epochs; i++) { for (n=0; n<num_train_data; n++) { NN_simulate_batch(network, train_data[n], train_property[n]); } NN_backpropagate(network); NN_adjust_weights_momentum(network); } return; }/********************************************************************** NN_test() synopsis: Test network. parameters: network_t *network float *trainerror float *testerror return: none last updated: **********************************************************************/void NN_test(network_t *network, float *trainerror, float *testerror) { int item; *trainerror = 0; for (item=0; item<num_train_data; item++) { NN_simulate(network, train_data[item], train_property[item]); *trainerror += network->error; } *trainerror /= num_train_data; *testerror = 0; for (item=0; item<num_test_data; item++) { NN_simulate(network, test_data[item], test_property[item]); *testerror += network->error; } *testerror /= num_test_data; return; }/********************************************************************** NN_evaluate() synopsis: Evaluate network and write result. parameters: network_t *network return: none last updated: **********************************************************************/void NN_evaluate(network_t *network) { int i; /* Loop variable over output neurons. */ int item; /* Loop variable over evaluation data. */ float *output; /* Output results. */ float evalerror=0; /* Network's output error. */ output = (float *) s_malloc(network->layer[network->num_layers-1].neurons*sizeof(float)); printf("\n\nItem Field Actual Prediction\n\n"); for (item=0; item<num_eval_data; item++) { NN_simulate_with_output(network, eval_data[item], eval_property[item], output); evalerror += network->error; printf("%4d 0 %0.4f %0.4f\n", item, eval_property[item][0], output[0]); for (i=1; i<network->layer[network->num_layers-1].neurons; i++) { printf(" %3d %0.4f %0.4f\n", i, eval_property[item][i], output[i]); } } evalerror /= num_eval_data; printf("Error is %f on evaluation set.\n", evalerror); s_free(output); return; }/********************************************************************** NN_predict() synopsis: Use network to make predictions and write results. parameters: network_t *network return: none last updated: 24 Jan 2002 **********************************************************************/void NN_predict(network_t *network) { int i; /* Loop variable over output neurons. */ int item; /* Loop variable over evaluation data. */ float *output; /* Output results. */ output = (float *) s_malloc(network->layer[network->num_layers-1].neurons*sizeof(float)); printf("\n\nItem Field Prediction\n\n"); for (item=0; item<num_eval_data; item++) { NN_run(network, eval_data[item], output); printf("%4d 0 %0.4f\n", item, output[0]); for (i=1; i<network->layer[network->num_layers-1].neurons; i++) { printf(" %3d %0.4f\n", i, output[i]); } } s_free(output); return; }/********************************************************************** NN_define_train_data() synopsis: Define training data. parameters: return: last updated: 04 Feb 2002 **********************************************************************/void NN_define_train_data(int ndata, float **data, float **prop) { train_data=data; /* Input data for training. */ num_train_data=ndata; /* Number of training target items. */ max_train_data=ndata; /* Maximum number of training target items. */ train_property=prop; /* Training target property. */ num_train_prop=ndata; /* Number of training target properties. */ train_labels=NULL; /* Labels for training data. */ return; }/********************************************************************** NN_define_test_data() synopsis: Define testing data. parameters: return: last updated: 04 Feb 2002 **********************************************************************/void NN_define_test_data(int ndata, float **data, float **prop) { test_data=data; /* Input data for testing. */ num_test_data=ndata; /* Number of testing target items. */ max_test_data=ndata; /* Maximum number of testing target items. */ test_property=prop; /* Testing target property. */ num_test_prop=ndata; /* Number of testing target properties. */ test_labels=NULL; /* Labels for testing data. */ return; }/********************************************************************** NN_define_eval_data() synopsis: Define evaluation data. parameters: return: last updated: 04 Feb 2002 **********************************************************************/void NN_define_eval_data(int ndata, float **data, float **prop) { eval_data=data; /* Input data for evaluation. */ num_eval_data=ndata; /* Number of evaluation target items. */ max_eval_data=ndata; /* Maximum number of evaluation target items. */ eval_property=prop; /* Evaluation target property. */ num_eval_prop=ndata; /* Number of evaluation target properties. */ eval_labels=NULL; /* Labels for evaluation data. */ return; }/********************************************************************** NN_define_predict_data() synopsis: Define prediction data. parameters: return: last updated: 04 Feb 2002 **********************************************************************/void NN_define_predict_data(int ndata, float **data) { predict_data=data; /* Input data for prediction. */ num_predict_data=ndata; /* Number of prediction target items. */ max_predict_data=ndata; /* Maximum number of prediction target items. */ predict_labels=NULL; /* Labels for prediction data. */ return; }/********************************************************************** NN_read_fingerprint_binary_header() synopsis: Read binary fingerprint info from given filehandle. Designed for future expansion rather than current utility. parameters: filehandle return: int size last updated: 28 Nov 2001 **********************************************************************/int NN_read_fingerprint_binary_header(FILE *fp) { char *fmt_str="FORMAT FP: 001\n"; char fmt_str_in[16]; int size; fread(fmt_str_in, sizeof(char), strlen(fmt_str), fp); if (strncmp(fmt_str, fmt_str_in, strlen(fmt_str))) die("Invalid fingerprint header"); fread(&size, sizeof(int), 1, fp); return size; }/********************************************************************** NN_read_data() synopsis: Read binary fingerprint with label from given file. parameters: filename data return: int size last updated: 3 Dec 2001 **********************************************************************/int NN_read_data(char *fname, float ***data, char ***labels, int *num_data, int *max_data) { FILE *fp; /* Filehandle. */ int label_len; /* Label length. */ int size; /* Dimensions of fingerprint. */ if ( !(fp = fopen(fname, "r")) ) dief("Unable to open file \"%s\" for input.\n", fname); size = NN_read_fingerprint_binary_header(fp); /* Check validity of file. */ while (fread(&label_len, sizeof(int), 1, fp) != 0) { if (*num_data == *max_data) { *max_data += NN_DATA_ALLOC_SIZE; *data = (float **) s_realloc(*data, sizeof(float *) * *max_data); *labels = (char **) s_realloc(*labels, sizeof(char *) * *max_data); } (*labels)[*num_data] = (char *) s_malloc(sizeof(char)*label_len+1); fread((*labels)[*num_data], sizeof(char), label_len, fp); (*labels)[*num_data][label_len] = '\0'; (*data)[*num_data] = (float *) s_malloc(sizeof(float)*size); fread((*data)[*num_data], sizeof(float), size, fp); (*num_data)++; } fclose(fp); return size; }/********************************************************************** nn_nreadline(FILE *fp, int len, char *dest) synopsis: Reads upto newline/eof from specified stream, to a maximum of len characters, also ensures that the string is always null-terminated. parameters: char *dest The destination string. FILE *fp The input stream. return: int actual number of characters read. -1 on failure. last updated: 08 Jan 2003 **********************************************************************/static int nn_nreadline(FILE *fp, const int len, char *dest) { int count=0, max_count; /* Number of chars read */ int c; /* Current character */ if (!fp) die("Null file handle passed.\n"); if (len < 1) die("Stupid length.\n"); if (!dest) die("Null string pointer passed.\n"); max_count = len-1;/* while((!feof(fp)) && (c=fgetc(fp)) && (c!='\n') && count<len)*/ while(count<max_count && (c=fgetc(fp))!=EOF && ((char)c!='\n')) dest[count++]=(char)c; dest[count]='\0'; return count-1; }/********************************************************************** NN_read_prop() synopsis: Read properties from given file. parameters: char *fname File to read. float ***data Data. char ***labels Data labels. int *num_prop Number of data items read in total. int *num_data Number of data items expected in total. int *dimensions Dimensions of data, or -1 to determine here. return: none. last updated: 18 Jul 2002 **********************************************************************/void NN_read_prop(char *fname, float ***data, char ***labels, int *num_prop, int *num_data, int *dimensions) { FILE *fp; /* Filehandle. */ char line_buffer[MAX_LINE_LEN]; /* Line buffer. */ char *line; /* Line pointer. */ int data_count; /* Number of fields input from current record. */ if ( !(fp = fopen(fname, "r")) ) dief("Unable to open file \"%s\" for input.\n", fname); *data = (float **) s_realloc(*data, sizeof(float*)*(*num_data));/* Count data items on first line, if necessary. */ if (*dimensions == -1) { char line_copy[MAX_LINE_LEN]; /* Line buffer copy. */ if (nn_nreadline(fp, MAX_LINE_LEN, line_buffer)<=0) dief("Error reading file \"%s\".\n", fname); strcpy(line_copy, line_buffer); line = line_copy; if (strncmp((*labels)[*num_prop], line, strlen((*labels)[*num_prop]))!=0) dief("Label mismatch \"%s\" to \"%s\"", (*labels)[*num_prop], line); line = strtok(&(line[strlen((*labels)[*num_prop])]), " "); *dimensions=1; while ( (line = strtok(NULL, " "))!=NULL ) { (*dimensions)++; } line = line_buffer; if (strncmp((*labels)[*num_prop], line, strlen((*labels)[*num_prop]))!=0) dief("Label mismatch \"%s\" to \"%s\"", (*labels)[*num_prop], line); (*data)[*num_prop] = (float *) s_malloc((*dimensions)*sizeof(float)); line = strtok(&(line[strlen((*labels)[*num_prop])]), " "); (*data)[*num_prop][0] = (float) atof(line); data_count=1; while ( (line = strtok(NULL, " "))!=NULL ) { if (data_count==*dimensions) die("Internal error which should never occur."); (*data)[*num_prop][data_count] = (float) atof(line); data_count++; } (*num_prop)++; }/* Read remainder of file. */ while (nn_nreadline(fp, MAX_LINE_LEN, line_buffer)>0) { if (*num_prop > *num_data) die("Too many property records input."); line = line_buffer; if (strncmp((*labels)[*num_prop], line, strlen((*labels)[*num_prop]))!=0) dief("Label mismatch \"%s\" to \"%s\"", (*labels)[*num_prop], line); (*data)[*num_prop] = (float *) s_malloc((*dimensions)*sizeof(float)); line = strtok(&(line[strlen((*labels)[*num_prop])]), " "); (*data)[*num_prop][0] = (float) atof(line); data_count=1; while ( (line = strtok(NULL, " "))!=NULL ) { if (data_count==*dimensions) die("Too many data items."); (*data)[*num_prop][data_count] = (float) atof(line); data_count++; } (*num_prop)++; /* Simple check. */ if (data_count!=*dimensions) { dief("Too few data items (%d instead of %d) for item %d.", data_count, *dimensions, *num_prop); } } fclose(fp); return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -