|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Domain adaptation with optimal transport |
| 4 | +""" |
| 5 | + |
| 6 | + |
| 7 | +import autograd.numpy as np |
| 8 | +from pymanopt.manifolds import Stiefel |
| 9 | +from pymanopt import Problem |
| 10 | +from pymanopt.solvers import SteepestDescent, TrustRegions |
| 11 | + |
| 12 | +def dist(x1,x2): |
| 13 | + """ Compute squared euclidena distance between samples |
| 14 | + """ |
| 15 | + x1p2=np.sum(np.square(x1),1) |
| 16 | + x2p2=np.sum(np.square(x2),1) |
| 17 | + return x1p2.reshape((-1,1))+x2p2.reshape((1,-1))-2*np.dot(x1,x2.T) |
| 18 | + |
| 19 | +def sinkhorn(w1,w2,M,reg,k): |
| 20 | + """ |
| 21 | + Simple solver for Sinkhorn algorithm with fixed number of iteration |
| 22 | + """ |
| 23 | + K=np.exp(-M/reg) |
| 24 | + ui=np.ones((M.shape[0],)) |
| 25 | + vi=np.ones((M.shape[1],)) |
| 26 | + for i in range(k): |
| 27 | + vi=w2/(np.dot(K.T,ui)) |
| 28 | + ui=w1/(np.dot(K,vi)) |
| 29 | + G=ui.reshape((M.shape[0],1))*K*vi.reshape((1,M.shape[1])) |
| 30 | + return G |
| 31 | + |
| 32 | +def split_classes(X,y): |
| 33 | + """ |
| 34 | + split samples in X by classes in y |
| 35 | + """ |
| 36 | + lstsclass=np.unique(y) |
| 37 | + return [X[y==i,:].astype(np.float32) for i in lstsclass] |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +def wda(X,y,p=2,reg=1,k=10,solver = None,maxiter=100,verbose=0): |
| 42 | + """ |
| 43 | + Wasserstein Discriminant Analysis |
| 44 | + |
| 45 | + The function solves the following optimization problem: |
| 46 | +
|
| 47 | + .. math:: |
| 48 | + P = arg\min_P \frac{\sum_i W(PX^i,PX^i)}{\sum_{i,j\neq i} W(PX^i,PX^j)} |
| 49 | +
|
| 50 | + where : |
| 51 | +
|
| 52 | + - :math:`W` is entropic regularized Wasserstein distances |
| 53 | + - :math:`X^i` are samples in the dataset corresponding to class i |
| 54 | + |
| 55 | + """ |
| 56 | + |
| 57 | + mx=np.mean(X) |
| 58 | + X-=mx.reshape((1,-1)) |
| 59 | + |
| 60 | + # data split between classes |
| 61 | + d=X.shape[1] |
| 62 | + xc=split_classes(X,y) |
| 63 | + # compute uniform weighs |
| 64 | + wc=[np.ones((x.shape[0]),dtype=np.float32)/x.shape[0] for x in xc] |
| 65 | + |
| 66 | + def cost(P): |
| 67 | + # wda loss |
| 68 | + loss_b=0 |
| 69 | + loss_w=0 |
| 70 | + |
| 71 | + for i,xi in enumerate(xc): |
| 72 | + xi=np.dot(xi,P) |
| 73 | + for j,xj in enumerate(xc[i:]): |
| 74 | + xj=np.dot(xj,P) |
| 75 | + M=dist(xi,xj) |
| 76 | + G=sinkhorn(wc[i],wc[j+i],M,reg,k) |
| 77 | + if j==0: |
| 78 | + loss_w+=np.sum(G*M) |
| 79 | + else: |
| 80 | + loss_b+=np.sum(G*M) |
| 81 | + |
| 82 | + # loss inversed because minimization |
| 83 | + return loss_w/loss_b |
| 84 | + |
| 85 | + |
| 86 | + # declare manifold and problem |
| 87 | + manifold = Stiefel(d, p) |
| 88 | + problem = Problem(manifold=manifold, cost=cost) |
| 89 | + |
| 90 | + # declare solver and solve |
| 91 | + if solver is None: |
| 92 | + solver= SteepestDescent(maxiter=maxiter,logverbosity=verbose) |
| 93 | + elif solver in ['tr','TrustRegions']: |
| 94 | + solver= TrustRegions(maxiter=maxiter,logverbosity=verbose) |
| 95 | + |
| 96 | + Popt = solver.solve(problem) |
| 97 | + |
| 98 | + def proj(X): |
| 99 | + return (X-mx.reshape((1,-1))).dot(Popt) |
| 100 | + |
| 101 | + return Popt, proj |
0 commit comments