Repository navigation
Expand file tree
/
Copy pathexample.cpp
More file actions
150 lines (123 loc) · 4.41 KB
/
Copy pathexample.cpp
File metadata and controls
150 lines (123 loc) · 4.41 KB
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
147
148
149
150
#include "mlp.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <chrono>
#include <utility>
#include <time.h>
#define CHECK_TIME(x) \
{ \
struct timespec start, end; \
clock_gettime(CLOCK_REALTIME, &start); \
x; \
clock_gettime(CLOCK_REALTIME, &end); \
double f = ((double)end.tv_sec * 1e9 + end.tv_nsec) - ((double)start.tv_sec * 1e9 + start.tv_nsec); \
printf("time %f ms\n", f / 1000000); \
}
typedef std::chrono::high_resolution_clock::time_point TimeVar;
#define duration(a) std::chrono::duration_cast<std::chrono::nanoseconds>(a).count()
#define timeNow() std::chrono::high_resolution_clock::now()
template <typename F, typename... Args>
double funcTime(F func, Args &&...args)
{
TimeVar t1 = timeNow();
func(std::forward<Args>(args)...);
return duration(timeNow() - t1);
}
int main(int argc, char **argv);
void readIris();
void shuffle(int *array, int n);
#define epochs 100000
#define learning_rate 0.1
#define rand_seed 0
#define cols 4
#define out_cols 3
#define rows 150
#define train_rows 105
#define n_layers 3
namespace mai = meta_ai;
mai::MLP<float, mai::INPUT<cols>, mai::HIDDEN<7, 3>, mai::OUTPUT<out_cols>> mlp;
int order[rows];
float feat[rows * cols];
float label[rows * out_cols];
int main(int argc, char **argv)
{
srand(rand_seed);
readIris();
for (int i = 0; i < rows; ++i)
order[i] = i;
shuffle(order, rows);
CHECK_TIME(
for (int i = 0; i < epochs; i++) {
shuffle(order, train_rows);
for (int j = 0; j < train_rows; j++)
{
int row = order[j];
mlp.train(feat + row * cols, label + row * out_cols, learning_rate);
}
})
int correct = 0;
int incorrect = 0;
for (int i = train_rows; i < rows; ++i)
{
int row = order[i];
// printf("ROW: %d\n", row);
auto const &prediction = mlp.predict(feat + row * cols);
if (abs(prediction[0] - label[row * out_cols + 0]) < 0.1 &&
abs(prediction[1] - label[row * out_cols + 1]) < 0.1 &&
abs(prediction[2] - label[row * out_cols + 2]) < 0.1)
correct++;
else
incorrect++;
// printf("Predicted: [%f, %f, %f] vs Answer: [ %f, %f, %f ]\n\n", prediction[0],
// prediction[1], prediction[2], label[row * out_cols + 0],
// label[row * out_cols + 1], label[row * out_cols + 2]);
}
printf("Total de predicciones correctas: %d\n", correct);
printf("Total de predicciones incorrectas: %d\n", incorrect);
return EXIT_SUCCESS;
}
void readIris()
{
char const *const dataFileName = "iris.data";
memset(feat, 0, rows * cols * sizeof(float));
memset(label, 0, rows * out_cols * sizeof(float));
printf("Obsns size is %d and feat size is %d.\n", rows, cols);
FILE *fpDataFile = fopen(dataFileName, "r");
if (!fpDataFile)
{
printf("Missing input file: %s\n", dataFileName);
exit(1);
}
int index = 0;
char line[1024];
char flowerType[20];
float l;
while (fgets(line, 1024, fpDataFile))
{
if (5 == sscanf(line, "%f,%f,%f,%f,%f[^\n]", &feat[index * cols + 0],
&feat[index * cols + 1], &feat[index * cols + 2],
&feat[index * cols + 3], &l))
{
// printf("%f,%f,%f,%f,%f\n", feat[index * cols + 0], feat[index * cols + 1],
// feat[index * cols + 2], feat[index * cols + 3], l);
label[index * out_cols + ((int)l)] = 1;
index++;
}
}
fclose(fpDataFile);
}
void shuffle(int *array, int n)
{
if (n > 1)
{
int i;
for (i = 0; i < n - 1; i++)
{
int j = i + rand() / (RAND_MAX / (n - i) + 1);
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}