What is the meaning of loss functions in keras?

This recipe explains what is the meaning of loss functions in keras

Recipe Objective

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.

Step 1- Importing Libraries.

from tensorflow import keras from tensorflow.keras import layers import numpy as np

Step 2- Loading the Sequential model.

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,)))

Step 3- Defining the activation function.

We will define the activation function as relu.

model.add(layers.Activation('relu'))

Step 4- Initialize the Loss function.

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')

Step 5- Taking a sample dataset

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()
-16.878973

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.

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.