📜 ⬆️ ⬇️

The brain from the inside (Visualization of the passage of the pattern through the model of an artificial neural network)

Introduction


The article is intended for those who have ever been interested in the question of what is happening inside the artificial neural network (artificial neural network) - ANN . Now almost everyone can develop their own INS using ready-made libraries in most programming languages. In this article I will try to show exactly how an object ( Pattern ) looks like passing through the layers of an INS, developed and compiled using the Tensorflow deep learning library with the Keras add- in .

Used software


The following components are necessary (the versions I have indicated for my own case):


It is also possible to draw a network architecture, but for this you need to install keras visualization tools in the method

PLOT_PATTERN_PROCCESS(...) 

to establish

PLOT_MODEL = True


 def PLOT_PATTERN_PROCCESS(model, pattern, FOLDER_TO_SAVE, grid_size=(3, 3), limit_size_layer=(15, 15), PLOT_MODEL=True): 

image

main idea


It is necessary to choose one pattern (passage, which we will observe), then the compiled network is divided into layers of tensors . In the cycle from the second to the last layer, a new network is created where its output is the layer number of the cycle and skipping the pattern, at its output a result is obtained in the form of an n-dimensional array.

Implementation


Connecting libraries

 from keras.models import * from keras.layers import * import matplotlib.pyplot as plt import os import numpy as np 

Used methods:


Program code

 model_ = build_model() pattern = np.random.sample((1,50,50)) os.makedirs("PLOT_PATTERN_PROCCESS") PLOT_PATTERN_PROCCESS( model = model_, pattern = pattern, FOLDER_TO_SAVE = "PLOT_PATTERN_PROCCESS", PLOT_MODEL=False, grid_size=(2, 2) ) 

Description of the program


Method

 build_model() 

returns the ANN model in Sequential format, designed to classify something into 5 classes.

model.summary ()
 _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) (None, 50, 50) 0 _________________________________________________________________ dense_1 (Dense) (None, 50, 75) 3825 _________________________________________________________________ my_dense (Dense) (None, 50, 50) 3800 _________________________________________________________________ dense_2 (Dense) (None, 50, 25) 1275 _________________________________________________________________ dense_3 (Dense) (None, 50, 10) 260 _________________________________________________________________ flatten_1 (Flatten) (None, 500) 0 _________________________________________________________________ dense_4 (Dense) (None, 5) 2505 ================================================================= Total params: 11,665 Trainable params: 11,665 Non-trainable params: 0 _________________________________________________________________ 


As can be seen from the architecture pattern, this is an array of 50x50. Variable

 pattern 

and there is an observable object.
Next, create a directory

 os.makedirs("PLOT_PATTERN_PROCCESS") 

where will save the result.

PLOT_PATTERN_PROCCESS Method Description


I described the meaning of the method above, but it is important to say that we do not need all the layers, since the outputs of some layers cannot be displayed or it will not be informative.
Getting the output pattern occurs here

 _output = _model.predict(pattern)[0] 

In this implementation, you can display, two-dimensional, the output pattren whose dimensions are not less than the parameter

 limit_size_layer 

Alternately iterating over the layers of the INS model variable

 SAVE_AR_LIST 
gradually filled with data:

  1. Layer number

     num_layer 
  2. Layer name

     model.layers[num_layer].name 
  3. Two-dimensional output array

      _output.tolist() 

Excluding gradually one by one from

 SAVE_AR_LIST 

and putting it in the canvas cell

 ax.imshow(np.array(ar), cmap='viridis', extent=(xmin, xmax, ymin, ymax)) 

The output file is created (0.png)

image

Recommendations


  • You can set the layer name as follows.

     Dense_2_3 = Dense(50, activation='relu', name="my_dense")(Dense_2_2) 

    Very handy when evaluating and comparing with neuroarchitecture.
  • Using this approach, it is interesting to see how the pattern changes while passing the network when learning from era to era
  • Do not install the grid

     grid_size 

    large size the size of the displayed images will be small and uninformative
  • If you observe the passage in the dynamics (when learning or passing a pack of patterns), we are already talking about large information. To reduce the amount of RAM used by the application, it is better to save the arrays to files on a PC, for example, in JSON format, and after processing all the patterns, iterate over the files one by one and turn them into images

Successes!

Source: https://habr.com/ru/post/438972/