# How to Calculate the KL Divergence for Machine Learning Last Updated on November 1, 2019

It is often desirable to quantify the difference between probability distributions for a given random variable.

This occurs frequently in machine learning, when we may be interested in calculating the difference between an actual and observed probability distribution.

This can be achieved using techniques from information theory, such as the Kullback-Leibler Divergence (KL divergence), or relative entropy, and the Jensen-Shannon Divergence that provides a normalized and symmetrical version of the KL divergence. These scoring methods can be used as shortcuts in the calculation of other widely used methods, such as mutual information for feature selection prior to modeling, and cross-entropy used as a loss function for many different classifier models.

In this post, you will discover how to calculate the divergence between probability distributions.

After reading this post, you will know:

• Statistical distance is the general idea of calculating the difference between statistical objects like different probability distributions for a random variable.
• Kullback-Leibler divergence calculates a score that measures the divergence of one probability distribution from another.
• Jensen-Shannon divergence extends KL divergence to calculate a symmetrical score and distance measure of one probability distribution from another.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8789597,"math_prob":0.9574627,"size":1500,"snap":"2023-40-2023-50","text_gpt3_token_len":266,"char_repetition_ratio":0.14973262,"word_repetition_ratio":0.018957347,"special_character_ratio":0.166,"punctuation_ratio":0.080168776,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99597186,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-10-03T17:02:39Z\",\"WARC-Record-ID\":\"<urn:uuid:5e6d34ea-68b5-422a-b6d0-621f664fb6f4>\",\"Content-Length\":\"145334\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:eabe18c9-0dcb-49bb-8ced-cde09d7b09c0>\",\"WARC-Concurrent-To\":\"<urn:uuid:78a57190-24ef-47c5-b751-ae55aea551e6>\",\"WARC-IP-Address\":\"139.162.217.232\",\"WARC-Target-URI\":\"https://www.deeplearningdaily.com/how-to-calculate-the-kl-divergence-for-machine-learning/\",\"WARC-Payload-Digest\":\"sha1:GNQO2YWGQW33T6NHB7OLRDFVJ7RMXFOQ\",\"WARC-Block-Digest\":\"sha1:RV62DM6ZF7FCPH73X7VI2IQ7V6JNX62C\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233511170.92_warc_CC-MAIN-20231003160453-20231003190453-00470.warc.gz\"}"}