Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# We'll need the indicator function def indicator _ function ( x ) : x _ in = np . array ( x ) x

# We'll need the indicator function
def indicator_function(x):
x_in = np.array(x)
x_in[x_in>=0]=1
x_in[x_in<0]=0
return x_in
# Main backward pass routine
def backward_pass(all_weights, all_biases, all_f, all_h, y):
# We'll store the derivatives dl_dweights and dl_dbiases in lists as well
all_dl_dweights =[None]*(K+1)
all_dl_dbiases =[None]*(K+1)
# And we'll store the derivatives of the loss with respect to the activation and preactivations in lists
all_dl_df =[None]*(K+1)
all_dl_dh =[None]*(K+1)
# Again for convenience we'll stick with the convention that all_h[0] is the net input and all_f[k] in the net output
# Compute derivatives of the loss with respect to the network output
all_dl_df[K]= np.array(d_loss_d_output(all_f[K],y))
# Now work backwards through the network
for layer in range(K,-1,-1):
# TODO Calculate the derivatives of the loss with respect to the biases at layer from all_dl_df[layer].(eq 7.21)
# NOTE! To take a copy of matrix X, use Z=np.array(X)
# REPLACE THIS LINE
all_dl_dbiases[layer]= np.zeros_like(all_biases[layer])
# TODO Calculate the derivatives of the loss with respect to the weights at layer from all_dl_df[layer] and all_h[layer](eq 7.22)
# Don't forget to use np.matmul
# REPLACE THIS LINE
all_dl_dweights[layer]= np.zeros_like(all_weights[layer])
# TODO: calculate the derivatives of the loss with respect to the activations from weight and derivatives of next preactivations (second part of last line of eq 7.24)
# REPLACE THIS LINE
all_dl_dh[layer]= np.zeros_like(all_h[layer])
if layer >0:
# TODO Calculate the derivatives of the loss with respect to the pre-activation f (use derivative of ReLu function, first part of last line of eq.7.24)
# REPLACE THIS LINE
all_dl_df[layer-1]= np.zeros_like(all_f[layer-1])
return all_dl_dweights, all_dl_dbiases
all_dl_dweights, all_dl_dbiases = backward_pass(all_weights, all_biases, all_f, all_h, y)
np.set_printoptions(precision=3)
# Make space for derivatives computed by finite differences
all_dl_dweights_fd =[None]*(K+1)
all_dl_dbiases_fd =[None]*(K+1)
# Let's test if we have the derivatives right using finite differences
delta_fd =0.000001
# Test the dervatives of the bias vectors
for layer in range(K):
dl_dbias = np.zeros_like(all_dl_dbiases[layer])
# For every element in the bias
for row in range(all_biases[layer].shape[0]):
# Take copy of biases We'll change one element each time
all_biases_copy =[np.array(x) for x in all_biases]
all_biases_copy[layer][row]+= delta_fd
network_output_1,*_= compute_network_output(net_input, all_weights, all_biases_copy)
network_output_2,*_= compute_network_output(net_input, all_weights, all_biases)
dl_dbias[row]=(least_squares_loss(network_output_1, y)- least_squares_loss(network_output_2,y))/delta_fd
all_dl_dbiases_fd[layer]= np.array(dl_dbias)
print("-----------------------------------------------")
print("Bias %d, derivatives from backprop:"%(layer))
print(all_dl_dbiases[layer])
print("Bias %d, derivatives from finite differences"%(layer))
print(all_dl_dbiases_fd[layer])
if np.allclose(all_dl_dbiases_fd[layer],all_dl_dbiases[layer],rtol=1e-05, atol=1e-08, equal_nan=False):
print("Success! Derivatives match.")
else:
print("Failure! Derivatives different.")
# Test the derivatives of the weights matrices
for layer in range(K):
dl_dweight = np.zeros_like(all_dl_dweights[layer])
# For every element in the bias
for row in range(all_weights[layer].shape[0]):
for col in range(all_weights[layer].shape[1]):
# Take copy of biases We'll change one element each time
all_weights_copy =[np.array(x) for x in all_weights]
all_weights_copy[layer][row][col]+= delta_fd
network_output_1,*_= compute_network_output(net_input, all_weights_copy, all_biases)
network_output_2,*_= compute_network_output(net_input, all_weights, all_biases)
dl_dweight[row][col]=(least_squares_loss(network_output_1, y)- least_squares_loss(network_output_2,y))/delta_fd
all_dl_dweights_fd[layer]= np.array(dl_dweight)
print("-----------------------------------------------")
print("Weight %d, derivatives from backprop:"%(layer))
print(all_dl_dweights[layer])
print("Weight %d, derivatives from finite differences"%(layer))
print(all_dl_dweights_fd[layer])
if np.allclose(all_dl_dweights_fd[layer],all_dl_dweights[layer],rtol=1e-05, atol=1e-08, equal_nan=False):
print("Success! Derivatives match.")
else:
print("Failure! Derivatives different.")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions