Question: Modify your 'gradient descent assignment I ' code in order to train the single hidden layer feedforward neural network ( FFNN ) discussed in class.

Modify your 'gradient descent assignment I' code in order to train the single hidden layer
feedforward neural network (FFNN) discussed in class.
You must use the same code to build your training set from 'gradient descent assignment
I'.
Instead of initializing a single weight matrix to random values, you will now need to
initialize W, and to random values.
You must choose the number of neurons m(a good starting point would be m=2n).
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,,.
e. Upload all your materials (code, output and plots) in one single file in either pdf or docx
format.
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'
"""
gradientofmse = np.dot((np.dot(w, a)- y), a.T)
"""
4. Given the variable 'gradientofmsenorm', update the network weight matrix, w using the gradient descent formula given in class
"""
w = w - eta * 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)
"""
nits =1000 # Set the number of iterations
eta =0.01 # Set the learning rate
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 mean squared error
plt.plot(mserror[0, :])
plt.title('Mean Squared Error versus Iteration')
plt.xlabel('Iteration #')
plt.ylabel('MSE')
plt.savefig('plot.png')
 Modify your 'gradient descent assignment I' code in order to train

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 Databases Questions!