### Cogs1 Spring 2007 Lab 3: Understanding Neural Networks Due by 11:30pm on Wednesday, February 21

#### Introduction

Based on the following quote from our textbook, understanding how neural networks function requires some analysis:

It turns out to be a nontrivial enterprise to determine how neural networks really function, what is really happening, why they produce certain output, and so forth. This is especially true if the neteworks learn. We can look at activation patterns given a certain input, or we can analyze the weight matrices. [...] Nevertheless, compared to natural neural systems and the difficulties in understanding them, artificial neural networks have the advantage of allowing one repeatedly to manipulate and analyze every single component (pg. 175)

We will spend this lab learning how to interpret activation patterns and weight matrices so as to better understand how a trained network is accomplishing a particular task. We will look at a number different problems. We will start with the logical operators, AND, OR, and XOR that we dicussed in class. Then we will work with images of people's faces and try to recognize whether they are wearing sunglasses. Finally we will work with handwritten digits and try to recognize the correct digit.

#### Getting Started

1. Log in, open a terminal window, and type update-cogs1 to get copies of files needed for this lab.
2. Start idle. From the File menu, traverse through the directories cogs1, then labs, to 3 and open the file and-net.py.
3. Remember that you must always "Run" the file before you can begin using it. You can choose "Run" from the menu, or you can simply press the F5 key. You should see some messages in the Python window that the network has been set up.
4. In all of the examples we will be using the network will be called n. In the Python window, you can now give this network a variety of commands:
• n.showPerformance() will display how the network responds to each of the training patterns. Initially it will probably get every pattern wrong since the weights are randomly initialized and no learning has taken place.
• n.printWeights('input', 'output') will display the network's current weights.
• n.train() will repeatedly train the network on the set of training patterns. Each time through all of the patterns is called an epoch. When the network is successfully learning, the total amount of error should decrease over time.
Before training, test the AND network's performance and look at its weights. Then train the network and re-test its performance and check out how the weights have changed. Do the weights make sense to you? Draw a picture of the network as we did on the board in class.
5. Now open the file or-net.py and try all of the same commands as before. Don't forget to "Run" the file before trying commands. Convince yourself that the weights make sense.
6. Next open the file xor-net.py. In this case the network has three layers (input, hidden, and output) instead of just two (input, output). As we discussed in class, this problem is not learnable with only two layers. To see all of the weights for this network requires two commands:
• n.printWeights('input','hidden')
• n.printWeights('hidden','output')
Again draw the network with all of the trained weights to better understand how it has managed to solve the problem.
7. Now we will move on to some more interesting problems. Open the file sunglasses-recognizer.py. When you "Run" this file it will open up a number of additional windows. Several windows show the activations of particular layers, and several windows show the weights between the input and hidden layers. This network has nearly 2000 weights and therefore takes much longer to train. You will notice a several second delay before you start to see any feedback about the training. We will discuss in lab how to interpret the activations and weights being displayed. When you are done testing this file, close all the windows associated with it.
8. Open the file digit-recognizer.py. Again, when you "Run" this file it will open a number of windows. For better clarity, move them so that they are in numbered order. Train this network and observe its behavior.

#### What to turn in for this lab

Write a detailed explanation of the trained behavior of the digit recognizing neural network. Create a table that shows which hidden units turn on (e.g. have high activation indicated) for particular input digits. Based on the input to hidden weights, can you hypothesize what particular hidden nodes are focusing on in the digit images? Be aware that every time you train this network, it may come up with a different solution.