Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
greyhatguy007
GitHub Repository: greyhatguy007/Machine-Learning-Specialization-Coursera
Path: blob/main/C2 - Advanced Learning Algorithms/week2/C2W2A1/public_tests.py
3520 views
1
import numpy as np
2
import tensorflow as tf
3
from tensorflow.keras.models import Sequential
4
from tensorflow.keras.layers import Dense
5
from tensorflow.keras.activations import linear, sigmoid, relu
6
7
def test_my_softmax(target):
8
z = np.array([1., 2., 3., 4.])
9
a = target(z)
10
atf = tf.nn.softmax(z)
11
12
assert np.allclose(a, atf, atol=1e-10), f"Wrong values. Expected {atf}, got {a}"
13
14
z = np.array([np.log(0.1)] * 10)
15
a = target(z)
16
atf = tf.nn.softmax(z)
17
18
assert np.allclose(a, atf, atol=1e-10), f"Wrong values. Expected {atf}, got {a}"
19
20
print("\033[92m All tests passed.")
21
22
def test_model(target, classes, input_size):
23
target.build(input_shape=(None,input_size))
24
25
assert len(target.layers) == 3, \
26
f"Wrong number of layers. Expected 3 but got {len(target.layers)}"
27
assert target.input.shape.as_list() == [None, input_size], \
28
f"Wrong input shape. Expected [None, {input_size}] but got {target.input.shape.as_list()}"
29
i = 0
30
expected = [[Dense, [None, 25], relu],
31
[Dense, [None, 15], relu],
32
[Dense, [None, classes], linear]]
33
34
for layer in target.layers:
35
assert type(layer) == expected[i][0], \
36
f"Wrong type in layer {i}. Expected {expected[i][0]} but got {type(layer)}"
37
assert layer.output.shape.as_list() == expected[i][1], \
38
f"Wrong number of units in layer {i}. Expected {expected[i][1]} but got {layer.output.shape.as_list()}"
39
assert layer.activation == expected[i][2], \
40
f"Wrong activation in layer {i}. Expected {expected[i][2]} but got {layer.activation}"
41
i = i + 1
42
43
print("\033[92mAll tests passed!")
44
45