Creating a Custom Loss Function in Keras
Step 1: Import the necessary libraries
In this step, we import TensorFlow and Keras libraries along with NumPy for numerical operations. We also import necessary modules like Sequential
for creating the model, Dense
for defining layers, and K
from keras.backend
for backend operations.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from keras import backend as K
import numpy as np
from tensorflow.python.ops import math_ops
Step 2: Define Custom Loss Function
Here I have defined a custom loss function which calculates mean of absolute difference of squares of true value and the predicted value.
def customloss(Y_actual, Y_predicted):
absolute_diff = abs((Y_predicted*Y_predicted) - (Y_actual*Y_actual)) #squared difference
final_loss =(K.mean(absolute_diff, axis=-1))/100 #mean over last dimension
return final_loss
Step 3: Create the model using Keras.
Using sequential model from the Keras API model is created using Rectified Linear Unit as activation function with two dense layers.
model = keras.Sequential([
keras.layers.Dense(10, activation='relu', input_shape=(1,)),
keras.layers.Dense(1)
])
Step 4: Compiling the model with custom loss.
Created model is compiled for custom loss function and the optimizer used in ‘adam’.
model.compile(loss=customloss, optimizer='adam')
Step 5: Fitting the Model
Input data (X_train
and Y_train
) is created in list format and then converted to NumPy arrays. The model is trained using the fit()
function for 5 epochs with a batch size of 3.
a=[[12.3],[20.0], [17.6],[15.0],[20.0],[7.5],[5.9], [20.0]]
b=[6.0, 2, 18,24,30, 3,6, 12]
X_train = np.array(a)
Y_train = np.array(b) #dummy data
model.fit(X_train, Y_train, batch_size=3, epochs=5)
Complete Code to create a Custom Loss Function in Keras
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from keras import backend as K
import numpy as np
from tensorflow.python.ops import math_ops
def customloss(Y_actual, Y_predicted):
absolute_diff = abs((Y_predicted*Y_predicted) - (Y_actual*Y_actual)) #squared difference
final_loss =(K.mean(absolute_diff, axis=-1))/100 #mean over last dimension
return final_loss
model = keras.Sequential([
keras.layers.Dense(10, activation='relu', input_shape=(1,)),
keras.layers.Dense(1)
])
model.compile(loss=customloss, optimizer='adam')
a=[[12.3],[20.0], [17.6],[15.0],[20.0],[7.5],[5.9], [20.0]]
b=[6.0, 2, 18,24,30, 3,6, 12]
X_train = np.array(a)
Y_train = np.array(b) #dummy data
model.fit(X_train, Y_train, batch_size=3, epochs=5)
Output:
Epoch 1/5
3/3 [==============================] - 1s 23ms/step - loss: 2.4541
Epoch 2/5
3/3 [==============================] - 0s 13ms/step - loss: 2.4497
Epoch 3/5
3/3 [==============================] - 0s 8ms/step - loss: 2.4474
Epoch 4/5
3/3 [==============================] - 0s 10ms/step - loss: 2.4445
Epoch 5/5
3/3 [==============================] - 0s 10ms/step - loss: 2.4402
The output displayed shows the loss calculated in each epoch using the custom loss function defined. Each epoch’s loss is printed, indicating how well the model is optimizing towards the desired outcome.
How to Create a Custom Loss Function in Keras
Creating a custom loss function in Keras is crucial for optimizing deep learning models. The article aims to learn how to create a custom loss function.