[libfann] 183/242: minor bugfixes

Christian Kastner chrisk-guest at moszumanska.debian.org
Sat Oct 4 21:10:41 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 92968be3c45ce77bc19111aa221cce7a808c28a0
Author: Steffen Nissen <lukesky at diku.dk>
Date:   Tue Jul 12 23:19:30 2005 +0000

    minor bugfixes
---
 examples/Makefile             | 17 +++++++++++++++--
 examples/xor.data             | 14 +++++++-------
 examples/xor_train.c          | 23 +++++++++++------------
 src/fann.c                    | 15 +++++++++------
 src/fann_train.c              | 24 ++++++++++++++++++------
 src/fann_train_data.c         |  5 +++--
 src/include/fann_activation.h | 14 ++++++--------
 src/include/fann_internal.h   |  4 ++--
 8 files changed, 71 insertions(+), 45 deletions(-)

diff --git a/examples/Makefile b/examples/Makefile
index 9e011c6..5c07970 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -41,9 +41,22 @@ rundebugtest: $(DEBUG_TARGETS)
 	@echo Testing network with floats
 	./xor_test_debug
 
+#	@echo
+#	@echo Testing network with fixed points
+#	./xor_test_fixed_debug
+
+runcompliancetest: $(DEBUG_TARGETS)
+	./xor_train_debug
+
 	@echo
-	@echo Testing network with fixed points
-	./xor_test_fixed_debug
+	@echo Testing network with floats
+	@cp xor_float.net xor_float_new.net
+	@cp xor_float_backup.net xor_float.net
+	@echo RUN
+	@./xor_test_debug | grep XOR
+	@echo CAT
+	@cat xor_float_backup.out
+
 
 #compiletest is used to test whether the library will compile easily in other compilers
 compiletest:
diff --git a/examples/xor.data b/examples/xor.data
index dcacd1a..e831fc6 100644
--- a/examples/xor.data
+++ b/examples/xor.data
@@ -1,9 +1,9 @@
 4 2 1
-0 0
--2
-0 1
-2
-1 0
-2
+-1 -1
+-1
+-1 1
+1
+1 -1
+1
 1 1
--2
+-1
diff --git a/examples/xor_train.c b/examples/xor_train.c
index e720c00..41e730c 100644
--- a/examples/xor_train.c
+++ b/examples/xor_train.c
@@ -35,9 +35,9 @@ int main()
 	const unsigned int num_input = 2;
 	const unsigned int num_output = 1;
 	const unsigned int num_layers = 3;
-	const unsigned int num_neurons_hidden = 3;
-	const float desired_error = (const float)0.001;
-	const unsigned int max_iterations = 300000;
+	const unsigned int num_neurons_hidden = 5;
+	const float desired_error = (const float)0.00001;
+	const unsigned int max_iterations = 100000;
 	const unsigned int iterations_between_reports = 1000;
 	struct fann *ann;
 	struct fann_train_data *data;
@@ -57,24 +57,23 @@ int main()
 	data = fann_read_train_from_file("xor.data");
 
 
-	fann_set_activation_steepness_hidden(ann, 1.0);
-	fann_set_activation_steepness_output(ann, 1.0);
+	fann_set_activation_steepness_hidden(ann, 1);
+	fann_set_activation_steepness_output(ann, 1);
+	fann_set_rprop_delta_max(ann, 50);
 	
-	fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC_STEPWISE);
-	fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC_STEPWISE);
+	fann_set_activation_function_hidden(ann, FANN_ELLIOT_SYMMETRIC);
+	fann_set_activation_function_output(ann, FANN_GAUSSIAN_SYMMETRIC);
 	
 	fann_init_weights(ann, data);
 
-	fann_set_training_algorithm(ann, FANN_TRAIN_QUICKPROP);
-
+	/*fann_set_training_algorithm(ann, FANN_TRAIN_QUICKPROP);*/
 	fann_train_on_data(ann, data, max_iterations, iterations_between_reports, desired_error);
 	
 	/*fann_train_on_data_callback(ann, data, max_iterations, iterations_between_reports, desired_error, print_callback);*/
 
