-
Notifications
You must be signed in to change notification settings - Fork 0
/
public_tests.py
67 lines (56 loc) · 2.24 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
import numpy as np
def compute_cost_test(target):
# print("Using X with shape (4, 1)")
# Case 1
x = np.array([2, 4, 6, 8]).T
y = np.array([7, 11, 15, 19]).T
initial_w = 2
initial_b = 3.0
cost = target(x, y, initial_w, initial_b)
assert cost == 0, f"Case 1: Cost must be 0 for a perfect prediction but got {cost}"
# Case 2
x = np.array([2, 4, 6, 8]).T
y = np.array([7, 11, 15, 19]).T
initial_w = 2.0
initial_b = 1.0
cost = target(x, y, initial_w, initial_b)
assert cost == 2, f"Case 2: Cost must be 2 but got {cost}"
# print("Using X with shape (5, 1)")
# Case 3
x = np.array([1.5, 2.5, 3.5, 4.5, 1.5]).T
y = np.array([4, 7, 10, 13, 5]).T
initial_w = 1
initial_b = 0.0
cost = target(x, y, initial_w, initial_b)
assert np.isclose(cost, 15.325), f"Case 3: Cost must be 15.325 for a perfect prediction but got {cost}"
# Case 4
initial_b = 1.0
cost = target(x, y, initial_w, initial_b)
assert np.isclose(cost, 10.725), f"Case 4: Cost must be 10.725 but got {cost}"
# Case 5
y = y - 2
initial_b = 1.0
cost = target(x, y, initial_w, initial_b)
assert np.isclose(cost, 4.525), f"Case 5: Cost must be 4.525 but got {cost}"
print("\033[92mAll tests passed!")
def compute_gradient_test(target):
print("Using X with shape (4, 1)")
# Case 1
x = np.array([2, 4, 6, 8]).T
y = np.array([4.5, 8.5, 12.5, 16.5]).T
initial_w = 2.
initial_b = 0.5
dj_dw, dj_db = target(x, y, initial_w, initial_b)
#assert dj_dw.shape == initial_w.shape, f"Wrong shape for dj_dw. {dj_dw} != {initial_w.shape}"
assert dj_db == 0.0, f"Case 1: dj_db is wrong: {dj_db} != 0.0"
assert np.allclose(dj_dw, 0), f"Case 1: dj_dw is wrong: {dj_dw} != [[0.0]]"
# Case 2
x = np.array([2, 4, 6, 8]).T
y = np.array([4, 7, 10, 13]).T + 2
initial_w = 1.5
initial_b = 1
dj_dw, dj_db = target(x, y, initial_w, initial_b)
#assert dj_dw.shape == initial_w.shape, f"Wrong shape for dj_dw. {dj_dw} != {initial_w.shape}"
assert dj_db == -2, f"Case 1: dj_db is wrong: {dj_db} != -2"
assert np.allclose(dj_dw, -10.0), f"Case 1: dj_dw is wrong: {dj_dw} != -10.0"
print("\033[92mAll tests passed!")