Implement kl divergence methods and tests
This commit is contained in:
18
tests/test_metrics.py
Normal file
18
tests/test_metrics.py
Normal 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)
|
||||
|
||||
Reference in New Issue
Block a user