def backward_propagation_test(target): np.random.seed(1) X = np.random.randn(3, 7) Y = (np.random.randn(1, 7) > 0) parameters = { 'W1': np.random.randn(9, 3), 'W2': np.random.randn(1, 9), 'b1': np.array([[0.], [0.], [0.], [0.], [0.], [0.], [0.], [0.], [0.]]), 'b2': np.array([[0.]]) } cache = { 'A1': np.random.randn(9, 7), 'A2': np.random.randn(1, 7), 'Z1': np.random.randn(7, 9), 'Z2': np.random.randn(1, 7), } expected_output = { 'dW1': np.array([[-0.24468458, -0.24371232, 0.15959609], [0.7370069, -0.64785999, 0.23669823], [0.47936123, -0.01516428, 0.01566728], [0.03361075, -0.0930929, 0.05581073], [0.52445178, -0.03895358, 0.09180612], [-0.17043596, 0.13406378, -0.20952062], [0.76144791, -0.41766018, 0.02544078], [0.22164791, -0.33081645, 0.19526915], [0.25619969, -0.09561825, 0.05679075]]), 'db1': np.array([[0.1463639], [-0.33647992], [-0.51738006], [-0.07780329], [-0.57889514], [0.28357278], [-0.39756864], [-0.10510329], [-0.13443244]]), 'dW2': np.array([[ -0.35768529, 0.22046323, -0.29551566, -0.12202786, 0.18809552, 0.13700323, 0.35892872, -0.02220353, -0.03976687 ]]), 'db2': np.array([[-0.78032466]]) } test_cases = [{ "name": "datatype_check", "input": [parameters, cache, X, Y], "expected": expected_output, "error": "The function should return a numpy array." }, { "name": "shape_check", "input": [parameters, cache, X, Y], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [parameters, cache, X, Y], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def initialize_parameters_random_test(target): layer_dims = [3, 2, 1] expected_output = { 'W1': np.array([[17.88628473, 4.36509851, 0.96497468], [-18.63492703, -2.77388203, -3.54758979]]), 'b1': np.array([[0.], [0.]]), 'W2': np.array([[-0.82741481, -6.27000677]]), 'b2': np.array([[0.]]) } test_cases = [{ "name": "datatype_check", "input": [layer_dims], "expected": expected_output, "error": "Datatype mismatch" }, { "name": "shape_check", "input": [layer_dims], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [layer_dims], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def initialize_velocity_test(target): parameters = initialize_velocity_test_case() expected_output = { 'dW1': np.array([[0., 0., 0.], [0., 0., 0.]]), 'db1': np.array([[0.], [0.]]), 'dW2': np.array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]), 'db2': np.array([[0.], [0.], [0.]]) } test_cases = [{ "name": "datatype_check", "input": [parameters], "expected": expected_output, "error": "Datatype mismatch" }, { "name": "shape_check", "input": [parameters], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [parameters], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def initialize_parameters_test(target): n_x, n_h, n_y = 3, 2, 1 expected_W1 = np.array([[0.01624345, -0.00611756, -0.00528172], [-0.01072969, 0.00865408, -0.02301539]]) expected_b1 = np.array([[0.], [0.]]) expected_W2 = np.array([[0.01744812, -0.00761207]]) expected_b2 = np.array([[0.]]) expected_output = { "W1": expected_W1, "b1": expected_b1, "W2": expected_W2, "b2": expected_b2 } test_cases = [{ "name": "datatype_check", "input": [n_x, n_h, n_y], "expected": expected_output, "error": "Datatype mismatch." }, { "name": "equation_output_check", "input": [n_x, n_h, n_y], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def conv_single_step_test(target): np.random.seed(1) a_slice_prev = np.random.randn(4, 4, 3) W = np.random.randn(4, 4, 3) b = np.random.randn(1, 1, 1) expected_output = np.float64(-6.999089450680221) test_cases = [{ "name": "datatype_check", "input": [a_slice_prev, W, b], "expected": expected_output, "error": "Datatype mismatch" }, { "name": "shape_check", "input": [a_slice_prev, W, b], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [a_slice_prev, W, b], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def linear_forward_test(target): np.random.seed(1) A_prev = np.random.randn(3, 2) W = np.random.randn(1, 3) b = np.random.randn(1, 1) expected_cache = (A_prev, W, b) expected_Z = np.array([[3.26295337, -1.23429987]]) expected_output = (expected_Z, expected_cache) test_cases = [ { "name": "datatype_check", "input": [A_prev, W, b], "expected": expected_output, "error": "Datatype mismatch" }, { "name": "shape_check", "input": [A_prev, W, b], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [A_prev, W, b], "expected": expected_output, "error": "Wrong output" }, ] multiple_test(test_cases, target)
def random_mini_batches_test(target): np.random.seed(1) mini_batch_size = 2 X = np.random.randn(7, 3) Y = np.random.randn(1, 3) < 0.5 expected_output = [(np.array([[-0.52817175, -0.61175641], [-2.3015387, 0.86540763], [0.3190391, -0.7612069], [-2.06014071, 1.46210794], [1.13376944, -0.38405435], [-0.87785842, -0.17242821], [-1.10061918, 0.58281521]]), np.array([[False, False]])), (np.array([[1.62434536], [-1.07296862], [1.74481176], [-0.24937038], [-0.3224172], [-1.09989127], [0.04221375]]), np.array([[False]]))] test_cases = [{ "name": "datatype_check", "input": [X, Y, mini_batch_size], "expected": expected_output, "error": "Datatype mismatch" }, { "name": "shape_check", "input": [X, Y, mini_batch_size], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [X, Y, mini_batch_size], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def initialize_parameters_he_test(target): layer_dims = [2, 4, 1] expected_output = { 'W1': np.array([[1.78862847, 0.43650985], [0.09649747, -1.8634927], [-0.2773882, -0.35475898], [-0.08274148, -0.62700068]]), 'b1': np.array([[0.], [0.], [0.], [0.]]), 'W2': np.array([[-0.03098412, -0.33744411, -0.92904268, 0.62552248]]), 'b2': np.array([[0.]]) } test_cases = [{ "name": "datatype_check", "input": [layer_dims], "expected": expected_output, "error": "Datatype mismatch" }, { "name": "shape_check", "input": [layer_dims], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [layer_dims], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def initialize_adam_test(target): parameters = initialize_adam_test_case() expected_v = { 'dW1': np.array([[0., 0., 0.], [0., 0., 0.]]), 'db1': np.array([[0.], [0.]]), 'dW2': np.array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]), 'db2': np.array([[0.], [0.], [0.]]) } expected_s = { 'dW1': np.array([[0., 0., 0.], [0., 0., 0.]]), 'db1': np.array([[0.], [0.]]), 'dW2': np.array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]), 'db2': np.array([[0.], [0.], [0.]]) } expected_output = (expected_v, expected_s) test_cases = [{ "name": "datatype_check", "input": [parameters], "expected": expected_output, "error": "The function should return a numpy array." }, { "name": "shape_check", "input": [parameters], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [parameters], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def initialize_parameters_he_test(target): layer_dims = [3, 1, 2] expected_output = { 'W1': np.array([[1.46040903, 0.3564088, 0.07878985]]), 'b1': np.array([[0.]]), 'W2': np.array([[-2.63537665], [-0.39228616]]), 'b2': np.array([[0.], [0.]]) } test_cases = [{ "name": "datatype_check", "input": [layer_dims], "expected": expected_output, "error": "Datatype mismatch" }, { "name": "shape_check", "input": [layer_dims], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [layer_dims], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def initialize_parameters_zeros_test(target): layer_dims = [3, 2, 1] expected_output = { 'W1': np.array([[0., 0., 0.], [0., 0., 0.]]), 'b1': np.array([[0.], [0.]]), 'W2': np.array([[0., 0.]]), 'b2': np.array([[0.]]) } test_cases = [{ "name": "datatype_check", "input": [layer_dims], "expected": expected_output, "error": "Datatype mismatch" }, { "name": "shape_check", "input": [layer_dims], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [layer_dims], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def linear_activation_backward_test(target): np.random.seed(2) dA = np.random.randn(1, 2) A = np.random.randn(3, 2) W = np.random.randn(1, 3) b = np.random.randn(1, 1) Z = np.random.randn(1, 2) linear_cache = (A, W, b) activation_cache = Z linear_activation_cache = (linear_cache, activation_cache) expected_dA_prev_sigmoid = np.array([[0.11017994, 0.01105339], [0.09466817, 0.00949723], [-0.05743092, -0.00576154]]) expected_dW_sigmoid = np.array([[0.10266786, 0.09778551, -0.01968084]]) expected_db_sigmoid = np.array([[-0.05729622]]) expected_output_sigmoid = (expected_dA_prev_sigmoid, expected_dW_sigmoid, expected_db_sigmoid) expected_dA_prev_relu = np.array([[0.44090989, 0.], [0.37883606, 0.], [-0.2298228, 0.]]) expected_dW_relu = np.array([[0.44513824, 0.37371418, -0.10478989]]) expected_db_relu = np.array([[-0.20837892]]) expected_output_relu = (expected_dA_prev_relu, expected_dW_relu, expected_db_relu) test_cases = [{ "name": "datatype_check", "input": [dA, linear_activation_cache, 'sigmoid'], "expected": expected_output_sigmoid, "error": "Data type mismatch with sigmoid activation" }, { "name": "shape_check", "input": [dA, linear_activation_cache, 'sigmoid'], "expected": expected_output_sigmoid, "error": "Wrong shape with sigmoid activation" }, { "name": "equation_output_check", "input": [dA, linear_activation_cache, 'sigmoid'], "expected": expected_output_sigmoid, "error": "Wrong output with sigmoid activation" }, { "name": "datatype_check", "input": [dA, linear_activation_cache, 'relu'], "expected": expected_output_relu, "error": "Data type mismatch with relu activation" }, { "name": "shape_check", "input": [dA, linear_activation_cache, 'relu'], "expected": expected_output_relu, "error": "Wrong shape with relu activation" }, { "name": "equation_output_check", "input": [dA, linear_activation_cache, 'relu'], "expected": expected_output_relu, "error": "Wrong output with relu activation" }] multiple_test(test_cases, target)
def L_model_backward_test(target): np.random.seed(3) AL = np.random.randn(1, 2) Y = np.array([[1, 0]]) A1 = np.random.randn(4, 2) W1 = np.random.randn(3, 4) b1 = np.random.randn(3, 1) Z1 = np.random.randn(3, 2) linear_cache_activation_1 = ((A1, W1, b1), Z1) A2 = np.random.randn(3, 2) W2 = np.random.randn(1, 3) b2 = np.random.randn(1, 1) Z2 = np.random.randn(1, 2) linear_cache_activation_2 = ((A2, W2, b2), Z2) caches = (linear_cache_activation_1, linear_cache_activation_2) expected_dA1 = np.array([[0.12913162, -0.44014127], [-0.14175655, 0.48317296], [0.01663708, -0.05670698]]) expected_dW2 = np.array([[-0.39202432, -0.13325855, -0.04601089]]) expected_db2 = np.array([[0.15187861]]) expected_dA0 = np.array([[0., 0.52257901], [0., -0.3269206], [0., -0.32070404], [0., -0.74079187]]) expected_dW1 = np.array([[0.41010002, 0.07807203, 0.13798444, 0.10502167], [0., 0., 0., 0.], [0.05283652, 0.01005865, 0.01777766, 0.0135308]]) expected_db1 = np.array([[-0.22007063], [0.], [-0.02835349]]) expected_output = { 'dA1': expected_dA1, 'dW2': expected_dW2, 'db2': expected_db2, 'dA0': expected_dA0, 'dW1': expected_dW1, 'db1': expected_db1 } test_cases = [{ "name": "datatype_check", "input": [AL, Y, caches], "expected": expected_output, "error": "Data type mismatch" }, { "name": "shape_check", "input": [AL, Y, caches], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [AL, Y, caches], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def forward_propagation_test(target): np.random.seed(1) X = np.random.randn(2, 3) b1 = np.random.randn(4, 1) b2 = np.array([[-1.3]]) parameters = { 'W1': np.array([[-0.00416758, -0.00056267], [-0.02136196, 0.01640271], [-0.01793436, -0.00841747], [0.00502881, -0.01245288]]), 'W2': np.array([[-0.01057952, -0.00909008, 0.00551454, 0.02292208]]), 'b1': b1, 'b2': b2 } expected_A1 = np.array([[0.9400694, 0.94101876, 0.94118266], [-0.67151964, -0.62547205, -0.65709025], [0.29034152, 0.31196971, 0.33449821], [-0.22397799, -0.25730819, -0.2197236]]) expected_A2 = np.array([[0.21292656, 0.21274673, 0.21295976]]) expected_Z1 = np.array([[1.7386459, 1.74687437, 1.74830797], [-0.81350569, -0.73394355, -0.78767559], [0.29893918, 0.32272601, 0.34788465], [-0.2278403, -0.2632236, -0.22336567]]) expected_Z2 = np.array([[-1.30737426, -1.30844761, -1.30717618]]) expected_cache = { "Z1": expected_Z1, "A1": expected_A1, "Z2": expected_Z2, "A2": expected_A2 } expected_output = (expected_A2, expected_cache) test_cases = [{ "name": "datatype_check", "input": [X, parameters], "expected": expected_output, "error": "Datatype mismatch" }, { "name": "shape_check", "input": [X, parameters], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [X, parameters], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def update_parameters_with_momentum_test(target): parameters, grads, v = update_parameters_with_momentum_test_case() beta = 0.9 learning_rate = 0.01 expected_parameters = { 'W1': np.array([[1.62544598, -0.61290114, -0.52907334], [-1.07347112, 0.86450677, -2.30085497]]), 'b1': np.array([[1.74493465], [-0.76027113]]), 'W2': np.array([[0.31930698, -0.24990073, 1.4627996], [-2.05974396, -0.32173003, -0.38320915], [1.13444069, -1.0998786, -0.1713109]]), 'b2': np.array([[-0.87809283], [0.04055394], [0.58207317]]) } expected_v = { 'dW1': np.array([[-0.11006192, 0.11447237, 0.09015907], [0.05024943, 0.09008559, -0.06837279]]), 'dW2': np.array([[-0.02678881, 0.05303555, -0.06916608], [-0.03967535, -0.06871727, -0.08452056], [-0.06712461, -0.00126646, -0.11173103]]), 'db1': np.array([[-0.01228902], [-0.09357694]]), 'db2': np.array([[0.02344157], [0.16598022], [0.07420442]]) } expected_output = (expected_parameters, expected_v) test_cases = [{ "name": "datatype_check", "input": [parameters, grads, v, beta, learning_rate], "expected": expected_output, "error": "Datatype mismatch" }, { "name": "shape_check", "input": [parameters, grads, v, beta, learning_rate], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [parameters, grads, v, beta, learning_rate], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def update_parameters_with_gd_test(target): np.random.seed(1) learning_rate = 0.01 W1 = np.random.randn(2, 3) b1 = np.random.randn(2, 1) W2 = np.random.randn(3, 3) b2 = np.random.randn(3, 1) dW1 = np.random.randn(2, 3) db1 = np.random.randn(2, 1) dW2 = np.random.randn(3, 3) db2 = np.random.randn(3, 1) params = {"W1": W1, "b1": b1, "W2": W2, "b2": b2} grads = {"dW1": dW1, "db1": db1, "dW2": dW2, "db2": db2} expected_output = { 'W1': np.array([[1.63535156, -0.62320365, -0.53718766], [-1.07799357, 0.85639907, -2.29470142]]), 'b1': np.array([[1.74604067], [-0.75184921]]), 'W2': np.array([[0.32171798, -0.25467393, 1.46902454], [-2.05617317, -0.31554548, -0.3756023], [1.1404819, -1.09976462, -0.1612551]]), 'b2': np.array([[-0.88020257], [0.02561572], [0.57539477]]) } test_cases = [{ "name": "datatype_check", "input": [params, grads, learning_rate], "expected": expected_output, "error": "Datatype mismatch." }, { "name": "shape_check", "input": [params, grads, learning_rate], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [params, grads, learning_rate], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def update_parameters_test(target): np.random.seed(2) W1 = np.random.randn(3, 4) b1 = np.random.randn(3, 1) W2 = np.random.randn(1, 3) b2 = np.random.randn(1, 1) parameters = {"W1": W1, "b1": b1, "W2": W2, "b2": b2} np.random.seed(3) dW1 = np.random.randn(3, 4) db1 = np.random.randn(3, 1) dW2 = np.random.randn(1, 3) db2 = np.random.randn(1, 1) grads = {"dW1": dW1, "db1": db1, "dW2": dW2, "db2": db2} learning_rate = 0.1 expected_W1 = np.array( [[-0.59562069, -0.09991781, -2.14584584, 1.82662008], [-1.76569676, -0.80627147, 0.51115557, -1.18258802], [-1.0535704, -0.86128581, 0.68284052, 2.20374577]]) expected_b1 = np.array([[-0.04659241], [-1.28888275], [0.53405496]]) expected_W2 = np.array([[-0.55569196, 0.0354055, 1.32964895]]) expected_b2 = np.array([[-0.84610769]]) expected_output = { "W1": expected_W1, 'b1': expected_b1, 'W2': expected_W2, 'b2': expected_b2 } test_cases = [{ "name": "datatype_check", "input": [parameters, grads, learning_rate], "expected": expected_output, "error": "Data type mismatch" }, { "name": "shape_check", "input": [parameters, grads, learning_rate], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [parameters, grads, 0.1], "expected": expected_output, "error": "Wrong output" }] #print(target(*test_cases[2]["input"])) multiple_test(test_cases, target)
def linear_activation_forward_test(target): np.random.seed(2) A_prev = np.random.randn(3, 2) W = np.random.randn(1, 3) b = np.random.randn(1, 1) expected_linear_cache = (A_prev, W, b) expected_Z = np.array([[3.43896131, -2.08938436]]) expected_cache = (expected_linear_cache, expected_Z) expected_A_sigmoid = np.array([[0.96890023, 0.11013289]]) expected_A_relu = np.array([[3.43896131, 0.]]) expected_output_sigmoid = (expected_A_sigmoid, expected_cache) expected_output_relu = (expected_A_relu, expected_cache) test_cases = [{ "name": "datatype_check", "input": [A_prev, W, b, 'sigmoid'], "expected": expected_output_sigmoid, "error": "Datatype mismatch with sigmoid activation" }, { "name": "shape_check", "input": [A_prev, W, b, 'sigmoid'], "expected": expected_output_sigmoid, "error": "Wrong shape with sigmoid activation" }, { "name": "equation_output_check", "input": [A_prev, W, b, 'sigmoid'], "expected": expected_output_sigmoid, "error": "Wrong output with sigmoid activation" }, { "name": "datatype_check", "input": [A_prev, W, b, 'relu'], "expected": expected_output_relu, "error": "Datatype mismatch with relu activation" }, { "name": "shape_check", "input": [A_prev, W, b, 'relu'], "expected": expected_output_relu, "error": "Wrong shape with relu activation" }, { "name": "equation_output_check", "input": [A_prev, W, b, 'relu'], "expected": expected_output_relu, "error": "Wrong output with relu activation" }] multiple_test(test_cases, target)
def update_lr_test(target): learning_rate = 1 epoch_num = 2 decay_rate = 1 expected_output = 0.3333333333333333 test_cases = [{ "name": "shape_check", "input": [learning_rate, epoch_num, decay_rate], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [learning_rate, epoch_num, decay_rate], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def distribute_value_test(target): test_cases = [{ "name": "datatype_check", "input": [X, Y, n_h], "expected": expected_output, "error": "Data type mismatch" }, { "name": "shape_check", "input": [X, Y, n_h], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [X, Y, n_h], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def layer_sizes_test(target): np.random.seed(1) X = np.random.randn(5, 3) Y = np.random.randn(2, 3) expected_output = (5, 4, 2) test_cases = [{ "name": "datatype_check", "input": [X, Y], "expected": expected_output, "error": "Datatype mismatch." }, { "name": "equation_output_check", "input": [X, Y], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def schedule_lr_decay_test(target): learning_rate = 1 epoch_num_1 = 100 epoch_num_2 = 10 decay_rate = 1 time_interval = 100 expected_output_1 = 0.5 expected_output_2 = 1 test_cases = [{ "name": "shape_check", "input": [learning_rate, epoch_num_1, decay_rate, time_interval], "expected": expected_output_1, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [learning_rate, epoch_num_1, decay_rate, time_interval], "expected": expected_output_1, "error": "Wrong output" }, { "name": "shape_check", "input": [learning_rate, epoch_num_2, decay_rate, time_interval], "expected": expected_output_2, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [learning_rate, epoch_num_2, decay_rate, time_interval], "expected": expected_output_2, "error": "Wrong output" }] multiple_test(test_cases, target)
def nn_model_test(target): np.random.seed(1) X = np.random.randn(2, 3) Y = (np.random.randn(1, 3) > 0) n_h = 4 expected_W1 = np.array([[-0.65848169, 1.21866811], [-0.76204273, 1.39377573], [ 0.5792005 , -1.10397703], [ 0.76773391, -1.41477129]]) expected_W2 = np.array([[-2.45566237, -3.27042274, 2.00784958, 3.36773273]]) expected_b1 = np.array([[ 0.287592 ], [ 0.3511264 ], [-0.2431246 ], [-0.35772805]]) expected_b2 = np.array([[0.20459656]]) expected_output = {"W1": expected_W1, "b1": expected_b1, "W2": expected_W2, "b2": expected_b2} test_cases = [ { "name":"datatype_check", "input": [X, Y, n_h], "expected": expected_output, "error":"Data type mismatch" }, { "name": "shape_check", "input": [X, Y, n_h], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [X, Y, n_h], "expected": expected_output, "error": "Wrong output" } ] multiple_test(test_cases, target)
def initialize_parameters_test(target): n_x, n_h, n_y = 3, 5, 2 expected_output = { 'W1': np.array([[-0.00416758, -0.00056267, -0.02136196], [0.01640271, -0.01793436, -0.00841747], [0.00502881, -0.01245288, -0.01057952], [-0.00909008, 0.00551454, 0.02292208], [0.00041539, -0.01117925, 0.00539058]]), 'b1': np.array([[0.], [0.], [0.], [0.], [0.]]), 'W2': np.array([[ -5.96159700e-03, -1.91304965e-04, 1.17500122e-02, -7.47870949e-03, 9.02525097e-05 ], [ -8.78107893e-03, -1.56434170e-03, 2.56570452e-03, -9.88779049e-03, -3.38821966e-03 ]]), 'b2': np.array([[0.], [0.]]) } test_cases = [{ "name": "datatype_check", "input": [n_x, n_h, n_y], "expected": expected_output, "error": "Datatype mismatch" }, { "name": "shape_check", "input": [n_x, n_h, n_y], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [n_x, n_h, n_y], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def conv_backward_test(target): test_cases = [{ "name": "datatype_check", "input": [parameters, cache, X, Y], "expected": expected_output, "error": "The function should return a numpy array." }, { "name": "shape_check", "input": [parameters, cache, X, Y], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [parameters, cache, X, Y], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def create_mask_from_window_test(target): test_cases = [{ "name": "datatype_check", "input": [parameters, grads], "expected": expected_output, "error": "Data type mismatch" }, { "name": "shape_check", "input": [parameters, grads], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [parameters, grads], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def initialize_parameters_test(target): n_x, n_h, n_y = 2, 4, 1 expected_W1 = np.array([[-0.00416758, -0.00056267], [-0.02136196, 0.01640271], [-0.01793436, -0.00841747], [ 0.00502881, -0.01245288]]) expected_b1 = np.array([[0.], [0.], [0.], [0.]]) expected_W2 = np.array([[-0.01057952, -0.00909008, 0.00551454, 0.02292208]]) expected_b2 = np.array([[0.]]) expected_output = {"W1": expected_W1, "b1": expected_b1, "W2": expected_W2, "b2": expected_b2} test_cases = [ { "name":"datatype_check", "input": [n_x, n_h, n_y], "expected": expected_output, "error":"Datatype mismatch" }, { "name": "shape_check", "input": [n_x, n_h, n_y], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [n_x, n_h, n_y], "expected": expected_output, "error": "Wrong output" } ] multiple_test(test_cases, target)
def linear_backward_test(target): np.random.seed(1) dZ = np.random.randn(3, 4) A = np.random.randn(5, 4) W = np.random.randn(3, 5) b = np.random.randn(3, 1) linear_cache = (A, W, b) expected_dA_prev = np.array( [[-1.15171336, 0.06718465, -0.3204696, 2.09812712], [0.60345879, -3.72508701, 5.81700741, -3.84326836], [-0.4319552, -1.30987417, 1.72354705, 0.05070578], [-0.38981415, 0.60811244, -1.25938424, 1.47191593], [-2.52214926, 2.67882552, -0.67947465, 1.48119548]]) expected_dW = np.array( [[0.07313866, -0.0976715, -0.87585828, 0.73763362, 0.00785716], [0.85508818, 0.37530413, -0.59912655, 0.71278189, -0.58931808], [0.97913304, -0.24376494, -0.08839671, 0.55151192, -0.10290907]]) expected_db = np.array([[-0.14713786], [-0.11313155], [-0.13209101]]) expected_output = (expected_dA_prev, expected_dW, expected_db) test_cases = [{ "name": "datatype_check", "input": [dZ, linear_cache], "expected": expected_output, "error": "Data type mismatch" }, { "name": "shape_check", "input": [dZ, linear_cache], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [dZ, linear_cache], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def initialize_parameters_deep_test(target): layer_dims = [5, 4, 3] expected_W1 = np.array( [[0.01788628, 0.0043651, 0.00096497, -0.01863493, -0.00277388], [-0.00354759, -0.00082741, -0.00627001, -0.00043818, -0.00477218], [-0.01313865, 0.00884622, 0.00881318, 0.01709573, 0.00050034], [-0.00404677, -0.0054536, -0.01546477, 0.00982367, -0.01101068]]) expected_b1 = np.array([[0.], [0.], [0.], [0.]]) expected_W2 = np.array( [[-0.01185047, -0.0020565, 0.01486148, 0.00236716], [-0.01023785, -0.00712993, 0.00625245, -0.00160513], [-0.00768836, -0.00230031, 0.00745056, 0.01976111]]) expected_b2 = np.array([[0.], [0.], [0.]]) expected_output = { "W1": expected_W1, "b1": expected_b1, "W2": expected_W2, "b2": expected_b2 } test_cases = [{ "name": "datatype_check", "input": [layer_dims], "expected": expected_output, "error": "Datatype mismatch" }, { "name": "shape_check", "input": [layer_dims], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [layer_dims], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)
def update_parameters_with_adam_test(target): parameters, grads, v, s = update_parameters_with_adam_test_case() t = 2 learning_rate = 0.02 beta1 = 0.8 beta2 = 0.888 epsilon = 1e-2 expected_parameters = { 'W1': np.array([[1.63949493, -0.62691477, -0.54326465], [-1.08769515, 0.85031501, -2.28657079]]), 'b1': np.array([[1.7549895], [-0.7461017]]), 'W2': np.array([[0.33262355, -0.26414959, 1.47708248], [-2.0457142, -0.30744639, -0.36898502], [1.14872646, -1.09849003, -0.15727519]]), 'b2': np.array([[-0.89102966], [0.02699863], [0.56780324]]) } expected_v = { 'v["dW1"]': np.array([[-0.22012384, 0.22894474, 0.18031814], [0.10049887, 0.18017119, -0.13674557]]), 'v["dW2"]': np.array([[-0.05357762, 0.10607109, -0.13833215], [-0.07935071, -0.13743454, -0.16904113], [-0.13424923, -0.00253292, -0.22346207]]), 'v["db1"]': np.array([[-0.02457805], [-0.18715389]]), 'v["db2"]': np.array([[0.04688314], [0.33196044], [0.14840883]]) } expected_s = { 's["dW1"]': np.array([[0.13567261, 0.14676395, 0.09104097], [0.02828006, 0.09089264, 0.05235818]]), 's["dW2"]': np.array([[0.00803757, 0.03150302, 0.05358019], [0.0176303, 0.05288711, 0.08000973], [5.04639932e-02, 1.79639114e-05, 1.39818830e-01]]), 's["db1"]': np.array([[0.00169142], [0.09807442]]), 's["db2"]': np.array([[0.00615448], [0.30855365], [0.06167051]]) } expected_output = (expected_parameters, expected_v, expected_s) test_cases = [{ "name": "datatype_check", "input": [parameters, grads, v, s, t, learning_rate, beta1, beta2, epsilon], "expected": expected_output, "error": "Data type mismatch" }, { "name": "shape_check", "input": [parameters, grads, v, s, t, learning_rate, beta1, beta2, epsilon], "expected": expected_output, "error": "Wrong shape" }, { "name": "equation_output_check", "input": [parameters, grads, v, s, t, learning_rate, beta1, beta2, epsilon], "expected": expected_output, "error": "Wrong output" }] multiple_test(test_cases, target)