-	fann_print_connections(ann);
-	fann_print_parameters(ann);		
+	
 
-	printf("Testing network.\n");
+	printf("Testing network. %f\n", fann_test_data(ann, data));
 
 	for(i = 0; i < data->num_data; i++){
 		calc_out = fann_run(ann, data->input[i]);
diff --git a/src/fann.c b/src/fann.c
index 6afaa7e..209869e 100644
--- a/src/fann.c
+++ b/src/fann.c
@@ -80,7 +80,9 @@ FANN_EXTERNAL struct fann * FANN_API fann_create_array(float connection_rate, fl
 	}
 	
 	/* seed random */
+#ifndef FANN_NO_SEED	
 	fann_seed_rand();
+#endif
 	
 	/* allocate the general structure */
 	ann = fann_allocate_structure(learning_rate, num_layers);
@@ -336,7 +338,9 @@ FANN_EXTERNAL struct fann * FANN_API fann_create_shortcut_array(float learning_r
 	unsigned int multiplier;
 #endif
 	/* seed random */
+#ifndef FANN_NO_SEED	
 	fann_seed_rand();
+#endif
 	
 	/* allocate the general structure */
 	ann = fann_allocate_structure(learning_rate, num_layers);
@@ -448,11 +452,9 @@ FANN_EXTERNAL fann_type * FANN_API fann_run(struct fann *ann, fann_type *input)
 	
 	
 	/* store some variabels local for fast access */
-#ifndef FIXEDFANN
 	fann_type steepness;
 	const fann_type activation_steepness_output = ann->activation_steepness_output;
 	const fann_type activation_steepness_hidden = ann->activation_steepness_hidden;
-#endif
 	
 	unsigned int activation_function_output = ann->activation_function_output;
 	unsigned int activation_function_hidden = ann->activation_function_hidden;
@@ -513,10 +515,8 @@ FANN_EXTERNAL fann_type * FANN_API fann_run(struct fann *ann, fann_type *input)
 	last_layer = ann->last_layer;
 	for(layer_it = ann->first_layer+1; layer_it != last_layer; layer_it++){
 		
-#ifndef FIXEDFANN
 		steepness = (layer_it == last_layer-1) ? 
 			activation_steepness_output : activation_steepness_hidden;
-#endif
 		
 		activation_function = (layer_it == last_layer-1) ?
 			activation_function_output : activation_function_hidden;
@@ -590,12 +590,14 @@ FANN_EXTERNAL fann_type * FANN_API fann_run(struct fann *ann, fann_type *input)
 						fann_mult(weights[i+2], neurons[i+2].value) +
 						fann_mult(weights[i+3], neurons[i+3].value);
 				}
-				
+
 				/*
 				for(i = 0;i != num_connections; i++){
+					printf("%f += %f*%f, ", neuron_sum, weights[i], neurons[i].value);
 					neuron_sum += fann_mult(weights[i], neurons[i].value);
 				}
 				*/
+				
 				/* unrolled loop end */
 			} else {
 				neuron_pointers = ann->connections + neuron_it->first_con;
@@ -621,11 +623,12 @@ FANN_EXTERNAL fann_type * FANN_API fann_run(struct fann *ann, fann_type *input)
 				}
 			}
 			
+			neuron_sum = fann_mult(steepness, neuron_sum);
 			neuron_it->sum = neuron_sum;
 
