-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimplementations.py
More file actions
327 lines (251 loc) · 9.11 KB
/
implementations.py
File metadata and controls
327 lines (251 loc) · 9.11 KB
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
import numpy as np
def mean_squared_error(y, X, w):
"""
Compute the cost by mean squared error
Args:
y: shape=(N, ), data labels
X: shape=(N, D), data points
w: shape=(D, ), model weights
Returns:
scalar, loss
"""
e = y - np.dot(X, w)
return np.dot(e.T, e) / (2 * y.shape[0])
def compute_mse_gradient(y, X, w):
"""
Compute the gradient of the mse loss at w
Args:
y: numpy array of shape (N, ), data labels
X: numpy array of shape (N,D), data points
w: numpy array of shape (D, ), model weights
Returns:
Numpy array of shape (D, ), gradient of the loss at w.
"""
return -np.dot(X.T, y - np.dot(X, w)) / y.shape[0]
def sigmoid(t):
"""
Apply sigmoid function on t
Args:
t: scalar or numpy array, values to apply sigmoid function on
Returns:
scalar or numpy array, sigmoid function applied on t
"""
return 1 / (1 + np.exp(-t))
def binary_crossentropy(y, X, w):
"""
Compute the cost by negative log likelihood
Args:
y: shape=(N, ), data labels
X: shape=(N, D), data points
w: shape=(D, ), model weights
Returns:
scalar, loss
"""
return (
-np.sum(y * np.log(sigmoid(X @ w)) + (1 - y) * np.log(1 - sigmoid(X @ w)))
/ y.shape[0]
)
def compute_bc_gradient(y, X, w):
"""
Compute the gradient of the negative log likelihood loss at w
Args:
y: shape=(N, ), data labels
X: shape=(N, D), data points
w: shape=(D, ), model weights
Returns:
Numpy array of shape (D, ), gradient of the loss at w
"""
return X.T.dot(sigmoid(X @ w) - y) / y.shape[0]
def mean_squared_error_gd(y, X, initial_w, max_iters, gamma):
"""
Gradient Descent algorithm
Args:
y: numpy array of shape (N, ), data labels
X: numpy array of shape (N,D), data points
initial_w: numpy array of shape=(D, ), model weights initialization
max_iters: scalar, total number of iterations
gamma: scalar, stepsize
Returns:
best_w: numpy array of shape=(D, ), final model weights
best_loss: scalar, final loss
"""
best_w = initial_w.copy()
current_w = initial_w.copy()
best_loss = mean_squared_error(y, X, best_w)
print_interval = (max_iters + 9) // 10
for n_iter in range(max_iters):
g = compute_mse_gradient(y, X, current_w)
current_w -= gamma * g
loss = mean_squared_error(y, X, current_w)
if loss < best_loss:
best_w = current_w.copy()
best_loss = loss
if n_iter % print_interval == 0:
print(
"GD iter. {bi}/{ti}: loss={l}".format(
bi=n_iter, ti=max_iters - 1, l=loss
)
)
print("Best loss: {l}".format(l=best_loss))
return best_w, best_loss
def batch_iter(y, X, batch_size, num_batches=1, shuffle=True):
"""
Generate a minibatch iterator for a dataset.
Args:
y: numpy array of shape (N, ), data labels
X: numpy array of shape (N,D), data points
batch_size: scalar, minibatch
num_batches: scalar, number of batches
shuffle: boolean, whether to shuffle the data
Yields:
Tuples of (y, X) with batch_size data points
"""
data_size = len(y)
batch_size = min(data_size, batch_size)
max_batches = int(data_size / batch_size)
remainder = data_size - max_batches * batch_size
if shuffle:
# Generate an array of indexes indicating the start of each batch
idxs = np.random.randint(max_batches, size=num_batches) * batch_size
if remainder != 0:
# Add an random offset to the start of each batch to eventually consider the remainder points
idxs += np.random.randint(remainder + 1, size=num_batches)
else:
# If no shuffle is done, the array of indexes is circular.
idxs = np.array([i % max_batches for i in range(num_batches)]) * batch_size
for start_index in idxs:
end_index = start_index + batch_size
yield y[start_index:end_index], X[start_index:end_index]
def mean_squared_error_sgd(y, X, initial_w, max_iters, gamma):
"""
Stochastic Gradient Descent algorithm
Args:
y: numpy array of shape (N, ), data labels
X: numpy array of shape (N,D), data points
initial_w: numpy array of shape (D, ), model weights initialization
batch_size: scalar, number of data points in a mini-batch
max_iters: scalar, total number of iterations
gamma: scalar, stepsize
Returns:
best_w: numpy array of shape (D, ), final model weights
best_loss: scalar, final loss
"""
best_w = initial_w.copy()
current_w = initial_w.copy()
best_loss = mean_squared_error(y, X, best_w)
print_interval = (max_iters + 9) // 10
n_iter = 0
for minibatch_y, minibatch_x in batch_iter(
y, X, 1, num_batches=max_iters, shuffle=True
):
g = compute_mse_gradient(minibatch_y, minibatch_x, current_w)
current_w -= gamma * g
loss = mean_squared_error(y, X, current_w)
if loss < best_loss:
best_w = current_w.copy()
best_loss = loss
if n_iter % print_interval == 0:
print(
"SGD iter. {bi}/{ti}: loss={l}".format(
bi=n_iter, ti=max_iters - 1, l=loss
)
)
n_iter += 1
print("Best loss: {l}".format(l=best_loss))
return best_w, best_loss
def least_squares(y, X):
"""
Compute the least squares solution.
Args:
y: numpy array of shape (N,), data labels
X: numpy array of shape (N,D), data points
Returns:
w: numpy array of shape (D,), final model weights
loss: scalar, final loss
"""
w = np.linalg.solve(X.T @ X + 1e-8 * np.eye(X.shape[1]), X.T @ y)
return w, mean_squared_error(y, X, w)
def ridge_regression(y, X, lambda_):
"""
Ridge regression.
Args:
y: numpy array of shape (N,), data labels
X: numpy array of shape (N,D), data points
lambda_: scalar, regularization parameter
Returns:
w: numpy array of shape (D,), final model weights
loss: scalar, final loss
"""
aI = 2 * X.shape[0] * lambda_ * np.identity(X.shape[1])
w = np.linalg.solve(X.T.dot(X) + aI, X.T.dot(y))
return w, mean_squared_error(y, X, w)
def logistic_regression(y, X, initial_w, max_iters, gamma):
"""
Logistic regression using gradient descent
Args:
y: numpy array of shape (N,), data labels
X: numpy array of shape (N,D), data points
initial_w: numpy array of shape (D, ), model weights initialization
max_iters: scalar, total number of iterations
gamma: scalar, stepsize
Returns:
best_w: numpy array of shape (D, ), final model weights
best_loss: scalar, final loss
"""
best_w = initial_w.copy()
current_w = initial_w.copy()
best_loss = binary_crossentropy(y, X, best_w)
print_interval = (max_iters + 9) // 10
for n_iter in range(max_iters):
gradient = compute_bc_gradient(y, X, current_w)
current_w -= gamma * gradient
loss = binary_crossentropy(y, X, current_w)
if loss < best_loss:
best_w = current_w.copy()
best_loss = loss
if n_iter % print_interval == 0:
print(
"GD iter. {bi}/{ti}: loss={l}".format(
bi=n_iter, ti=max_iters - 1, l=loss
)
)
print("Best loss: {l}".format(l=best_loss))
return best_w, best_loss
def reg_logistic_regression(y, X, lambda_, initial_w, max_iters, gamma):
"""
Regularized logistic regression using gradient descent
Args:
y: numpy array of shape (N,), data labels
X: numpy array of shape (N,D), data points
lambda_: scalar, regularization parameter
initial_w: numpy array of shape (D, ), model weights initialization
max_iters: scalar, total number of iterations
gamma: scalar, stepsize
Returns:
best_w: numpy array of shape (D, ), final model weights
best_loss: scalar, final loss
"""
best_w = initial_w.copy()
current_w = initial_w.copy()
best_loss = binary_crossentropy(y, X, best_w) + lambda_ * np.squeeze(
best_w.T @ best_w
)
print_interval = (max_iters + 9) // 10
for n_iter in range(max_iters):
gradient = compute_bc_gradient(y, X, current_w) + 2 * lambda_ * current_w
current_w -= gamma * gradient
loss = binary_crossentropy(y, X, current_w) + lambda_ * np.squeeze(
current_w.T @ current_w
)
if loss < best_loss:
best_w = current_w.copy()
best_loss = loss
# log info
if n_iter % print_interval == 0:
print(
"GD iter. {bi}/{ti}: loss (with regularization) = {l}".format(
bi=n_iter, ti=max_iters - 1, l=loss
)
)
print("Best loss: {l}".format(l=best_loss))
return best_w, binary_crossentropy(y, X, best_w)