Skip to content
Snippets Groups Projects
Commit b91b0072 authored by Philip Maas's avatar Philip Maas
Browse files

Added a prototype for an mlp visualizer

parent 46616c15
Branches
Tags
1 merge request!2Evo neuro
mlp.png 0 → 100644
mlp.png

346 KiB

from numpy import cos, sin, arctan
from matplotlib import pyplot
class Neuron:
def __init__(self, x, y):
self.x = x
self.y = y
def draw(self):
circle = pyplot.Circle((self.x, self.y), radius=neuron_radius, fill=False)
pyplot.gca().add_patch(circle)
class Layer:
def __init__(self, network, number_of_neurons):
self.previous_layer = self.get_previous_layer(network)
self.y = self.calculate_layer_y_position()
self.neurons = self.init_neurons(number_of_neurons)
def init_neurons(self, number_of_neurons):
neurons = []
x = self.calc_left_margin(number_of_neurons)
for iteration in range(number_of_neurons):
neuron = Neuron(x, self.y)
neurons.append(neuron)
x += horizontal_distance_between_neurons
return neurons
def calc_left_margin(self, number_of_neurons): # so it's centered
return -horizontal_distance_between_neurons * number_of_neurons/2
def calculate_layer_y_position(self):
if self.previous_layer:
return self.previous_layer.y + 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(self, neuron1, neuron2):
angle = arctan((neuron2.x - neuron1.x) / float(neuron2.y - neuron1.y))
x_adjustment = neuron_radius * sin(angle)
y_adjustment = neuron_radius * cos(angle)
line = pyplot.Line2D((neuron1.x - x_adjustment, neuron2.x + x_adjustment),
(neuron1.y - y_adjustment, neuron2.y + y_adjustment), linewidth=1, color='blue') # HIER
pyplot.gca().add_line(line)
def draw(self):
for neuron in self.neurons:
if self.previous_layer:
for previous_layer_neuron in self.previous_layer.neurons:
self.line(neuron, previous_layer_neuron)
neuron.draw()
class NeuralNetwork():
def __init__(self, architecture):
self.layers = []
for layer_size in architecture:
self.layers.append(Layer(self, layer_size))
def add_layer(self, number_of_neurons):
layer = Layer(self, number_of_neurons)
self.layers.append(layer)
def draw(self):
for layer in self.layers:
layer.draw()
pyplot.axis('scaled')
pyplot.savefig('mlp.png', dpi=300)
pyplot.show()
if __name__ == "__main__":
vertical_distance_between_layers = 40
horizontal_distance_between_neurons = 4
neuron_radius = 1
network = NeuralNetwork([24, 12, 4])
network.draw()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment