-
Notifications
You must be signed in to change notification settings - Fork 0
/
WDA_subfunc.py
197 lines (175 loc) · 5.49 KB
/
WDA_subfunc.py
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""
=============================================
Wasserstein Discriminant Analysis (WDA)
Subfunctions shared by WDAgd, WDAeig, WDAnepv
=============================================
"""
# Author: Dong Min Roh <[email protected]>
#
import autograd.numpy as np
from scipy import linalg
###########################################################################
def split_classes(X, y):
"""
Split data samples in data matrix X by classes using the labels y
PARAMETERS
----------
X : ndarray, shape (n, d)
Data matrix
y : ndarray, shape (n,)
Labels for X
RETURNS
-------
X_c : list
List of data class matrices
"""
labels = np.unique(y)
X_c = [X[y == i, :].astype(np.float32) for i in labels]
return X_c
def dist(X, Y):
"""
Compute squared Euclidean distance between
all possible data points in data matrices X and Y
PARAMETERS
----------
X : ndarray, shape (n, d)
Data matrix
Y : ndarray, shape (m, d)
Data matrix
RETURNS
-------
M : ndarray, shape (n, m)
Metric matrix
"""
X_inn_prod = np.sum(np.square(X), 1)
Y_inn_prod = np.sum(np.square(Y), 1)
M = X_inn_prod.reshape((-1, 1)) + Y_inn_prod.reshape((1, -1)) - 2 * np.matmul(X, Y.T)
return M
def SK(K, tol = 1e-5, maxitr = 50):
"""
Performs Sinkhorn-Knopp Iteration on a positive matrix K to obtain
positive vectors u and v such that diag(u) * K * diag(v) is normalized doubly stochastic
i.e., the sum of rows and the sum of columns of the resulting matrix are
vectors 1/n and 1/m, respectively
Article on Sinkhorn iteration:
@article{sinkhorn1967diagonal,
title={Diagonal equivalence to matrices with prescribed row and column sums},
author={Sinkhorn, Richard},
journal={The American Mathematical Monthly},
volume={74},
number={4},
pages={402--405},
year={1967},
publisher={JSTOR}
}
PARAMETERS
----------
K : ndarray, shape (n, m)
Positive matrix
tol : float, optional, default set to 1e-5
Tolerance parameter for stopping criteria
maxitr : int, optional, default set to 50
Number of maximum number of iterations
RETURNS
-------
u : ndarray, shape (n,)
Optimal left vector
v : ndarray, shape (m,)
Optimal right vector
Err : list
List of 2-norm errors between consecutive v vectors
"""
n, m = K.shape[0], K.shape[1]
Err = []
vk = np.ones(m) / m # initial point
# updates
for i in range(maxitr):
uk = np.ones(n) / (np.dot(K, vk)) / n
new_vk = np.ones(m) / (np.dot(K.T, uk)) / m
v_err = np.linalg.norm(new_vk - vk) # error
Err.append(v_err)
vk = new_vk
if v_err < tol:
break
u = uk
v = vk
return u, v, Err
def Acc_SK(K, tol = 1e-5, maxitr = 50):
"""
Performs Accelerated Sinkhorn-Knopp Iteration on a positive matrix K to obtain
positive vectors u and v such that diag(u) * K * diag(v) is normalized doubly stochastic
i.e., the sum of rows and the sum of columns of the resulting matrix are
vectors 1/n and 1/m, respectively
Article on Acclerated SK iteration:
@article{aristodemo2020accelerating,
title={Accelerating the Sinkhorn--Knopp iteration by Arnoldi-type methods},
author={Aristodemo, A and Gemignani, L},
journal={Calcolo},
volume={57},
number={1},
pages={1--17},
year={2020},
publisher={Springer}
}
PARAMETERS
----------
K : ndarray, shape (n, m)
Positive matrix
tol : float, optional, default set to 1e-5
Tolerance parameter for stopping criteria
maxitr : int, optional, default set to 50
Number of maximum number of iterations
RETURNS
-------
u : ndarray, shape (n,)
Optimal left vector
v : ndarray, shape (m,)
Optimal right vector
Err : list
List of 2-norm errors between consecutive v vectors
"""
n, m = K.shape[0], K.shape[1]
Err = []
vk = np.ones(m) / m # initial point
# updates
for i in range(maxitr):
S = np.ones(n) / np.matmul(K, vk)
R = (n/m) * np.ones(m) / np.matmul(K.T, S)
J = (m/n) * np.matmul((S ** 2)[:, None].T * ((R **2)[:, None] * K.T), K)
D, V = np.linalg.eig(J)
D = np.real(D)
V = np.real(V)
idx = np.argsort(-D)
new_vk = V[:, idx[0]]
v_err = np.linalg.norm(new_vk - vk) # error
Err.append(v_err)
vk = new_vk
if v_err < tol:
break
u = np.ones(n) / (np.matmul(K, vk)) / n
v = vk
return u, v, v_err
def pair_tensor(T, X, Y):
"""
Computes the sum of rank one matrices
\sum_{ij}T(i,j) * [X(i,:) - Y(j,:)] * [X(i,:) - Y(j,:)]'
efficiently by matrix-matrix multiplication
PARAMETERS
----------
T : ndarray, shape (n, m)
Cost matrix
X : ndarray, shape (n, d)
Data matrix
Y : ndarray, shape (m, d)
Data matrix
RETURNS
-------
C_XY : ndarray, shape (d, d)
Matrix
"""
d = X.shape[1]
temp = X[:, None] - Y
C = temp * np.sqrt(T)[:, :, None]
C = C.reshape((-1, d))
C_XY = np.matmul(C.T, C)
return C_XY