| 1 |
|
| 2 |
""" |
| 3 |
Dictionary Learning based on Bregman projections for entropic regularized OT |
| 4 |
""" |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
import warnings |
| 12 |
|
| 13 |
|
| 14 |
from ..utils import list_to_array |
| 15 |
from ..backend import get_backend |
| 16 |
|
| 17 |
from ._utils import projC, projR |
| 18 |
|
| 19 |
|
| 20 |
def unmix( |
| 21 |
a, |
| 22 |
D, |
| 23 |
M, |
| 24 |
M0, |
| 25 |
h0, |
| 26 |
reg, |
| 27 |
reg0, |
| 28 |
alpha, |
| 29 |
numItermax=1000, |
| 30 |
stopThr=1e-3, |
| 31 |
verbose=False, |
| 32 |
log=False, |
| 33 |
warn=True, |
| 34 |
): |
| 35 |
r""" |
| 36 |
Compute the unmixing of an observation with a given dictionary using Wasserstein distance |
| 37 |
|
| 38 |
The function solve the following optimization problem: |
| 39 |
|
| 40 |
.. math:: |
| 41 |
|
| 42 |
\mathbf{h} = \mathop{\arg \min}_\mathbf{h} \quad |
| 43 |
(1 - \alpha) W_{\mathbf{M}, \mathrm{reg}}(\mathbf{a}, \mathbf{Dh}) + |
| 44 |
\alpha W_{\mathbf{M_0}, \mathrm{reg}_0}(\mathbf{h}_0, \mathbf{h}) |
| 45 |
|
| 46 |
|
| 47 |
where : |
| 48 |
|
| 49 |
- :math:`W_{M,reg}(\cdot,\cdot)` is the entropic regularized Wasserstein distance |
| 50 |
with :math:`\mathbf{M}` loss matrix (see :py:func:`ot.bregman.sinkhorn`) |
| 51 |
- :math:`\mathbf{D}` is a dictionary of `n_atoms` atoms of dimension `dim_a`, |
| 52 |
its expected shape is `(dim_a, n_atoms)` |
| 53 |
- :math:`\mathbf{h}` is the estimated unmixing of dimension `n_atoms` |
| 54 |
- :math:`\mathbf{a}` is an observed distribution of dimension `dim_a` |
| 55 |
- :math:`\mathbf{h}_0` is a prior on :math:`\mathbf{h}` of dimension `dim_prior` |
| 56 |
- `reg` and :math:`\mathbf{M}` are respectively the regularization term and the |
| 57 |
cost matrix (`dim_a`, `dim_a`) for OT data fitting |
| 58 |
- `reg`:math:`_0` and :math:`\mathbf{M_0}` are respectively the regularization |
| 59 |
term and the cost matrix (`dim_prior`, `n_atoms`) regularization |
| 60 |
- :math:`\alpha` weight data fitting and regularization |
| 61 |
|
| 62 |
The optimization problem is solved following the algorithm described |
| 63 |
in :ref:`[4] <references-unmix>` |
| 64 |
|
| 65 |
|
| 66 |
Parameters |
| 67 |
---------- |
| 68 |
a : array-like, shape (dim_a) |
| 69 |
observed distribution (histogram, sums to 1) |
| 70 |
D : array-like, shape (dim_a, n_atoms) |
| 71 |
dictionary matrix |
| 72 |
M : array-like, shape (dim_a, dim_a) |
| 73 |
loss matrix |
| 74 |
M0 : array-like, shape (n_atoms, dim_prior) |
| 75 |
loss matrix |
| 76 |
h0 : array-like, shape (n_atoms,) |
| 77 |
prior on the estimated unmixing h |
| 78 |
reg : float |
| 79 |
Regularization term >0 (Wasserstein data fitting) |
| 80 |
reg0 : float |
| 81 |
Regularization term >0 (Wasserstein reg with h0) |
| 82 |
alpha : float |
| 83 |
How much should we trust the prior ([0,1]) |
| 84 |
numItermax : int, optional |
| 85 |
Max number of iterations |
| 86 |
stopThr : float, optional |
| 87 |
Stop threshold on error (>0) |
| 88 |
verbose : bool, optional |
| 89 |
Print information along iterations |
| 90 |
log : bool, optional |
| 91 |
record log if True |
| 92 |
warn : bool, optional |
| 93 |
if True, raises a warning if the algorithm doesn't convergence. |
| 94 |
|
| 95 |
Returns |
| 96 |
------- |
| 97 |
h : array-like, shape (n_atoms,) |
| 98 |
Wasserstein barycenter |
| 99 |
log : dict |
| 100 |
log dictionary return only if log==True in parameters |
| 101 |
|
| 102 |
|
| 103 |
.. _references-unmix: |
| 104 |
References |
| 105 |
---------- |
| 106 |
|
| 107 |
.. [4] S. Nakhostin, N. Courty, R. Flamary, D. Tuia, T. Corpetti, |
| 108 |
Supervised planetary unmixing with optimal transport, Workshop |
| 109 |
on Hyperspectral Image and Signal Processing : |
| 110 |
Evolution in Remote Sensing (WHISPERS), 2016. |
| 111 |
""" |
| 112 |
|
| 113 |
a, D, M, M0, h0 = list_to_array(a, D, M, M0, h0) |
| 114 |
|
| 115 |
nx = get_backend(a, D, M, M0, h0) |
| 116 |
|
| 117 |
|
| 118 |
K = nx.exp(-M / reg) |
| 119 |
|
| 120 |
|
| 121 |
K0 = nx.exp(-M0 / reg0) |
| 122 |
old = h0 |
| 123 |
|
| 124 |
err = 1 |
| 125 |
|
| 126 |
if log: |
| 127 |
log = {"err": []} |
| 128 |
|
| 129 |
for ii in range(numItermax): |
| 130 |
K = projC(K, a) |
| 131 |
K0 = projC(K0, h0) |
| 132 |
new = nx.sum(K0, axis=1) |
| 133 |
|
| 134 |
inv_new = nx.dot(D, new) |
| 135 |
other = nx.sum(K, axis=1) |
| 136 |
|
| 137 |
delta = nx.exp(alpha * nx.log(other) + (1 - alpha) * nx.log(inv_new)) |
| 138 |
K = projR(K, delta) |
| 139 |
K0 = nx.dot(D.T, delta / inv_new)[:, None] * K0 |
| 140 |
err = nx.norm(nx.sum(K0, axis=1) - old) |
| 141 |
old = new |
| 142 |
if log: |
| 143 |
log["err"].append(err) |
| 144 |
|
| 145 |
if verbose: |
| 146 |
if ii % 200 == 0: |
| 147 |
print("{:5s}|{:12s}".format("It.", "Err") + "\n" + "-" * 19) |
| 148 |
print("{:5d}|{:8e}|".format(ii, err)) |
| 149 |
if err < stopThr: |
| 150 |
break |
| 151 |
else: |
| 152 |
if warn: |
| 153 |
warnings.warn( |
| 154 |
"Unmixing algorithm did not converge. You might want to " |
| 155 |
"increase the number of iterations `numItermax` " |
| 156 |
"or the regularization parameter `reg`." |
| 157 |
) |
| 158 |
if log: |
| 159 |
log["niter"] = ii |
| 160 |
return nx.sum(K0, axis=1), log |
| 161 |
else: |
| 162 |
return nx.sum(K0, axis=1) |