-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimplementations.py
296 lines (242 loc) · 7.58 KB
/
implementations.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
import numpy as np
def mse(y,tx,w):
"""
Calculate the Mean Square Error of the given paramaters
Parameters
----------
y : np.array
Array of labels (N,)
tx : np.array
Array of the features (N,D)
w : np.array
The weights of the model (D,)
Returns:
The result of the MSE calculation
"""
e = y- tx@w
return (e.T@e) / (2*len(y))
def least_squares(y, tx):
"""
Least squares regression using normal equations
Parameters
----------
y : np.array
Array of labels (N,)
tx : np.array
Array of the features (N,D)
Returns:
(w, loss) the last weight vector of the calculation, and the corresponding loss value (cost function).
"""
#Calculate the weight through the normal equation solution
gram_matrix=tx.T@tx
w= np.linalg.solve(gram_matrix, tx.T@y)
loss=mse(y,tx,w)
return w,loss
def ridge_regression(y, tx, lambda_):
"""
Calculate the Ridge Regression using normal equations
Parameters
----------
y : np.array
Array of labels (N,)
tx : np.array
Array of the features (N,D)
lambda_ : np.float64
Regularization parameter
Returns:
(w, loss) the last weight vector of the calculation, and the corresponding loss value (cost function).
"""
gram_matrix=np.dot(tx.T,tx)
lambda_prime=lambda_*2*len(y)
w=np.linalg.solve(gram_matrix + (lambda_prime*np.identity(tx.shape[1])),tx.T@y)
loss=mse(y,tx,w)
return w,loss
"""
Compute the gradient
Parameters
----------
y : np.array
Array of labels (N,)
tx : np.array
Array of the features (N,D)
w : np.array
The weights of the model (D,)
Returns:
The gradient
"""
def compute_gradient(y, tx, w):
e = y- np.dot(tx,w)
return -np.dot(tx.T,e)/len(y)
"""
Linear regression using gradient descent
Parameters
----------
y : np.array
Array of labels (N,)
tx : np.array
Array of the features (N,D)
initial_w : np.array
Initial random weights of the model (D,)
max_iters: int
The maximum number of iterations
gamma: float
The step size
Returns:
(w, loss) the last weight vector of the calculation, and the corresponding loss value (cost function).
"""
def gradient_descent(y, tx, initial_w, max_iters, gamma):
w = initial_w
for n_iter in range(max_iters):
gradient=compute_gradient(y, tx, w)
w=w-gamma*gradient
loss=mse(y, tx, w)
return w,loss
def batch_iter(y, tx, batch_size, num_batches=1, shuffle=True):
"""
Generate a minibatch iterator for a dataset.
Takes as input two iterables (here the output desired values 'y' and the input data 'tx')
Outputs an iterator which gives mini-batches of `batch_size` matching elements from `y` and `tx`.
Data can be randomly shuffled to avoid ordering in the original data messing with the randomness of the minibatches.
----------
y : np.array
Array of labels (N,)
tx : np.array
Array of the features (N,D)
batch_size : int
Number of samples of the batch
num_batches: int
The number of batches (default is 1)
shuffle: bool
Randomize the dataset (default is True)
"""
data_size = len(y)
if shuffle:
shuffle_indices = np.random.permutation(np.arange(data_size))
shuffled_y = y[shuffle_indices]
shuffled_tx = tx[shuffle_indices]
else:
shuffled_y = y
shuffled_tx = tx
for batch_num in range(num_batches):
start_index = batch_num * batch_size
end_index = min((batch_num + 1) * batch_size, data_size)
if start_index != end_index:
yield shuffled_y[start_index:end_index], shuffled_tx[start_index:end_index]
"""
Linear regression using stochastic gradient descent
Parameters
----------
y : np.array
Array of labels (N,)
tx : np.array
Array of the features (N,D)
initial_w : np.array
Initial random weights of the model (D,)
batch_size : int
Number of samples of the batch
max_iters: int
The maximum number of iterations
gamma: float
The step size
Returns:
(w, loss) the last weight vector of the calculation, and the corresponding loss value (cost function).
"""
def stochastic_gradient_descent(
y, tx, initial_w, batch_size, max_iters, gamma):
w = initial_w
for n_iter in range(max_iters):
for minibatch_y, minibatch_tx in batch_iter(y, tx, batch_size=batch_size,num_batches=1):
gradient=compute_gradient(minibatch_y, minibatch_tx, w)
w=w-gamma*gradient
loss=mse(minibatch_y, minibatch_tx, w)
return w,loss
"""
Compute the sigmoid
Parameters
----------
t : float
Returns:
The sigmoid of t
"""
def sigmoid(t):
return 1/(1+np.exp(-t))
"""
Logistic regression using gradient descent
Parameters
----------
y : np.array
Array of labels (N,)
tx : np.array
Array of the features (N,D)
initial_w : np.array
Initial random weights of the model (D,)
max_iters: int
The maximum number of iterations
gamma: float
The step size
Returns:
(w, loss) the last weight vector of the calculation, and the corresponding loss value (cost function).
"""
def logistic_regression(y, tx, initial_w, max_iters, gamma):
# init parameters
threshold = 1e-8
losses = []
w = initial_w
# start the logistic regression
for iter in range(max_iters):
# get loss and update w.
pred = sigmoid(tx.dot(w))
loss = y.T.dot(np.log(pred)) + (1 - y).T.dot(np.log(1 - pred))
loss = np.squeeze(- loss)
pred = sigmoid(tx.dot(w))
gradient = tx.T.dot(pred - y)
w= w - gamma*gradient
# converge criterion
losses.append(loss)
if len(losses) > 1 and np.abs(losses[-1] - losses[-2]) < threshold:
break
return w.squeeze(),loss
def calculate_loss(y, tx, w):
"""compute the cost by negative log likelihood."""
pred = sigmoid(tx.dot(w))
loss = y.T.dot(np.log(pred)) + (1 - y).T.dot(np.log(1 - pred))
return np.squeeze(- loss)
def calculate_gradient(y, tx, w):
"""compute the gradient of loss."""
pred = sigmoid(tx.dot(w))
grad = tx.T.dot(pred - y)
return grad
"""
Regularized logistic regression using gradient descent
Parameters
----------
y : np.array
Array of labels (N,)
tx : np.array
Array of the features (N,D)
lambda_ : np.float64
Regularization parameter
initial_w : np.array
Initial random weights of the model (D,)
max_iters: int
The maximum number of iterations
gamma: float
The step size
Returns:
(w, loss) the last weight vector of the calculation, and the corresponding loss value (cost function).
"""
def reg_logistic_regression(y, tx, lambda_ ,initial_w, max_iters, gamma):
# init parameters
threshold = 1e-8
losses = []
w = initial_w
# start the logistic regression
for iter in range(max_iters):
# get loss and update w.
loss = calculate_loss(y, tx, w) + lambda_ * np.squeeze(w.T.dot(w))
gradient = calculate_gradient(y, tx, w) + 2 * lambda_ * w
w= w - gamma*gradient
losses.append(loss)
if len(losses) > 1 and np.abs(losses[-1] - losses[-2]) < threshold:
break
return w.squeeze(),loss