<jupyter_start><jupyter_text>Ungraded Lab: Huber LossIn this lab, we'll walk through how to create custom loss functions. In particular, we'll code the [Huber Loss](https://en.wikipedia.org/wiki/Huber_loss) and use that in training the model.  Imports<jupyter_code>try:
  # %tensorflow_version only exists in Colab.
  %tensorflow_version 2.x
except Exception:
  pass

import tensorflow as tf
import numpy as np
from tensorflow import keras<jupyter_output><empty_output><jupyter_text>Prepare the DataOur dummy dataset is just a pair of arrays `xs` and `ys` defined by the relationship $y = 2x - 1$. `xs` are the inputs while `ys` are the labels.<jupyter_code># inputs
xs = np.array([-1.0,  0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)

# labels
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)<jupyter_output><empty_output><jupyter_text>Training the modelLet's build a simple model and train using a built-in loss function like the `mean_squared_error`.<jupyter_code>model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
model.fit(xs, ys, epochs=500,verbose=0)

print(model.predict([10.0]))<jupyter_output>[[18.98586]]<jupyter_text>Custom LossNow let's see how we can use a custom loss. We first define a function that accepts the ground truth labels (`y_true`) and model predictions (`y_pred`) as parameters. We then compute and return the loss value in the function definition.<jupyter_code>def my_huber_loss(y_true, y_pred):
    threshold = 1
    error = y_true - y_pred
    is_small_error = tf.abs(error) <= threshold
    small_error_loss = tf.square(error) / 2
    big_error_loss = threshold * (tf.abs(error) - (0.5 * threshold))
    return tf.where(is_small_error, small_error_loss, big_error_loss)<jupyter_output><empty_output><jupyter_text>Using the loss function is as simple as specifying the loss function in the `loss` argument of `model.compile()`.<jupyter_code>model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss=my_huber_loss)
model.fit(xs, ys, epochs=500,verbose=0)
print(model.predict([10.0]))<jupyter_output>[[18.805832]]
{'fraction_non_alphanumeric': 0.12454212454212454, 'fraction_numerical': 0.029304029304029304, 'mean_word_length': 5.681957186544342, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 19, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '90794', 'license': 'MIT', 'n_tokens_mistral': 805, 'n_tokens_neox': 760, 'n_words': 210, 'path': '1. Custom Models, Layers, and Loss Functions with TensorFlow/Week 2/C1_W2_Lab_1_huber-loss.ipynb', 'repo_name': 'Abdelrahman350/TensorFlow-Advanced-Techniques-Specialization', 'size': '2184'}