[libfann] 84/242: error handling for fann_train_data

Christian Kastner chrisk-guest at moszumanska.debian.org
Sat Oct 4 21:10:22 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 467bcb865abd89e30fa90764eaa786264088dfb2
Author: Evan Nemerson <evan at coeus-group.com>
Date:   Mon Feb 16 18:01:13 2004 +0000

    error handling for fann_train_data
---
 src/fann.c                  | 46 +++++++++++++++++++++------------------
 src/fann_internal.c         | 52 +++++++++++++++++++++++++++------------------
 src/include/fann.h          | 12 +++++------
 src/include/fann_data.h     | 42 ++++++++++++++++++++++++------------
 src/include/fann_internal.h |  3 ++-
 5 files changed, 92 insertions(+), 63 deletions(-)

diff --git a/src/fann.c b/src/fann.c
index 422e5b7..2855140 100644
--- a/src/fann.c
+++ b/src/fann.c
@@ -428,7 +428,7 @@ void fann_train(struct fann *ann, fann_type *input, fann_type *desired_output)
 	if(ann->train_deltas == NULL){
 		ann->train_deltas = (fann_type *)calloc(ann->total_neurons, sizeof(fann_type));
 		if(ann->train_deltas == NULL){
-			fann_error(ann, FANN_E_CANT_ALLOCATE_MEM);
+			fann_error((struct fann_error *)ann, FANN_E_CANT_ALLOCATE_MEM);
 			return;
 		}
 	}
@@ -458,7 +458,7 @@ void fann_train(struct fann *ann, fann_type *input, fann_type *desired_output)
 				*delta_it = fann_sigmoid_symmetric_derive(activation_output_steepness, neuron_value) * (*desired_output - neuron_value);
 				break;
 			default:
-				fann_error(ann, FANN_E_CANT_TRAIN_ACTIVATION);
+				fann_error((struct fann_error *)ann, FANN_E_CANT_TRAIN_ACTIVATION);
 				return;
 		}
 		
@@ -541,7 +541,7 @@ void fann_train(struct fann *ann, fann_type *input, fann_type *desired_output)
 				}
 				break;
 			default:
-				fann_error(ann, FANN_E_CANT_TRAIN_ACTIVATION);
+				fann_error((struct fann_error *)ann, FANN_E_CANT_TRAIN_ACTIVATION);
 				return;
 		}
 	}
@@ -984,7 +984,7 @@ fann_type* fann_run(struct fann *ann, fann_type *input)
 					neuron_it->value = (neuron_value < 0) ? 0 : 1;
 					break;
 				default:
-					fann_error(ann, FANN_E_CANT_USE_ACTIVATION);
+					fann_error((struct fann_error *)ann, FANN_E_CANT_USE_ACTIVATION);
 			}
 		}
 	}	
@@ -1001,51 +1001,51 @@ fann_type* fann_run(struct fann *ann, fann_type *input)
 
 /* resets the last error number
  */
-void fann_reset_errno(struct fann *ann)
+void fann_reset_errno(struct fann_error *errdat)
 {
-	ann->errno_f = 0;
+	errdat->errno_f = 0;
 }
 
 /* resets the last errstr
  */
-void fann_reset_errstr(struct fann *ann)
+void fann_reset_errstr(struct fann_error *errdat)
 {
-	if ( ann->errstr != NULL )
-		free(ann->errstr);
-	ann->errstr = NULL;
+	if ( errdat->errstr != NULL )
+		free(errdat->errstr);
+	errdat->errstr = NULL;
 }
 
 /* returns the last error number
  */
-unsigned int fann_get_errno(struct fann *ann)
+unsigned int fann_get_errno(struct fann_error *errdat)
 {
-	return ann->errno_f;
+	return errdat->errno_f;
 }
 
 /* returns the last errstr
  */
