Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Custom Models, Layers, and Loss Functions with TensorFlow/Week 3 - Custom Layers/utils.py
Views: 13373
import tensorflow as tf1from tensorflow.python.ops.resource_variable_ops import ResourceVariable2import numpy as np34def test_loop(test_cases):56success = 07fails = 089for test_case in test_cases:10try:11assert test_case["result"] == test_case["expected"]12success += 11314except:15fails += 116print(f'{test_case["name"]}: {test_case["error_message"]}\nExpected: {test_case["expected"]}\nResult: {test_case["result"]}\n')1718if fails == 0:19print("\033[92m All public tests passed")2021else:22print('\033[92m', success," Tests passed")23print('\033[91m', fails, " Tests failed")24raise Exception(test_case["error_message"])252627def test_simple_quadratic(SimpleQuadratic):2829expected_units = 12830expected_activation_function = tf.keras.activations.relu31expected_activation_string = 'relu'32shape_0 = 833shape_1 = 23435test_layer = SimpleQuadratic(units=expected_units, activation=expected_activation_string)3637test_layer.build((shape_0, shape_1))3839test_inputs = tf.random.uniform((shape_0, shape_1))4041test_call_value = test_layer.call(test_inputs)4243a_type = type(test_layer.a)44b_type = type(test_layer.b)45c_type = type(test_layer.c)4647test_layer_forced_weights = SimpleQuadratic(units=1, activation=None)48test_layer_forced_weights.a = tf.constant([2.0], dtype='float32', shape=(1,1))49test_layer_forced_weights.b = tf.constant([2.0], dtype='float32', shape=(1,1))50test_layer_forced_weights.c = tf.constant([2.0], dtype='float32', shape=(1,1))51test_layer_forced_weights_inputs = tf.constant([4.0], dtype='float32', shape=(1,1))52test_layer_forced_weights_expected_output = 42.05354test_cases = [55{56"name": "units_check",57"result": test_layer.units,58"expected": expected_units,59"error_message": f'Incorrect number of units.'60},61{62"name": "activations_check",63"result": test_layer.activation,64"expected": tf.keras.activations.relu,65"error_message": "Got different activation function."66},67{68"name": "a_type_check",69"result": a_type,70"expected": ResourceVariable,71"error_message": f'State variable a is of different type. Expected ResourceVariable but got {a_type}'72},73{74"name": "b_type_check",75"result": b_type,76"expected": ResourceVariable,77"error_message": f'State variable b is of different type. Expected ResourceVariable but got {b_type}'78},79{80"name": "c_type_check",81"result": c_type,82"expected": ResourceVariable,83"error_message": f'State variable c is of different type. Expected ResourceVariable but got {c_type}'84},85{86"name": "a_initializer_check",87"result": test_layer.a.numpy().sum() != 0,88"expected": True,89"error_message": f'State variable a is not initialized randomly. Please check initializer used.'90},91{92"name": "b_initializer_check",93"result": test_layer.b.numpy().sum() != 0,94"expected": True,95"error_message": f'State variable b is not initialized randomly. Please check initializer used.'96},97{98"name": "c_initializer_check",99"result": test_layer.c.numpy().sum() == 0,100"expected": True,101"error_message": f'State variable c is not initialized to zeroes. Please check initializer used.'102},103{104"name": "output_check",105"result": test_layer_forced_weights.call(test_layer_forced_weights_inputs).numpy()[0][0],106"expected": test_layer_forced_weights_expected_output,107"error_message": f'Expected output is incorrect. Please check operations in the call() method.'108}109]110111test_loop(test_cases)112113114