Path: blob/main/C2 - Advanced Learning Algorithms/week2/C2W2A1/public_tests.py
3520 views
import numpy as np1import tensorflow as tf2from tensorflow.keras.models import Sequential3from tensorflow.keras.layers import Dense4from tensorflow.keras.activations import linear, sigmoid, relu56def test_my_softmax(target):7z = np.array([1., 2., 3., 4.])8a = target(z)9atf = tf.nn.softmax(z)1011assert np.allclose(a, atf, atol=1e-10), f"Wrong values. Expected {atf}, got {a}"1213z = np.array([np.log(0.1)] * 10)14a = target(z)15atf = tf.nn.softmax(z)1617assert np.allclose(a, atf, atol=1e-10), f"Wrong values. Expected {atf}, got {a}"1819print("\033[92m All tests passed.")2021def test_model(target, classes, input_size):22target.build(input_shape=(None,input_size))2324assert len(target.layers) == 3, \25f"Wrong number of layers. Expected 3 but got {len(target.layers)}"26assert target.input.shape.as_list() == [None, input_size], \27f"Wrong input shape. Expected [None, {input_size}] but got {target.input.shape.as_list()}"28i = 029expected = [[Dense, [None, 25], relu],30[Dense, [None, 15], relu],31[Dense, [None, classes], linear]]3233for layer in target.layers:34assert type(layer) == expected[i][0], \35f"Wrong type in layer {i}. Expected {expected[i][0]} but got {type(layer)}"36assert layer.output.shape.as_list() == expected[i][1], \37f"Wrong number of units in layer {i}. Expected {expected[i][1]} but got {layer.output.shape.as_list()}"38assert layer.activation == expected[i][2], \39f"Wrong activation in layer {i}. Expected {expected[i][2]} but got {layer.activation}"40i = i + 14142print("\033[92mAll tests passed!")434445