-char * fann_get_errstr(struct fann *ann)
+char * fann_get_errstr(struct fann_error *errdat)
 {
-	char *errstr = ann->errstr;
+	char *errstr = errdat->errstr;
 
-	fann_reset_errno(ann);
-	fann_reset_errstr(ann);
+	fann_reset_errno(errdat);
+	fann_reset_errstr(errdat);
 
 	return errstr;
 }
 
 /* change where errors are logged to
  */
-void fann_set_error_log(struct fann *ann, FILE *log)
+void fann_set_error_log(struct fann_error *errdat, FILE *log)
 {
-  ann->error_log = log;
+  errdat->error_log = log;
 }
 
 /* prints the last error to the error log (default stderr)
  */
-void fann_print_error(struct fann *ann) {
-	if ( (ann->errno_f != FANN_E_NO_ERROR) && (ann->error_log != NULL) ){
-		fputs(ann->errstr, ann->error_log);
+void fann_print_error(struct fann_error *errdat) {
+	if ( (errdat->errno_f != FANN_E_NO_ERROR) && (errdat->error_log != NULL) ){
+		fputs(errdat->errstr, errdat->error_log);
 	}
 }
 
@@ -1087,6 +1087,8 @@ struct fann_train_data * fann_merge_train_data(struct fann_train_data *data1, st
 
 	train_data = (struct fann_train_data *)malloc(sizeof(struct fann_train_data));
 
+	fann_init_error_data((struct fann_error *)train_data);
+
 	train_data->num_data = data1->num_data + data2->num_data;
 	train_data->num_input = data1->num_input;
 	train_data->num_output = data1->num_output;
@@ -1126,6 +1128,8 @@ struct fann_train_data * fann_duplicate_train_data(struct fann_train_data *data)
 		return NULL;
 	}
 
+	fann_init_error_data((struct fann_error *)dest);
+
 	dest->num_data = data->num_data;
 	dest->num_input = data->num_input;
 	dest->num_output = data->num_output;
diff --git a/src/fann_internal.c b/src/fann_internal.c
index b4f58cb..aec17ba 100644
--- a/src/fann_internal.c
+++ b/src/fann_internal.c
@@ -56,10 +56,8 @@ struct fann * fann_allocate_structure(float learning_rate, unsigned int num_laye
 	ann->num_output = 0;
 	ann->train_deltas = NULL;
 	ann->num_errors = 0;
-	ann->error_value = 0;
-	ann->errstr = NULL;
-	ann->errno_f = 0;
-	ann->error_log = stderr;
+
+	fann_init_error_data((struct fann_error *)ann);
 
 #ifdef FIXEDFANN
 	/* these values are only boring defaults, and should really
@@ -103,7 +101,7 @@ void fann_allocate_neurons(struct fann *ann)
 	/* all the neurons is allocated in one long array */
 	neurons = (struct fann_neuron *)calloc(ann->total_neurons, sizeof(struct fann_neuron));
 	if(neurons == NULL){
-		fann_error(ann, FANN_E_CANT_ALLOCATE_MEM);
+		fann_error((struct fann_error *)ann, FANN_E_CANT_ALLOCATE_MEM);
 		return;
 	}
 	
@@ -119,7 +117,7 @@ void fann_allocate_neurons(struct fann *ann)
 
 	ann->output = (fann_type *)calloc(num_neurons, sizeof(fann_type));
 	if(ann->output == NULL){
-		fann_error(ann, FANN_E_CANT_ALLOCATE_MEM);
+		fann_error((struct fann_error *)ann, FANN_E_CANT_ALLOCATE_MEM);
 		return;
 	}
 }
@@ -136,7 +134,7 @@ void fann_allocate_connections(struct fann *ann)
 	
 	weights = (fann_type *)calloc(ann->total_connections, sizeof(fann_type));
 	if(weights == NULL){
-		fann_error(ann, FANN_E_CANT_ALLOCATE_MEM);
+		fann_error((struct fann_error *)ann, FANN_E_CANT_ALLOCATE_MEM);
 		return;
 	}
 	
@@ -145,7 +143,7 @@ void fann_allocate_connections(struct fann *ann)
 	*/
 	connected_neurons = (struct fann_neuron **) calloc(ann->total_connections, sizeof(struct fann_neuron*));
 	if(connected_neurons == NULL){
-		fann_error(ann, FANN_E_CANT_ALLOCATE_MEM);
+		fann_error((struct fann_error *)ann, FANN_E_CANT_ALLOCATE_MEM);
 		return;
 	}
 	
@@ -161,7 +159,7 @@ void fann_allocate_connections(struct fann *ann)
 	}
 
 	if(connections_so_far != ann->total_connections){
-		fann_error(ann, FANN_E_WRONG_NUM_CONNECTIONS, connections_so_far, ann->total_connections);
+		fann_error((struct fann_error *)ann, FANN_E_WRONG_NUM_CONNECTIONS, connections_so_far, ann->total_connections);
 		return;
 	}
 }
