This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVissualizeNN.py
199 lines (173 loc) · 9.45 KB
/
VissualizeNN.py
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
# This Libraray is modified based the work by Milo Spencer-Harper and Oli Blum, https://stackoverflow.com/a/37366154/10404826
# On top of that, I added support for showing weights (linewidth, colors, etc.)
# Contributor: Jianzheng Liu
# Contact: [email protected]
# Unsupported running through default pipfile
from matplotlib import pyplot
from math import cos, sin, atan
from palettable.tableau import Tableau_10
from time import localtime, strftime
import numpy as np
class Neuron():
def __init__(self, x, y):
self.x = x
self.y = y
def draw(self, neuron_radius, id=-1):
circle = pyplot.Circle((self.x, self.y), radius=neuron_radius, fill=False)
pyplot.gca().add_patch(circle)
pyplot.gca().text(self.x, self.y-0.15, str(id), size=10, ha='center')
class Layer():
def __init__(self, network, number_of_neurons, number_of_neurons_in_widest_layer):
self.vertical_distance_between_layers = 60
self.horizontal_distance_between_neurons = 10
self.neuron_radius = 0.5
self.number_of_neurons_in_widest_layer = number_of_neurons_in_widest_layer
self.previous_layer = self.__get_previous_layer(network)
self.y = self.__calculate_layer_y_position()
self.neurons = self.__intialise_neurons(number_of_neurons)
def __intialise_neurons(self, number_of_neurons):
neurons = []
x = self.__calculate_left_margin_so_layer_is_centered(number_of_neurons)
for iteration in range(number_of_neurons):
neuron = Neuron(x, self.y)
neurons.append(neuron)
x += self.horizontal_distance_between_neurons
return neurons
def __calculate_left_margin_so_layer_is_centered(self, number_of_neurons):
return self.horizontal_distance_between_neurons * (self.number_of_neurons_in_widest_layer - number_of_neurons) / 2
def __calculate_layer_y_position(self):
if self.previous_layer:
return self.previous_layer.y + self.vertical_distance_between_layers
else:
return 0
def __get_previous_layer(self, network):
if len(network.layers) > 0:
return network.layers[-1]
else:
return None
def __line_between_two_neurons(self, neuron1, neuron2, weight=0.4, textoverlaphandler=None):
angle = atan((neuron2.x - neuron1.x) / float(neuron2.y - neuron1.y))
x_adjustment = self.neuron_radius * sin(angle)
y_adjustment = self.neuron_radius * cos(angle)
# assign colors to lines depending on the sign of the weight
color=Tableau_10.mpl_colors[0]
if weight > 0: color=Tableau_10.mpl_colors[1]
# assign different linewidths to lines depending on the size of the weight
abs_weight = abs(weight)
if abs_weight > 0.5:
linewidth = 5*abs_weight
elif abs_weight > 0.8:
linewidth = 20*abs_weight
else:
linewidth = abs_weight
linewidth *= 0.75
# draw the weights and adjust the labels of weights to avoid overlapping
if abs_weight > 0.5:
# while loop to determine the optimal locaton for text lables to avoid overlapping
index_step = 2
num_segments = 10
txt_x_pos = neuron1.x - x_adjustment+index_step*(neuron2.x-neuron1.x+2*x_adjustment)/num_segments
txt_y_pos = neuron1.y - y_adjustment+index_step*(neuron2.y-neuron1.y+2*y_adjustment)/num_segments
while ((not textoverlaphandler.getspace([txt_x_pos-0.5, txt_y_pos-0.5, txt_x_pos+0.5, txt_y_pos+0.5])) and index_step < num_segments):
index_step = index_step + 1
txt_x_pos = neuron1.x - x_adjustment+index_step*(neuron2.x-neuron1.x+2*x_adjustment)/num_segments
txt_y_pos = neuron1.y - y_adjustment+index_step*(neuron2.y-neuron1.y+2*y_adjustment)/num_segments
# print("Label positions: ", "{:.2f}".format(txt_x_pos), "{:.2f}".format(txt_y_pos), "{:3.2f}".format(weight))
a=pyplot.gca().text(txt_x_pos, txt_y_pos, "{:3.2f}".format(weight), size=5, ha='center')
# a=pyplot.gca().text(txt_x_pos, txt_y_pos, "", size=8, ha='center') #uncomment for no labels
a.set_bbox(dict(facecolor='white', alpha=0))
# print(a.get_bbox_patch().get_height())
line = pyplot.Line2D((neuron1.x - x_adjustment, neuron2.x + x_adjustment), (neuron1.y - y_adjustment, neuron2.y + y_adjustment), linewidth=linewidth, color=color)
pyplot.gca().add_line(line)
def draw(self, layerType=0, weights=None, textoverlaphandler=None):
j=0 # index for neurons in this layer
for neuron in self.neurons:
i=0 # index for neurons in previous layer
neuron.draw( self.neuron_radius, id=j+1 )
if self.previous_layer:
for previous_layer_neuron in self.previous_layer.neurons:
self.__line_between_two_neurons(neuron, previous_layer_neuron, weights[i,j], textoverlaphandler)
i=i+1
j=j+1
# write Text
x_text = self.number_of_neurons_in_widest_layer * self.horizontal_distance_between_neurons
if layerType == 0:
pyplot.text(x_text, self.y, 'Input Layer', fontsize = 12)
elif layerType == -1:
pyplot.text(x_text, self.y, 'Output Layer', fontsize = 12)
else:
pyplot.text(x_text, self.y, 'Hidden Layer '+str(layerType), fontsize = 12)
# A class to handle Text Overlapping
# The idea is to first create a grid space, if a grid is already occupied, then
# the grid is not available for text labels.
class TextOverlappingHandler():
# initialize the class with the width and height of the plot area
def __init__(self, width, height, grid_size=0.2):
self.grid_size = grid_size
self.cells = np.ones((int(np.ceil(width / grid_size)), int(np.ceil(height / grid_size))), dtype=bool)
# input test_coordinates(bottom left and top right),
# getspace will tell you whether a text label can be put in the test coordinates
def getspace(self, test_coordinates):
x_left_pos = int(np.floor(test_coordinates[0]/self.grid_size))
y_botttom_pos = int(np.floor(test_coordinates[1]/self.grid_size))
x_right_pos = int(np.floor(test_coordinates[2]/self.grid_size))
y_top_pos = int(np.floor(test_coordinates[3]/self.grid_size))
if self.cells[x_left_pos, y_botttom_pos] and self.cells[x_left_pos, y_top_pos] \
and self.cells[x_right_pos, y_top_pos] and self.cells[x_right_pos, y_botttom_pos]:
for i in range(x_left_pos, x_right_pos):
for j in range(y_botttom_pos, y_top_pos):
self.cells[i, j] = False
return True
else:
return False
class NeuralNetwork():
def __init__(self, number_of_neurons_in_widest_layer):
self.number_of_neurons_in_widest_layer = number_of_neurons_in_widest_layer
self.layers = []
self.layertype = 0
def add_layer(self, number_of_neurons ):
layer = Layer(self, number_of_neurons, self.number_of_neurons_in_widest_layer)
self.layers.append(layer)
def draw(self, weights_list=None):
# vertical_distance_between_layers and horizontal_distance_between_neurons are the same with the variables of the same name in layer class
vertical_distance_between_layers = 60
horizontal_distance_between_neurons = 10
overlaphandler = TextOverlappingHandler(\
self.number_of_neurons_in_widest_layer*horizontal_distance_between_neurons,\
len(self.layers)*vertical_distance_between_layers, grid_size=0.2 )
pyplot.figure(figsize=(18, 13.5))
for i in range( len(self.layers) ):
layer = self.layers[i]
if i == 0:
layer.draw( layerType=0 )
elif i == len(self.layers)-1:
layer.draw( layerType=-1, weights=weights_list[i-1], textoverlaphandler=overlaphandler)
else:
layer.draw( layerType=i, weights=weights_list[i-1], textoverlaphandler=overlaphandler)
pyplot.axis('scaled')
pyplot.axis('off')
pyplot.title( 'Neural Network architecture', fontsize=15 )
figureName='ANN_'+strftime("%Y%m%d_%H%M%S", localtime())+'.png'
pyplot.savefig(figureName, dpi=800, bbox_inches="tight")
pyplot.show()
class DrawNN():
# para: neural_network is an array of the number of neurons
# from input layer to output layer, e.g., a neural network of 5 nerons in the input layer,
# 10 neurons in the hidden layer 1 and 1 neuron in the output layer is [5, 10, 1]
# para: weights_list (optional) is the output weights list of a neural network which can be obtained via classifier.coefs_
def __init__( self, neural_network, weights_list=None ):
self.neural_network = neural_network
self.weights_list = weights_list
# if weights_list is none, then create a uniform list to fill the weights_list
if weights_list is None:
weights_list=[]
for first, second in zip(neural_network, neural_network[1:]):
tempArr = np.ones((first, second))*0.4
weights_list.append(tempArr)
self.weights_list = weights_list
def draw( self ):
widest_layer = max( self.neural_network )
network = NeuralNetwork( widest_layer )
for l in self.neural_network:
network.add_layer(l)
network.draw(self.weights_list)