Starting from:

$24.99

NSC3270 -6270 Homework 4-Face image recognition Solution



In class, we discussed the simplest type of learning in neural networks, called Hebbian learning, and its variant that normalizes activations and weights, called Oja’s Rule.

In this assignment, you will train a simple neural network to do some basic face recognition using this variant of Hebbian learning.

Please refer to Homework4.ipynb, which is the starting point for this assignment, and the class slides where we went over details of the assignment. The coding you do should be added to Homework4.ipynb. Save it to a new file with your last name appended to “Homework4”. Submit on Brightspace. There is no need to submit any figures with your code. Your Jupyter Notebook should create all the figures needed. Make sure you submit your notebook after it has been run (with figures and calculations shown).

The code in Homework4.ipynb opens and displays four small (32x32) images of wellknown people (Carrie Fisher, Jon Stewart, Michelle Obama, and Bryan Cranston). The code centers the pixel values of each image around 0, reshapes each images from a matrix to a vector (like the code for Homework 3 did with the MNIST digits), and normalizes each vector to have a length equal to 1.

You will code up a simple neural network that takes vectors of pixel values as input and classifies each vector as being Carrie Fisher, Jon Stewart, Michelle Obama, or Bryan Cranston. So the number of input units is equal to 32x32 = 1024 (the total number of pixels in the image, with the image represented as a vector) and the number of output units is equal to 4 (there are 4 identities). The output activation function is a simple linear function (as discussed in class).

The network will be trained on noisy versions of each face. The Homework4.ipynb code creates 500 training patterns per face identity (2000 training patterns total) by adding a small amount of noise (normally distributed), as discussed in class (with normalization). The code displays four examples of noisy versions of each face.

train_pats is a 2000x1024 numpy array : 2000 training patterns (500 patterns x 4 identities), each of which is a vector produced from one of the 32x32 original images (centered on 0, with noise added, and normalized).

You must not use hard-coded values. I have defined a lot of variables in the code that you should use in your code.


Q1. Create a new 2000x4 numpy array called train_outs that has the “teacher” (vector of output values you are training the network to reproduce) for each training pattern. For now, have the output of the correct answer equal to 1 and of the incorrect answers equal to 0


Q2. Create a weight matrix with the appropriate dimensions. Initialize the weights in the matrix to random numbers drawn from a normal distribution. Normalize the weights so that the vector of weights contributing to the net input of any given output node has length equal to 1.


Q3. Train the network using Oja’s rule. As described in class, tor each of the 2000 patterns, the change in weight Δwij between input node ii and output node oj is given by
Δwij = λ ii oj
where λ is the learning rate (assume λ = .01). If you stopped here, this would be Hebbian learning. For Oja’s rule, you will need to normalize the weights after updating the weights for each training pattern such that the vector of weights contributing to the net input of any given output node has length equal to 1 (you don’t normalize after every individual Δwi update, but after you update all the weights in the network).

Obviously, to train the network, you will need to write code to train the network. In Homework 3, we gave you Keras code to do the training. For this assignment, you need to write that training code from scratch. This should be only a few lines of code. It’s not complicated.

You do not need to worry about making your code efficient (vectorizing and the like) – you can if you want, but you do not have to. Slow is fine, so long as it runs correctly. Note that it will take a while to train the network (several minutes, depending on the speed of your computer).


Q4. Visualize the weight going to each output unit of the network (like you did in Homework 3). You will need to produce four figures of “weight images”.


Q5. Test your trained network. First, present each original image to the network (the four identities). Second, present a new noisy version (using the same level of noise used during training) of each of these images to the network. For each test image presented to the network, print out the correct answer and the answer produced by the network (winnertake-all).

Try creating a new set of four test images (one for each identity) with a higher level of noise that used during training that causes the network to make classification errors.





EXTRA CREDIT (2 point)

See what happens when you change the coding for the output units from what was assumed above (the output of the correct answer equal to 1 and of the incorrect answers equal to 0) to a different coding of output units, with the output of the correct answer equal to 1 and the incorrect answers equal to -1). Retrain the network (like Q3), visualize the weights (like Q4), and test the network (like Q5).

Why does the visualization of the weights look different with this coding of output units from the one used in the main of the assignment? What is different about what is learned (and hence represented in the weights in the network) between the two ways of coding outputs? Add your written responses to a markdown cell in your assignment (an explanation is required to receive full extra credit).


Reminder: Check the class slides for some additional hints and suggestions and examples of what some of the output might look like.

Unexcused late assignments will be penalized 10% for every 24 hours late, starting from the time class ends, for a maximum of two days, after which they will earn a 0.

More products