[PYTHON] Imaging of neural networks that recognize MNIST

Visualize the effect of the original image on each node for handwritten digit recognition by a neural network.

algorithm

[Note] This is an algorithm that I came up with. It has not been verified how accurately the impact can be measured.

Calculate the value of each node on a source with all zero pixels as a basis for comparison.

Prepare the image to be evaluated. Calculate the value of each node by putting only one pixel in the source and compare the value with when all 0s. Doing this for all pixels is the effect of a particular pixel on each node.

By imaging this, you can see the effect visually.

Preparation

I will use the sample of "Deep Learning from scratch".

Since MNIST (handwritten digit data) is downloaded at the first execution, execute the script that uses MNIST.

$ cd ch03
$ python neuralnet_mnist.py
Accuracy:0.9352

Extract the following two files.

Run the following script to generate the image.

Image example

Compute the neural network and extract the nodes in the middle. Since the image hardly changes before and after applying sigmoid, ʻa1, a2 is discarded. However, as will be shown later, ʻa3 is picked up because it changes significantly before and after applying softmax.

def predict(x):
    a1 = np.dot(x, W1) + b1
    z1 = sigmoid(a1)
    a2 = np.dot(z1, W2) + b2
    z2 = sigmoid(a2)
    a3 = np.dot(z2, W3) + b3
    z3 = softmax(a3)
    return z1, z2, a3, z3

[Addition 2017.08.26] z1 is good because the influence is independent for each pixel, but it turned out that there is an influence between pixels after z2 and it is not taken into consideration. We are considering how to fix it. I will add it if there is any progress.

Check the image that is all white. The number represents the probability calculated by softmax.

見出し.png white.png

If it is brighter than the background color (gray), it will be positive, if it is dark, it will be negative, and the one with a large positive overall will have a higher probability. Plus and minus cancel each other out, so something that is clearly visible does not always have a high probability. If you go through softmax, you will lose the sharpness and you will not know what it is.

Check the numbers. You can see that the all-white image is hollowed out in the shape of numbers.

0.png

1.png

2.png

3.png

4.png

5.png

6.png

7.png

8.png

9.png

reference

I referred to how to write Python.

Recommended Posts

Imaging of neural networks that recognize MNIST
Do Neural Networks Dream of Electric Rats?
Guidelines for Output Layer Design of Neural Networks
Notes on neural networks
That of / etc / shadow
[PyTorch Tutorial ③] NEURAL NETWORKS
Construction of a neural network that reproduces XOR by Z3