/
mnist_test.c
101 lines (83 loc) · 2.75 KB
/
mnist_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2012 Steffen Nissen (sn@leenissen.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include "fann.h"
int main()
{
fann_type *calc_out;
unsigned int i;
int ret = 0;
int max_expected_idx=0,max_predicted_idx=0,count=0;
struct fann *ann;
struct fann_train_data *data;
printf("Creating network.\n");
#ifdef FIXEDFANN
ann = fann_create_from_file("mnist_fixed1.net");
#else
ann = fann_create_from_file("mnist_float.net");
#endif
if(!ann)
{
printf("Error creating ann --- ABORTING.\n");
return -1;
}
fann_print_connections(ann);
fann_print_parameters(ann);
printf("Testing network.\n");
#ifdef FIXEDFANN
data = fann_read_train_from_file("mnist.data");
#else
data = fann_read_train_from_file("mnist.data");
#endif
for(i = 0; i < fann_length_train_data(data); i++)
{
fann_reset_MSE(ann);
calc_out = fann_test(ann, data->input[i], data->output[i]);
#ifdef FIXEDFANN
printf("XOR test (%d, %d) -> %d, should be %d, difference=%f\n",
data->input[i][0], data->input[i][1], calc_out[0], data->output[i][0],
(float) fann_abs(calc_out[0] - data->output[i][0]) / fann_get_multiplier(ann));
if((float) fann_abs(calc_out[0] - data->output[i][0]) / fann_get_multiplier(ann) > 0.2)
{
printf("Test failed\n");
ret = -1;
}
#else
max_expected_idx = 0;
max_predicted_idx = 0;
for(int k=1;k<10;k++)
{
if(data->output[i][max_expected_idx] < data->output[i][k])
{
max_expected_idx = k;
}
if(calc_out[max_predicted_idx] < calc_out[k])
{
max_predicted_idx = k;
}
}
printf("MNIST test %d Expected %d , returned=%d\n",
i,max_expected_idx, max_predicted_idx);
if(max_expected_idx == max_predicted_idx)
count++;
#endif
}
printf("Cleaning up.\n");
fann_destroy_train(data);
fann_destroy(ann);
printf("Number correct=%d\n",count);
return ret;
}