Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Neural Networks and Deep Learning/Week 4/Building your Deep Neural Network - Step by Step/testCases_v4.py
Views: 13378
import numpy as np12def linear_forward_test_case():3np.random.seed(1)4"""5X = np.array([[-1.02387576, 1.12397796],6[-1.62328545, 0.64667545],7[-1.74314104, -0.59664964]])8W = np.array([[ 0.74505627, 1.97611078, -1.24412333]])9b = np.array([[1]])10"""11A = np.random.randn(3,2)12W = np.random.randn(1,3)13b = np.random.randn(1,1)1415return A, W, b1617def linear_activation_forward_test_case():18"""19X = np.array([[-1.02387576, 1.12397796],20[-1.62328545, 0.64667545],21[-1.74314104, -0.59664964]])22W = np.array([[ 0.74505627, 1.97611078, -1.24412333]])23b = 524"""25np.random.seed(2)26A_prev = np.random.randn(3,2)27W = np.random.randn(1,3)28b = np.random.randn(1,1)29return A_prev, W, b3031def L_model_forward_test_case():32"""33X = np.array([[-1.02387576, 1.12397796],34[-1.62328545, 0.64667545],35[-1.74314104, -0.59664964]])36parameters = {'W1': np.array([[ 1.62434536, -0.61175641, -0.52817175],37[-1.07296862, 0.86540763, -2.3015387 ]]),38'W2': np.array([[ 1.74481176, -0.7612069 ]]),39'b1': np.array([[ 0.],40[ 0.]]),41'b2': np.array([[ 0.]])}42"""43np.random.seed(1)44X = np.random.randn(4,2)45W1 = np.random.randn(3,4)46b1 = np.random.randn(3,1)47W2 = np.random.randn(1,3)48b2 = np.random.randn(1,1)49parameters = {"W1": W1,50"b1": b1,51"W2": W2,52"b2": b2}5354return X, parameters5556def compute_cost_test_case():57Y = np.asarray([[1, 1, 1]])58aL = np.array([[.8,.9,0.4]])5960return Y, aL6162def linear_backward_test_case():63"""64z, linear_cache = (np.array([[-0.8019545 , 3.85763489]]), (np.array([[-1.02387576, 1.12397796],65[-1.62328545, 0.64667545],66[-1.74314104, -0.59664964]]), np.array([[ 0.74505627, 1.97611078, -1.24412333]]), np.array([[1]]))67"""68np.random.seed(1)69dZ = np.random.randn(1,2)70A = np.random.randn(3,2)71W = np.random.randn(1,3)72b = np.random.randn(1,1)73linear_cache = (A, W, b)74return dZ, linear_cache7576def linear_activation_backward_test_case():77"""78aL, linear_activation_cache = (np.array([[ 3.1980455 , 7.85763489]]), ((np.array([[-1.02387576, 1.12397796], [-1.62328545, 0.64667545], [-1.74314104, -0.59664964]]), np.array([[ 0.74505627, 1.97611078, -1.24412333]]), 5), np.array([[ 3.1980455 , 7.85763489]])))79"""80np.random.seed(2)81dA = np.random.randn(1,2)82A = np.random.randn(3,2)83W = np.random.randn(1,3)84b = np.random.randn(1,1)85Z = np.random.randn(1,2)86linear_cache = (A, W, b)87activation_cache = Z88linear_activation_cache = (linear_cache, activation_cache)8990return dA, linear_activation_cache9192def L_model_backward_test_case():93"""94X = np.random.rand(3,2)95Y = np.array([[1, 1]])96parameters = {'W1': np.array([[ 1.78862847, 0.43650985, 0.09649747]]), 'b1': np.array([[ 0.]])}9798aL, caches = (np.array([[ 0.60298372, 0.87182628]]), [((np.array([[ 0.20445225, 0.87811744],99[ 0.02738759, 0.67046751],100[ 0.4173048 , 0.55868983]]),101np.array([[ 1.78862847, 0.43650985, 0.09649747]]),102np.array([[ 0.]])),103np.array([[ 0.41791293, 1.91720367]]))])104"""105np.random.seed(3)106AL = np.random.randn(1, 2)107Y = np.array([[1, 0]])108109A1 = np.random.randn(4,2)110W1 = np.random.randn(3,4)111b1 = np.random.randn(3,1)112Z1 = np.random.randn(3,2)113linear_cache_activation_1 = ((A1, W1, b1), Z1)114115A2 = np.random.randn(3,2)116W2 = np.random.randn(1,3)117b2 = np.random.randn(1,1)118Z2 = np.random.randn(1,2)119linear_cache_activation_2 = ((A2, W2, b2), Z2)120121caches = (linear_cache_activation_1, linear_cache_activation_2)122123return AL, Y, caches124125def update_parameters_test_case():126"""127parameters = {'W1': np.array([[ 1.78862847, 0.43650985, 0.09649747],128[-1.8634927 , -0.2773882 , -0.35475898],129[-0.08274148, -0.62700068, -0.04381817],130[-0.47721803, -1.31386475, 0.88462238]]),131'W2': np.array([[ 0.88131804, 1.70957306, 0.05003364, -0.40467741],132[-0.54535995, -1.54647732, 0.98236743, -1.10106763],133[-1.18504653, -0.2056499 , 1.48614836, 0.23671627]]),134'W3': np.array([[-1.02378514, -0.7129932 , 0.62524497],135[-0.16051336, -0.76883635, -0.23003072]]),136'b1': np.array([[ 0.],137[ 0.],138[ 0.],139[ 0.]]),140'b2': np.array([[ 0.],141[ 0.],142[ 0.]]),143'b3': np.array([[ 0.],144[ 0.]])}145grads = {'dW1': np.array([[ 0.63070583, 0.66482653, 0.18308507],146[ 0. , 0. , 0. ],147[ 0. , 0. , 0. ],148[ 0. , 0. , 0. ]]),149'dW2': np.array([[ 1.62934255, 0. , 0. , 0. ],150[ 0. , 0. , 0. , 0. ],151[ 0. , 0. , 0. , 0. ]]),152'dW3': np.array([[-1.40260776, 0. , 0. ]]),153'da1': np.array([[ 0.70760786, 0.65063504],154[ 0.17268975, 0.15878569],155[ 0.03817582, 0.03510211]]),156'da2': np.array([[ 0.39561478, 0.36376198],157[ 0.7674101 , 0.70562233],158[ 0.0224596 , 0.02065127],159[-0.18165561, -0.16702967]]),160'da3': np.array([[ 0.44888991, 0.41274769],161[ 0.31261975, 0.28744927],162[-0.27414557, -0.25207283]]),163'db1': 0.75937676204411464,164'db2': 0.86163759922811056,165'db3': -0.84161956022334572}166"""167np.random.seed(2)168W1 = np.random.randn(3,4)169b1 = np.random.randn(3,1)170W2 = np.random.randn(1,3)171b2 = np.random.randn(1,1)172parameters = {"W1": W1,173"b1": b1,174"W2": W2,175"b2": b2}176np.random.seed(3)177dW1 = np.random.randn(3,4)178db1 = np.random.randn(3,1)179dW2 = np.random.randn(1,3)180db2 = np.random.randn(1,1)181grads = {"dW1": dW1,182"db1": db1,183"dW2": dW2,184"db2": db2}185186return parameters, grads187188189def L_model_forward_test_case_2hidden():190np.random.seed(6)191X = np.random.randn(5,4)192W1 = np.random.randn(4,5)193b1 = np.random.randn(4,1)194W2 = np.random.randn(3,4)195b2 = np.random.randn(3,1)196W3 = np.random.randn(1,3)197b3 = np.random.randn(1,1)198199parameters = {"W1": W1,200"b1": b1,201"W2": W2,202"b2": b2,203"W3": W3,204"b3": b3}205206return X, parameters207208def print_grads(grads):209print ("dW1 = "+ str(grads["dW1"]))210print ("db1 = "+ str(grads["db1"]))211print ("dA1 = "+ str(grads["dA1"]))212213214215