Question: You must hand in both your Python code and your output to receive credit for all questions in this assignment Questions 1 . Complete the

You must hand in both your Python code and your output to receive credit for
all questions in this assignment
Questions
1. Complete the program in the file grad descent shell.py in order to implement the gradient
descent algorithm for the linear neural network. To do this, in order to compute the gradient
of the error function, you may use either
E =
N
k=1
(W Ak Yk)AT
k (1)
(where N is the number of training pairs, Yk is the kth output training vector and Ak represents
a column in the matrix A discussed in class) or the matrix form
E =(W A Y )AT .(2)
Complete the program by filling in Python code at the specified points within the file
a. Upload your working Python script file
b. Make sure to output your training matrices X and Y or you will not receive credit for
this assignment.
c. Upload an image of your mserror plot in .png or .jpg format
d. Make sure to output your weight matrices W for both pinv and linearnn along with
their sum squared difference: w sum abs diff.
"""
Complete the progran below by filling in Python
code at the specified points.
To be handed into Canvas assignments:
A. Upload your Python script file
B. Upload an image of your MSE plot in .png or .jpg format
C. Make sure to show your weight matrices
for both pinv and linearnn along with sum squred difference: w_sum_abs_diff
"""
import numpy as np
import matplotlib.pyplot as plt
def linearnn(x,y,nits,eta):
#initialize dimensions ...
xshape=x.shape
yshape=y.shape
n=xshape[0]
N=xshape[1]
k=yshape[0]
#build the input data matrix ...
z=np.ones((1,N))
a=np.concatenate((x,z), axis=0)
#initialize random weights ...
w=np.random.normal(size=(k, n+1))
#initialize mserror storage ..
mserror=np.zeros((1,nits))
#-----------------------------------------
#train the network using gradient descent ...
for itcount in range(nits):
"""
Section of code to update network weights ...
3. Compute the gradient of the mean squared error
and store it in a variable named 'gradientofmse'
4. Given the variable 'gradientofmsenorm', update the
network weight matrix, w using the gradient descent formula
given in class
"""
gradientofmsenorm=np.sqrt(np.sum(gradientofmse*gradientofmse))
#calculate MSE ...
etemp=0;
for trcount in range(N):
#get (f(Xk)-Yk) column vector ...
avec=np.concatenate((x[:,trcount],[1]), axis=0)
vk=np.dot(w,avec)-y[:,trcount]
#calculate the error ...
etemp=etemp+vk.transpose()*vk
mserror[0,itcount]=(1/(2*N))*etemp[0];
#test the output ...
netout=np.dot(w,a)
return netout,mserror,w
#===============================
#'main' starts here ....
#set up the training set ...
n=3
k=2
N=5
x=np.random.normal(size=(n, N))
y=np.random.normal(size=(k, N))
#compute the closed form solution
z=np.ones((1,N))
a=np.concatenate((x,z), axis=0)
wpinv=np.dot(y,np.linalg.pinv(a))
print('Weight matrix computed using the pseudoinverse: ')
print(wpinv)
print()
"""
Section of code to call the gradient descent function to computer the weight matrix
1. You must input a value for the number of iterations, nits
2. You must input a value for the learning rate, eta
(You should run numerical tests with a few different values)
"""
netout,mserror,w=linearnn(x,y,nits,eta)
print('Weight matrix computed via gradient descent: ')
print(w)
print()
""""
Compute the sum of the absolute value of the
difference between the weight matrices
from pinv and gradient descent
"""
w_sum_abs_diff=np.sum(np.absolute(wpinv-w))
print('Sum of absolute value of the difference between weight matrices:')
print(w_sum_abs_diff)
#plot the nean squared error
plt.plot(mserror[0,:])
plt.title('Mean Squared Error versus Iteration')
plt.xlabel('Iteration #')
plt.ylabel('MSE')
plt.title('Mean Squared Error versus Iteration')
#plt.show()
plt.savefig('plot.png')

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!