Instagram
youtube
Facebook
Twitter

Loss or Cost Function in TensorFlow

Loss orCost function in TensorFlow are used to find how accurate the model is performing. In this tutorial we will learn about loss functions in Tensorflow and their types.

In simple words, this function predict the output of the model and the true output, and then adjust the model's parameters to reduce this difference.

What is Loss or Cost Function?

  • A loss function in TensorFlow is a mathematical function. 

  • It is used to measure the difference between the predicted output and the actual output. 

  • After predicting output it helps optimizer to adjust the model's parameters to minimize the error or loss.

There are many types of loss functions in Tensorflow. Some commonly used functions are:

Mean Absolute Error (MAE) Loss - This function is used to calculate mean squared error between the predicted output and actual output.

Example:

import tensorflow as tf
true_values = tf.constant([1,1,0,0,1])
predicted_values = tf.constant([0.30, 0.7, 1,0,0.5])
loss = tf.keras.losses.MeanAbsoluteError()(true_values, predicted_values)
print("MAE Loss: ", loss.numpy())

Output:

MAE Loss:  0.5

 

Mean Squared Error (MSE) Loss - This function is used to calculate the mean squared difference between the predicted output and actual output.

 Example:

import tensorflow as tf
true_values = tf.constant([1,1,0,0,1])
predicted_values = tf.constant([0.30, 0.7, 1,0,0.5])
loss = tf.keras.losses.MeanSquaredError()(true_values, predicted_values)
print("MAE Loss: ", loss.numpy())

Output:

MAE Loss:  0.366

 

Log Loss or Binary Cross-Entropy Loss - This function is used to calculate the cross-entropy loss between the predicted and actual output.

 Example:

import tensorflow as tf
true_values = tf.constant([1,1,0,0,1])
predicted_values = tf.constant([0.30, 0.7, 1,0,0.5])
loss = tf.keras.losses.BinaryCrossentropy()(true_values, predicted_values)
print("MAE Loss: ", loss.numpy())

Output:

MAE Loss:  3.517407