From 3364eeaf918c348094e3b61d7b6e0aad90bb2361 Mon Sep 17 00:00:00 2001 From: Jakub Zelenka Date: Mon, 6 Jul 2026 17:24:57 +0200 Subject: [PATCH] Use new format version for adam The optional was problematic due to BC with old format --- src/fann_io.c | 26 ++++++++++---------------- src/include/fann_internal.h | 4 ++-- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/fann_io.c b/src/fann_io.c index 71450bf..85744f3 100644 --- a/src/fann_io.c +++ b/src/fann_io.c @@ -325,18 +325,6 @@ struct fann *fann_create_from_fd_1_1(FILE *conf, const char *configuration_file) } \ } -/* Optional scanf that sets a default value if the field is not present in the file. - * This is used for new parameters to maintain backward compatibility with older saved networks. - */ -#define fann_scanf_optional(type, name, val, default_val) \ - { \ - long pos = ftell(conf); \ - if (fscanf(conf, name "=" type "\n", val) != 1) { \ - fseek(conf, pos, SEEK_SET); \ - *(val) = (default_val); \ - } \ - } - #define fann_skip(name) \ { \ if (fscanf(conf, name) != 0) { \ @@ -363,6 +351,8 @@ struct fann *fann_create_from_fd(FILE *conf, const char *configuration_file) { struct fann *ann = NULL; char *read_version; + /* only the current version stores the adam parameters */ + int file_has_adam = 0; read_version = (char *)calloc(strlen(FANN_CONF_VERSION "\n"), 1); if (read_version == NULL) { @@ -400,6 +390,8 @@ struct fann *fann_create_from_fd(FILE *conf, const char *configuration_file) { return NULL; } + } else { + file_has_adam = 1; } free(read_version); @@ -435,10 +427,12 @@ struct fann *fann_create_from_fd(FILE *conf, const char *configuration_file) { fann_scanf("%f", "rprop_delta_min", &ann->rprop_delta_min); fann_scanf("%f", "rprop_delta_max", &ann->rprop_delta_max); fann_scanf("%f", "rprop_delta_zero", &ann->rprop_delta_zero); - /* Adam parameters are optional for backward compatibility with older saved networks */ - fann_scanf_optional("%f", "adam_beta1", &ann->adam_beta1, 0.9f); - fann_scanf_optional("%f", "adam_beta2", &ann->adam_beta2, 0.999f); - fann_scanf_optional("%f", "adam_epsilon", &ann->adam_epsilon, 1e-8f); + /* adam parameters are only present in the current version */ + if (file_has_adam) { + fann_scanf("%f", "adam_beta1", &ann->adam_beta1); + fann_scanf("%f", "adam_beta2", &ann->adam_beta2); + fann_scanf("%f", "adam_epsilon", &ann->adam_epsilon); + } fann_scanf("%u", "cascade_output_stagnation_epochs", &ann->cascade_output_stagnation_epochs); fann_scanf("%f", "cascade_candidate_change_fraction", &ann->cascade_candidate_change_fraction); fann_scanf("%u", "cascade_candidate_stagnation_epochs", diff --git a/src/include/fann_internal.h b/src/include/fann_internal.h index 8eedab2..b6f2b99 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 #include "fann_data.h" -#define FANN_FIX_VERSION "FANN_FIX_2.0" -#define FANN_FLO_VERSION "FANN_FLO_2.1" +#define FANN_FIX_VERSION "FANN_FIX_2.2" +#define FANN_FLO_VERSION "FANN_FLO_2.2" #ifdef FIXEDFANN #define FANN_CONF_VERSION FANN_FIX_VERSION