Lab 8
Lab 8
In this lab, you'll work through a complete neural network pipeline from data loading, parameter
initialization, forward propagation, cost calculation, backpropagation, and parameter updates to
model evaluation.
Objectives
2. Visualize a random sample from X_train by reshaping it to a 28x28 grayscale image and
displaying it with matplotlib.pyplot.imshow.
Model
Initialize parameters Randomly
W1=np.random.randn(n1,n0)
b1=np.zeros((n1,1))
W2=np.random.randn(n2,n1)
b2=np.zeros((n2,1))
*Repeat Below Steps for many times : *
Forward Propagation
Z1=W1∗X+B1
A1=f(Z1)
Z2=W2∗A1+B2
A2=Softmax(Z2)
Updating Parameters
W2=W2−α∗∂Cost∂W2
B2=B2−α∗∂Cost∂B2
W1=W1−α∗∂Cost∂W1
B1=B1−α∗∂Cost∂B1
Task 2: Parameter Initialization
2. #activation functions
3. def tanh(x):
4. return np.tanh(x)
5.
6. def relu(x):
7. return np.maximum(x, 0)
8.
9. def softmax(x):
10. expX = np.exp(x)
11. return expX/np.sum(expX, axis = 0)
12.
13. def derivative_tanh(x):
14. return (1 - np.power(np.tanh(x), 2))
15.
16. def derivative_relu(x):
17. return np.array(x > 0, dtype = np.float32)
1. Implement the forward_propagation function using the formulas given in the code:
o Compute the intermediate values Z1, A1, Z2, and A2 using tanh for the hidden
layer and softmax for the output layer.
Forward Propagation
Z1=W1∗X+B1
A1=f(Z1)
Z2=W2∗A1+B2
A2=Softmax(Z2)
w1 = parameters['w1']
b1 = parameters['b1']
w2 = parameters['w2']
b2 = parameters['b2']
z1 = np.dot(w1, x) + b1
a1 = tanh(z1)
z2 = np.dot(w2, a1) + b2
a2 = softmax(z2)
forward_cache = {
"z1" : z1,
"a1" : a1,
"z2" : z2,
"a2" : a2
}
return forward_cache
1. Implement the cost_function that calculates the cross-entropy loss between the
predicted and actual labels.
Cost Function
Cost=−1m∑mi=1∑nk=1[yk∗log(ak)]
1. Implement the backward_prop function to compute gradients of the loss with respect to
each parameter (dW1, db1, dW2, and db2).
2. Use the derivative functions provided (derivative_tanh and derivative_relu) where
necessary.
Backpropagation
dZ2=(A2−Y)
dW2=1m.dZ2.AT1
dZ1=WT2.dZ2∗f|1(Z1)
dB2=1m.sum(dZ2,1)
dW1=1m.dZ1.XT
dB1=1m.sum(dZ1,1)
def backward_prop(x, y, parameters, forward_cache):
w1 = parameters['w1']
b1 = parameters['b1']
w2 = parameters['w2']
b2 = parameters['b2']
a1 = forward_cache['a1']
a2 = forward_cache['a2']
m = x.shape[1]
dz2 = (a2 - y)
dw2 = (1/m)*np.dot(dz2, a1.T)
db2 = (1/m)*np.sum(dz2, axis = 1, keepdims = True)
gradients = {
"dw1" : dw1,
"db1" : db1,
"dw2" : dw2,
"db2" : db2
}
return gradients
1. Complete the model function to train the neural network over a given number of
iterations.
2. def model(x, y, n_h, learning_rate, iterations):
3.
4. n_x = x.shape[0]
5. n_y = y.shape[0]
6.
7. cost_list = []
8.
9. parameters = initialize_parameters(n_x, n_h, n_y)
10.
11. for i in range(iterations):
12.
13. forward_cache = forward_propagation(x, parameters)
14.
15. cost = cost_function(forward_cache['a2'], y)
16.
17. gradients = backward_prop(x, y, parameters,
forward_cache)
18.
19. parameters = update_parameters(parameters, gradients,
learning_rate)
20.
21. cost_list.append(cost)
22.
23. if(i%(iterations/10) == 0):
24. print("Cost after", i, "iterations is :", cost)
25.
26. return parameters, cost_list
2. Track the cost at each iteration and plot the cost values using matplotlib.pyplot.plot.
Task 8: Model Evaluation
1. Implement the accuracy function to compute the classification accuracy for both the
training and test datasets.
print(a_out)
# finds the class with the highest probability for each column
return acc
1. Select a random test image, visualize it, and use the trained model to predict its label.
Print the predicted label and compare it to the true label.
Submission