<p><strong>Edit:</strong>
<br>
OK. I figured out it doesn't work in the input space. So the old explanation is probably wrong but I'll keep it anyway.</p>
<p>Here is my new thoughts:</p>
<p>In my senior project, I'm using the algorithm called <a href="https://arxiv.org/pdf/1912.02781.pdf" rel="nofollow noreferrer"><strong>AugMix</strong></a>. In this algorithm they calculated the Shannon-Jensen Divergence between two augmented images, which is the symmetrical form of the KL Divergence.</p>
<p>They used the model output as the probability distribution of the dataset. The idea is to fit a model to a dataset, then interpret the output of the model as the probability density function.</p>
<p>For example, you fitted a dataset without overfitting. Then (assuming this is an classification problem) you feed your logits (the output of the last layer) to the softmax function for each class (sometimes the softmax function is added as a layer to the end of the network, careful). The output of your softmax function (or layer) can be interpreted as P(Y|X_{1}) where X_{1} is the input sample and the Y is the groundtruth class. Then you make a prediction for another sample X_{2}, P(Y|X_{2}), where X_{1} and X_{2} comes from different datasets (say dataset_1 and dataset_2) and the model is not trained with any of those datasets.</p>
<p>Then the KL divergence between dataset_1 and dataset_2 can be calculated by KL(dataset_1 || dataset_2) = P(Y|X_{1}) * log(P(Y|X_{1}) / P(Y|X_{2}))</p>
<p>Make sure that X_{1} and X_{2} belongs to the same class.</p>
<p>I'm not sure if this is the correct way. <strong>Alternatively</strong>, you can train two different models (model_1 and model_2) using different datasets (dataset_1 and dataset_2) and then calculate the KL divergence on the predictions of those two models using the samples of another dataset called dataset_3. In other words:</p>
<p>KL(dataset_1 || dataset_2) = sum x in dataset_3 model_1(x) * log(model_1(x) / model_2(x))</p>
<p>where model_1(x) is the softmax output of model_1, which is trained using dataset_1 without overfitting, for the correct label.</p>
<p>The latter sounds more reasonable to me but I'm not sure either of them. I could not find a proper answer on my own.</p>
<hr />
<p>The things I'm going to explain are adopted from the blog of the Jason Brownlee from <a href="http://machinelearningmastery.com" rel="nofollow noreferrer">machinelearningmastery.com</a> <a href="https://machinelearningmastery.com/divergence-between-probability-distributions/" rel="nofollow noreferrer">KL Divergence</a></p>
<p>As far as I understood, firstly, you have to convert your datasets into the probability distribution so that you can calculate the probability of each of the samples from the union (or intersect?) of the both datasets.</p>
<p>KL(P || Q) = sum x in X P(x) * log(P(x) / Q(x))</p>
<p>However, most of the time the intersection of the datasets are none. For example, if you want to measure the divergence between CIFAR10 and ImageNet, there is not any samples in common. The only way you can calculate this metric is to sample from the same dataset to create two different datasets. Therefore you can have samples that are present in both datasets, and calculate the KL divergence.</p>
<p>Lastly, maybe you want to check the <a href="https://arxiv.org/pdf/1712.01026.pdf" rel="nofollow noreferrer">Wasserstein Divergence</a> that is used in GANs in order to compare the source distribution and the target distribution.</p>
How to Kullback Leibler divergence of two datasets
<p>I have two datasets that contain 40000 samples. I want to calculate the Kullback-Leibler divergence between these two datasets in python. Is there any efficient way of doing this in python? </p>