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 3 - Custom Layers/utils.py
Views: 13373
1
import tensorflow as tf
2
from tensorflow.python.ops.resource_variable_ops import ResourceVariable
3
import numpy as np
4
5
def test_loop(test_cases):
6
7
success = 0
8
fails = 0
9
10
for test_case in test_cases:
11
try:
12
assert test_case["result"] == test_case["expected"]
13
success += 1
14
15
except:
16
fails += 1
17
print(f'{test_case["name"]}: {test_case["error_message"]}\nExpected: {test_case["expected"]}\nResult: {test_case["result"]}\n')
18
19
if fails == 0:
20
print("\033[92m All public tests passed")
21
22
else:
23
print('\033[92m', success," Tests passed")
24
print('\033[91m', fails, " Tests failed")
25
raise Exception(test_case["error_message"])
26
27
28
def test_simple_quadratic(SimpleQuadratic):
29
30
expected_units = 128
31
expected_activation_function = tf.keras.activations.relu
32
expected_activation_string = 'relu'
33
shape_0 = 8
34
shape_1 = 2
35
36
test_layer = SimpleQuadratic(units=expected_units, activation=expected_activation_string)
37
38
test_layer.build((shape_0, shape_1))
39
40
test_inputs = tf.random.uniform((shape_0, shape_1))
41
42
test_call_value = test_layer.call(test_inputs)
43
44
a_type = type(test_layer.a)
45
b_type = type(test_layer.b)
46
c_type = type(test_layer.c)
47
48
test_layer_forced_weights = SimpleQuadratic(units=1, activation=None)
49
test_layer_forced_weights.a = tf.constant([2.0], dtype='float32', shape=(1,1))
50
test_layer_forced_weights.b = tf.constant([2.0], dtype='float32', shape=(1,1))
51
test_layer_forced_weights.c = tf.constant([2.0], dtype='float32', shape=(1,1))
52
test_layer_forced_weights_inputs = tf.constant([4.0], dtype='float32', shape=(1,1))
53
test_layer_forced_weights_expected_output = 42.0
54
55
test_cases = [
56
{
57
"name": "units_check",
58
"result": test_layer.units,
59
"expected": expected_units,
60
"error_message": f'Incorrect number of units.'
61
},
62
{
63
"name": "activations_check",
64
"result": test_layer.activation,
65
"expected": tf.keras.activations.relu,
66
"error_message": "Got different activation function."
67
},
68
{
69
"name": "a_type_check",
70
"result": a_type,
71
"expected": ResourceVariable,
72
"error_message": f'State variable a is of different type. Expected ResourceVariable but got {a_type}'
73
},
74
{
75
"name": "b_type_check",
76
"result": b_type,
77
"expected": ResourceVariable,
78
"error_message": f'State variable b is of different type. Expected ResourceVariable but got {b_type}'
79
},
80
{
81
"name": "c_type_check",
82
"result": c_type,
83
"expected": ResourceVariable,
84
"error_message": f'State variable c is of different type. Expected ResourceVariable but got {c_type}'
85
},
86
{
87
"name": "a_initializer_check",
88
"result": test_layer.a.numpy().sum() != 0,
89
"expected": True,
90
"error_message": f'State variable a is not initialized randomly. Please check initializer used.'
91
},
92
{
93
"name": "b_initializer_check",
94
"result": test_layer.b.numpy().sum() != 0,
95
"expected": True,
96
"error_message": f'State variable b is not initialized randomly. Please check initializer used.'
97
},
98
{
99
"name": "c_initializer_check",
100
"result": test_layer.c.numpy().sum() == 0,
101
"expected": True,
102
"error_message": f'State variable c is not initialized to zeroes. Please check initializer used.'
103
},
104
{
105
"name": "output_check",
106
"result": test_layer_forced_weights.call(test_layer_forced_weights_inputs).numpy()[0][0],
107
"expected": test_layer_forced_weights_expected_output,
108
"error_message": f'Expected output is incorrect. Please check operations in the call() method.'
109
}
110
]
111
112
test_loop(test_cases)
113
114