-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradients.py
148 lines (111 loc) · 4.48 KB
/
gradients.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
import numpy as np
import scipy.linalg as scl
import time
import copy
import ot
import torch
__all__ = ['grad_AD_double', 'gradient_chol']
def grad_AD_double(a, b, M, reg, niter, tresh):
"""Gradient with automatic differentiation."""
n = a.shape[0]
m = b.shape[0]
assert n == M.shape[0] and m == M.shape[1]
u = torch.ones(n, dtype=torch.float64) / n
v = torch.ones(m, dtype=torch.float64) / m
K = torch.exp(-M / reg)
x = torch.tensor(a, dtype=torch.double, requires_grad=True)
Kp = (1/x).view(n, 1) * K
cpt = 0
err = 1
t1 = time.time()
while err > tresh and cpt < niter:
uprev = u
vprev = v
KtransposeU = torch.mm(torch.transpose(K, 0, 1), u.view(n, 1))
v = b / KtransposeU.view(m)
u = 1. / torch.mm(Kp, v.view(m, 1))
if (KtransposeU == 0).max() or torch.isnan(u).max() \
or torch.isnan(v).max() or (u == float('inf')).max():
# we have reached the machine precision
# come back to previous solution and quit loop
print('Warning: numerical errors at iteration', cpt)
u = uprev
v = vprev
break
if cpt % 10 == 0:
err = torch.sum((u - uprev) ** 2) / torch.sum(u ** 2) + \
torch.sum((v - vprev) ** 2) / torch.sum(v ** 2)
cpt = cpt + 1
T = u.view(n, 1) * K * v.view(1, m)
cost = torch.sum(T * M)
cost.backward()
grad = x.grad
grad_norm = grad - torch.dot(torch.ones(n, dtype=torch.float64), grad) * torch.ones(n, dtype=torch.float64) / n
return T, cost, grad_norm
def gradient_chol(a, b, M, reg, numIter, tresh):
"""Compute gradient with closed formula using cholesky factorization."""
n = a.shape[0]
m = b.shape[0]
assert n == M.shape[0] and m == M.shape[1]
#T = ot.sinkhorn(a, b, M, reg, method='sinkhorn', numItermax=numIter,
# stopThr=tresh)
T = Tpytorch(a, b, M, reg, numIter, tresh)
# if m < n we use Sherman Woodbury formula
if m < n:
D1i = 1/(np.sum(T, axis=1))
D2 = np.sum(T[:, 0:m-1], axis=0)
L = T*M
f = -np.sum(L, axis=1) + T[:, 0:m-1] @ ((np.sum(L[:, 0:m-1].T, axis=1)) / D2)
grada = D1i * f
TDhalf = np.multiply(T[:, 0:m - 1].T, np.sqrt(D1i))
K = np.diag(D2) - TDhalf @ TDhalf.T
Lchol = scl.cho_factor(K+1e-15*np.eye(K.shape[0]), lower=True)
grada = grada + D1i * (T[:, 0:m-1] @ scl.cho_solve(Lchol, T[:, 0:m-1].T @ grada))
else:
D1 = np.sum(T, axis=1)
D2i = 1 / (np.sum(T[:,0:m-1], axis=0))
L = T * M
f = -np.sum(L, axis=1) + T[:, 0:m - 1] @ ((np.sum(L[:, 0:m - 1].T, axis=1)) * D2i)
TDhalf = np.multiply(T[:, 0:m - 1], np.sqrt(D2i))
K = np.diag(D1) - TDhalf @ TDhalf.T
Lchol = scl.cho_factor(K + 1e-15 * np.eye(K.shape[0]), lower=True)
grada = scl.cho_solve(Lchol, f)
grada = -(grada - np.ones(n) * np.dot(grada, np.ones(n)) / n)
return grada
def Tpytorch(a, b, M, reg, niter, tresh):
n = a.shape[0]
m = b.shape[0]
assert n == M.shape[0] and m == M.shape[1]
u = torch.ones(n, dtype=torch.float64)/n
v = torch.ones(m, dtype=torch.float64)/m
tM=torch.DoubleTensor(M)
K = torch.exp(-tM/reg)
x = torch.tensor(a, dtype=torch.double, requires_grad=False)
y = torch.DoubleTensor(b)
Kp = (1/x).view(n,1) * K
cpt = 0
err = 1
while (err > tresh and cpt < niter):
uprev = u
vprev = v
KtransposeU = torch.mm(torch.transpose(K,0,1),u.view(n,1))
v = y / KtransposeU.view(m)
u = 1./torch.mm(Kp, v.view(m,1))
if (KtransposeU == 0).any() or torch.isnan(u).any()\
or torch.isnan(v).any() or (u == float('inf')).any():
# we have reached the machine precision
# come back to previous solution and quit loop
print('Warning: numerical errors at iteration', cpt)
u = uprev
v = vprev
break
if cpt % 10 == 0:
# we can speed up the process by checking for the error only all
# the 10th iterations
err = torch.sum( (u - uprev) ** 2)/ torch.sum(u ** 2) + \
torch.sum((v - vprev) ** 2)/torch.sum(v ** 2)
cpt = cpt + 1
T = u.view(n, 1) * K * v.view(1, m)
T = T.data.numpy()
TT = copy.deepcopy(T)
return TT