[libfann] 209/242: changes to save dataformat
Christian Kastner
chrisk-guest at moszumanska.debian.org
Sat Oct 4 21:10:45 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 6a16b6212776cf8f7c10d3a46734211e471e0a9c
Author: Steffen Nissen <lukesky at diku.dk>
Date: Tue Nov 22 19:50:19 2005 +0000
changes to save dataformat
---
examples/xor_train.c | 4 +-
src/fann_error.c | 2 +-
src/fann_io.c | 252 +++++++++++++++-----------------------------
src/fann_train_data.c | 88 ++++++++++++++++
src/include/fann_internal.h | 4 +-
5 files changed, 176 insertions(+), 174 deletions(-)
diff --git a/examples/xor_train.c b/examples/xor_train.c
index 44c4131..e1381e4 100644
--- a/examples/xor_train.c
+++ b/examples/xor_train.c
@@ -57,10 +57,10 @@ int main()
fann_set_activation_steepness_output(ann, 1);
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
- fann_set_activation_function_output(ann, FANN_GAUSSIAN_SYMMETRIC);
+ fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_train_stop_function(ann, FANN_STOPFUNC_BIT);
- fann_set_bit_fail_limit(ann, 0.1);
+ fann_set_bit_fail_limit(ann, 0.01);
fann_init_weights(ann, data);
diff --git a/src/fann_error.c b/src/fann_error.c
index 17da363..034b99a 100644
--- a/src/fann_error.c
+++ b/src/fann_error.c
@@ -130,7 +130,7 @@ void fann_error(struct fann_error *errdat, const enum fann_errno_enum errno_f, .
ap);
break;
case FANN_E_CANT_READ_CONFIG:
- vsprintf(errstr, "Error reading info from configuration file \"%s\".\n", ap);
+ vsprintf(errstr, "Error reading \"%s\" from configuration file \"%s\".\n", ap);
break;
case FANN_E_CANT_READ_NEURON:
vsprintf(errstr, "Error reading neuron info from configuration file \"%s\".\n", ap);
diff --git a/src/fann_io.c b/src/fann_io.c
index 42d2094..d328920 100644
--- a/src/fann_io.c
+++ b/src/fann_io.c
@@ -166,35 +166,20 @@ int fann_save_internal_fd(struct fann *ann, FILE * conf, const char *configurati
/* save the decimal_point on a seperate line */
fprintf(conf, "%u\n", decimal_point);
-
- /* save the number layers "num_layers learning_rate connection_rate shortcut_connections activation_function_hidden activation_function_output activation_steepness_hidden activation_steepness_output"
- * fprintf(conf, "%u %f %f %u %u %u %d %d\n", ann->last_layer - ann->first_layer, ann->learning_rate, ann->connection_rate, ann->shortcut_connections, ann->activation_function_hidden, ann->activation_function_output, (int)(ann->activation_steepness_hidden * fixed_multiplier), (int)(ann->activation_steepness_output * fixed_multiplier));
- */
- /* save the number layers "num_layers learning_rate connection_rate shortcut_connections" */
- fprintf(conf, "%u %f %f %u\n", ann->last_layer - ann->first_layer, ann->learning_rate,
- ann->connection_rate, ann->shortcut_connections);
- }
- else
- {
- /* save the number layers "num_layers learning_rate connection_rate shortcut_connections activation_function_hidden activation_function_output activation_steepness_hidden activation_steepness_output"
- * fprintf(conf, "%u %f %f %u %u %u "FANNPRINTF" "FANNPRINTF"\n", ann->last_layer - ann->first_layer, ann->learning_rate, ann->connection_rate, ann->shortcut_connections, ann->activation_function_hidden, ann->activation_function_output, ann->activation_steepness_hidden, ann->activation_steepness_output);
- */
- /* save the number layers "num_layers learning_rate connection_rate shortcut_connections" */
- fprintf(conf, "%u %f %f %u\n", ann->last_layer - ann->first_layer, ann->learning_rate,
- ann->connection_rate, ann->shortcut_connections);
}
#else
/* save the decimal_point on a seperate line */
fprintf(conf, "%u\n", ann->decimal_point);
- /* save the number layers "num_layers learning_rate connection_rate shortcut_connections activation_function_hidden activation_function_output activation_steepness_hidden activation_steepness_output"
- * fprintf(conf, "%u %f %f %u %u %u "FANNPRINTF" "FANNPRINTF"\n", ann->last_layer - ann->first_layer, ann->learning_rate, ann->connection_rate, ann->shortcut_connections, ann->activation_function_hidden, ann->activation_function_output, ann->activation_steepness_hidden, ann->activation_steepness_output);
- */
-
- /* save the number layers "num_layers learning_rate connection_rate shortcut_connections activation_function_hidden activation_function_output activation_steepness_hidden activation_steepness_output" */
- fprintf(conf, "%u %f %f %u\n", ann->last_layer - ann->first_layer, ann->learning_rate,
- ann->connection_rate, ann->shortcut_connections);
#endif
+/* fprintf(conf, "%u %f %f %u\n", ann->last_layer - ann->first_layer, ann->learning_rate,
+ ann->connection_rate, ann->shortcut_connections);*/
+
+ /* Save network parameters */
+ fprintf(conf, "num_layers=%u\n", ann->last_layer - ann->first_layer);
+ fprintf(conf, "learning_rate=%f\n", ann->learning_rate);
+ fprintf(conf, "connection_rate=%f\n", ann->connection_rate);
+ fprintf(conf, "shortcut_connections=%u\n", ann->shortcut_connections);
for(layer_it = ann->first_layer; layer_it != ann->last_layer; layer_it++)
{
@@ -268,110 +253,66 @@ int fann_save_internal_fd(struct fann *ann, FILE * conf, const char *configurati
return calculated_decimal_point;
}
-/* INTERNAL FUNCTION
- Save the train data structure.
- */
-void fann_save_train_internal(struct fann_train_data *data, const char *filename,
- unsigned int save_as_fixed, unsigned int decimal_point)
-{
- FILE *file = fopen(filename, "w");
+struct fann *fann_create_from_fd_1_1(FILE * conf, const char *configuration_file);
- if(!file)
- {
- fann_error((struct fann_error *) data, FANN_E_CANT_OPEN_TD_W, filename);
- return;
- }
- fann_save_train_internal_fd(data, file, filename, save_as_fixed, decimal_point);
- fclose(file);
+#define fann_scanf(type, name, val) \
+{ \
+ if(fscanf(conf, name"="type"\n", &val) != 1) \
+ { \
+ fann_error(NULL, FANN_E_CANT_READ_CONFIG, name, configuration_file); \
+ fann_destroy(ann); \
+ return NULL; \
+ } \
}
/* INTERNAL FUNCTION
- Save the train data structure.
+ Create a network from a configuration file descriptor.
*/
-void fann_save_train_internal_fd(struct fann_train_data *data, FILE * file, const char *filename,
- unsigned int save_as_fixed, unsigned int decimal_point)
+struct fann *fann_create_from_fd(FILE * conf, const char *configuration_file)
{
- unsigned int num_data = data->num_data;
- unsigned int num_input = data->num_input;
- unsigned int num_output = data->num_output;
- unsigned int i, j;
+ unsigned int num_layers, layer_size, input_neuron, i, shortcut_connections, num_connections;
-#ifndef FIXEDFANN
- unsigned int multiplier = 1 << decimal_point;
+#ifdef FIXEDFANN
+ unsigned int decimal_point, multiplier;
#endif
+ 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;
- fprintf(file, "%u %u %u\n", data->num_data, data->num_input, data->num_output);
+ char *read_version;
- for(i = 0; i < num_data; i++)
+ read_version = (char *) calloc(strlen(FANN_CONF_VERSION "\n"), 1);
+ if(read_version == NULL)
{
- for(j = 0; j < num_input; j++)
- {
-#ifndef FIXEDFANN
- if(save_as_fixed)
- {
- fprintf(file, "%d ", (int) (data->input[i][j] * multiplier));
- }
- else
- {
- if(((int) floor(data->input[i][j] + 0.5) * 1000000) ==
- ((int) floor(data->input[i][j] * 1000000.0 + 0.5)))
- {
- fprintf(file, "%d ", (int) data->input[i][j]);
- }
- else
- {
- fprintf(file, "%f ", data->input[i][j]);
- }
- }
-#else
- fprintf(file, FANNPRINTF " ", data->input[i][j]);
-#endif
- }
- fprintf(file, "\n");
+ fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
+ return NULL;
+ }
+
+ fread(read_version, 1, strlen(FANN_CONF_VERSION "\n"), conf); /* reads version */
- for(j = 0; j < num_output; j++)
+ /* compares the version information */
+ if(strncmp(read_version, FANN_CONF_VERSION "\n", strlen(FANN_CONF_VERSION "\n")) != 0)
+ {
+#ifdef FIXEDFANN
+ if(strncmp(read_version, "FANN_FIX_1.1\n", strlen("FANN_FIX_1.1\n")) == 0)
{
-#ifndef FIXEDFANN
- if(save_as_fixed)
- {
- fprintf(file, "%d ", (int) (data->output[i][j] * multiplier));
- }
- else
- {
- if(((int) floor(data->output[i][j] + 0.5) * 1000000) ==
- ((int) floor(data->output[i][j] * 1000000.0 + 0.5)))
- {
- fprintf(file, "%d ", (int) data->output[i][j]);
- }
- else
- {
- fprintf(file, "%f ", data->output[i][j]);
- }
- }
#else
- fprintf(file, FANNPRINTF " ", data->output[i][j]);
+ if(strncmp(read_version, "FANN_FLO_1.1\n", strlen("FANN_FLO_1.1\n")) == 0)
+ {
#endif
+ free(read_version);
+ return fann_create_from_fd_1_1(conf, configuration_file);
}
- fprintf(file, "\n");
+
+ free(read_version);
+ fann_error(NULL, FANN_E_WRONG_CONFIG_VERSION, configuration_file);
+
+ return NULL;
}
-}
-/* INTERNAL FUNCTION
- Create a network from a configuration file descriptor. (backward compatible read of version 1.1 files)
- */
-struct fann *fann_create_from_fd_1_1(FILE * conf, const char *configuration_file)
-{
- unsigned int num_layers, layer_size, input_neuron, i, shortcut_connections, num_connections;
- unsigned int activation_function_hidden, activation_function_output;
-#ifdef FIXEDFANN
- unsigned int decimal_point, multiplier;
-#endif
- fann_type activation_steepness_hidden, activation_steepness_output;
- 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;
+ free(read_version);
#ifdef FIXEDFANN
if(fscanf(conf, "%u\n", &decimal_point) != 1)
@@ -382,24 +323,27 @@ struct fann *fann_create_from_fd_1_1(FILE * conf, const char *configuration_file
multiplier = 1 << decimal_point;
#endif
+ fann_scanf("%u", "num_layers", num_layers);
+
+/*
if(fscanf
- (conf, "%u %f %f %u %u %u " FANNSCANF " " FANNSCANF "\n", &num_layers, &learning_rate,
- &connection_rate, &shortcut_connections, &activation_function_hidden,
- &activation_function_output, &activation_steepness_hidden,
- &activation_steepness_output) != 8)
+ (conf, "%u %f %f %u\n", &num_layers, &learning_rate, &connection_rate,
+ &shortcut_connections) != 4)
{
fann_error(NULL, FANN_E_CANT_READ_CONFIG, configuration_file);
return NULL;
}
+ */
ann = fann_allocate_structure(num_layers);
if(ann == NULL)
{
return NULL;
}
- ann->connection_rate = connection_rate;
- ann->shortcut_connections = shortcut_connections;
- ann->learning_rate = learning_rate;
+
+ fann_scanf("%f", "learning_rate", ann->learning_rate);
+ fann_scanf("%f", "connection_rate", ann->connection_rate);
+ fann_scanf("%u", "shortcut_connections", ann->shortcut_connections);
#ifdef FIXEDFANN
ann->decimal_point = decimal_point;
@@ -460,7 +404,9 @@ struct fann *fann_create_from_fd_1_1(FILE * conf, const char *configuration_file
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 ", &num_connections) != 1)
+ if(fscanf
+ (conf, "%u %u " FANNSCANF " ", &num_connections, (unsigned int *)&neuron_it->activation_function,
+ &neuron_it->activation_steepness) != 3)
{
fann_error((struct fann_error *) ann, FANN_E_CANT_READ_NEURON, configuration_file);
fann_destroy(ann);
@@ -493,10 +439,12 @@ struct fann *fann_create_from_fd_1_1(FILE * conf, const char *configuration_file
connected_neurons[i] = first_neuron + input_neuron;
}
- fann_set_activation_steepness_hidden(ann, activation_steepness_hidden);
- fann_set_activation_steepness_output(ann, activation_steepness_output);
- fann_set_activation_function_hidden(ann, (enum fann_activationfunc_enum)activation_function_hidden);
- fann_set_activation_function_output(ann, (enum fann_activationfunc_enum)activation_function_output);
+ /*
+ * fann_set_activation_steepness_hidden(ann, activation_steepness_hidden);
+ * fann_set_activation_steepness_output(ann, activation_steepness_output);
+ * fann_set_activation_function_hidden(ann, activation_function_hidden);
+ * fann_set_activation_function_output(ann, activation_function_output);
+ */
#ifdef DEBUG
printf("output\n");
@@ -504,55 +452,24 @@ struct fann *fann_create_from_fd_1_1(FILE * conf, const char *configuration_file
return ann;
}
+
/* INTERNAL FUNCTION
- Create a network from a configuration file descriptor.
+ Create a network from a configuration file descriptor. (backward compatible read of version 1.1 files)
*/
-struct fann *fann_create_from_fd(FILE * conf, const char *configuration_file)
+struct fann *fann_create_from_fd_1_1(FILE * conf, const char *configuration_file)
{
unsigned int num_layers, layer_size, input_neuron, i, shortcut_connections, num_connections;
-
+ unsigned int activation_function_hidden, activation_function_output;
#ifdef FIXEDFANN
unsigned int decimal_point, multiplier;
#endif
+ fann_type activation_steepness_hidden, activation_steepness_output;
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)
- {
-#ifdef FIXEDFANN
- if(strncmp(read_version, "FANN_FIX_1.1\n", strlen("FANN_FIX_1.1\n")) == 0)
- {
-#else
- if(strncmp(read_version, "FANN_FLO_1.1\n", strlen("FANN_FLO_1.1\n")) == 0)
- {
-#endif
- free(read_version);
- return fann_create_from_fd_1_1(conf, configuration_file);
- }
-
- free(read_version);
- fann_error(NULL, FANN_E_WRONG_CONFIG_VERSION, configuration_file);
-
- return NULL;
- }
-
- free(read_version);
-
#ifdef FIXEDFANN
if(fscanf(conf, "%u\n", &decimal_point) != 1)
{
@@ -562,9 +479,10 @@ struct fann *fann_create_from_fd(FILE * conf, const char *configuration_file)
multiplier = 1 << decimal_point;
#endif
- if(fscanf
- (conf, "%u %f %f %u\n", &num_layers, &learning_rate, &connection_rate,
- &shortcut_connections) != 4)
+ if(fscanf(conf, "%u %f %f %u %u %u " FANNSCANF " " FANNSCANF "\n", &num_layers, &learning_rate,
+ &connection_rate, &shortcut_connections, &activation_function_hidden,
+ &activation_function_output, &activation_steepness_hidden,
+ &activation_steepness_output) != 8)
{
fann_error(NULL, FANN_E_CANT_READ_CONFIG, configuration_file);
return NULL;
@@ -638,9 +556,7 @@ struct fann *fann_create_from_fd(FILE * conf, const char *configuration_file)
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 %u " FANNSCANF " ", &num_connections, (unsigned int *)&neuron_it->activation_function,
- &neuron_it->activation_steepness) != 3)
+ if(fscanf(conf, "%u ", &num_connections) != 1)
{
fann_error((struct fann_error *) ann, FANN_E_CANT_READ_NEURON, configuration_file);
fann_destroy(ann);
@@ -673,12 +589,10 @@ struct fann *fann_create_from_fd(FILE * conf, const char *configuration_file)
connected_neurons[i] = first_neuron + input_neuron;
}
- /*
- * fann_set_activation_steepness_hidden(ann, activation_steepness_hidden);
- * fann_set_activation_steepness_output(ann, activation_steepness_output);
- * fann_set_activation_function_hidden(ann, activation_function_hidden);
- * fann_set_activation_function_output(ann, activation_function_output);
- */
+ fann_set_activation_steepness_hidden(ann, activation_steepness_hidden);
+ fann_set_activation_steepness_output(ann, activation_steepness_output);
+ fann_set_activation_function_hidden(ann, (enum fann_activationfunc_enum)activation_function_hidden);
+ fann_set_activation_function_output(ann, (enum fann_activationfunc_enum)activation_function_output);
#ifdef DEBUG
printf("output\n");
diff --git a/src/fann_train_data.c b/src/fann_train_data.c
index 1de8d0f..aff133f 100644
--- a/src/fann_train_data.c
+++ b/src/fann_train_data.c
@@ -626,6 +626,94 @@ FANN_EXTERNAL unsigned int FANN_API fann_num_output_train_data(struct fann_train
return data->num_output;
}
+/* INTERNAL FUNCTION
+ Save the train data structure.
+ */
+void fann_save_train_internal(struct fann_train_data *data, const char *filename,
+ unsigned int save_as_fixed, unsigned int decimal_point)
+{
+ FILE *file = fopen(filename, "w");
+
+ if(!file)
+ {
+ fann_error((struct fann_error *) data, FANN_E_CANT_OPEN_TD_W, filename);
+ return;
+ }
+ fann_save_train_internal_fd(data, file, filename, save_as_fixed, decimal_point);
+ fclose(file);
+}
+
+/* INTERNAL FUNCTION
+ Save the train data structure.
+ */
+void fann_save_train_internal_fd(struct fann_train_data *data, FILE * file, const 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;
+ unsigned int num_output = data->num_output;
+ unsigned int i, j;
+
+#ifndef FIXEDFANN
+ unsigned int multiplier = 1 << decimal_point;
+#endif
+
+ fprintf(file, "%u %u %u\n", data->num_data, data->num_input, data->num_output);
+
+ for(i = 0; i < num_data; i++)
+ {
+ for(j = 0; j < num_input; j++)
+ {
+#ifndef FIXEDFANN
+ if(save_as_fixed)
+ {
+ fprintf(file, "%d ", (int) (data->input[i][j] * multiplier));
+ }
+ else
+ {
+ if(((int) floor(data->input[i][j] + 0.5) * 1000000) ==
+ ((int) floor(data->input[i][j] * 1000000.0 + 0.5)))
+ {
+ fprintf(file, "%d ", (int) data->input[i][j]);
+ }
+ else
+ {
+ fprintf(file, "%f ", data->input[i][j]);
+ }
+ }
+#else
+ fprintf(file, FANNPRINTF " ", data->input[i][j]);
+#endif
+ }
+ fprintf(file, "\n");
+
+ for(j = 0; j < num_output; j++)
+ {
+#ifndef FIXEDFANN
+ if(save_as_fixed)
+ {
+ fprintf(file, "%d ", (int) (data->output[i][j] * multiplier));
+ }
+ else
+ {
+ if(((int) floor(data->output[i][j] + 0.5) * 1000000) ==
+ ((int) floor(data->output[i][j] * 1000000.0 + 0.5)))
+ {
+ fprintf(file, "%d ", (int) data->output[i][j]);
+ }
+ else
+ {
+ fprintf(file, "%f ", data->output[i][j]);
+ }
+ }
+#else
+ fprintf(file, FANNPRINTF " ", data->output[i][j]);
+#endif
+ }
+ fprintf(file, "\n");
+ }
+}
+
/*
* INTERNAL FUNCTION Reads training data from a file descriptor.
diff --git a/src/include/fann_internal.h b/src/include/fann_internal.h
index 9325be0..f334281 100644
--- a/src/include/fann_internal.h
+++ b/src/include/fann_internal.h
@@ -27,8 +27,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <stdlib.h>
#include "fann_data.h"
-#define FANN_FIX_VERSION "FANN_FIX_1.3"
-#define FANN_FLO_VERSION "FANN_FLO_1.3"
+#define FANN_FIX_VERSION "FANN_FIX_2.0"
+#define FANN_FLO_VERSION "FANN_FLO_2.0"
#ifdef FIXEDFANN
#define FANN_CONF_VERSION FANN_FIX_VERSION
--
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