Skip to content

Commit

Permalink
[MRG] add normalization of distances for WDA (#172)
Browse files Browse the repository at this point in the history
* edit dr.py

* Correct normalization + optional parameter

* pep8?

* final!

Co-authored-by: Rémi Flamary <[email protected]>
  • Loading branch information
HexuanLiu and rflamary authored Oct 29, 2021
1 parent 79a7a29 commit 1b5c35b
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ot/dr.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def proj(X):
return Popt, proj


def wda(X, y, p=2, reg=1, k=10, solver=None, maxiter=100, verbose=0, P0=None):
def wda(X, y, p=2, reg=1, k=10, solver=None, maxiter=100, verbose=0, P0=None, normalize=False):
r"""
Wasserstein Discriminant Analysis [11]_
Expand Down Expand Up @@ -139,6 +139,8 @@ def wda(X, y, p=2, reg=1, k=10, solver=None, maxiter=100, verbose=0, P0=None):
else should be a pymanopt.solvers
P0 : ndarray, shape (d, p)
Initial starting point for projection.
normalize : bool, optional
Normalise the Wasserstaiun distane by the average distance on P0 (default : False)
verbose : int, optional
Print information along iterations.
Expand All @@ -164,6 +166,18 @@ def wda(X, y, p=2, reg=1, k=10, solver=None, maxiter=100, verbose=0, P0=None):
# compute uniform weighs
wc = [np.ones((x.shape[0]), dtype=np.float32) / x.shape[0] for x in xc]

# pre-compute reg_c,c'
if P0 is not None and normalize:
regmean = np.zeros((len(xc), len(xc)))
for i, xi in enumerate(xc):
xi = np.dot(xi, P0)
for j, xj in enumerate(xc[i:]):
xj = np.dot(xj, P0)
M = dist(xi, xj)
regmean[i, j] = np.sum(M) / (len(xi) * len(xj))
else:
regmean = np.ones((len(xc), len(xc)))

def cost(P):
# wda loss
loss_b = 0
Expand All @@ -174,7 +188,7 @@ def cost(P):
for j, xj in enumerate(xc[i:]):
xj = np.dot(xj, P)
M = dist(xi, xj)
G = sinkhorn(wc[i], wc[j + i], M, reg, k)
G = sinkhorn(wc[i], wc[j + i], M, reg * regmean[i, j], k)
if j == 0:
loss_w += np.sum(G * M)
else:
Expand Down

0 comments on commit 1b5c35b

Please sign in to comment.