-			neuron_sum = fann_mult(steepness, neuron_sum);
 			fann_activation_switch(ann, activation_function, neuron_sum, neuron_it->value);
 			/*
+			printf(" sum=%f, value=%f\n", neuron_it->sum, neuron_it->value);
 			switch(activation_function){
 #ifdef FIXEDFANN
 				case FANN_SIGMOID:
diff --git a/src/fann_train.c b/src/fann_train.c
index 5d3cae2..11ce9ec 100644
--- a/src/fann_train.c
+++ b/src/fann_train.c
@@ -41,18 +41,24 @@ fann_type fann_activation_derived(unsigned int activation_function,
 			return (fann_type)fann_linear_derive(steepness, value);
 		case FANN_SIGMOID:
 		case FANN_SIGMOID_STEPWISE:
+			value = fann_clip(value, 0.01f, 0.99f);
 			return (fann_type)fann_sigmoid_derive(steepness, value);
 		case FANN_SIGMOID_SYMMETRIC:
 		case FANN_SIGMOID_SYMMETRIC_STEPWISE:
+			value = fann_clip(value, -0.98f, 0.98f);
 			return (fann_type)fann_sigmoid_symmetric_derive(steepness, value);
 		case FANN_GAUSSIAN:
+			value = fann_clip(value, 0.01f, 0.99f);
 			return (fann_type)fann_gaussian_derive(steepness, value, sum);
 		case FANN_GAUSSIAN_SYMMETRIC:
+			value = fann_clip(value, -0.98f, 0.98f);
 			return (fann_type)fann_gaussian_symmetric_derive(steepness, value, sum);
 		case FANN_ELLIOT:
+			value = fann_clip(value, 0.01f, 0.99f);
 			return (fann_type)fann_elliot_derive(steepness, value, sum);
 		case FANN_ELLIOT_SYMMETRIC:
-			return (fann_type)fann_elliot_symmetric_derive(steepness, value, sum);			
+			value = fann_clip(value, -0.98f, 0.98f);
+			return (fann_type)fann_elliot_symmetric_derive(steepness, value, sum);
 		default:
 			return 0;
 	}
@@ -213,7 +219,9 @@ FANN_EXTERNAL fann_type * FANN_API fann_test(struct fann *ann, fann_type *input,
 		neuron_diff = (*desired_output - neuron_value);
 		
 		if(ann->activation_function_output == FANN_SIGMOID_SYMMETRIC ||
-			ann->activation_function_output == FANN_SIGMOID_SYMMETRIC_STEPWISE){
+			ann->activation_function_output == FANN_SIGMOID_SYMMETRIC_STEPWISE ||
+			ann->activation_function_output == FANN_ELLIOT_SYMMETRIC ||
+			ann->activation_function_output == FANN_GAUSSIAN_SYMMETRIC){
 			neuron_diff /= (fann_type)2;
 		}
 		
@@ -309,15 +317,18 @@ void fann_compute_MSE(struct fann *ann, fann_type *desired_output)
 	for(; last_layer_begin != last_layer_end; last_layer_begin++){
 		neuron_value = last_layer_begin->value;
 		neuron_diff = *desired_output - neuron_value;
-		/*printf("neuron_diff %f = %f - %f ... %f\n", neuron_diff, *desired_output, neuron_value, last_layer_begin->sum);*/
 
 		if(ann->activation_function_output == FANN_SIGMOID_SYMMETRIC ||
-			ann->activation_function_output == FANN_SIGMOID_SYMMETRIC_STEPWISE){
+			ann->activation_function_output == FANN_SIGMOID_SYMMETRIC_STEPWISE ||
+			ann->activation_function_output == FANN_ELLIOT_SYMMETRIC ||
+			ann->activation_function_output == FANN_GAUSSIAN_SYMMETRIC){
 			neuron_diff /= 2.0;
 		}
 
 		neuron_diff2 = (float)(neuron_diff * neuron_diff);
 		ann->MSE_value += neuron_diff2;
+
+		/*printf("neuron_diff %f = (%f - %f)[/2], neuron_diff2=%f, sum=%f, MSE_value=%f, num_MSE=%d\n", neuron_diff, *desired_output, neuron_value, neuron_diff2, last_layer_begin->sum, ann->MSE_value, ann->num_MSE);*/
 		if(neuron_diff2 >= 0.25){
 			ann->num_bit_fail++;
 		}
