[libfann] 50/242: Split up file accessing functions so file descriptors can be passed instead of file names.
Christian Kastner
chrisk-guest at moszumanska.debian.org
Sat Oct 4 21:10:19 UTC 2014
This is an automated email from the git hooks/post-receive script.
chrisk-guest pushed a commit to tag Version2_0_0
in repository libfann.
commit 1858fef322c47794c0056a75376a71e0325a0c29
Author: Evan Nemerson <evan at coeus-group.com>
Date: Tue Jan 13 12:12:06 2004 +0000
Split up file accessing functions so file descriptors can be passed instead of file names.
---
src/fann.c | 204 ++----------------------------------
src/fann_internal.c | 249 +++++++++++++++++++++++++++++++++++++++++---
src/include/fann_internal.h | 6 ++
3 files changed, 246 insertions(+), 213 deletions(-)
diff --git a/src/fann.c b/src/fann.c
index bed3c46..de1ed30 100644
--- a/src/fann.c
+++ b/src/fann.c
@@ -275,142 +275,17 @@ struct fann * fann_create(float connection_rate, float learning_rate,
*/
struct fann * fann_create_from_file(const char *configuration_file)
{
- unsigned int num_layers, layer_size, activation_function_hidden, activation_function_output, input_neuron, i;
-#ifdef FIXEDFANN
- unsigned int decimal_point, multiplier;
-#endif
- fann_type activation_hidden_steepness, activation_output_steepness;
- float learning_rate, connection_rate;
- struct fann_neuron *first_neuron, *neuron_it, *last_neuron, **connected_neurons;
- fann_type *weights;
- struct fann_layer *layer_it;
struct fann *ann;
-
- char *read_version;
FILE *conf = fopen(configuration_file, "r");
-
if(!conf){
fann_error(NULL, FANN_E_CANT_OPEN_CONFIG_R, configuration_file);
return NULL;
}
-
- read_version = (char *)calloc(strlen(FANN_CONF_VERSION"\n"), 1);
- if(read_version == NULL){
- fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
- return NULL;
- }
-
- fread(read_version, 1, strlen(FANN_CONF_VERSION"\n"), conf); /* reads version */
-
- /* compares the version information */
- if(strncmp(read_version, FANN_CONF_VERSION"\n", strlen(FANN_CONF_VERSION"\n")) != 0){
- fann_error(NULL, FANN_E_WRONG_CONFIG_VERSION, configuration_file);
- free(read_version);
- return NULL;
- }
-
- free(read_version);
-
-#ifdef FIXEDFANN
- if(fscanf(conf, "%u\n", &decimal_point) != 1){
- fann_error(NULL, FANN_E_CANT_READ_CONFIG, configuration_file);
- return NULL;
- }
- multiplier = 1 << decimal_point;
-#endif
-
- if(fscanf(conf, "%u %f %f %u %u "FANNSCANF" "FANNSCANF"\n", &num_layers, &learning_rate, &connection_rate, &activation_function_hidden, &activation_function_output, &activation_hidden_steepness, &activation_output_steepness) != 7){
- fann_error(NULL, FANN_E_CANT_READ_CONFIG, configuration_file);
- return NULL;
- }
-
- ann = fann_allocate_structure(learning_rate, num_layers);
- if(ann == NULL){
- return NULL;
- }
- ann->connection_rate = connection_rate;
-
-#ifdef FIXEDFANN
- ann->decimal_point = decimal_point;
- ann->multiplier = multiplier;
-#endif
- fann_initialise_result_array(ann);
-
- fann_set_activation_hidden_steepness(ann, activation_hidden_steepness);
- fann_set_activation_output_steepness(ann, activation_output_steepness);
- fann_set_activation_function_hidden(ann, activation_function_hidden);
- fann_set_activation_function_output(ann, activation_function_output);
-
-#ifdef DEBUG
- printf("creating network with learning rate %f\n", learning_rate);
- printf("input\n");
-#endif
-
- /* determine how many neurons there should be in each layer */
- for(layer_it = ann->first_layer; layer_it != ann->last_layer; layer_it++){
- if(fscanf(conf, "%u ", &layer_size) != 1){
- fann_error(ann, FANN_E_CANT_READ_NEURON, configuration_file);
- fann_destroy(ann);
- return NULL;
- }
- /* we do not allocate room here, but we make sure that
- last_neuron - first_neuron is the number of neurons */
- layer_it->first_neuron = NULL;
- layer_it->last_neuron = layer_it->first_neuron + layer_size;
- ann->total_neurons += layer_size;
-#ifdef DEBUG
- printf(" layer : %d neurons, 1 bias\n", layer_size);
-#endif
- }
-
- ann->num_input = ann->first_layer->last_neuron - ann->first_layer->first_neuron;
- ann->num_output = ((ann->last_layer-1)->last_neuron - (ann->last_layer-1)->first_neuron) - 1;
-
- /* allocate room for the actual neurons */
- fann_allocate_neurons(ann);
- if(ann->errno_f == FANN_E_CANT_ALLOCATE_MEM){
- fann_destroy(ann);
- return NULL;
- }
-
- last_neuron = (ann->last_layer-1)->last_neuron;
- for(neuron_it = ann->first_layer->first_neuron;
- neuron_it != last_neuron; neuron_it++){
- if(fscanf(conf, "%u ", &neuron_it->num_connections) != 1){
- fann_error(ann, FANN_E_CANT_READ_NEURON, configuration_file);
- fann_destroy(ann);
- return NULL;
- }
- ann->total_connections += neuron_it->num_connections;
- }
-
- fann_allocate_connections(ann);
- if(ann->errno_f == FANN_E_CANT_ALLOCATE_MEM){
- fann_destroy(ann);
- return NULL;
- }
-
- connected_neurons = (ann->first_layer+1)->first_neuron->connected_neurons;
- weights = (ann->first_layer+1)->first_neuron->weights;
- first_neuron = ann->first_layer->first_neuron;
-
- for(i = 0; i < ann->total_connections; i++){
- if(fscanf(conf, "(%u "FANNSCANF") ", &input_neuron, &weights[i]) != 2){
- fann_error(ann, FANN_E_CANT_READ_CONNECTIONS, configuration_file);
- fann_destroy(ann);
- return NULL;
- }
- connected_neurons[i] = first_neuron+input_neuron;
- }
-
-#ifdef DEBUG
- printf("output\n");
-#endif
+ ann = fann_create_from_fd(conf, configuration_file);
fclose(conf);
return ann;
}
-
/* deallocate the network.
*/
void fann_destroy(struct fann *ann)
@@ -681,83 +556,18 @@ fann_type *fann_test(struct fann *ann, fann_type *input, fann_type *desired_outp
/* Reads training data from a file.
*/
-struct fann_train_data* fann_read_train_from_file(char *filename)
+struct fann_train_data* fann_read_train_from_file(char *configuration_file)
{
- unsigned int num_input, num_output, num_data, i, j;
- unsigned int line = 1;
struct fann_train_data* data;
-
- FILE *file = fopen(filename, "r");
-
+ FILE *file = fopen(configuration_file, "r");
+
if(!file){
- fann_error(NULL, FANN_E_CANT_OPEN_TD_R, filename);
- return NULL;
- }
-
- data = (struct fann_train_data *)malloc(sizeof(struct fann_train_data));
- if(data == NULL){
- fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
- return NULL;
- }
-
- if(fscanf(file, "%u %u %u\n", &num_data, &num_input, &num_output) != 3){
- fann_error(NULL, FANN_E_CANT_READ_TD, filename, line);
- fann_destroy_train(data);
- return NULL;
- }
- line++;
-
- data->num_data = num_data;
- data->num_input = num_input;
- data->num_output = num_output;
- data->input = (fann_type **)calloc(num_data, sizeof(fann_type *));
- if(data->input == NULL){
- fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
- fann_destroy_train(data);
- return NULL;
- }
-
- data->output = (fann_type **)calloc(num_data, sizeof(fann_type *));
- if(data->output == NULL){
- fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
- fann_destroy_train(data);
+ fann_error(NULL, FANN_E_CANT_OPEN_CONFIG_R, configuration_file);
return NULL;
}
-
- for(i = 0; i != num_data; i++){
- data->input[i] = (fann_type *)calloc(num_input, sizeof(fann_type));
- if(data->input[i] == NULL){
- fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
- fann_destroy_train(data);
- return NULL;
- }
-
- for(j = 0; j != num_input; j++){
- if(fscanf(file, FANNSCANF" ", &data->input[i][j]) != 1){
- fann_error(NULL, FANN_E_CANT_READ_TD, filename, line);
- fann_destroy_train(data);
- return NULL;
- }
- }
- line++;
-
- data->output[i] = (fann_type *)calloc(num_output, sizeof(fann_type));
- if(data->output[i] == NULL){
- fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
- fann_destroy_train(data);
- return NULL;
- }
- for(j = 0; j != num_output; j++){
- if(fscanf(file, FANNSCANF" ", &data->output[i][j]) != 1){
- fann_error(NULL, FANN_E_CANT_READ_TD, filename, line);
- fann_destroy_train(data);
- return NULL;
- }
- }
- line++;
- }
-
+ data = fann_read_train_from_fd(file, configuration_file);
+ fclose(file);
return data;
}
diff --git a/src/fann_internal.c b/src/fann_internal.c
index 1794199..27a62ec 100644
--- a/src/fann_internal.c
+++ b/src/fann_internal.c
@@ -169,6 +169,21 @@ void fann_allocate_connections(struct fann *ann)
*/
int fann_save_internal(struct fann *ann, const char *configuration_file, unsigned int save_as_fixed)
{
+ int retval;
+ FILE *conf = fopen(configuration_file, "w+");
+ if(!conf){
+ fann_error(NULL, FANN_E_CANT_OPEN_CONFIG_W, configuration_file);
+ return -1;
+ }
+ retval = fann_save_internal_fd(ann, conf, configuration_file, save_as_fixed);
+ fclose(conf);
+ return retval;
+}
+
+/* Used to save the network to a file descriptor.
+ */
+int fann_save_internal_fd(struct fann *ann, FILE *conf, const char *configuration_file, unsigned int save_as_fixed)
+{
struct fann_layer *layer_it;
int calculated_decimal_point = 0;
struct fann_neuron *neuron_it, *first_neuron;
@@ -184,12 +199,6 @@ int fann_save_internal(struct fann *ann, const char *configuration_file, unsigne
fann_type current_max_value = 0;
#endif
- FILE *conf = fopen(configuration_file, "w+");
- if(!conf){
- fann_error(ann, FANN_E_CANT_OPEN_CONFIG_W, configuration_file);
- return -1;
- }
-
#ifndef FIXEDFANN
if(save_as_fixed){
/* save the version information */
@@ -311,14 +320,25 @@ int fann_save_internal(struct fann *ann, const char *configuration_file, unsigne
}
fprintf(conf, "\n");
- fclose(conf);
-
return calculated_decimal_point;
}
/* Save the train data structure.
*/
void fann_save_train_internal(struct fann_train_data* data, char *filename, unsigned int save_as_fixed, unsigned int decimal_point)
+{
+ FILE *file = fopen(filename, "w");
+ if(!file){
+ fann_error(NULL, FANN_E_CANT_OPEN_TD_W, filename);
+ return;
+ }
+ fann_save_train_internal_fd(data, file, filename, save_as_fixed, decimal_point);
+ fclose(file);
+}
+
+/* Save the train data structure.
+ */
+void fann_save_train_internal_fd(struct fann_train_data* data, FILE *file, char *filename, unsigned int save_as_fixed, unsigned int decimal_point)
{
unsigned int num_data = data->num_data;
unsigned int num_input = data->num_input;
@@ -328,12 +348,6 @@ void fann_save_train_internal(struct fann_train_data* data, char *filename, unsi
unsigned int multiplier = 1 << decimal_point;
#endif
- FILE *file = fopen(filename, "w");
- if(!file){
- fann_error(NULL, FANN_E_CANT_OPEN_TD_W, filename);
- return;
- }
-
fprintf(file, "%u %u %u\n", data->num_data, data->num_input, data->num_output);
for(i = 0; i < num_data; i++){
@@ -363,8 +377,6 @@ void fann_save_train_internal(struct fann_train_data* data, char *filename, unsi
}
fprintf(file, "\n");
}
-
- fclose(file);
}
void fann_initialise_result_array(struct fann *ann)
@@ -544,3 +556,208 @@ void fann_error(struct fann *ann, const unsigned int errno, ...)
ann->errstr = errstr;
}
}
+
+/* Create a network from a configuration file descriptor.
+ */
+struct fann * fann_create_from_fd(FILE *conf, const char *configuration_file)
+{
+ unsigned int num_layers, layer_size, activation_function_hidden, activation_function_output, input_neuron, i;
+#ifdef FIXEDFANN
+ unsigned int decimal_point, multiplier;
+#endif
+ fann_type activation_hidden_steepness, activation_output_steepness;
+ float learning_rate, connection_rate;
+ struct fann_neuron *first_neuron, *neuron_it, *last_neuron, **connected_neurons;
+ fann_type *weights;
+ struct fann_layer *layer_it;
+ struct fann *ann;
+
+ char *read_version;
+
+ read_version = (char *)calloc(strlen(FANN_CONF_VERSION"\n"), 1);
+ if(read_version == NULL){
+ fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
+ return NULL;
+ }
+
+ fread(read_version, 1, strlen(FANN_CONF_VERSION"\n"), conf); /* reads version */
+
+ /* compares the version information */
+ if(strncmp(read_version, FANN_CONF_VERSION"\n", strlen(FANN_CONF_VERSION"\n")) != 0){
+ fann_error(NULL, FANN_E_WRONG_CONFIG_VERSION, configuration_file);
+ free(read_version);
+ return NULL;
+ }
+
+ free(read_version);
+
+#ifdef FIXEDFANN
+ if(fscanf(conf, "%u\n", &decimal_point) != 1){
+ fann_error(NULL, FANN_E_CANT_READ_CONFIG, configuration_file);
+ return NULL;
+ }
+ multiplier = 1 << decimal_point;
+#endif
+
+ if(fscanf(conf, "%u %f %f %u %u "FANNSCANF" "FANNSCANF"\n", &num_layers, &learning_rate, &connection_rate, &activation_function_hidden, &activation_function_output, &activation_hidden_steepness, &activation_output_steepness) != 7){
+ fann_error(NULL, FANN_E_CANT_READ_CONFIG, configuration_file);
+ return NULL;
+ }
+
+ ann = fann_allocate_structure(learning_rate, num_layers);
+ if(ann == NULL){
+ return NULL;
+ }
+ ann->connection_rate = connection_rate;
+
+#ifdef FIXEDFANN
+ ann->decimal_point = decimal_point;
+ ann->multiplier = multiplier;
+#endif
+ fann_initialise_result_array(ann);
+
+ fann_set_activation_hidden_steepness(ann, activation_hidden_steepness);
+ fann_set_activation_output_steepness(ann, activation_output_steepness);
+ fann_set_activation_function_hidden(ann, activation_function_hidden);
+ fann_set_activation_function_output(ann, activation_function_output);
+
+#ifdef DEBUG
+ printf("creating network with learning rate %f\n", learning_rate);
+ printf("input\n");
+#endif
+
+ /* determine how many neurons there should be in each layer */
+ for(layer_it = ann->first_layer; layer_it != ann->last_layer; layer_it++){
+ if(fscanf(conf, "%u ", &layer_size) != 1){
+ fann_error(ann, FANN_E_CANT_READ_NEURON, configuration_file);
+ fann_destroy(ann);
+ return NULL;
+ }
+ /* we do not allocate room here, but we make sure that
+ last_neuron - first_neuron is the number of neurons */
+ layer_it->first_neuron = NULL;
+ layer_it->last_neuron = layer_it->first_neuron + layer_size;
+ ann->total_neurons += layer_size;
+#ifdef DEBUG
+ printf(" layer : %d neurons, 1 bias\n", layer_size);
+#endif
+ }
+
+ ann->num_input = ann->first_layer->last_neuron - ann->first_layer->first_neuron;
+ ann->num_output = ((ann->last_layer-1)->last_neuron - (ann->last_layer-1)->first_neuron) - 1;
+
+ /* allocate room for the actual neurons */
+ fann_allocate_neurons(ann);
+ if(ann->errno_f == FANN_E_CANT_ALLOCATE_MEM){
+ fann_destroy(ann);
+ return NULL;
+ }
+
+ last_neuron = (ann->last_layer-1)->last_neuron;
+ for(neuron_it = ann->first_layer->first_neuron;
+ neuron_it != last_neuron; neuron_it++){
+ if(fscanf(conf, "%u ", &neuron_it->num_connections) != 1){
+ fann_error(ann, FANN_E_CANT_READ_NEURON, configuration_file);
+ fann_destroy(ann);
+ return NULL;
+ }
+ ann->total_connections += neuron_it->num_connections;
+ }
+
+ fann_allocate_connections(ann);
+ if(ann->errno_f == FANN_E_CANT_ALLOCATE_MEM){
+ fann_destroy(ann);
+ return NULL;
+ }
+
+ connected_neurons = (ann->first_layer+1)->first_neuron->connected_neurons;
+ weights = (ann->first_layer+1)->first_neuron->weights;
+ first_neuron = ann->first_layer->first_neuron;
+
+ for(i = 0; i < ann->total_connections; i++){
+ if(fscanf(conf, "(%u "FANNSCANF") ", &input_neuron, &weights[i]) != 2){
+ fann_error(ann, FANN_E_CANT_READ_CONNECTIONS, configuration_file);
+ fann_destroy(ann);
+ return NULL;
+ }
+ connected_neurons[i] = first_neuron+input_neuron;
+ }
+
+#ifdef DEBUG
+ printf("output\n");
+#endif
+ return ann;
+}
+
+/* Reads training data from a file descriptor.
+ */
+struct fann_train_data* fann_read_train_from_fd(FILE *file, char *filename)
+{
+ unsigned int num_input, num_output, num_data, i, j;
+ unsigned int line = 1;
+ struct fann_train_data* data = (struct fann_train_data *)malloc(sizeof(struct fann_train_data));
+
+ if(data == NULL){
+ fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
+ return NULL;
+ }
+
+ if(fscanf(file, "%u %u %u\n", &num_data, &num_input, &num_output) != 3){
+ fann_error(NULL, FANN_E_CANT_READ_TD, filename, line);
+ fann_destroy_train(data);
+ return NULL;
+ }
+ line++;
+
+ data->num_data = num_data;
+ data->num_input = num_input;
+ data->num_output = num_output;
+ data->input = (fann_type **)calloc(num_data, sizeof(fann_type *));
+ if(data->input == NULL){
+ fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
+ fann_destroy_train(data);
+ return NULL;
+ }
+
+ data->output = (fann_type **)calloc(num_data, sizeof(fann_type *));
+ if(data->output == NULL){
+ fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
+ fann_destroy_train(data);
+ return NULL;
+ }
+
+ for(i = 0; i != num_data; i++){
+ data->input[i] = (fann_type *)calloc(num_input, sizeof(fann_type));
+ if(data->input[i] == NULL){
+ fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
+ fann_destroy_train(data);
+ return NULL;
+ }
+
+ for(j = 0; j != num_input; j++){
+ if(fscanf(file, FANNSCANF" ", &data->input[i][j]) != 1){
+ fann_error(NULL, FANN_E_CANT_READ_TD, filename, line);
+ fann_destroy_train(data);
+ return NULL;
+ }
+ }
+ line++;
+
+ data->output[i] = (fann_type *)calloc(num_output, sizeof(fann_type));
+ if(data->output[i] == NULL){
+ fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
+ fann_destroy_train(data);
+ return NULL;
+ }
+
+ for(j = 0; j != num_output; j++){
+ if(fscanf(file, FANNSCANF" ", &data->output[i][j]) != 1){
+ fann_error(NULL, FANN_E_CANT_READ_TD, filename, line);
+ fann_destroy_train(data);
+ return NULL;
+ }
+ }
+ line++;
+ }
+ return data;
+}
diff --git a/src/include/fann_internal.h b/src/include/fann_internal.h
index ee40100..c39d3a4 100644
--- a/src/include/fann_internal.h
+++ b/src/include/fann_internal.h
@@ -23,6 +23,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <math.h>
+#include <stdio.h>
#include "fann_data.h"
#define FANN_FIX_VERSION "FANN_FIX_1.0"
@@ -40,7 +41,9 @@ void fann_allocate_neurons(struct fann *ann);
void fann_allocate_connections(struct fann *ann);
int fann_save_internal(struct fann *ann, const char *configuration_file, unsigned int save_as_fixed);
+int fann_save_internal_fd(struct fann *ann, FILE *conf, const char *configuration_file, unsigned int save_as_fixed);
void fann_save_train_internal(struct fann_train_data* data, char *filename, unsigned int save_as_fixed, unsigned int decimal_point);
+void fann_save_train_internal_fd(struct fann_train_data* data, FILE *file, char *filename, unsigned int save_as_fixed, unsigned int decimal_point);
int fann_compare_connections(const void* c1, const void* c2);
void fann_seed_rand();
@@ -51,6 +54,9 @@ void fann_update_stepwise_output(struct fann *ann);
void fann_error(struct fann *ann, unsigned int errno, ...);
+struct fann * fann_create_from_fd(FILE *conf, const char *configuration_file);
+struct fann_train_data* fann_read_train_from_fd(FILE *file, char *filename);
+
/* called fann_max, in order to not interferre with predefined versions of max */
#define fann_max(x, y) (((x) > (y)) ? (x) : (y))
#define fann_min(x, y) (((x) < (y)) ? (x) : (y))
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/libfann.git
More information about the debian-science-commits
mailing list