Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Binary Cross Entropy/Log Loss for Binary Classification
Next article icon

Binary Cross Entropy/Log Loss for Binary Classification

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Binary cross-entropy (log loss) is a loss function used in binary classification problems. It quantifies the difference between the actual class labels (0 or 1) and the predicted probabilities output by the model. The lower the binary cross-entropy value, the better the model’s predictions align with the true labels.

Mathematically, Binary Cross-Entropy (BCE) is defined as:

\text{BCE} = - \frac{1}{N} \sum_{i=1}^{N} \left[ y_i \log(p_i) + (1 - y_i) \log(1 - p_i) \right]

where:

  • N is the number of observations
  • y_i is the actual binary label (0 or 1) of the i^{th} observation.
  • p_i is the predicted probability of the i^{th} observation being in class 1.

Since the model’s output is a probability between 0 and 1, minimizing binary cross-entropy during training helps improve predictive accuracy, ensuring the model effectively distinguishes between two classes.

How Does Binary Cross-Entropy Work?

Binary Cross-Entropy measures the distance between the true labels and the predicted probabilities. When the predicted probability p_i ​is close to the actual label y_i , the BCE value is low, indicating a good prediction.

Conversely, when the predicted probability deviates significantly from the actual label, the BCE value is high, indicating a poor prediction. The logarithmic component of the BCE function penalizes wrong predictions more heavily than correct ones.

For example, if the true label is 1 and the predicted probability is close to 0, the loss is substantial. This characteristic makes BCE particularly effective in driving the model to improve its predictions during training.

Why is Binary Cross-Entropy Important?

  1. Training Deep Learning Models: Binary Cross-Entropy is used as the loss function for training neural networks in binary classification tasks. It helps in adjusting the model's weights to minimize the prediction error.
  2. Probabilistic Interpretation: BCE provides a probabilistic interpretation of the model's predictions, making it suitable for applications where understanding the confidence of predictions is important, such as in medical diagnosis or fraud detection.
  3. Model Evaluation: BCE is a clear and interpretable metric for evaluating the performance of binary classification models. Lower BCE values indicate better model performance.
  4. Handling Imbalanced Data: BCE can be particularly useful in scenarios with imbalanced datasets, where one class is significantly more frequent than the other. By focusing on probability predictions, it helps the model learn to make accurate predictions even in the presence of class imbalance.

Mathematical Example of Binary Cross-Entropy

Consider a binary classification problem where we have the following true labels y and predicted probabilities p for a set of observations:

ObservationTrue Label (y)Predicted Probability (p)
110.9
200.2
310.8
400.4

We will calculate the Binary Cross-Entropy loss for this set of observations step-by-step.

Observation 1:

Here, True label y_1 =1 and Predicted probability p_1 =0.1

\text{Loss}_1 = - \left( 1 \cdot \log(0.9) + (1 - 1) \cdot \log(1 - 0.9) \right) = - \log(0.9) \approx -(-0.1054) = 0.1054

Similarly, for other classes,

  1. Predicted probability p2 ​=0.2 and Loss2=0.223
  2. Predicted probability p3 =0.8and Loss3=0.2231
  3. Predicted probability p4 =0.4 and Loss4=0.5108

Next, we sum the individual losses and calculate the average:

\text{Total Loss}=0.1054+0.2231+0.2231+0.5108=1.0624

\text{Average Loss (BCE)}=\frac{1.06244}{4}=0.2656

Therefore, the Binary Cross-Entropy loss for these observations is approximately 0.2656.

Implementation of Binary Cross Entropy in Python

  • Manual Calculation with NumPy: The function binary_cross_entropy manually calculates BCE loss using the formula, averaging individual losses for true labels (y_true) and predicted probabilities (y_pred).
  • Keras Calculation: The binary_crossentropy function from Keras computes BCE loss directly and efficiently, taking the same inputs (y_true and y_pred), with results converted to NumPy format.
  • Verification: The close match between manual (bce_loss) and Keras (bce_loss_keras) calculations validates the manual implementation, ensuring accuracy in computing BCE loss for binary classification models.
Python
import numpy as np from keras.losses import binary_crossentropy  # Example true labels and predicted probabilities y_true = np.array([0, 1, 1, 0, 1]) y_pred = np.array([0.1, 0.9, 0.8, 0.2, 0.7])  # Compute Binary Cross-Entropy using NumPy def binary_cross_entropy(y_true, y_pred):     bce = -np.mean(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred))     return bce  bce_loss = binary_cross_entropy(y_true, y_pred) print(f"Binary Cross-Entropy Loss (manual calculation): {bce_loss}")  # Compute Binary Cross-Entropy using Keras bce_loss_keras = binary_crossentropy(y_true, y_pred).numpy() print(f"Binary Cross-Entropy Loss (Keras): {bce_loss_keras}") 

Output:

Binary Cross-Entropy Loss (manual calculation): 0.20273661557656092
Binary Cross-Entropy Loss (Keras): 0.2027364925606956

The manual calculation using NumPy might have slightly different floating-point precision or rounding behavior compared to the Keras implementation. Keras might use optimized backend operations and higher precision floating-point arithmetic, leading to a very slightly different results.

Understanding and implementing BCE ensures robust evaluation and enhancement of binary classification models, especially in deep learning applications.


Next Article
Binary Cross Entropy/Log Loss for Binary Classification

U

uddithmachiraju
Improve
Article Tags :
  • Blogathon
  • Deep Learning
  • AI-ML-DS
  • Data Science Blogathon 2024

Similar Reads

    Categorical Cross-Entropy in Multi-Class Classification
    Categorical Cross-Entropy (CCE), also known as softmax loss or log loss, is one of the most commonly used loss functions in machine learning, particularly for classification problems. It measures the difference between the predicted probability distribution and the actual (true) distribution of clas
    6 min read
    Cross-Entropy Cost Functions used in Classification
    Cost functions play a crucial role in improving a Machine Learning model's performance by being an integrated part of the gradient descent algorithm which helps us optimize the weights of a particular model. In this article, we will learn about one such cost function which is the cross-entropy funct
    4 min read
    Binary classification using CatBoost
    CatBoost is a high-performance, open-source gradient boosting library developed by Yandex, a Russian multinational IT company. It is designed for categorical feature support, making it particularly powerful for structured data like those often encountered in real-world datasets. In this article, we
    13 min read
    Binary classification using LightGBM
    In this article, we will learn about one of the state-of-the-art machine learning models: Lightgbm or light gradient boosting machine. After improvising more and more on the XGB model for better performance XGBoost which is an eXtreme Gradient Boosting machine but by the lightgbm we can achieve simi
    12 min read
    Binary Cross-Entropy In R
    Binary Cross-Entropy (BCE), also known as log loss, is a crucial concept in binary classification problems within machine learning and statistical modeling. It measures the performance of a classification model whose output is a probability value between 0 and 1. The objective is to minimize the BCE
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences