| 1 |
"""Tests for module gaussian""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import numpy as np |
| 10 |
import pytest |
| 11 |
from ot.utils import proj_simplex |
| 12 |
from ot.gmm import ( |
| 13 |
gaussian_pdf, |
| 14 |
gmm_pdf, |
| 15 |
dist_bures_squared, |
| 16 |
gmm_ot_loss, |
| 17 |
gmm_ot_plan, |
| 18 |
gmm_ot_apply_map, |
| 19 |
gmm_ot_plan_density, |
| 20 |
) |
| 21 |
|
| 22 |
try: |
| 23 |
import torch |
| 24 |
except ImportError: |
| 25 |
torch = False |
| 26 |
|
| 27 |
|
| 28 |
def get_gmms(nx=None): |
| 29 |
rng = np.random.RandomState(seed=42) |
| 30 |
ks = 3 |
| 31 |
kt = 5 |
| 32 |
d = 3 |
| 33 |
m_s = rng.randn(ks, d) |
| 34 |
m_t = rng.randn(kt, d) |
| 35 |
C_s = rng.randn(ks, d, d) |
| 36 |
C_s = np.matmul(C_s, np.transpose(C_s, (0, 2, 1))) |
| 37 |
C_t = rng.randn(kt, d, d) |
| 38 |
C_t = np.matmul(C_t, np.transpose(C_t, (0, 2, 1))) |
| 39 |
w_s = proj_simplex(rng.rand(ks)) |
| 40 |
w_t = proj_simplex(rng.rand(kt)) |
| 41 |
if nx is not None: |
| 42 |
m_s = nx.from_numpy(m_s) |
| 43 |
m_t = nx.from_numpy(m_t) |
| 44 |
C_s = nx.from_numpy(C_s) |
| 45 |
C_t = nx.from_numpy(C_t) |
| 46 |
w_s = nx.from_numpy(w_s) |
| 47 |
w_t = nx.from_numpy(w_t) |
| 48 |
return m_s, m_t, C_s, C_t, w_s, w_t |
| 49 |
|
| 50 |
|
| 51 |
def test_gaussian_pdf(nx): |
| 52 |
rng = np.random.RandomState(seed=42) |
| 53 |
n = 7 |
| 54 |
d = 3 |
| 55 |
x = nx.from_numpy(rng.randn(n, d)) |
| 56 |
m, _, C, _, _, _ = get_gmms(nx) |
| 57 |
pdf = gaussian_pdf(x, m[0], C[0]) |
| 58 |
assert pdf.shape == (n,) |
| 59 |
|
| 60 |
x = nx.from_numpy(rng.randn(n, n, d)) |
| 61 |
pdf = gaussian_pdf(x, m[0], C[0]) |
| 62 |
assert pdf.shape == ( |
| 63 |
n, |
| 64 |
n, |
| 65 |
) |
| 66 |
|
| 67 |
with pytest.raises(AssertionError): |
| 68 |
gaussian_pdf(x, m[0, :-1], C[0]) |
| 69 |
|
| 70 |
|
| 71 |
def test_gmm_pdf(nx): |
| 72 |
rng = np.random.RandomState(seed=42) |
| 73 |
n = 7 |
| 74 |
d = 3 |
| 75 |
x = nx.from_numpy(rng.randn(n, d)) |
| 76 |
m, _, C, _, w, _ = get_gmms(nx) |
| 77 |
pdf = gmm_pdf(x, m, C, w) |
| 78 |
assert pdf.shape == (n,) |
| 79 |
|
| 80 |
x = nx.from_numpy(rng.randn(n, n, d)) |
| 81 |
pdf = gmm_pdf(x, m, C, w) |
| 82 |
assert pdf.shape == ( |
| 83 |
n, |
| 84 |
n, |
| 85 |
) |
| 86 |
|
| 87 |
with pytest.raises(AssertionError): |
| 88 |
gmm_pdf(x, m[:-1], C, w) |
| 89 |
|
| 90 |
|
| 91 |
@pytest.skip_backend("tf") |
| 92 |
@pytest.skip_backend("jax") |
| 93 |
def test_dist_bures_squared(nx): |
| 94 |
m_s, m_t, C_s, C_t, _, _ = get_gmms(nx) |
| 95 |
dist_bures_squared(m_s, m_t, C_s, C_t) |
| 96 |
D0 = dist_bures_squared(m_s, m_s, C_s, C_s) |
| 97 |
|
| 98 |
assert np.allclose(np.diag(D0), 0, atol=1e-6) |
| 99 |
|
| 100 |
with pytest.raises(AssertionError): |
| 101 |
dist_bures_squared(m_s[:, 1:], m_t, C_s, C_t) |
| 102 |
|
| 103 |
with pytest.raises(AssertionError): |
| 104 |
dist_bures_squared(m_s[1:], m_t, C_s, C_t) |
| 105 |
|
| 106 |
with pytest.raises(AssertionError): |
| 107 |
dist_bures_squared(m_s, m_t[1:], C_s, C_t) |
| 108 |
|
| 109 |
|
| 110 |
@pytest.skip_backend("tf") |
| 111 |
@pytest.skip_backend("jax") |
| 112 |
def test_gmm_ot_loss(nx): |
| 113 |
m_s, m_t, C_s, C_t, w_s, w_t = get_gmms(nx) |
| 114 |
loss = gmm_ot_loss(m_s, m_t, C_s, C_t, w_s, w_t) |
| 115 |
|
| 116 |
assert loss > 0 |
| 117 |
|
| 118 |
loss = gmm_ot_loss(m_s, m_s, C_s, C_s, w_s, w_s) |
| 119 |
|
| 120 |
assert np.allclose(loss, 0, atol=1e-6) |
| 121 |
|
| 122 |
with pytest.raises(AssertionError): |
| 123 |
gmm_ot_loss(m_s, m_t, C_s, C_t, w_s[1:], w_t) |
| 124 |
|
| 125 |
with pytest.raises(AssertionError): |
| 126 |
gmm_ot_loss(m_s, m_t, C_s, C_t, w_s, w_t[1:]) |
| 127 |
|
| 128 |
|
| 129 |
@pytest.skip_backend("tf") |
| 130 |
@pytest.skip_backend("jax") |
| 131 |
def test_gmm_ot_plan(nx): |
| 132 |
m_s, m_t, C_s, C_t, w_s, w_t = get_gmms(nx) |
| 133 |
|
| 134 |
plan = gmm_ot_plan(m_s, m_t, C_s, C_t, w_s, w_t) |
| 135 |
|
| 136 |
assert np.allclose(plan.sum(0), w_t, atol=1e-6) |
| 137 |
assert np.allclose(plan.sum(1), w_s, atol=1e-6) |
| 138 |
|
| 139 |
plan = gmm_ot_plan(m_s, m_s + 1, C_s, C_s, w_s, w_s) |
| 140 |
|
| 141 |
assert np.allclose(plan, np.diag(w_s), atol=1e-6) |
| 142 |
|
| 143 |
with pytest.raises(AssertionError): |
| 144 |
gmm_ot_loss(m_s, m_t, C_s, C_t, w_s[1:], w_t) |
| 145 |
|
| 146 |
with pytest.raises(AssertionError): |
| 147 |
gmm_ot_loss(m_s, m_t, C_s, C_t, w_s, w_t[1:]) |
| 148 |
|
| 149 |
|
| 150 |
def test_gmm_apply_map(): |
| 151 |
m_s, m_t, C_s, C_t, w_s, w_t = get_gmms() |
| 152 |
rng = np.random.RandomState(seed=42) |
| 153 |
x = rng.randn(7, 3) |
| 154 |
|
| 155 |
for method in ["bary", "rand"]: |
| 156 |
gmm_ot_apply_map(x, m_s, m_t, C_s, C_t, w_s, w_t, method=method) |
| 157 |
|
| 158 |
plan = gmm_ot_plan(m_s, m_t, C_s, C_t, w_s, w_t) |
| 159 |
gmm_ot_apply_map(x, m_s, m_t, C_s, C_t, w_s, w_t, plan=plan) |
| 160 |
|
| 161 |
|
| 162 |
@pytest.mark.skipif(not torch, reason="No torch available") |
| 163 |
def test_gradient_gmm_ot_loss_pytorch(): |
| 164 |
m_s, m_t, C_s, C_t, w_s, w_t = get_gmms() |
| 165 |
m_s = torch.tensor(m_s, requires_grad=True) |
| 166 |
m_t = torch.tensor(m_t, requires_grad=True) |
| 167 |
C_s = torch.tensor(C_s, requires_grad=True) |
| 168 |
C_t = torch.tensor(C_t, requires_grad=True) |
| 169 |
w_s = torch.tensor(w_s, requires_grad=True) |
| 170 |
w_t = torch.tensor(w_t, requires_grad=True) |
| 171 |
loss = gmm_ot_loss(m_s, m_t, C_s, C_t, w_s, w_t) |
| 172 |
loss.backward() |
| 173 |
grad_m_s = m_s.grad |
| 174 |
grad_C_s = C_s.grad |
| 175 |
grad_w_s = w_s.grad |
| 176 |
assert (grad_m_s**2).sum().item() > 0 |
| 177 |
assert (grad_C_s**2).sum().item() > 0 |
| 178 |
assert (grad_w_s**2).sum().item() > 0 |
| 179 |
|
| 180 |
|
| 181 |
def test_gmm_ot_plan_density(nx): |
| 182 |
m_s, m_t, C_s, C_t, w_s, w_t = get_gmms(nx) |
| 183 |
rng = np.random.RandomState(seed=42) |
| 184 |
n = 7 |
| 185 |
x = nx.from_numpy(rng.randn(n, 3)) |
| 186 |
y = nx.from_numpy(rng.randn(n + 1, 3)) |
| 187 |
|
| 188 |
density = gmm_ot_plan_density(x, y, m_s, m_t, C_s, C_t, w_s, w_t) |
| 189 |
assert density.shape == (n, n + 1) |
| 190 |
|
| 191 |
plan = gmm_ot_plan(m_s, m_t, C_s, C_t, w_s, w_t) |
| 192 |
gmm_ot_plan_density(x, x, m_s, m_t, C_s, C_t, w_s, w_t, plan=plan) |
| 193 |
|
| 194 |
with pytest.raises(AssertionError): |
| 195 |
gmm_ot_plan_density(x[:, 1:], y, m_s, m_t, C_s, C_t, w_s, w_t) |