Implement kl divergence methods and tests

This commit is contained in:
Johannes Fischer
2021-08-02 11:04:03 +02:00
parent 99f7df2e7c
commit 281f7773c4
2 changed files with 59 additions and 2 deletions

18
tests/test_metrics.py Normal file
View File

@@ -0,0 +1,18 @@
import torch
import numpy as np
from src import metrics
from src.metrics import divergence
def test_kl_divergence():
p = torch.randn(1000000)
q = 1.0 + 2.0 * torch.randn(1000000)
d1 = divergence(p, q, type='kl', n_components=0)
assert isinstance(d1, float)
d2 = divergence(p, q, type='kl', n_components=1)
assert isinstance(d2, float)
assert np.isclose(d1, d2, atol=1e-5)
d3 = divergence(p, q, type='kl', n_components=3)
assert isinstance(d3, float)