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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.