-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylib.c
143 lines (119 loc) · 3.39 KB
/
mylib.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "flexarray.h"
#include "mylib.h"
void *emalloc(size_t s) {
void *result = malloc(s);
if (NULL == result) {
fprintf(stderr, "Memory allocation failed!\n");
exit(EXIT_FAILURE);
}
return result;
}
void *erealloc(void *p, size_t s) {
void *result = realloc(p, s);
if (NULL == result) {
fprintf(stderr, "Memory realloction failed!\n");
exit(EXIT_FAILURE);
}
return result;
}
double sigmoid(double x) {
return (1 / (1 + exp(-x)));
}
#define DEFAULT_SIZE 6000
/* A function for returning the label of a single instance of the mnist training
* data.
* @param: file - the name of the file which contains the labels and the
* respective input values.
* @return: label - the value for that particular example.
*/
int *get_labels(char *file) {
FILE *infile = fopen(file, "r");
int label, count = 0, i = 0;
char c;
int *labels = emalloc(DEFAULT_SIZE * sizeof labels[0]);
if (infile) {
while (EOF != (c = getc(infile)) && i < DEFAULT_SIZE) {
if (c == ',' || c == '\n') {
count++;
c = getc(infile);
}
if (count % 785 == 0) {
label = c - '0';
labels[i++] = label;
}
}
}
fclose(infile);
return labels;
}
/* A function returning an array of integer values between 0 and 255
* representing the greyscale values of each pixel in a 28 x 28 image of a
* handdrawn digit. get_inputs uses a flexarray ADT to concatonate each value
* and add it to a array.
* @param: file - the filename wich contains the labels and respective input
* values.
* @return: inputs - the new array containg 784 in integers representing each
* pixel.
*/
double *get_inputs(char *file, int position) {
FILE *infile = fopen(file, "r");
flexarray f;
double *inputs = emalloc(784 * sizeof inputs[0]);
char c;
int num_inputs = 0, i;
f = flexarray_new();
if (infile) {
i = 0;
while (i < position * 785) {
c = getc(infile);
if (c == ',' || c == '\n') {
i++;
}
}
c = getc(infile);
c = getc(infile);
while (EOF != (c = getc(infile)) && num_inputs < 784) {
if (c >= '0' && c <= '9') {
flexarray_append(f, c);
} else {
inputs[num_inputs] = (double)flexarray_get_key(f)/ 255;
flexarray_free(f);
f = flexarray_new();
num_inputs++;
}
}
}
if (f != NULL) {
flexarray_free(f);
}
fclose(infile);
return inputs;
}
void print_inputs(char *file) {
FILE *infile = fopen(file, "r");
char c;
int num_inputs = 0, columns = 0;;
if (infile) {
c = getc(infile);
c = getc(infile);
while (EOF != (c = getc(infile)) && num_inputs < 784) {
if (c >= '0' && c <= '9') {
fprintf(stderr, "%c", c);
} else {
if (columns < 27) {
num_inputs++;
columns++;
fprintf(stderr, "%c", ',');
} else {
num_inputs++;
columns = 0;
fprintf(stderr, "\n");
}
}
}
}
fclose(infile);
}