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 2 - Custom Loss Functions/utils.py
Views: 13373
import numpy as np1from sklearn.model_selection import train_test_split2from tensorflow.python.framework.ops import EagerTensor3from numpy import int6445def test_loop(test_cases):67success = 08fails = 0910for test_case in test_cases:11try:12assert test_case["result"] == test_case["expected"]13success += 11415except:16fails += 117print(f'{test_case["name"]}: {test_case["error_message"]}\nExpected: {test_case["expected"]}\nResult: {test_case["result"]}\n')1819if fails == 0:20print("\033[92m All public tests passed")2122else:23print('\033[92m', success," Tests passed")24print('\033[91m', fails, " Tests failed")25raise Exception(test_case["error_message"])262728def test_my_rmse(my_rmse):2930test_y_true = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)31test_y_pred = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)3233expected = 1.77951304200521853435result = my_rmse(test_y_true, test_y_pred)3637test_cases = [38{39"name": "type_check",40"result": type(result),41"expected": EagerTensor,42"error_message": f'output has an incorrect type.'43},44{45"name": "output_check",46"result": result,47"expected": expected,48"error_message": "Output is incorrect. Please check the equation."49}50]5152test_loop(test_cases)5354def test_model_loss(model_loss):5556test_y_true = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)57test_y_pred = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)5859expected = 1.77951304200521856061result = model_loss(test_y_true, test_y_pred)6263test_cases = [64{65"name": "type_check",66"result": type(result),67"expected": EagerTensor,68"error_message": f'output has an incorrect type.'69},70{71"name": "output_check",72"result": result,73"expected": expected,74"error_message": "Output is incorrect. Please check the equation."75}76]7778test_loop(test_cases)7980