Close Menu
    Trending
    • STOP Building Useless ML Projects – What Actually Works
    • Credit Risk Scoring for BNPL Customers at Bati Bank | by Sumeya sirmula | Jul, 2025
    • The New Career Crisis: AI Is Breaking the Entry-Level Path for Gen Z
    • Musk’s X appoints ‘king of virality’ in bid to boost growth
    • Why Entrepreneurs Should Stop Obsessing Over Growth
    • Implementing IBCS rules in Power BI
    • What comes next for AI copyright lawsuits?
    • Why PDF Extraction Still Feels LikeHack
    AIBS News
    • Home
    • Artificial Intelligence
    • Machine Learning
    • AI Technology
    • Data Science
    • More
      • Technology
      • Business
    AIBS News
    Home»Machine Learning»KL Divergence vs. Cross-Entropy: Understanding the Difference and Similarities | by Ekaterina Kasilina | Mar, 2025
    Machine Learning

    KL Divergence vs. Cross-Entropy: Understanding the Difference and Similarities | by Ekaterina Kasilina | Mar, 2025

    Team_AIBS NewsBy Team_AIBS NewsMarch 3, 2025No Comments3 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Easy rationalization of two essential ML ideas

    Dall-e

    Once we prepare machine studying fashions, particularly for classification duties, two basic ideas incessantly come up: cross-entropy and KL(Kullback–Leibler) divergence. Whereas cross-entropy is extra incessantly used as an optimization goal, KL divergence isn’t utilized for this objective.

    A fast reminder of why we think about them collectively within the first place:

    Cross-Entropy Formulation:

    Cross-Entropy

    KL Divergence Formulation:

    KL Divergence Formulation

    So the distinction is simply the left half! Let’s dive in and discover particulars.

    Be happy to skip this part in case you’re already skilled.

    Think about we’re engaged on a binary classification process, the place now we have the precise class distribution (p) and the anticipated distribution (q) for labels.

    import numpy as np

    p = np.array([1, 0]) # True label (one-hot encoded for sophistication 0)
    q = np.array([0.7, 0.3]) # Predicted likelihood distribution

    The target is to coach the machine studying mannequin in order that the anticipated distribution (q) carefully matches the precise distribution (p).

    KL divergence (Kullback-Leibler divergence) is a measure of how one likelihood distribution q(x) diverges from a real distribution p(x). It tells us how a lot info is misplaced when q(x) is used to approximate p(x).

    KL Divergence Formulation
    def kl_divergence(p, q):
    # Add a small fixed to keep away from log(0) errors
    epsilon = 1e-10
    p = np.clip(p, epsilon, 1.0)
    q = np.clip(q, epsilon, 1.0)

    # Calculate KL Divergence
    return np.sum(p * np.log(p)) - np.sum(p * np.log(q))

    # Compute KL Divergence
    kl = kl_divergence(p, q)
    print(f"KL Divergence: {kl}")

    Output:

    KL Divergence: 0.3566749417565447
    • If q(x) is an ideal match for p(x), KL divergence is 0.
    • If q(x) poorly approximates p(x), KL divergence will increase.
    • It’s not symmetric: KL(p || q) ≠ KL(q || p)

    Cross-entropy measures how totally different one likelihood distribution is from one other. It quantifies the variety of bits required to encode knowledge from a real distribution p(x) utilizing an approximating distribution q(x).

    Cross-Entropy

    We are able to rewrite the formulation:

    Cross-Entropy with KL Divergence

    The place H(P) represents the entropy of the true distribution

    def cross_entropy(p, q):
    return -np.sum(p * np.log(q))

    def entropy(p):
    epsilon = 1e-10
    p = np.clip(p, epsilon, 1.0)
    return - np.sum(p * np.log(p))

    ce = cross_entropy(p, q)
    print(f"Cross-Entropy: {ce}")

    hp = entropy(p)
    print(f"Cross-Entropy utilizing KL Divergence:", hp + kl)

    Output:

    Cross-Entropy: 0.35667494405912975
    Cross-Entropy utilizing KL Divergence: 0.35667494405912975

    Since entropy H(p) is fastened for a given p, minimizing cross-entropy successfully minimizes KL divergence**. For this reason cross-entropy is often used as a loss perform — it ensures that the anticipated distribution q(x) will get as shut as attainable to p(x).

    **Within the context of Most Chance Estimation (MLE), minimizing KL divergence and cross-entropy primarily result in the identical consequence. Nevertheless, in Bayesian Inference (Variational Inference), we reduce the Proof Decrease Sure (ELBO).

    • Cross-Entropy is often used as a loss perform in classification duties, particularly when the aim is to coach a mannequin to foretell a likelihood distribution.
    • KL divergence is used to measure the distinction between two likelihood distributions, p (true distribution) and q (predicted distribution). It’s usually utilized in unsupervised settings or in instances the place you’re approximating a distribution (e.g., in variational inference, GANs, and many others.)



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleLenovo Unveils AI Inferencing Server
    Next Article SEC Offering $50K Buyout Incentive; Education Dept $25K
    Team_AIBS News
    • Website

    Related Posts

    Machine Learning

    Credit Risk Scoring for BNPL Customers at Bati Bank | by Sumeya sirmula | Jul, 2025

    July 1, 2025
    Machine Learning

    Why PDF Extraction Still Feels LikeHack

    July 1, 2025
    Machine Learning

    🚗 Predicting Car Purchase Amounts with Neural Networks in Keras (with Code & Dataset) | by Smruti Ranjan Nayak | Jul, 2025

    July 1, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    STOP Building Useless ML Projects – What Actually Works

    July 1, 2025

    I Tried Buying a Car Through Amazon: Here Are the Pros, Cons

    December 10, 2024

    Amazon and eBay to pay ‘fair share’ for e-waste recycling

    December 10, 2024

    Artificial Intelligence Concerns & Predictions For 2025

    December 10, 2024

    Barbara Corcoran: Entrepreneurs Must ‘Embrace Change’

    December 10, 2024
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    Most Popular

    Ohuouhohu – مرسی مرسی – Medium

    February 15, 2025

    Taiwan President Defends TSMC’s $100 Billion U.S. Chip Investment

    March 6, 2025

    How to Make Money Without Selling Your Startup’s Soul

    February 5, 2025
    Our Picks

    STOP Building Useless ML Projects – What Actually Works

    July 1, 2025

    Credit Risk Scoring for BNPL Customers at Bati Bank | by Sumeya sirmula | Jul, 2025

    July 1, 2025

    The New Career Crisis: AI Is Breaking the Entry-Level Path for Gen Z

    July 1, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2024 Aibsnews.comAll Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.