-
Notifications
You must be signed in to change notification settings - Fork 1
/
public_tests.py
139 lines (114 loc) · 5.08 KB
/
public_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import numpy as np
import math
def sigmoid_test(target):
assert np.isclose(target(3.0), 0.9525741268224334), "Failed for scalar input"
assert np.allclose(target(np.array([2.5, 0])), [0.92414182, 0.5]), "Failed for 1D array"
assert np.allclose(target(np.array([[2.5, -2.5], [0, 1]])),
[[0.92414182, 0.07585818], [0.5, 0.73105858]]), "Failed for 2D array"
print('\033[92mAll tests passed!')
def compute_cost_test(target):
X = np.array([[0, 0, 0, 0]]).T
y = np.array([0, 0, 0, 0])
w = np.array([0])
b = 1
result = target(X, y, w, b)
if math.isinf(result):
raise ValueError("Did you get the sigmoid of z_wb?")
np.random.seed(17)
X = np.random.randn(5, 2)
y = np.array([1, 0, 0, 1, 1])
w = np.random.randn(2)
b = 0
result = target(X, y, w, b)
assert np.isclose(result, 2.15510667), f"Wrong output. Expected: {2.15510667} got: {result}"
X = np.random.randn(4, 3)
y = np.array([1, 1, 0, 0])
w = np.random.randn(3)
b = 0
result = target(X, y, w, b)
assert np.isclose(result, 0.80709376), f"Wrong output. Expected: {0.80709376} got: {result}"
X = np.random.randn(4, 3)
y = np.array([1, 0,1, 0])
w = np.random.randn(3)
b = 3
result = target(X, y, w, b)
assert np.isclose(result, 0.4529660647), f"Wrong output. Expected: {0.4529660647} got: {result}. Did you inizialized z_wb = b?"
print('\033[92mAll tests passed!')
def compute_gradient_test(target):
np.random.seed(1)
X = np.random.randn(7, 3)
y = np.array([1, 0, 1, 0, 1, 1, 0])
test_w = np.array([1, 0.5, -0.35])
test_b = 1.7
dj_db, dj_dw = target(X, y, test_w, test_b)
assert np.isclose(dj_db, 0.28936094), f"Wrong value for dj_db. Expected: {0.28936094} got: {dj_db}"
assert dj_dw.shape == test_w.shape, f"Wrong shape for dj_dw. Expected: {test_w.shape} got: {dj_dw.shape}"
assert np.allclose(dj_dw, [-0.11999166, 0.41498775, -0.71968405]), f"Wrong values for dj_dw. Got: {dj_dw}"
print('\033[92mAll tests passed!')
def predict_test(target):
np.random.seed(5)
b = 0.5
w = np.random.randn(3)
X = np.random.randn(8, 3)
result = target(X, w, b)
wrong_1 = [1., 1., 0., 0., 1., 0., 0., 1.]
expected_1 = [1., 1., 1., 0., 1., 0., 0., 1.]
if np.allclose(result, wrong_1):
raise ValueError("Did you apply the sigmoid before applying the threshold?")
assert result.shape == (len(X),), f"Wrong length. Expected : {(len(X),)} got: {result.shape}"
assert np.allclose(result, expected_1), f"Wrong output: Expected : {expected_1} got: {result}"
b = -1.7
w = np.random.randn(4) + 0.6
X = np.random.randn(6, 4)
result = target(X, w, b)
expected_2 = [0., 0., 0., 1., 1., 0.]
assert result.shape == (len(X),), f"Wrong length. Expected : {(len(X),)} got: {result.shape}"
assert np.allclose(result,expected_2), f"Wrong output: Expected : {expected_2} got: {result}"
print('\033[92mAll tests passed!')
def compute_cost_reg_test(target):
np.random.seed(1)
w = np.random.randn(3)
b = 0.4
X = np.random.randn(6, 3)
y = np.array([0, 1, 1, 0, 1, 1])
lambda_ = 0.1
expected_output = target(X, y, w, b, lambda_)
assert np.isclose(expected_output, 0.5469746792761936), f"Wrong output. Expected: {0.5469746792761936} got:{expected_output}"
w = np.random.randn(5)
b = -0.6
X = np.random.randn(8, 5)
y = np.array([1, 0, 1, 0, 0, 1, 0, 1])
lambda_ = 0.01
output = target(X, y, w, b, lambda_)
assert np.isclose(output, 1.2608591964119995), f"Wrong output. Expected: {1.2608591964119995} got:{output}"
w = np.array([2, 2, 2, 2, 2])
b = 0
X = np.zeros((8, 5))
y = np.array([0.5] * 8)
lambda_ = 3
output = target(X, y, w, b, lambda_)
expected = -np.log(0.5) + 3. / (2. * 8.) * 20.
assert np.isclose(output, expected), f"Wrong output. Expected: {expected} got:{output}"
print('\033[92mAll tests passed!')
def compute_gradient_reg_test(target):
np.random.seed(1)
w = np.random.randn(5)
b = 0.2
X = np.random.randn(7, 5)
y = np.array([0, 1, 1, 0, 1, 1, 0])
lambda_ = 0.1
expected1 = (-0.1506447567869257, np.array([ 0.19530838, -0.00632206, 0.19687367, 0.15741161, 0.02791437]))
dj_db, dj_dw = target(X, y, w, b, lambda_)
assert np.isclose(dj_db, expected1[0]), f"Wrong dj_db. Expected: {expected1[0]} got: {dj_db}"
assert np.allclose(dj_dw, expected1[1]), f"Wrong dj_dw. Expected: {expected1[1]} got: {dj_dw}"
w = np.random.randn(7)
b = 0
X = np.random.randn(7, 7)
y = np.array([1, 0, 0, 0, 1, 1, 0])
lambda_ = 0
expected2 = (0.02660329857573818, np.array([ 0.23567643, -0.06921029, -0.19705212, -0.0002884 , 0.06490588,
0.26948175, 0.10777992]))
dj_db, dj_dw = target(X, y, w, b, lambda_)
assert np.isclose(dj_db, expected2[0]), f"Wrong dj_db. Expected: {expected2[0]} got: {dj_db}"
assert np.allclose(dj_dw, expected2[1]), f"Wrong dj_dw. Expected: {expected2[1]} got: {dj_dw}"
print('\033[92mAll tests passed!')