-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathprint_nn.py
More file actions
230 lines (160 loc) · 5.43 KB
/
print_nn.py
File metadata and controls
230 lines (160 loc) · 5.43 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
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
221
222
223
224
225
226
227
228
229
230
import numpy as np
import pygame
import time
import random
INPUT_NEURON = 24
HIDDEN_NEURON1 = 8
HIDDEN_NEURON2 = 8
OUTPUT_NEURON = 4
BLACK = (0,0,0)
RED = (255,0,0)
WHITE=(255,255,255)
BLUE=(0,0,255)
ORANGE = (255, 165, 0)
DARK_ORANGE = (223, 103, 34)
PURPLE = (106, 0, 106)
LIGHT_PURPLE = (112, 75, 123)
class DrawNN():
def __init__(self, archtecture, weights, screen):
self.screen2 = screen
self.archtecture = archtecture
self.weights = weights
self.layer1_neurons_positions = []
self.layer2_neurons_positions = []
self.layer3_neurons_positions = []
self.layer4_neurons_positions = []
self.input = []
self.layer1 = []
self.lauer2 = []
self.output = []
self.lines_to_write_on_top = []
def drawNeurons(self):
border_width = 50
inter_neurons_width = 23
inter_layer_width = 50
neuron_radius = 8
# For layer 1
for i in range(self.archtecture[0]):
x = border_width + 0 * inter_layer_width
y = border_width + (i * inter_neurons_width)
#myfont = pygame.font.SysFont('Comic Sans MS', 25)
#if self.input[i] == 0:
# new_value = 0
#else:
# new_value = round(self.input[i],1)
#textsurface = myfont.render(str(new_value), False, (0, 0, 0))
#self.screen2.blit(textsurface,(50,y-10))
border_width + (i * inter_neurons_width)
self.layer1_neurons_positions.append([x,y])
if self.input[i] > 0.5:
color = PURPLE
else:
color = DARK_ORANGE
pygame.draw.circle(self.screen2, color, (x,y), neuron_radius)
# For layer 2
offset = 170
for i in range(self.archtecture[1]):
x = border_width + 1 * inter_layer_width + 20
y = border_width + (i * inter_neurons_width) + offset
border_width + (i * inter_neurons_width)
self.layer2_neurons_positions.append([x,y])
if self.layer1[i] > 0:
color = PURPLE
else:
color = DARK_ORANGE
pygame.draw.circle(self.screen2, color, (x,y), neuron_radius)
# For layer 3
offset = 170
for i in range(self.archtecture[2]):
x = border_width + 2 * inter_layer_width + 20
y = border_width + (i * inter_neurons_width) + offset
border_width + (i * inter_neurons_width)
self.layer3_neurons_positions.append([x,y])
if self.layer2[i] > 0:
color = PURPLE
else:
color = DARK_ORANGE
pygame.draw.circle(self.screen2, color, (x,y), neuron_radius)
# For layer 4
offset = 220
# Neurônio que será ativado será com o maio valor de output
argmax = np.argmax(self.output)
for i in range(self.archtecture[3]):
x = border_width + 3 * inter_layer_width
y = border_width + (i * inter_neurons_width) + offset
border_width + (i * inter_neurons_width)
self.layer4_neurons_positions.append([x,y])
if i == argmax:
color = PURPLE
else:
color = DARK_ORANGE
pygame.draw.circle(self.screen2, color, (x,y), neuron_radius)
def drawLines(self):
# Layer 1 - 2
self.lines_to_write_on_top.append([])
for i in range(self.archtecture[0]):
for j in range(self.archtecture[1]):
#if self.weights[0][i][j] > 0:
# color = PURPLE
#else:
# color = BLUE
if self.input[i] > 0.5:
color = LIGHT_PURPLE
self.lines_to_write_on_top[0].append([self.layer1_neurons_positions[i],self.layer2_neurons_positions[j]])
else:
color = ORANGE
pygame.draw.line(self.screen2, color, self.layer1_neurons_positions[i], self.layer2_neurons_positions[j], 1)
# Layer 2 - 3
self.lines_to_write_on_top.append([])
for i in range(self.archtecture[1]):
for j in range(self.archtecture[2]):
#if self.weights[1][i][j] > 0:
# color = RED
#else:
# color = BLUE
if self.layer1[i] > 0:
color = LIGHT_PURPLE
self.lines_to_write_on_top[1].append([self.layer2_neurons_positions[i],self.layer3_neurons_positions[j]])
else:
color = ORANGE
pygame.draw.line(self.screen2, color, self.layer2_neurons_positions[i], self.layer3_neurons_positions[j], 1)
# Layer 3 - 4
self.lines_to_write_on_top.append([])
for i in range(self.archtecture[2]):
for j in range(self.archtecture[3]):
#if self.weights[2][i][j] > 0:
# color = RED
#else:
# color = BLUE
if self.layer2[i] > 0:
color = LIGHT_PURPLE
self.lines_to_write_on_top[2].append([self.layer3_neurons_positions[i],self.layer4_neurons_positions[j]])
else:
color = ORANGE
pygame.draw.line(self.screen2, color, self.layer3_neurons_positions[i], self.layer4_neurons_positions[j], 1)
def write_top_lines_again(self):
for i in range(len(self.lines_to_write_on_top[0])):
pygame.draw.line(self.screen2, LIGHT_PURPLE, self.lines_to_write_on_top[0][i][0], self.lines_to_write_on_top[0][i][1], 1)
for i in range(len(self.lines_to_write_on_top[1])):
pygame.draw.line(self.screen2, LIGHT_PURPLE, self.lines_to_write_on_top[1][i][0], self.lines_to_write_on_top[1][i][1], 1)
for i in range(len(self.lines_to_write_on_top[2])):
pygame.draw.line(self.screen2, LIGHT_PURPLE, self.lines_to_write_on_top[2][i][0], self.lines_to_write_on_top[2][i][1], 1)
# Lista das saídas dos neurônios
def setInput(self, input_):
self.input = input_
def setLayer1(self, layer1):
self.layer1 = layer1
def setLayer2(self, layer2):
self.layer2 = layer2
def setOutput(self, output):
self.output = output
def update(self, neurons):
pass
def setWeights(self, weights):
self.weights = weights
def draw(self):
self.drawNeurons()
self.drawLines()
self.write_top_lines_again()
self.drawNeurons()
self.lines_to_write_on_top = []