@@ -587,15 +585,15 @@ void fann_seed_rand()
 
 /* Populate the error information
  */
-void fann_error(struct fann *ann, const unsigned int errno, ...)
+void fann_error(struct fann_error *errdat, const unsigned int errno, ...)
 {
 	va_list ap;
 	char * errstr;
 
-	if(ann != NULL)	ann->errno_f = errno;
+	if (errdat != NULL) errdat->errno_f = errno;
 	
-	if(ann != NULL && ann->errstr != NULL){
-		errstr = ann->errstr;
+	if(errdat != NULL && errdat->errstr != NULL){
+		errstr = errdat->errstr;
 	}else{
 		errstr = (char *)malloc(FANN_ERRSTR_MAX);
 		if(errstr == NULL){
@@ -656,12 +654,12 @@ void fann_error(struct fann *ann, const unsigned int errno, ...)
 	}
 	va_end(ap);
 
-	if ( ann == NULL ) {
+	if ( errdat == NULL ) {
 		fprintf(stderr, "FANN Error %d: %s", errno, errstr);
 	} else {
-		ann->errstr = errstr;
-		if ( ann->error_log != NULL ) {
-			fprintf(ann->error_log, "FANN Error %d: %s", errno, errstr);
+		errdat->errstr = errstr;
+		if ( errdat->error_log != NULL ) {
+			fprintf(errdat->error_log, "FANN Error %d: %s", errno, errstr);
 		}
 	}
 }
@@ -739,7 +737,7 @@ struct fann * fann_create_from_fd(FILE *conf, const char *configuration_file)
 	/* 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_error((struct fann_error *)ann, FANN_E_CANT_READ_NEURON, configuration_file);
 			fann_destroy(ann);
 			return NULL;
 		}
@@ -767,7 +765,7 @@ struct fann * fann_create_from_fd(FILE *conf, const char *configuration_file)
 	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_error((struct fann_error *)ann, FANN_E_CANT_READ_NEURON, configuration_file);
 			fann_destroy(ann);
 			return NULL;
 		}
@@ -786,7 +784,7 @@ struct fann * fann_create_from_fd(FILE *conf, const char *configuration_file)
 	
 	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_error((struct fann_error *)ann, FANN_E_CANT_READ_CONNECTIONS, configuration_file);
 			fann_destroy(ann);
 			return NULL;
 		}
@@ -818,7 +816,9 @@ struct fann_train_data* fann_read_train_from_fd(FILE *file, char *filename)
 		return NULL;
 	}
 	line++;
-	
+
+	fann_init_error_data((struct fann_error *)data);
+
 	data->num_data = num_data;
 	data->num_input = num_input;
 	data->num_output = num_output;
@@ -871,3 +871,13 @@ struct fann_train_data* fann_read_train_from_fd(FILE *file, char *filename)
 	}
 	return data;
 }
+
+/* Initialize an error data strcuture
+ */
+void fann_init_error_data(struct fann_error *errdat)
+{
+	errdat->error_value = 0;
+	errdat->errstr = NULL;
+	errdat->errno_f = 0;
+	errdat->error_log = stderr;
+}
diff --git a/src/include/fann.h b/src/include/fann.h
index 40f2cfb..2b00532 100644
--- a/src/include/fann.h
+++ b/src/include/fann.h
@@ -283,28 +283,28 @@ void fann_reset_MSE(struct fann *ann);
 
 /* resets the last error number
  */
-void fann_reset_errno(struct fann *ann);
+void fann_reset_errno(struct fann_error *errdat);
 
 /* resets the last error string
  */
-void fann_reset_errstr(struct fann *ann);
+void fann_reset_errstr(struct fann_error *errdat);
 
 /* change where errors are logged to
  */
-void fann_set_error_log(struct fann *ann, FILE *log);
+void fann_set_error_log(struct fann_error *errdat, FILE *log);
 
 /* returns the last error number
  */
-unsigned int fann_get_errno(struct fann *ann);
+unsigned int fann_get_errno(struct fann_error *errdat);
 
 /* returns the last errstr.
  * This function calls fann_reset_errno and fann_reset_errstr
  */
-char * fann_get_errstr(struct fann *ann);
+char * fann_get_errstr(struct fann_error *errdat);
 
 /* prints the last error to stderr
  */
-void fann_print_error(struct fann *ann) ;
+void fann_print_error(struct fann_error *errdat);
 
 /* ----- Running�----- */
 
diff --git a/src/include/fann_data.h b/src/include/fann_data.h
index 4ffa9a5..319dabc 100644
--- a/src/include/fann_data.h
+++ b/src/include/fann_data.h
@@ -58,6 +58,20 @@ struct fann_layer
  */
 struct fann
 {
+	/* the total error value.
+	   the real mean square error is error_value/num_errors
+	 */
+	float error_value;
+
+	/* The type of error that last occured. */
+	unsigned int errno_f;
+
+	/* Where to log error messages. */
+	FILE *error_log;
+
+	/* A string representation of the last error. */
+	char * errstr;
+
 	/* the learning rate of the network */
 	float learning_rate;
 
@@ -141,25 +155,16 @@ struct fann
 	/* the number of data used to calculate the error.
 	 */
 	unsigned int num_errors;
-
-	/* the total error value.
-	   the real mean square error is error_value/num_errors
-	 */
-	float error_value;
-
-	/* The type of error that last occured. */
-	unsigned int errno_f;
-
-	/* Where to log error messages. */
-	FILE *error_log;
-
-	/* A string representation of the last error. */
-	char * errstr;
 };
 
 /* Structure used to store data, for use with training. */
 struct fann_train_data
 {
+	float error_value;
+	unsigned int errno_f;
+	FILE *error_log;
+	char *errstr;
+
 	unsigned int num_data;
 	unsigned int num_input;
 	unsigned int num_output;
@@ -167,4 +172,13 @@ struct fann_train_data
 	fann_type **output;
 };
 
+/* Structure used to store error-related information */
+struct fann_error
+{
+	float error_value;
+	unsigned int errno_f;
+	FILE *error_log;
+	char * errstr;
+};
+
 #endif
diff --git a/src/include/fann_internal.h b/src/include/fann_internal.h
index b4d07b6..9b85353 100644
--- a/src/include/fann_internal.h
+++ b/src/include/fann_internal.h
@@ -51,7 +51,8 @@ void fann_seed_rand();
 void fann_update_stepwise_hidden(struct fann *ann);
 void fann_update_stepwise_output(struct fann *ann);
 
-void fann_error(struct fann *ann, unsigned int errno, ...);
+void fann_error(struct fann_error *errdat, unsigned int errno, ...);
+void fann_init_error_data(struct fann_error *errdat);
 
 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);

-- 
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