To understand the meaning of loss functions in keras.
The loss is calculated to get the gradients(please refer to gradient descent graph to understand) concerning model weights and update those weights accordingly via backpropagation. Loss is calculated then network weights are updated after every iteration until model updates don't get close or make any improvement in the desired evaluation metric.
from tensorflow import keras from tensorflow.keras import layers import numpy as np
We will define the layers, kernel initializer, and its input nodes shape in the model.
model = keras.Sequential() model.add(layers.Dense(64, kernel_initializer='uniform', input_shape=(10,)))
We will define the activation function as relu.
We will initialize the loss function as 'Binary_Cross_entropy' with reduction as 'sum_over_batch_size'.
BC = keras.losses.BinaryCrossentropy(reduction='sum_over_batch_size') model.compile(loss=loss_fn, optimizer='adamax')
Let's take a sample dataset of predicted and true values then calculate the loss.
y_true = [[1, 2], [4, 6],[0.5, 0.7],[0.4, 0.6]] y_pred = [[1.5, 1.4], [5, 7],[0.6, 0.5],[0.7, 0.7]] BC(y_true, y_pred).numpy()
As we can see minimum loss with this model is -16.878973 for the sample dataset. We can improve it by choosing another type of loss function or optimizer.