-
Notifications
You must be signed in to change notification settings - Fork 11
/
logistic_parallel.py
300 lines (235 loc) · 11.4 KB
/
logistic_parallel.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
import multiprocessing as mp
import queue
import time
from multiprocessing import sharedctypes
import numpy as np
from numpy import ctypeslib
from scipy.sparse import isspmatrix
from scipy.special import expit as sigmoid
from base_logistic import BaseLogistic
from constants import INIT_WEIGHT_STD
from memory import GradientMemory
from parameters import Parameters
TIMEOUT = 1
LOSS_PER_EPOCH = 10
class LogisticParallelSGD(BaseLogistic):
"""
2 classes logistic regression on dense dataset.
X: (num_samples, num_features)
y: (num_features, ) 0, 1 labels
"""
def __init__(self, params: Parameters):
super().__init__(params)
self.params = params
self.w = None
self.epoch_callback = None
self.print = False
def fit_until(self, X, y, num_samples, num_features, baseline=None):
# num_samples, num_features = X.shape
p = self.params
if self.w is None:
self.w = np.random.normal(0, INIT_WEIGHT_STD, size=(num_features,))
def worker_fit(id_w, num_workers, X_w, y_w, weights_w, shape, indices, results, params_w, stopper):
# reconstruct numpy shared array
num_samples, num_features = shape
weights_w = ctypeslib.as_array(weights_w)
weights_w.shape = (num_features,)
if not isspmatrix(X_w):
X_w = ctypeslib.as_array(X_w)
X_w.shape = (num_samples, num_features)
y_w = ctypeslib.as_array(y_w)
y_w.shape = (num_samples,)
memory = GradientMemory(take_k=params_w.take_k, take_top=params_w.take_top,
with_memory=params_w.with_memory)
if id_w == 0:
losses = np.zeros(params_w.num_epoch * LOSS_PER_EPOCH + 1)
losses[0] = self.loss(X, y)
start_time = time.time()
last_printed = 0
loss_every = num_samples // LOSS_PER_EPOCH
for epoch in range(params_w.num_epoch):
for iteration in range(id_w, num_samples, num_workers):
# worker 0 gave stop signal, reached accuracy
if stopper.value:
return
sample_idx = indices[epoch][iteration]
lr = self.lr(epoch, iteration, num_samples, num_features)
x = X_w[sample_idx]
if isspmatrix(x):
x = np.array(x.todense()).squeeze(0)
minus_grad = y[sample_idx] * x * sigmoid(-y[sample_idx] * np.dot(x, self.w))
# minus_grad = - x * (pred_proba - y_w[sample_idx])
if params_w.regularizer:
minus_grad -= 2 * params_w.regularizer * weights_w
sparse = params_w.take_k and (params_w.take_k < num_features)
# next_real -= 1
lr_minus_grad = memory(lr * minus_grad, sparse=sparse) # , no_apply=(next_real != 0))
# if next_real == 0:
# next_real = params_w.real_update_every
if sparse:
weights_w[lr_minus_grad[0]] += lr_minus_grad[1]
else:
weights_w += lr_minus_grad
if id_w == 0 and num_samples * epoch + iteration - last_printed >= loss_every:
last_printed = num_samples * epoch + iteration
timing = time.time() - start_time
loss = self.loss(X, y)
losses[epoch * LOSS_PER_EPOCH + (iteration // loss_every) + 1] = loss
print("epoch {} iter {} loss {} time {}s".format(
epoch, iteration, loss, timing))
if baseline and loss <= baseline:
stopper.value = True
results['epoch'] = epoch
results['losses'] = losses
results['iteration'] = iteration
results['timing'] = timing
return
# if failed to converge...
if id_w == 0:
results['epoch'] = epoch
results['losses'] = losses
results['iteration'] = iteration
results['timing'] = time.time() - start_time
with mp.Manager() as manager:
results = manager.dict()
stopper = manager.Value('b', False)
indices = np.zeros((p.num_epoch, num_samples), dtype=int)
for i in range(p.num_epoch):
indices[i] = np.arange(num_samples)
np.random.shuffle(indices[i])
weights_w = sharedctypes.RawArray('d', self.w)
self.w = ctypeslib.as_array(weights_w)
self.w.shape = (num_features,)
if isspmatrix(X):
X_w = X
y_w = y
else:
X_w = sharedctypes.RawArray('d', np.ravel(X))
y_w = sharedctypes.RawArray('d', y)
processes = [mp.Process(target=worker_fit,
args=(
i, p.n_cores, X_w, y_w, weights_w, X.shape, indices, results, self.params,
stopper))
for i in range(p.n_cores)]
for p in processes:
p.start()
for i, p in enumerate(processes):
p.join()
print(results)
return results['timing'], results['epoch'], results['iteration'], results['losses']
def fit(self, X, y, num_samples, num_features, loss_per_epoch=10):
p = self.params
if self.w is None:
self.w = np.random.normal(0, INIT_WEIGHT_STD, size=(num_features,))
def worker_fit(id_w, num_workers, X_w, y_w, weights_w, shape, indices, counter, start_barrier, params_w):
assert params_w.regularizer is not None
# reconstruct numpy shared array
num_samples, num_features = shape
weights_w = ctypeslib.as_array(weights_w)
weights_w.shape = (num_features,)
if not isspmatrix(X_w):
X_w = ctypeslib.as_array(X_w)
X_w.shape = (num_samples, num_features)
y_w = ctypeslib.as_array(y_w)
y_w.shape = (num_samples,)
memory = GradientMemory(take_k=params_w.take_k, take_top=params_w.take_top,
with_memory=params_w.with_memory)
start_barrier.wait()
while True:
with counter.get_lock():
idx = counter.value
counter.value += 1
if idx >= num_samples * params_w.num_epoch:
break
sample_idx = indices[idx]
epoch = idx // num_samples
iteration = idx % num_samples
lr = self.lr(epoch, iteration, num_samples, num_features)
x = X_w[sample_idx]
if isspmatrix(x):
minus_grad = -1. * params_w.regularizer * weights_w
sparse_minus_grad = y[sample_idx] * x * sigmoid(-y[sample_idx] * x.dot(weights_w).squeeze(0))
minus_grad[sparse_minus_grad.indices] += sparse_minus_grad.data
else:
minus_grad = y[sample_idx] * x * sigmoid(-y[sample_idx] * x.dot(weights_w))
minus_grad -= params_w.regularizer * weights_w
sparse = params_w.take_k and (params_w.take_k < num_features)
lr_minus_grad = memory(lr * minus_grad, sparse=sparse)
if sparse:
weights_w[lr_minus_grad[0]] += lr_minus_grad[1]
else:
weights_w += lr_minus_grad
with mp.Manager() as manager:
counter = mp.Value('i', 0)
start_barrier = manager.Barrier(p.n_cores + 1) # wait all worker and the monitor to be ready
indices = np.zeros((p.num_epoch, num_samples), dtype=int)
for i in range(p.num_epoch):
indices[i] = np.arange(num_samples)
np.random.shuffle(indices[i])
indices = indices.flatten()
weights_w = sharedctypes.RawArray('d', self.w)
self.w = ctypeslib.as_array(weights_w)
self.w.shape = (num_features,)
if isspmatrix(X):
X_w = X
y_w = y
else:
X_w = sharedctypes.RawArray('d', np.ravel(X))
y_w = sharedctypes.RawArray('d', y)
processes = [mp.Process(target=worker_fit,
args=(
i, p.n_cores, X_w, y_w, weights_w, X.shape, indices, counter, start_barrier,
self.params))
for i in range(p.n_cores)]
for p in processes:
p.start()
# monitor the progress
print_every = num_samples // loss_per_epoch
next_print = 0
# loss computing on another thread through the queue
stop = manager.Value('b', False)
w_queue = mp.Queue()
results = manager.dict()
def loss_computer(q, regularizer, res, stop): # should be stoppable
print('start loss computer')
losses = []
iters = []
timers = []
while not q.empty() or not stop.value:
try:
epoch, iter_, total_iter, chrono, w = q.get(block=True, timeout=1)
except queue.Empty:
# print('empty queue')
continue
# print('dequeue', epoch, iter_)
loss = np.sum(np.log(1 + np.exp(-y * (X @ w)))) / X.shape[0]
if regularizer is not None:
loss += regularizer * np.square(w).sum() / 2
timers.append(chrono)
losses.append(loss)
iters.append(total_iter)
print("epoch {} iteration {} loss {} time {}s".format(epoch, iter_, loss, chrono))
res['losses'] = np.array(losses)
res['iters'] = np.array(iters)
res['timers'] = np.array(timers)
start_barrier.wait()
start_time = time.time()
loss_computer = mp.Process(target=loss_computer, args=(w_queue, self.params.regularizer, results, stop))
loss_computer.start()
while counter.value < self.params.num_epoch * num_samples:
if counter.value > next_print:
w_copy = (self.w_estimate if self.w_estimate is not None else self.w).copy()
epoch = next_print // num_samples
iter_ = next_print % num_samples
chrono = time.time() - start_time
w_queue.put((epoch, iter_, next_print, chrono, w_copy))
# print('enqueue', epoch, iter_)
next_print += print_every
else:
time.sleep(.1)
stop.value = True # stop the loss computer
for i, p in enumerate(processes):
p.join()
loss_computer.join()
print(results)
return results['iters'], results['timers'], results['losses']