-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdstableFGM.py
265 lines (207 loc) · 7.16 KB
/
dstableFGM.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from copy import deepcopy
import pdb
import torch
import numpy as np
from scipy.linalg import polar
def graddstableform(A, S, U, B, return_grad=False):
# Throw away any imaginary components that leak in
S = np.real(S)
U = np.real(U)
B = np.real(B)
n = A.shape[0]
At = torch.tensor(A, requires_grad=True)
St = torch.tensor(S, requires_grad=True)
Ut = torch.tensor(U, requires_grad=True)
Bt = torch.tensor(B, requires_grad=True)
loss = torch.pow(torch.norm(At - torch.chain_matmul(torch.inverse(St), Ut, Bt, St)), 2)
# Gradient
if return_grad:
loss.backward()
gradS = St.grad.detach().numpy()
gradU = Ut.grad.detach().numpy()
gradB = Bt.grad.detach().numpy()
return loss.detach().numpy(), gradS, gradU, gradB
else:
return loss.detach().numpy()
def projectPSD(Q, epsilon=1e-6, delta=np.inf):
Q = (Q + Q.T)/2
e, V = np.linalg.eig(Q)
Qp = np.diag(np.minimum(delta * np.ones(e.size), np.maximum(e, epsilon * np.ones(e.size))))
Qp = V @ Qp @ V.T
return Qp
# Just implement a simple projected gradient descent
def dstable_descent(A, maxiter=100, inneriter=20, step_reduc=1.5, tol=1e-3, posdef=1e-6, astab=1e-6, init='default'):
n = A.shape[0]
if not astab >= 0 and astab <= 1:
raise ValueError('astab must be between - and 1')
if init == 'default':
S = np.eye(n)
U, B = polar(A)
B = projectPSD(B, 0, 1 - astab)
elif init == 'random':
S = np.diag(np.random.uniform(1e-6, 1 - 1e-6, size=(n,)))
U, B = polar(S @ A @ np.linalg.inv(S))
B = projectPSD(B, 0, 1 - astab)
if np.linalg.cond(S) > 1e12:
print('The initial S is ill-conditioned')
eS = np.linalg.eigvals(S)
e = np.zeros(maxiter + 1)
L=(max(eS) / min(eS)) ** 2
e[0]=graddstableform(A,S,U,B)
print(e[0])
step=1 / L
i = 0
delta_e = np.inf
while i < maxiter and delta_e > tol:
# Throw away any imaginary components that leak in
S = np.real(S)
U = np.real(U)
B = np.real(B)
_, gS, gU, gB = graddstableform(A, S, U, B, return_grad=True)
j = 0
e_ = []
while j < inneriter:
Sn=S - gS * step
Un=U - gU * step
Bn=B - gB * step
Sn=projectPSD(Sn, posdef)
Un, _ = polar(Un)
Bn=projectPSD(Bn,0,1 - astab)
e_.append(graddstableform(A, Sn, Un, Bn))
if e_[-1] < e[i]:
break
else:
step /= step_reduc
j += 1
if j == inneriter:
print('Descent failed')
return np.linalg.inv(S) @ U @ B @ S
else:
i += 1
e[i] = e_[-1]
if i > 10:
delta_e = np.mean(np.abs(np.diff(e))[i - 5:i])
print(e[i])
S = Sn
U = Un
B = Bn
step *= 2
return np.linalg.inv(S) @ U @ B @ S
def dstableFGM(A=None, maxiter=int(1e3), posdef=1e-6, astab=1e-6, display=1,
alpha0=0.5, lsparam=1.5, lsitermax=50, gradient=0, init='default'):
n = A.shape[0]
nA2 = np.linalg.norm(A)**2
if not astab >= 0 and astab <= 1:
raise ValueError('astab must be between - and 1')
if init == 'default':
S = np.eye(n)
U, B = polar(A)
B = projectPSD(B, 0, 1 - astab)
elif init == 'random':
S = np.diag(np.random.uniform(1e-6, 1 - 1e-6, size=(n,)))
U, B = polar(S @ A @ np.linalg.inv(S))
B = projectPSD(B, 0, 1 - astab)
if not (alpha0 > 0 and alpha0 < 1):
raise ValueError('alpha0 has to be in (0, 1).')
if not lsparam > 1:
raise ValueError('lsparam has to be larger than 1 for convergence.')
if np.linalg.cond(S) > 1e12:
print('The initial S is ill-conditioned')
eS = np.linalg.eigvals(S)
e = np.zeros(maxiter)
alpha = np.zeros(maxiter)
alpha[0] = alpha0
beta = np.zeros(maxiter)
L=(max(eS) / min(eS)) ** 2
e[0]=graddstableform(A,S,U,B)
print(e[0])
step=1 / L
i=0
Ys = deepcopy(S)
Yu = deepcopy(U)
Yb = deepcopy(B)
restarti=1
if display:
print('Display of iteration number and error:')
if n < 10:
ndisplay=1000
elif n < 50:
ndisplay=100
elif n < 100:
ndisplay=10
elif n < 500:
ndisplay = 5
else:
ndisplay = 1
# Main loop
while i < maxiter:
# Compute gradient
__,gS,gU,gB=graddstableform(A,Ys,Yu,Yb,return_grad=True)
e[i + 1] =+ np.inf
inneriter=1
e_ = []
while e[i + 1] > e[i] and ((i == 0 and inneriter < 100) or inneriter < lsitermax):
# For i == 1, we always have a descent direction
Sn=Ys - gS * step
Un=Yu - gU * step
Bn=Yb - gB * step
Sn=projectPSD(Sn, posdef)
Un, _ = polar(Un)
Bn=projectPSD(Bn,0,1 - astab)
e_.append(graddstableform(A, Sn, Un, Bn))
e[i + 1]= e_[-1]
step=step / lsparam
inneriter=inneriter + 1
if i == 0:
inneriter0=deepcopy(inneriter)
# Conjugate with FGM weights, if decrease was achieved
# otherwise restart FGM
alpha[i + 1]= (np.sqrt(alpha[i]**4 + 4 * alpha[i]**2) - alpha[i]**2)/2
beta[i]= alpha[i] * (1 - alpha[i]) / (alpha[i] ** 2 + alpha[i + 1])
if inneriter >= lsitermax:
if restarti == 1:
# Restart FGM if not a descent direction
restarti=0
alpha[i + 1]=alpha0
Ys=deepcopy(S)
Yu=deepcopy(U)
Yb=deepcopy(B)
e[i + 1]=e[i]
if display:
print('Descent could not be achieved: restart. (Iteration %3.0f) \n',i)
# Reinitialize step length
eS=np.linalg.eigvals(S)
L=(max(eS) / min(eS)) ** 2
# step were necessary to obtain decrease
step=1 / L / (lsparam ** inneriter0)
else:
if restarti == 0:
if display:
print('The algorithm has converged.')
e[i + 1]=e[i]
break
else:
restarti=1
if gradient == 1:
beta[i]=0
Ys=Sn + (beta[i] * (Sn - S))
Yu=Un + (beta[i] *(Un - U))
Yb=Bn + (beta[i] * (Bn - B))
S=deepcopy(Sn)
U=deepcopy(Un)
B=deepcopy(Bn)
A_ = np.linalg.inv(Ys) @ Yu @ Yb @ Ys
loss = np.linalg.norm(A - A_)
# Display
if display:
if i % ndisplay == 0:
print('%2.0f:%2.3f - ',i ,e[i + 1])
if i % (ndisplay * 10) == 0:
print('\n')
if e[i] < (1e-12 * nA2) or (i > 100 and (e[i - 100] - e[i]) < 1e-06 * e[i - 100]):
pdb.set_trace()
if display:
print('The algorithm has converged.')
break
step *= 2
return np.linalg.inv(S) @ U @ B @ S