1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
"""Tests for ot.smooth model"""
# Author: Remi Flamary <remi.flamary@unice.fr>
#
# License: MIT License
import numpy as np
import ot
import pytest
from scipy.optimize import check_grad
def test_smooth_ot_dual():
# get data
n = 100
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
with pytest.raises(NotImplementedError):
Gl2, log = ot.smooth.smooth_ot_dual(u, u, M, 1, reg_type="none")
# squared l2 regularisation
Gl2, log = ot.smooth.smooth_ot_dual(
u, u, M, 1, reg_type="l2", log=True, stopThr=1e-10
)
# check constraints
np.testing.assert_allclose(u, Gl2.sum(1), atol=1e-05) # cf convergence sinkhorn
np.testing.assert_allclose(u, Gl2.sum(0), atol=1e-05) # cf convergence sinkhorn
# kl regularisation
G = ot.smooth.smooth_ot_dual(u, u, M, 1, reg_type="kl", stopThr=1e-10)
# check constraints
np.testing.assert_allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn
np.testing.assert_allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn
G2 = ot.sinkhorn(u, u, M, 1, stopThr=1e-10)
np.testing.assert_allclose(G, G2, atol=1e-05)
# sparsity-constrained regularisation
max_nz = 2
Gsc, log = ot.smooth.smooth_ot_dual(
u,
u,
M,
1,
max_nz=max_nz,
log=True,
reg_type="sparsity_constrained",
stopThr=1e-10,
)
# check marginal constraints
np.testing.assert_allclose(u, Gsc.sum(1), atol=1e-03)
np.testing.assert_allclose(u, Gsc.sum(0), atol=1e-03)
# check sparsity constraints
np.testing.assert_array_less(np.sum(Gsc > 0, axis=0), np.ones(n) * max_nz + 1)
def test_smooth_ot_semi_dual():
# get data
n = 100
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
with pytest.raises(NotImplementedError):
Gl2, log = ot.smooth.smooth_ot_semi_dual(u, u, M, 1, reg_type="none")
# squared l2 regularisation
Gl2, log = ot.smooth.smooth_ot_semi_dual(
u, u, M, 1, reg_type="l2", log=True, stopThr=1e-10
)
# check constraints
np.testing.assert_allclose(u, Gl2.sum(1), atol=1e-05) # cf convergence sinkhorn
np.testing.assert_allclose(u, Gl2.sum(0), atol=1e-05) # cf convergence sinkhorn
# kl regularisation
G = ot.smooth.smooth_ot_semi_dual(u, u, M, 1, reg_type="kl", stopThr=1e-10)
# check constraints
np.testing.assert_allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn
np.testing.assert_allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn
G2 = ot.sinkhorn(u, u, M, 1, stopThr=1e-10)
np.testing.assert_allclose(G, G2, atol=1e-05)
# sparsity-constrained regularisation
max_nz = 2
Gsc = ot.smooth.smooth_ot_semi_dual(
u, u, M, 1, reg_type="sparsity_constrained", max_nz=max_nz, stopThr=1e-10
)
# check marginal constraints
np.testing.assert_allclose(u, Gsc.sum(1), atol=1e-03)
np.testing.assert_allclose(u, Gsc.sum(0), atol=1e-03)
# check sparsity constraints
np.testing.assert_array_less(np.sum(Gsc > 0, axis=0), np.ones(n) * max_nz + 1)
def test_sparsity_constrained_gradient():
max_nz = 5
regularizer = ot.smooth.SparsityConstrained(max_nz=max_nz)
rng = np.random.RandomState(0)
X = rng.randn(
10,
)
b = 0.5
def delta_omega_func(X):
return regularizer.delta_Omega(X)[0]
def delta_omega_grad(X):
return regularizer.delta_Omega(X)[1]
dual_grad_err = check_grad(delta_omega_func, delta_omega_grad, X)
np.testing.assert_allclose(dual_grad_err, 0.0, atol=1e-07)
def max_omega_func(X, b):
return regularizer.max_Omega(X, b)[0]
def max_omega_grad(X, b):
return regularizer.max_Omega(X, b)[1]
semi_dual_grad_err = check_grad(max_omega_func, max_omega_grad, X, b)
np.testing.assert_allclose(semi_dual_grad_err, 0.0, atol=1e-07)
|