CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

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: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Custom Models, Layers, and Loss Functions with TensorFlow/Week 2 - Custom Loss Functions/utils.py
Views: 13373
1
import numpy as np
2
from sklearn.model_selection import train_test_split
3
from tensorflow.python.framework.ops import EagerTensor
4
from numpy import int64
5
6
def test_loop(test_cases):
7
8
success = 0
9
fails = 0
10
11
for test_case in test_cases:
12
try:
13
assert test_case["result"] == test_case["expected"]
14
success += 1
15
16
except:
17
fails += 1
18
print(f'{test_case["name"]}: {test_case["error_message"]}\nExpected: {test_case["expected"]}\nResult: {test_case["result"]}\n')
19
20
if fails == 0:
21
print("\033[92m All public tests passed")
22
23
else:
24
print('\033[92m', success," Tests passed")
25
print('\033[91m', fails, " Tests failed")
26
raise Exception(test_case["error_message"])
27
28
29
def test_my_rmse(my_rmse):
30
31
test_y_true = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
32
test_y_pred = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)
33
34
expected = 1.7795130420052185
35
36
result = my_rmse(test_y_true, test_y_pred)
37
38
test_cases = [
39
{
40
"name": "type_check",
41
"result": type(result),
42
"expected": EagerTensor,
43
"error_message": f'output has an incorrect type.'
44
},
45
{
46
"name": "output_check",
47
"result": result,
48
"expected": expected,
49
"error_message": "Output is incorrect. Please check the equation."
50
}
51
]
52
53
test_loop(test_cases)
54
55
def test_model_loss(model_loss):
56
57
test_y_true = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
58
test_y_pred = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)
59
60
expected = 1.7795130420052185
61
62
result = model_loss(test_y_true, test_y_pred)
63
64
test_cases = [
65
{
66
"name": "type_check",
67
"result": type(result),
68
"expected": EagerTensor,
69
"error_message": f'output has an incorrect type.'
70
},
71
{
72
"name": "output_check",
73
"result": result,
74
"expected": expected,
75
"error_message": "Output is incorrect. Please check the equation."
76
}
77
]
78
79
test_loop(test_cases)
80