Skip to content
Snippets Groups Projects
Commit f82ea3df authored by Tobias Döring's avatar Tobias Döring
Browse files

added weights to mlp visualisation

parent 238d1bc6
Branches
No related tags found
1 merge request!2Evo neuro
...@@ -13,7 +13,7 @@ MAX_STEPS = 1200 # after 1600 steps the Environment gives us a done anyway. ...@@ -13,7 +13,7 @@ MAX_STEPS = 1200 # after 1600 steps the Environment gives us a done anyway.
VERSION = 1 VERSION = 1
TEST_WALKER = False TEST_WALKER = True
LOAD_BRAIN = False LOAD_BRAIN = False
RENDER_BEST = False RENDER_BEST = False
if TEST_WALKER: if TEST_WALKER:
......
from numpy import cos, sin, arctan from numpy import cos, sin, arctan
from matplotlib import pyplot from matplotlib import pyplot
vertical_distance_between_layers = 40
horizontal_distance_between_neurons = 4
neuron_radius = 1
default_line_width = 1
class Neuron: class Neuron:
def __init__(self, x, y): def __init__(self, x, y):
...@@ -13,10 +17,11 @@ class Neuron: ...@@ -13,10 +17,11 @@ class Neuron:
class Layer: class Layer:
def __init__(self, network, number_of_neurons): def __init__(self, network, number_of_neurons, weights):
self.previous_layer = self.get_previous_layer(network) self.previous_layer = self.get_previous_layer(network)
self.y = self.calculate_layer_y_position() self.y = self.calculate_layer_y_position()
self.neurons = self.init_neurons(number_of_neurons) self.neurons = self.init_neurons(number_of_neurons)
self.weights = weights
def init_neurons(self, number_of_neurons): def init_neurons(self, number_of_neurons):
neurons = [] neurons = []
...@@ -42,27 +47,38 @@ class Layer: ...@@ -42,27 +47,38 @@ class Layer:
else: else:
return None return None
def line(self, neuron1, neuron2): def line(self, neuron1, neuron2, weight):
angle = arctan((neuron2.x - neuron1.x) / float(neuron2.y - neuron1.y)) angle = arctan((neuron2.x - neuron1.x) / float(neuron2.y - neuron1.y))
x_adjustment = neuron_radius * sin(angle) x_adjustment = neuron_radius * sin(angle)
y_adjustment = neuron_radius * cos(angle) y_adjustment = neuron_radius * cos(angle)
color = 'blue'
if weight < 0:
color = 'red'
line = pyplot.Line2D((neuron1.x - x_adjustment, neuron2.x + x_adjustment), line = pyplot.Line2D((neuron1.x - x_adjustment, neuron2.x + x_adjustment),
(neuron1.y - y_adjustment, neuron2.y + y_adjustment), linewidth=1, color='blue') # HIER (neuron1.y - y_adjustment, neuron2.y + y_adjustment),
linewidth=default_line_width * weight, color=color) # HIER
pyplot.gca().add_line(line) pyplot.gca().add_line(line)
def draw(self): def draw(self):
y = 0
for neuron in self.neurons: for neuron in self.neurons:
if self.previous_layer: if self.previous_layer:
x = 0
for previous_layer_neuron in self.previous_layer.neurons: for previous_layer_neuron in self.previous_layer.neurons:
self.line(neuron, previous_layer_neuron) self.line(neuron, previous_layer_neuron, self.weights[x][y])
x += 1
y += 1
neuron.draw() neuron.draw()
class NeuralNetwork(): class NeuralNetwork():
def __init__(self, architecture): def __init__(self, architecture, weights):
self.layers = [] self.layers = []
for layer_size in architecture: for i in range(len(architecture)):
self.layers.append(Layer(self, layer_size)) if i > 0:
self.layers.append(Layer(self, architecture[i], weights[i - 1]))
else:
self.layers.append(Layer(self, architecture[i], None))
def add_layer(self, number_of_neurons): def add_layer(self, number_of_neurons):
layer = Layer(self, number_of_neurons) layer = Layer(self, number_of_neurons)
...@@ -77,8 +93,5 @@ class NeuralNetwork(): ...@@ -77,8 +93,5 @@ class NeuralNetwork():
if __name__ == "__main__": if __name__ == "__main__":
vertical_distance_between_layers = 40
horizontal_distance_between_neurons = 4
neuron_radius = 1
network = NeuralNetwork([24, 12, 4]) network = NeuralNetwork([24, 12, 4])
network.draw() network.draw()
...@@ -4,6 +4,8 @@ import copy ...@@ -4,6 +4,8 @@ import copy
import os import os
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import mlp_visualizer
np.random.seed(42) np.random.seed(42)
class Walker: class Walker:
...@@ -87,6 +89,15 @@ class Walker: ...@@ -87,6 +89,15 @@ class Walker:
plt.xticks(rotation = 45, ha = "right") plt.xticks(rotation = 45, ha = "right")
plt.show() plt.show()
mlp_visualizer.vertical_distance_between_layers = 40
mlp_visualizer.horizontal_distance_between_neurons = 4
mlp_visualizer.neuron_radius = 1
mlp_visualizer.default_line_width = 1
network = mlp_visualizer.NeuralNetwork([24, self.hidden_layer, 4],
[self.weights['W1'], self.weights['W2']])
network.draw()
def save(self): def save(self):
if not os.path.isdir('./models'): if not os.path.isdir('./models'):
os.mkdir('./models') os.mkdir('./models')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment