📄 nn_util.c
字号:
NN_write() synopsis: Write a network_t structure and its contents to disk in a binary format. parameters: network_t *network const char *fname return: none last updated: 29 Apr 2002 **********************************************************************/void NN_write(network_t *network, const char *fname) { FILE *fp; /* File handle. */ char *fmt_str="FORMAT NN: 002\n"; /* File identifier tag. */ int l; /* Layer index. */ int i; /* Neuron index. */ if ( !(fp = fopen(fname, "w")) ) dief("Unable to open file \"%s\" for output.\n", fname); fwrite(fmt_str, sizeof(char), strlen(fmt_str), fp); fwrite(&(network->momentum), sizeof(float), 1, fp); fwrite(&(network->gain), sizeof(float), 1, fp); fwrite(&(network->rate), sizeof(float), 1, fp); fwrite(&(network->bias), sizeof(float), 1, fp); fwrite(&(network->num_layers), sizeof(int), 1, fp); for (l=0;l<network->num_layers; l++) { fwrite(&(network->layer[l].neurons), sizeof(int), 1, fp); } for (l=1; l<network->num_layers; l++) { for (i=1; i<=network->layer[l].neurons; i++) { fwrite(network->layer[l].weight[i], sizeof(float), network->layer[l-1].neurons+1, fp); } } fclose(fp); return; }/********************************************************************** NN_read_compat() synopsis: Read (and allocate) a network_t structure and its contents from a binary format file on disk. Version for backwards compatiability. parameters: const char *fname return: network_t *network last updated: 30 Nov 2001 **********************************************************************/network_t *NN_read_compat(const char *fname) { FILE *fp; /* File handle. */ char *fmt_str="FORMAT NN: 001\n"; /* File identifier tag. */ char *fmt_str_in=" "; /* File identifier tag. */ network_t *network; /* The new network. */ int l; /* Layer index. */ int i; /* Neuron index. */ if ( !(fp = fopen(fname, "r")) ) dief("Unable to open file \"%s\" for input.\n", fname); fread(fmt_str_in, sizeof(char), strlen(fmt_str), fp); if (strncmp(fmt_str, fmt_str_in, strlen(fmt_str))) die("Invalid neural network file header") network = (network_t*) s_malloc(sizeof(network_t)); fread(&(network->momentum), sizeof(float), 1, fp); fread(&(network->gain), sizeof(float), 1, fp); fread(&(network->rate), sizeof(float), 1, fp); fread(&(network->bias), sizeof(float), 1, fp); fread(&(network->num_layers), sizeof(int), 1, fp); network->layer = (layer_t*) s_malloc(network->num_layers*sizeof(layer_t)); fread(&(network->layer[0].neurons), sizeof(int), 1, fp); network->layer[0].output = (float*) s_calloc(network->layer[0].neurons+1, sizeof(float)); network->layer[0].error = (float*) s_calloc(network->layer[0].neurons+1, sizeof(float)); network->layer[0].weight = NULL; network->layer[0].weight_save = NULL; network->layer[0].weight_change = NULL; network->layer[0].output[0] = network->bias; for (l=1; l<network->num_layers; l++) { fread(&(network->layer[l].neurons), sizeof(float), 1, fp); network->layer[l].output = (float*) s_calloc(network->layer[l].neurons+1, sizeof(float)); network->layer[l].error = (float*) s_calloc(network->layer[l].neurons+1, sizeof(float)); network->layer[l].weight = (float**) s_calloc(network->layer[l].neurons+1, sizeof(float*)); network->layer[l].weight_save = (float**) s_calloc(network->layer[l].neurons+1, sizeof(float*)); network->layer[l].weight_change = (float**) s_calloc(network->layer[l].neurons+1, sizeof(float*)); network->layer[l].output[0] = network->bias; for (i=1; i<=network->layer[l].neurons; i++) { network->layer[l].weight[i] = (float*) s_calloc(network->layer[l-1].neurons+1, sizeof(float)); fread(network->layer[l].weight[i], sizeof(float), network->layer[l-1].neurons, fp); network->layer[l].weight_save[i] = (float*) s_calloc(network->layer[l-1].neurons+1, sizeof(float)); network->layer[l].weight_change[i] = (float*) s_calloc(network->layer[l-1].neurons+1, sizeof(float)); } } fclose(fp); return network; }/********************************************************************** NN_read() synopsis: Read (and allocate) a network_t structure and its contents from a binary format file on disk. parameters: const char *fname return: network_t *network last updated: 29 Apr 2002 **********************************************************************/network_t *NN_read(const char *fname) { FILE *fp; /* File handle. */ char *fmt_str="FORMAT NN: 002\n"; /* File identifier tag. */ char *fmt_str_in=" "; /* File identifier tag. */ network_t *network; /* The new network. */ int l; /* Layer index. */ int i; /* Neuron index. */ if ( !(fp = fopen(fname, "r")) ) dief("Unable to open file \"%s\" for input.\n", fname); fread(fmt_str_in, sizeof(char), strlen(fmt_str), fp); if (strncmp(fmt_str, fmt_str_in, strlen(fmt_str))) { return NN_read_compat(fname); } network = (network_t*) s_malloc(sizeof(network_t)); fread(&(network->momentum), sizeof(float), 1, fp); fread(&(network->gain), sizeof(float), 1, fp); fread(&(network->rate), sizeof(float), 1, fp); fread(&(network->bias), sizeof(float), 1, fp); fread(&(network->decay), sizeof(float), 1, fp); fread(&(network->num_layers), sizeof(int), 1, fp); network->layer = (layer_t*) s_malloc(network->num_layers*sizeof(layer_t)); fread(&(network->layer[0].neurons), sizeof(int), 1, fp); network->layer[0].output = (float*) s_calloc(network->layer[0].neurons+1, sizeof(float)); network->layer[0].error = (float*) s_calloc(network->layer[0].neurons+1, sizeof(float)); network->layer[0].weight = NULL; network->layer[0].weight_save = NULL; network->layer[0].weight_change = NULL; for (l=1; l<network->num_layers; l++) { fread(&(network->layer[l].neurons), sizeof(float), 1, fp); network->layer[l].output = (float*) s_calloc(network->layer[l].neurons+1, sizeof(float)); network->layer[l].error = (float*) s_calloc(network->layer[l].neurons+1, sizeof(float)); network->layer[l].weight = (float**) s_calloc(network->layer[l].neurons+1, sizeof(float*)); network->layer[l].weight_save = (float**) s_calloc(network->layer[l].neurons+1, sizeof(float*)); network->layer[l].weight_change = (float**) s_calloc(network->layer[l].neurons+1, sizeof(float*)); network->layer[l].output[0] = network->bias; for (i=1; i<=network->layer[l].neurons; i++) { network->layer[l].weight[i] = (float*) s_calloc(network->layer[l-1].neurons+1, sizeof(float)); fread(network->layer[l].weight[i], sizeof(float), network->layer[l-1].neurons+1, fp); network->layer[l].weight_save[i] = (float*) s_calloc(network->layer[l-1].neurons+1, sizeof(float)); network->layer[l].weight_change[i] = (float*) s_calloc(network->layer[l-1].neurons+1, sizeof(float)); } } fclose(fp); return network; }/********************************************************************** NN_destroy() synopsis: Deallocate a network_t structure and its contents. parameters: network_t *network return: none last updated: 29 Nov 2001 **********************************************************************/void NN_destroy(network_t *network) { int l,i; for (l=0; l<network->num_layers; l++) { if (l != 0) { for (i=1; i<=network->layer[l].neurons; i++) { s_free(network->layer[l].weight[i]); s_free(network->layer[l].weight_save[i]); s_free(network->layer[l].weight_change[i]); } s_free(network->layer[l].output); s_free(network->layer[l].error); s_free(network->layer[l].weight); s_free(network->layer[l].weight_save); s_free(network->layer[l].weight_change); } } s_free(network->layer); s_free(network); return; }/********************************************************************** NN_set_all_weights() synopsis: Sets of of the weights of all neurons in a network to a given value. parameters: network_t *network const float weight return: none last updated: 29 Nov 2001 **********************************************************************/void NN_set_all_weights(network_t *network, const float weight) { int l,i,j; for (l=1; l<network->num_layers; l++) { for (i=1; i<=network->layer[l].neurons; i++) { for (j=0; j<=network->layer[l-1].neurons; j++) { network->layer[l].weight[i][j] = weight; } } } return; }/********************************************************************** NN_randomize_weights() synopsis: Randomize the weights of all neurons in a network. Random values selected from a linear distribution between the passed values. parameters: network_t *network return: none last updated: 22 Jul 2002 **********************************************************************/void NN_randomize_weights(network_t *network, const float lower, const float upper) { int l,i,j; for (l=1; l<network->num_layers; l++) { for (i=1; i<=network->layer[l].neurons; i++) { for (j=0; j<=network->layer[l-1].neurons; j++) { network->layer[l].weight[i][j] = random_float_range(lower, upper); } } } return; }/********************************************************************** NN_randomize_weights_11() synopsis: Randomize the weights of all neurons in a network. Random values selected from a linear distribution between -1.0 and 1.0 parameters: network_t *network return: none last updated: 29 Nov 2001 **********************************************************************/void NN_randomize_weights_11(network_t *network) { int l,i,j; for (l=1; l<network->num_layers; l++) { for (i=1; i<=network->layer[l].neurons; i++) { for (j=0; j<=network->layer[l-1].neurons; j++) { network->layer[l].weight[i][j] = random_float_range(-1.0, 1.0); } } } return; }/********************************************************************** NN_randomize_weights_01() synopsis: Randomize the weights of all neurons in a network. Random values selected from a linear distribution between 0.0 and 1.0 parameters: network_t *network return: none last updated: 3 Dec 2001 **********************************************************************/void NN_randomize_weights_01(network_t *network) { int l,i,j; for (l=1; l<network->num_layers; l++) { for (i=1; i<=network->layer[l].neurons; i++) { for (j=0; j<=network->layer[l-1].neurons; j++) { network->layer[l].weight[i][j] = random_float(1.0); } } } return; }/********************************************************************** NN_input() synopsis: Write input values into network. parameters: network_t *network float *input return: none last updated: **********************************************************************/void NN_input(network_t *network, float *input) { int i; for (i=1; i<=network->layer[0].neurons; i++) { network->layer[0].output[i] = input[i-1]; } return; }/********************************************************************** NN_output() synopsis: Read output values from network. parameters: network_t *network float *input return: none last updated: **********************************************************************/void NN_output(network_t *network, float *output) { int i; for (i=1; i<=network->layer[network->num_layers-1].neurons; i++) { output[i-1] = network->layer[network->num_layers-1].output[i]; } return; }/********************************************************************** NN_save_weights() synopsis: Internally save the weights in a network. parameters: network_t *network return: none last updated: **********************************************************************/void NN_save_weights(network_t *network) { int l,i,j; for (l=1; l<network->num_layers; l++) { for (i=1; i<=network->layer[l].neurons; i++) { for (j=0; j<=network->layer[l-1].neurons; j++) { network->layer[l].weight_save[i][j] = network->layer[l].weight[i][j]; } } } return; }/********************************************************************** NN_restore_weights() synopsis: Restore internally saved weights in a network. parameters: network_t *network return: none last updated: **********************************************************************/void NN_restore_weights(network_t *network) { int l,i,j; for (l=1; l<network->num_layers; l++) { for (i=1; i<=network->layer[l].neurons; i++) { for (j=0; j<=network->layer[l-1].neurons; j++) { network->layer[l].weight[i][j] = network->layer[l].weight_save[i][j]; } } } return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -