python - How to visualize a neural network -


i want draw dynamic picture neural network watch weights changed , activation of neurons during learning. how simulate process in python?

more precisely, if network shape is: [1000, 300, 50], wish draw 3 layer nn contains 1000, 300 , 50 neurons respectively. further, hope picture reflect saturation of neurons on each layer during each epoch.

i've no idea how it. can shed light on me?

i adapted parts answer of milo

from matplotlib import pyplot math import cos, sin, atan   class neuron():     def __init__(self, x, y):         self.x = x         self.y = y      def draw(self, neuron_radius):         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, number_of_neurons_in_widest_layer):         self.vertical_distance_between_layers = 6         self.horizontal_distance_between_neurons = 2         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)         iteration in xrange(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):         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)         line = pyplot.line2d((neuron1.x - x_adjustment, neuron2.x + x_adjustment), (neuron1.y - y_adjustment, neuron2.y + y_adjustment))         pyplot.gca().add_line(line)      def draw(self, layertype=0):         neuron in self.neurons:             neuron.draw( self.neuron_radius )             if self.previous_layer:                 previous_layer_neuron in self.previous_layer.neurons:                     self.__line_between_two_neurons(neuron, previous_layer_neuron)         # 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)  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):         pyplot.figure()         in range( len(self.layers) ):             layer = self.layers[i]             if == len(self.layers)-1:                 = -1             layer.draw( )         pyplot.axis('scaled')         pyplot.axis('off')         pyplot.title( 'neural network architecture', fontsize=15 )         pyplot.show()  class drawnn():     def __init__( self, neural_network ):         self.neural_network = neural_network      def draw( self ):         widest_layer = max( self.neural_network )         network = neuralnetwork( widest_layer )         l in self.neural_network:             network.add_layer(l)         network.draw() 

now layers labeled, axis deleted , constructing plot easier. it's done by:

network = plotnn.drawnn( [2,8,8,1] ) network.draw() 

here net following structure constructed:

  • 2 neurons in input layer
  • 8 neurons in 1st hidden layer
  • 8 neurons in 2nd hidden layer
  • 1 neuron in output layerenter image description here

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

utf 8 - split utf-8 string into bytes in python -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -