CSE 4311 - Assignments - Assignment 2

List of assignment due dates.

The assignment should be submitted via Canvas. Submit a file called assignment2.zip, containing the following files: These naming conventions are mandatory, non-adherence to these specifications can incur a penalty of up to 20 points.

Your name and UTA ID number should appear on the top line of both documents.


Task 1 (20 points, programming)

File perceptron_inference_base.py contains incomplete code that implements the inference module for a perceptron. When completed, the code creates a perceptron with specified weights, and will compute the output of that perceptron given an input vector.

To complete that code, you must create a file called perceptron_inference_solution.py, where you implement the following Python function:

The function arguments provide the following information: The function returns a tuple of two values: You can refer to slide 8 from the introductory neural network slides for the full definitions of a and z.

The code provided in perceptron_inference_base.py defines some test cases by setting the function arguments to appropriate values. Note that, in perceptron_inference_base.py, bias and weight values are read from a file, and the values for the input vector are read from another file. For the weights, you can use these example files: weights1.txt and weights2.txt. Note that the weights in weights1.txt specify the AND perceptron as discussed in the lecture slides. You are free to create more files in the same format, to test your code with more cases.

Examples of files specifying input vectors are the following:

To test your code with different test cases, you can edit the top of file perceptron_inference_base.py to modify the values of variables weights_file, input file, activation_string. You are also free, and encouraged, to create your own files for weights and input vectors.

Output

You do not need to write any code to produce program output. The existing code already handles that. When completed, the code in perceptron_inference_base.py prints the values of: These are some examples of output from my solution:

Task 2 (20 points, programming)

File nn_inference_base.py contains incomplete code that implements the inference module for a simple neural network. When completed, the code creates a layered feed-forward neural network with a specified number of layers, number of units in each layer, and weights. Your code will then compute the output of that neural network for a specified input vector. All layers in your neural network are fully connected.

To complete that code, you must create a file called nn_inference_solution.py, where you implement the following Python function:

The function arguments provide the following information:

The function returns tuple (a_values, z_values), defined as following:

As mentioned above, the code that is provided (files nn_inference_base.py and nn_load.py) loads values for parameters layers, units, biases, weights from a text file. Examples of such files are nn_xor.txt and nn2.txt. Note that the weights in nn_xor.txt specify the XOR network as discussed in the lecture slides.

Although it is not necessary, you are encouraged to read the format specifications that these text files have to follow, so that you can create your own files to test with. To minimize the amount of time you spend writing code that deals with these files, you are provided with function nn_load in file nn_load.py. Please refer to the documentation for that function to understand its return values. The code provided in nn_inference_base.py uses the nn_load function to create a test case for your program. You are encouraged to create and use your own test cases.

Input vectors are also read from a file. The files storing input vectors contain one number per line, as in Task 1. Examples of such files are

To test your code with different test cases, you can edit file nn_inference_base.py to modify the values of variables nn_file, input file, activation_string.

Output

You do not need to write any code to produce program output. The existing code already handles that. When completed, the code in perceptron_inference_base.py prints the values of the a values and z values of all units in all layers. These values are printed layer by layer, starting from the input layer, and finishing with the output layer. All values are rounded to four decimal digits.

These are some examples of output from my solution.


Task 3 (10 points).

Consider function foo(x,y) = sin(cos(x)+sin(2y)). The chain rule can be useful in computing partial derivatives here. The slides on training perceptrons and training neural networks show many examples of using the chain rule.

Task 4 (20 points, programming).

File gradient_descent_base.py contains incomplete code that implements gradient descent as shown in the lecture slides on optimization. When completed, the code applies gradient descent to two functions:

To complete that code, you must create a file called gradient_descent_solution.py, where you implement the following Python functions:

Output

The code provided in gradient_descent_base.py, when completed, will print out the history result of gradient descent for two test cases. The first test case is:
(x_min, y_min, history) = gradient_descent(f1, f1_gradient, 300, 150, 1, 0.001)
For that test case, the following output should be printed:
t =  1, xt = 300.0000, yt = 150.0000, function(xt, yt) = -119950.0000
t =  2, xt = 225.0000, yt = 100.0000, function(xt, yt) = -121825.0000
t =  3, xt = 237.5000, yt = 143.7500, function(xt, yt) = -125575.0000
t =  4, xt = 232.8125, yt = 140.6250, function(xt, yt) = -125645.8008
t =  5, xt = 231.2500, yt = 141.7969, function(xt, yt) = -125657.7026
t =  6, xt = 230.1758, yt = 142.1875, function(xt, yt) = -125661.8893
t =  7, xt = 229.5410, yt = 142.4561, function(xt, yt) = -125663.4128
t =  8, xt = 229.1565, yt = 142.6147, function(xt, yt) = -125663.9677
t =  9, xt = 228.9246, yt = 142.7109, function(xt, yt) = -125664.1699
t = 10, xt = 228.7846, yt = 142.7689, function(xt, yt) = -125664.2435
t = 11, xt = 228.7001, yt = 142.8039, function(xt, yt) = -125664.2703
t = 12, xt = 228.6491, yt = 142.8250, function(xt, yt) = -125664.2801
t = 13, xt = 228.6183, yt = 142.8377, function(xt, yt) = -125664.2837
t = 14, xt = 228.5997, yt = 142.8454, function(xt, yt) = -125664.2850
t = 15, xt = 228.5885, yt = 142.8501, function(xt, yt) = -125664.2854
t = 16, xt = 228.5817, yt = 142.8529, function(xt, yt) = -125664.2856
t = 17, xt = 228.5776, yt = 142.8546, function(xt, yt) = -125664.2857
t = 18, xt = 228.5752, yt = 142.8556, function(xt, yt) = -125664.2857
t = 19, xt = 228.5737, yt = 142.8562, function(xt, yt) = -125664.2857
t = 20, xt = 228.5728, yt = 142.8566, function(xt, yt) = -125664.2857
t = 21, xt = 228.5723, yt = 142.8568, function(xt, yt) = -125664.2857
t = 22, xt = 228.5719, yt = 142.8569, function(xt, yt) = -125664.2857
We will not be providing the correct output for other test cases (at least not before the submission deadline).

Task 5 (10 points).

Note: In this question you should assume that the activation function of a perceptron is the step function. More specifically, this function: Design a perceptron that takes three Boolean inputs (i.e., inputs that are equal to 0 for false, and 1 for true), and outputs: 1 if at least two of the three inputs are true, 0 otherwise. You should NOT worry about what your perceptron does when the input values are not 0 or 1.

Your answer can be a drawing, as in the slides, or it can simply be text. Either way, your answer should clearly specify the values for the bias weight and the three regular weights.


Task 6 (10 points).

Note: In this question you should assume that the activation function of a perceptron is the step function, same as in Task 5.

Design a neural network that:

Your answer can be a drawing, as in the slides, or it can simply be text. Either way, your answer should clearly specify:


Task 7 (10 points).

Note: In this question you should assume that the activation function of a perceptron is the step function, same as for Tasks 5 and 6.

Is it possible to design a neural network (which could be a single perceptron or a larger network), that satisfies these specs?

We don't care what the network outputs when X = 3 or X = 7.

If your answer is no, explain why not. If your answer is yes, your solution should fully specify, either using a drawing or in text, the network, as described in Task 6, by specifying the units, layers, connections between layers, and values of weights (including bias weights).


CSE 4311 - Assignments - Assignment 2