-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
digit5x7.c
146 lines (118 loc) · 4.45 KB
/
digit5x7.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**********************************************************************************/
/* Copyright (c) 2023 Mark Seminatore */
/* All rights reserved. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a copy */
/* of this software and associated documentation files(the "Software"), to deal */
/* in the Software without restriction, including without limitation the rights */
/* to use, copy, modify, merge, publish, distribute, sublicense, and / or sell */
/* copies of the Software, and to permit persons to whom the Software is */
/* furnished to do so, subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in all */
/* copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "ann.h"
#ifdef _WIN32
# define DIR_FIX "..\\"
#else
# define DIR_FIX
#endif
//---------------------------------------
// print the Ci with highest probability
//---------------------------------------
void print_class_pred(real * data)
{
int class = ann_class_prediction(data, 10);
printf("\n5x7 character prediction is: %d\n\n", class + 1);
}
//------------------------------
// display a 5x7 char from flat data
//------------------------------
void print5x7(real *data)
{
char c;
puts("\nInput vector\n");
for (int row = 0 ; row < 7; row++)
{
for (int col = 0; col < 5; col++)
{
c = data[row * 5 + col] == 1.0 ? '+' : ' ';
putchar(c);
}
puts("");
}
}
//------------------------------
// add noise to image
//------------------------------
void add_noise(real *data, int size, int amount)
{
for (int i = 0; i < amount; i++)
{
int index = rand() % size;
data[index] = (data[index] == 1.0) ? (real)0.0 : (real)1.0;
}
}
//------------------------------
// main program start
//------------------------------
int main(int argc, char *argv[])
{
char *filename = DIR_FIX "num5x7.csv";
PNetwork pnet = ann_make_network(OPT_DEFAULT, LOSS_DEFAULT);
if (!pnet)
return ERR_FAIL;
real *data;
int rows, stride;
// load the data
if (argc > 1)
filename = argv[1];
printf("Loading %s...", filename);
CHECK_OK(ann_load_csv(filename, CSV_NO_HEADER, &data, &rows, &stride));
puts("done.");
PTensor x_train = tensor_create_from_array(rows, stride, data);
if (!x_train)
return ERR_FAIL;
PTensor y_train = tensor_slice_cols(x_train, 35);
if (!y_train)
return ERR_FAIL;
pnet->learning_rate = (real)0.35;
// define our network
CHECK_OK(ann_add_layer(pnet, 35, LAYER_INPUT, ACTIVATION_NULL));
CHECK_OK(ann_add_layer(pnet, 48, LAYER_HIDDEN, ACTIVATION_SIGMOID));
CHECK_OK(ann_add_layer(pnet, 10, LAYER_OUTPUT, ACTIVATION_SIGMOID));
ann_train_network(pnet, x_train, y_train, x_train->rows);
int correct = 0;
for (int i = 0; i < 10; i++)
{
add_noise(&data[i * 45], 35, 3);
real outputs[10];
CHECK_OK(ann_predict(pnet, &data[i * 45], outputs));
print5x7(&data[i * 45]);
print_class_pred(outputs);
int pred_class = ann_class_prediction(outputs, 10);
int class = ann_class_prediction(&y_train->values[i * 10], 10);
if (pred_class == class)
correct++;
printf("Actual class: %d\n", class + 1);
}
real acc = ann_evaluate_accuracy(pnet, x_train, y_train);
printf("Train accuracy: %g%%\n", acc * 100);
printf("Test accuracy: %g%%\n", (real)correct * 10);
ann_free_network(pnet);
tensor_free(x_train);
tensor_free(y_train);
free(data);
return ERR_OK;
}