CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
amanchadha

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: amanchadha/coursera-deep-learning-specialization
Path: blob/master/C2 - Improving Deep Neural Networks Hyperparameter tuning, Regularization and Optimization/Week 2/testCases.py
Views: 4802
1
import numpy as np
2
3
def update_parameters_with_gd_test_case():
4
np.random.seed(1)
5
learning_rate = 0.01
6
W1 = np.random.randn(2,3)
7
b1 = np.random.randn(2,1)
8
W2 = np.random.randn(3,3)
9
b2 = np.random.randn(3,1)
10
11
dW1 = np.random.randn(2,3)
12
db1 = np.random.randn(2,1)
13
dW2 = np.random.randn(3,3)
14
db2 = np.random.randn(3,1)
15
16
parameters = {"W1": W1, "b1": b1, "W2": W2, "b2": b2}
17
grads = {"dW1": dW1, "db1": db1, "dW2": dW2, "db2": db2}
18
19
return parameters, grads, learning_rate
20
21
"""
22
def update_parameters_with_sgd_checker(function, inputs, outputs):
23
if function(inputs) == outputs:
24
print("Correct")
25
else:
26
print("Incorrect")
27
"""
28
29
def random_mini_batches_test_case():
30
np.random.seed(1)
31
mini_batch_size = 64
32
X = np.random.randn(12288, 148)
33
Y = np.random.randn(1, 148) < 0.5
34
return X, Y, mini_batch_size
35
36
def initialize_velocity_test_case():
37
np.random.seed(1)
38
W1 = np.random.randn(2,3)
39
b1 = np.random.randn(2,1)
40
W2 = np.random.randn(3,3)
41
b2 = np.random.randn(3,1)
42
parameters = {"W1": W1, "b1": b1, "W2": W2, "b2": b2}
43
return parameters
44
45
def update_parameters_with_momentum_test_case():
46
np.random.seed(1)
47
W1 = np.random.randn(2,3)
48
b1 = np.random.randn(2,1)
49
W2 = np.random.randn(3,3)
50
b2 = np.random.randn(3,1)
51
52
dW1 = np.random.randn(2,3)
53
db1 = np.random.randn(2,1)
54
dW2 = np.random.randn(3,3)
55
db2 = np.random.randn(3,1)
56
parameters = {"W1": W1, "b1": b1, "W2": W2, "b2": b2}
57
grads = {"dW1": dW1, "db1": db1, "dW2": dW2, "db2": db2}
58
v = {'dW1': np.array([[ 0., 0., 0.],
59
[ 0., 0., 0.]]), 'dW2': np.array([[ 0., 0., 0.],
60
[ 0., 0., 0.],
61
[ 0., 0., 0.]]), 'db1': np.array([[ 0.],
62
[ 0.]]), 'db2': np.array([[ 0.],
63
[ 0.],
64
[ 0.]])}
65
return parameters, grads, v
66
67
def initialize_adam_test_case():
68
np.random.seed(1)
69
W1 = np.random.randn(2,3)
70
b1 = np.random.randn(2,1)
71
W2 = np.random.randn(3,3)
72
b2 = np.random.randn(3,1)
73
parameters = {"W1": W1, "b1": b1, "W2": W2, "b2": b2}
74
return parameters
75
76
def update_parameters_with_adam_test_case():
77
np.random.seed(1)
78
v, s = ({'dW1': np.array([[ 0., 0., 0.],
79
[ 0., 0., 0.]]), 'dW2': np.array([[ 0., 0., 0.],
80
[ 0., 0., 0.],
81
[ 0., 0., 0.]]), 'db1': np.array([[ 0.],
82
[ 0.]]), 'db2': np.array([[ 0.],
83
[ 0.],
84
[ 0.]])}, {'dW1': np.array([[ 0., 0., 0.],
85
[ 0., 0., 0.]]), 'dW2': np.array([[ 0., 0., 0.],
86
[ 0., 0., 0.],
87
[ 0., 0., 0.]]), 'db1': np.array([[ 0.],
88
[ 0.]]), 'db2': np.array([[ 0.],
89
[ 0.],
90
[ 0.]])})
91
W1 = np.random.randn(2,3)
92
b1 = np.random.randn(2,1)
93
W2 = np.random.randn(3,3)
94
b2 = np.random.randn(3,1)
95
96
dW1 = np.random.randn(2,3)
97
db1 = np.random.randn(2,1)
98
dW2 = np.random.randn(3,3)
99
db2 = np.random.randn(3,1)
100
101
parameters = {"W1": W1, "b1": b1, "W2": W2, "b2": b2}
102
grads = {"dW1": dW1, "db1": db1, "dW2": dW2, "db2": db2}
103
104
return parameters, grads, v, s
105
106