Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
greyhatguy007
GitHub Repository: greyhatguy007/Machine-Learning-Specialization-Coursera
Path: blob/main/C3 - Unsupervised Learning, Recommenders, Reinforcement Learning/week2/C3W2/C3W2A2/public_tests.py
3565 views
1
from tensorflow.keras.activations import relu, linear
2
from tensorflow.keras.layers import Dense
3
4
import numpy as np
5
6
def test_tower(target):
7
num_outputs = 32
8
i = 0
9
assert len(target.layers) == 3, f"Wrong number of layers. Expected 3 but got {len(target.layers)}"
10
expected = [[Dense, [None, 256], relu],
11
[Dense, [None, 128], relu],
12
[Dense, [None, num_outputs], linear]]
13
14
for layer in target.layers:
15
assert type(layer) == expected[i][0], \
16
f"Wrong type in layer {i}. Expected {expected[i][0]} but got {type(layer)}"
17
assert layer.output.shape.as_list() == expected[i][1], \
18
f"Wrong number of units in layer {i}. Expected {expected[i][1]} but got {layer.output.shape.as_list()}"
19
assert layer.activation == expected[i][2], \
20
f"Wrong activation in layer {i}. Expected {expected[i][2]} but got {layer.activation}"
21
i = i + 1
22
23
print("\033[92mAll tests passed!")
24
25
26
def test_sq_dist(target):
27
a1 = np.array([1.0, 2.0, 3.0]); b1 = np.array([1.0, 2.0, 3.0])
28
c1 = target(a1, b1)
29
a2 = np.array([1.1, 2.1, 3.1]); b2 = np.array([1.0, 2.0, 3.0])
30
c2 = target(a2, b2)
31
a3 = np.array([0, 1]); b3 = np.array([1, 0])
32
c3 = target(a3, b3)
33
a4 = np.array([1, 1, 1, 1, 1]); b4 = np.array([0, 0, 0, 0, 0])
34
c4 = target(a4, b4)
35
36
assert np.isclose(c1, 0), f"Wrong value. Expected {0}, got {c1}"
37
assert np.isclose(c2, 0.03), f"Wrong value. Expected {0.03}, got {c2}"
38
assert np.isclose(c3, 2), f"Wrong value. Expected {2}, got {c3}"
39
assert np.isclose(c4, 5), f"Wrong value. Expected {5}, got {c4}"
40
41
print('\033[92mAll tests passed!')
42
43