-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
220 lines (178 loc) · 5.84 KB
/
main.cc
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Fichier principal de l'utilisation du réseau de neurones
#include <algorithm>
#include <time.h>
#include <sys/types.h>
#include <GL/freeglut.h>
#include <string>
#include "reseau.h"
static Reseau Network;
void Line(int x1, int y1, int x2, int y2)
{
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd();
}
void Rectangle(int x1, int y1, int x2, int y2, bool rempli)
{
if (rempli)
glBegin(GL_POLYGON);
else
glBegin(GL_LINE_LOOP);
glVertex2f(x1, y1);
glVertex2f(x2, y1);
glVertex2f(x2, y2);
glVertex2f(x1, y2);
glEnd();
}
void displayText(const std::string text)
{
glutBitmapString(GLUT_BITMAP_8_BY_13, (const unsigned char*)text.c_str());
}
void display_input()
{
glRasterPos2f(80, 70);
displayText("Input Layer");
for (unsigned u = 0; u < Network.getLayerSize(Reseau::Layer::Input); ++u)
{
auto state = Network.getLayer(Reseau::Layer::Input).at(u).etat;
auto gray_level = (state + Network.getNoise() / 2.0) / (1 + Network.getNoise());
glColor3f(gray_level, gray_level, gray_level);
auto x = (u % 16) * 5 + 80;
auto y = (u / 16) * 5 + 80;
Rectangle(x, y, x + 5, y + 5, true);
}
}
std::string TypeToString(const Type& type)
{
switch (type)
{
case Type::CheckedPattern: return "Checked Pattern";
case Type::Cross: return "Cross";
case Type::HorizontalLine: return "Horizontal Line";
case Type::VerticalLine: return "Vertical Line";
case Type::Square: return "Square";
case Type::Triangle: return "Triangle";
}
}
void display()
{
glClearColor(1,1,1, 1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 0, 0);
Line(20, 2, 980, 2);
Line(20, 45, 980, 45);
Line(220, 70, 220, 480);
Line(680, 70, 680, 480);
const float line1 = 20.f;
const float line2 = 40.f;
float pos = 40.f;
glRasterPos2f(pos, line1);
displayText("Iteration");
glRasterPos2f(pos, line2);
displayText(std::to_string(Network.getIteration()));
pos += 150.f;
glRasterPos2f(pos, line1);
displayText("Epsilon");
glRasterPos2f(pos, line2);
displayText(std::to_string(EPSILON));
pos += 150.f;
glRasterPos2f(pos, line1);
displayText("Bruit");
glRasterPos2f(pos, line2);
displayText(std::to_string(Network.getNoise()));
pos += 150.f;
glRasterPos2f(pos, line1);
displayText("Input");
glRasterPos2f(pos, line2);
displayText(TypeToString(Network.getType()));
pos += 150.f;
glRasterPos2f(pos, line1);
displayText("Global success");
glRasterPos2f(pos, line2);
displayText(std::to_string(Network.getGlobalSuccess()) + "%");
pos += 150.f;
glRasterPos2f(pos, line1);
displayText("Instant success");
glRasterPos2f(pos, line2);
displayText(std::to_string(Network.getInstantSuccess()) + "%");
display_input();
glRasterPos2f(420, 70);
displayText("Hidden Layer");
auto minmax = Network.minmaxWeightLayer(Reseau::Layer::Hidden);
for (unsigned i = 0; i < Network.getLayerSize(Reseau::Layer::Hidden); ++i)
{
for (unsigned j = 0; j < Network.getLayerSize(Reseau::Layer::Input); ++j)
{
float weight = Network.getLayer(Reseau::Layer::Hidden)[i].poids[j];
float gray_level = (weight - minmax.first) / (minmax.second - minmax.first);
glColor3f(gray_level, gray_level, gray_level);
const int side = 3;
const int posX = 250;
const int posY = 80;
auto x = posX + (i % 6) * 70 + (j % 16) * side;
auto y = posY + (i / 6) * 70 + (j / 16) * side;
Rectangle(x, y, x + side, y + side, true);
}
}
glColor3f(0, 0, 0);
glRasterPos2f(750, 70);
displayText("Output Layer");
minmax = Network.minmaxWeightLayer(Reseau::Layer::Output);
for (unsigned i = 0; i < Network.getLayerSize(Reseau::Layer::Output); ++i)
{
for (unsigned j = 0; j < Network.getLayerSize(Reseau::Layer::Hidden); ++j)
{
float weight = Network.getLayer(Reseau::Layer::Output)[i].poids[j];
float gray_level = (weight - minmax.first) / (minmax.second - minmax.first);
glColor3f(gray_level, gray_level, gray_level);
const int side = 12;
const int posX = 720;
const int posY = 100;
auto x = posX + (i / 3) * 150 + (j % 6) * side;
auto y = posY + (i % 3) * 150 + (j / 6) * side;
Rectangle(x, y, x + side, y + side, true);
}
glColor3f(0, 0, 0);
auto typeX = 720 + (i / 3) * 150;
auto typeY = 90 + (i % 3) * 150;
glRasterPos2f(typeX, typeY);
displayText(TypeToString(static_cast<Type>(i)));
float state = Network.getLayer(Reseau::Layer::Output)[i].etat;
glColor3f(state, state, state);
typeY += 90;
Rectangle(typeX, typeY, typeX + 10, typeY + 10, true);
float outputState = std::abs(Network.goalOutputData[i]-1);
glColor3f(outputState, outputState, outputState);
typeX += 10;
Rectangle(typeX, typeY, typeX + 10, typeY + 10, true);
}
Network.updateStats();
glColor3f(0, 0, 0);
glutSwapBuffers();
}
void idle_callback()
{
auto newType = static_cast<Type>(rand() % 6);
Network.newInput(newType);
Network.feedforward(Reseau::Layer::Hidden);
Network.feedforward(Reseau::Layer::Output);
Network.backpropagation();
glutPostRedisplay();
}
int main(int argc, char** argv)
{
const int g_Width = 1000;
const int g_Height = 500;
glutInit(&argc, argv);
glutInitWindowSize(g_Width, g_Height);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("NN with GLUT");
srand( time(0));
Network.init();
glutDisplayFunc(display);
glutIdleFunc(idle_callback);
glOrtho(0, g_Width, g_Height, 0, -1, 1);
glutMainLoop();
return 0;
}