/
VL_BFGS.py
331 lines (216 loc) · 8.2 KB
/
VL_BFGS.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Utils for the "Large-scale L-BFGS using MapReduce" project
"""
#=========================================================================================================
#================================ 0. MODULE
# Computation on CPU
import numpy as np
# Computation on GPU
import torch
# Utils
from time import time
#=========================================================================================================
#================================ 1. FUNCTIONS
def two_loops(grad_w, s_list, y_list):
'''
Parameters
----------
grad_w (ndarray, shape [p,]) : current gradient
s_list (list[]) : the past m values of s
y_list (list[]) : the past m values of y
Returns
-------
r (ndarray, shape [p, p]) : the L-BFGS direction
'''
q = grad_w.clone().cpu()
alpha_list = []
m = len(y_list)
# First loop
for i in reversed(range(m)):
y = y_list[i]
s = s_list[i]
alpha_list.insert(0, s.matmul(q) / y.matmul(s))
q -= alpha_list[0] * y
if m != 0:
y = y_list[-1]
s = s_list[-1]
q = (y.matmul(s) / y.matmul(y)) * q
# Second loop
for i in range(m):
y = y_list[i]
s = s_list[i]
alpha = alpha_list[i]
beta = y.matmul(q) / y.matmul(s)
q += (alpha - beta) * s
return -q
def two_loops_vector_free(dot_matrix, b):
'''
Parameters
----------
dot_matrix (ndarray, shape [2m + 1, 2m + 1]) : the precomputed dot product between all vectors
b (ndarray, shape [2m + 1, n_features]) : all memory vectors and current gradient
Returns
-------
r (ndarray, shape [p, p]) : the L-BFGS direction
'''
m = int((dot_matrix.size(0) - 1) / 2)
alpha_list = []
delta = torch.zeros((2*m + 1), dtype=torch.float).to(dot_matrix.device)
delta[2*m] = -1
for i in reversed(range(m)):
denom = dot_matrix[i, m + i]
num = torch.sum(delta * dot_matrix[i, :])
alpha_list.insert(0, num / denom)
delta[m + i] -= alpha_list[0]
for i in range(2*m + 1):
delta[i] *= dot_matrix[m - 1, 2*m -1] / dot_matrix[2*m - 1, 2*m - 1]
for i in range(m):
denom = dot_matrix[i, m + i]
num = torch.sum(delta * dot_matrix[m + i, :])
beta = num / denom
delta[i] += alpha_list[i] - beta
for i in range(2*m + 1):
b[i, :] *= delta[i]
direction = b.sum(dim=0)
return direction
def dot_product(y_list, s_list, grad_w):
"""
Parameters
----------
y_list (list of m array of size n_features) : memory vectors y's
s_list (list of m array of size n_features) : memory vectors s's
grad_w (ndarray, shape [n_features]) : current gradient
Returns
-------
dot_matrix (ndarray, shape [2m + 1, 2m + 1]) : the precomputed dot product between all vectors
b (ndarray, shape [2m + 1, n_features]) : all memory vectors and current gradient
"""
m = len(y_list)
n_features = grad_w.size(0)
# Build matrix of all vectors
b = torch.empty((2*m + 1, n_features), dtype=torch.float).to(grad_w.device)
for i, tensor in enumerate(s_list):
b[i, :] = tensor
for i, tensor in enumerate(y_list):
b[m + i, :] = tensor
b[2 * m, :] = grad_w
# Dot product between all vectors
dot_matrix = b.matmul(b.transpose(0, 1))
return dot_matrix, b
def line_search(f, f_grad, c1, c2, current_f, current_grad, direction, X, y, lbda, w):
"""
Find the best gradient descent step using the Armijo and Wolfe condition
"""
alpha = 0
beta = 'inf'
step = 1
for i in range(10):
next_f = f(X, y, lbda, w.add(step * direction)).item()
f1 = (current_f + c1 * step * current_grad.matmul(direction)).item()
next_grad = f_grad(X, y, lbda, w.add(step * direction))
f2 = next_grad.matmul(direction).item()
f3 = (c2 * current_grad.matmul(direction)).item()
"""
(the method ".item()" bring back the scalars on CPU)
Here the computation takes places on the CPU because
GPUs are slower when it comes to conditions (if statement)
"""
if next_f > f1: # Armijo condition
beta = step
step = (alpha + beta) / 2
elif f2 < f3: # Wolfe condition
alpha = step
if beta == 'inf':
step = 2 * alpha
else:
step = (alpha + beta) / 2
else:
break
"""
Since the step has already been done, we return the next value of the loss function
and the next value of gradient so as to prevent from recomputing it
"""
return step, next_f, next_grad
#=========================================================================================================
#================================ 2. ALGORITHM
class lbfgs(object):
def __init__(self, f, f_grad, m=10, vector_free=False, device='cpu'):
self.c1 = 0.0001
self.c2 = 0.9
self.max_iter = 20
self.m = m
self.all_f = []
self.f = f
self.f_grad = f_grad
self.device = device
self.vector_free = vector_free
def fit(self, X, target, w0, lbda):
t0 = time()
#========================================
# Moving data to computing device
X = X.to(self.device)
target = target.to(self.device)
w = w0.to(self.device)
t1 = time()
#========================================
# Computing first value of the objective function
new_f = self.f(X, target, lbda, w).item()
self.all_f.append(new_f)
#========================================
# Computing first gradient
grad_w = self.f_grad(X, target, lbda, w)
#========================================
# Creating memory lists
y_list = []
s_list = []
for k in range(self.max_iter):
#========================================
# Compute the search direction
"""
Both two_loop functions are computed on the CPU
because they outperform GPUs when it comes to
small sequential computations with numerous for-loops.
"""
if self.vector_free:
dot_matrix, b = dot_product(y_list, s_list, grad_w)
dot_matrix = dot_matrix.cpu()
b = b.cpu()
d = two_loops_vector_free(dot_matrix, b)
d = d.to(self.device)
else:
d = two_loops(grad_w, s_list, y_list)
d = d.to(self.device)
#========================================
# Compute the step size using line search
step, new_f, new_grad = line_search(self.f, self.f_grad, self.c1, self.c2,
new_f, grad_w, d, X, target, lbda, w)
#========================================
# Compute the new value of w
s = step * d
w = w.add(s)
#========================================
# Compute y
y = new_grad.add(- grad_w)
#========================================
# Update the memory
"""
The memory vectors are direcly stored on the CPU since the
for-loops take place there
"""
y_list.append(y.cpu())
s_list.append(s.cpu())
if len(y_list) > self.m:
y_list.pop(0)
s_list.pop(0)
#========================================
# Monitoring
self.all_f.append(new_f)
l_inf_norm_grad = torch.max(torch.abs(new_grad)).item()
if l_inf_norm_grad < 1e-5:
break
grad_w = new_grad
computing_time = time() - t1
communication_time = t1 - t0
return w.cpu().numpy(), np.array(self.all_f), computing_time, communication_time