@@ -745,7 +756,6 @@ void fann_update_weights_irpropm(struct fann *ann, unsigned int first_weight, un
 		prev_step = fann_max(prev_steps[i], (fann_type)0.001); /* prev_step may not be zero because then the training will stop */
 		slope = train_slopes[i];
 		prev_slope = prev_train_slopes[i];
-		/* next_step = 0.0; */
 
 		same_sign = prev_slope * slope;
 	
@@ -754,6 +764,8 @@ void fann_update_weights_irpropm(struct fann *ann, unsigned int first_weight, un
 		} else if(same_sign < 0.0) {
 			next_step = fann_max(prev_step * decrease_factor, delta_min);
 			slope = 0;
+		} else {
+			next_step = 0.0;
 		}
 
 		if(slope < 0){
@@ -763,7 +775,7 @@ void fann_update_weights_irpropm(struct fann *ann, unsigned int first_weight, un
 		}
 
 		/*if(i == 2){
-			printf("weight=%f, slope=%f, next_step=%f, prev_step=%f\n", weights[i], slope, next_step, prev_step);
+		printf("weight=%f, slope=%f, next_step=%f, prev_step=%f\n", weights[i], slope, next_step, prev_step);
 			}*/
 	
 		/* update global data arrays */
diff --git a/src/fann_train_data.c b/src/fann_train_data.c
index 02d2c7d..e05f83f 100644
--- a/src/fann_train_data.c
+++ b/src/fann_train_data.c
@@ -129,6 +129,7 @@ float fann_train_epoch_batch(struct fann *ann, struct fann_train_data *data)
 		fann_backpropagate_MSE(ann);
 		fann_update_slopes_batch(ann, ann->first_layer+1, ann->last_layer-1);
 	}
+	
 	fann_update_weights_batch(ann, data->num_data, 0, ann->total_connections);
 
 	return fann_get_MSE(ann);
@@ -313,10 +314,10 @@ void fann_scale_data(fann_type **data, unsigned int num_data, unsigned int num_e
 			temp = (data[dat][elem] - old_min) * factor + new_min;
 			if(temp < new_min){
 				data[dat][elem] = new_min;
-				printf("error %f < %f\n", temp, new_min);
+				/*printf("error %f < %f\n", temp, new_min);*/
 			} else if(temp > new_max){
 				data[dat][elem] = new_max;
-				printf("error %f > %f\n", temp, new_max);
+				/*printf("error %f > %f\n", temp, new_max);*/
 			} else {
 				data[dat][elem] = temp;
 			}
diff --git a/src/include/fann_activation.h b/src/include/fann_activation.h
index efbc65b..3491277 100644
--- a/src/include/fann_activation.h
+++ b/src/include/fann_activation.h
@@ -81,7 +81,7 @@ enum {
 	   0 when x = -inf, 1 when x = 0 and 0 when x = inf
 	   span: 0 < y < 1
 	   y = exp(-x*s*x*s)
-	   d = -2*x*y*s
+	   d = -2*x*s*y*s
 	*/
 	FANN_GAUSSIAN,
 
@@ -89,7 +89,7 @@ enum {
 	   -1 when x = -inf, 1 when x = 0 and 0 when x = inf
 	   span: -1 < y < 1
 	   y = exp(-x*s*x*s)*2-1
-	   d = -4*x*y*s
+	   d = -2*x*s*(y+1)*s
 	*/
 	FANN_GAUSSIAN_SYMMETRIC,
 
@@ -102,14 +102,14 @@ enum {
 	/* Fast (sigmoid like) activation function defined by David Elliott
 	   span: 0 < y < 1
 	   y = ((x*s) / 2) / (1 + |x*s|) + 0.5
-	   d = s*1/(2*(1+|x|)*(1+|x|))
+	   d = s*1/(2*(1+|x*s|)*(1+|x*s|))
 	*/
 	FANN_ELLIOT,
 
 	/* Fast (symmetric sigmoid like) activation function defined by David Elliott
 	   span: -1 < y < 1   
 	   y = (x*s) / (1 + |x*s|)
-	   d = s*1/((1+|x|)*(1+|x|))
+	   d = s*1/((1+|x*s|)*(1+|x*s|))
 	*/
 	FANN_ELLIOT_SYMMETRIC
 };
@@ -139,8 +139,6 @@ static char const * const FANN_ACTIVATION_NAMES[] = {
 #define fann_linear_func(v1, r1, v2, r2, sum) ((((r2-r1) * (sum-v1))/(v2-v1)) + r1)
 #define fann_stepwise(v1, v2, v3, v4, v5, v6, r1, r2, r3, r4, r5, r6, min, max, sum) (sum < v5 ? (sum < v3 ? (sum < v2 ? (sum < v1 ? min : fann_linear_func(v1, r1, v2, r2, sum)) : fann_linear_func(v2, r2, v3, r3, sum)) : (sum < v4 ? fann_linear_func(v3, r3, v4, r4, sum) : fann_linear_func(v4, r4, v5, r5, sum))) : (sum < v6 ? fann_linear_func(v5, r5, v6, r6, sum) : max))
 
-
-
 /* FANN_LINEAR */
 #define fann_linear(steepness, sum) fann_mult(steepness, sum)
 #define fann_linear_derive(steepness, value) (steepness)
@@ -163,7 +161,7 @@ static char const * const FANN_ACTIVATION_NAMES[] = {
 /* FANN_GAUSSIAN_SYMMETRIC */
 #define fann_gaussian_symmetric(steepness, sum) ((exp(-sum * steepness * sum * steepness)*2.0)-1.0)
 #define fann_gaussian_symmetric_real(sum) ((exp(-sum * sum)*2.0)-1.0)
-#define fann_gaussian_symmetric_derive(steepness, value, sum) (-4.0f * sum * value * steepness)
+#define fann_gaussian_symmetric_derive(steepness, value, sum) (-2.0f * sum * (value+1.0f) * steepness)
 
 /* FANN_ELLIOT */
 #define fann_elliot(steepness, sum) (((sum * steepness) / 2.0f) / (1.0f + abs(sum * steepness)) + 0.5f)
@@ -172,7 +170,7 @@ static char const * const FANN_ACTIVATION_NAMES[] = {
 
 /* FANN_ELLIOT_SYMMETRIC */
 #define fann_elliot_symmetric(steepness, sum) ((sum * steepness) / (1.0f + abs(sum * steepness)))
-#define fann_elliot_symmetric_real(sum) ((sum) / (1.0f + abs(steepness)))
+#define fann_elliot_symmetric_real(sum) ((sum) / (1.0f + abs(sum)))
 #define fann_elliot_symmetric_derive(steepness, value, sum) (steepness * 1.0f / ((1.0f + abs(sum)) * (1.0f + abs(sum))))
 
 #define fann_activation_switch(ann, activation_function, value, result) \
diff --git a/src/include/fann_internal.h b/src/include/fann_internal.h
index e6f1ff8..69258da 100644
--- a/src/include/fann_internal.h
+++ b/src/include/fann_internal.h
@@ -80,8 +80,8 @@ fann_type fann_activation_derived(unsigned int activation_function,
 #define fann_max(x, y) (((x) > (y)) ? (x) : (y))
 #define fann_min(x, y) (((x) < (y)) ? (x) : (y))
 #define fann_safe_free(x) {if(x) { free(x); x = NULL; }}
-/* #define fann_clip(x, lo, hi) (((x) < (lo)) ? (lo) : (((x) > (hi)) ? (hi) : (x))) */
-#define fann_clip(x, lo, hi) (x)
+#define fann_clip(x, lo, hi) (((x) < (lo)) ? (lo) : (((x) > (hi)) ? (hi) : (x)))
+/*#define fann_clip(x, lo, hi) (x)*/
 
 #define fann_rand(min_value, max_value) (((double)(min_value))+(((double)(max_value)-((double)(min_value)))*rand()/(RAND_MAX+1.0)))
 

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