-
Notifications
You must be signed in to change notification settings - Fork 0
/
implementations.py
201 lines (177 loc) · 5.95 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
import numpy as np
from costs import *
from gradients import *
from hw_helpers import *
def least_squares_GD(y, tx, initial_w, max_iters, gamma):
"""
Perform a gradient descent using the least-squares method(MSE cost function)
:param y: labels
:param tx: features
:param initial_w: initial weights
:param max_iters: maximum number of iteration
:param gamma: step-size
:return: the optimized weights
"""
ws = [initial_w]
losses = []
w = initial_w
loss = float('inf')
for n_iter in range(max_iters):
gradient = compute_gradient_mse(y,tx,w)
print(gradient)
loss = compute_mse(y,tx,w)
print(loss)
w = w - gamma*gradient
ws.append(w)
print(w)
losses.append(loss)
return w, loss
def least_squares_SDG(y, tx, initial_w, max_iters, gamma):
"""
Perfom a stochastic gradient descent using the least squares method(MSE cost function)
:param y: labels
:param tx: features
:param initial_w: weights
:param max_iters: maximum number of iteration
:param gamma: step-size
:return: the optimized weights
"""
losses= []
w = initial_w
ws = [initial_w]
losses = []
loss = float('inf')
batch_size = 1
for i in range(max_iters):
for minbatch_y, minbatch_x in batch_iter(y, tx, batch_size):
gradient = compute_gradient_mse(minbatch_y, minbatch_x, w)
#SGD using mse
loss = compute_mse(minbatch_y, minbatch_x, w)
w = w-gamma*gradient
ws.append(w)
losses.append(loss)
#returns only the final choice
return w, loss
def least_squares(y,tx):
"""
Return the optimized w using the normal equation of the least-squares method(MSE cost function)
:param y: labels
:param tx: features
:return: the optimized weights
"""
if np.linalg.matrix_rank(tx) == tx.shape[1]:
w = np.linalg.inv(tx.T@tx)@tx.T@y
ls = compute_mse(y, tx, w)
return w, ls
else:
w = np.linalg.pinv(tx)@y
ls = compute_mse(y, tx, w)
return w, ls
def ridge_regression(y, tx, lambda_):
"""
Perform the ridge regression
:param y: labels
:param tx: features
:param lambda_: regularization
:return: the optimized w + the result of the minimum square error cost function for the optimized w
"""
w = np.linalg.inv(tx.T@tx + (lambda_*2.0*float(tx.shape[0]))*np.identity(tx.shape[1]))@(tx.T@y)
#w = np.linalg.inv([email protected] + lambda_*np.identity(tx.shape[1]))@tx@y
e = compute_mse(y, tx, w)
return w, e
def logistic_regression(y, tx, initial_w, max_iters, gamma):
"""
Perform the logistic regression using a gradient descent
:param y: labels
:param tx: features
:param initial_w: initial weight vectors
:param max_iters: maximum number of iteration
:param gamma: step-size
:return: the optimized weights and the result of the negative log likelihood cost function
"""
w = initial_w
losses = []
threshold = 1e-8
for i in range(max_iters):
g = compute_gradient_likelihood(y, tx, w)
w = w-gamma*g
loss = calculate_loss(y, tx, w)
losses.append(loss)
if len(losses) > 1 and np.abs(losses[-1] - losses[-2] < threshold):
break
loss = calculate_loss(y, tx, w)
return w, loss
def reg_logistic_regression(y, tx, lambda_, initial_w, max_iters, gamma):
"""
Perform the regularized logistic regression using a gradient descent
:param y: labels
:param tx: features
:param lambda_: regularization hyper parameter
:param initial_w: initial weights
:param max_iters: maximum number of iteration
:param gamma: step-size
:return:
"""
w = initial_w
losses = []
threshold = 1e-8
for i in range(max_iters):
#loss = ((-1)*(2*y*np.log(sigmoid(tx@w))+(1-y)*np.log(1-sigmoid(tx@w)))).sum()
#loss, g = penalized_logistic_regression(y, tx, w, lambda_)
loss = calculate_loss(y, tx, w, lambda_)
g = compute_gradient_likelihood(y, tx, w, lambda_)
#loss = calculate_loss(y, tx, w)
losses.append(loss)
#g = compute_gradient_likelihood(y, tx, w) - lambda_ * w
#print(loss)
w = w - gamma*g
if len(losses) > 1 and np.abs(losses[-1]- losses[-2] < threshold):
break
return w, loss
def new_logistic_regression(y, tx, lambda_, initial_w, max_iters, gamma):
"""
Perform the regularized logistic regression using the newton's method
:param y: labels
:param tx: features
:param lambda_: regularization
:param initial_w: initial weights
:param max_iters: maximum number of iteration
:param gamma: step-size
:return:
"""
losses = []
w = initial_w
threshold = 1e-8
for i in range(max_iters):
loss = calculate_loss(y, tx, w)
losses.append(loss)
g = compute_gradient_likelihood(y, tx, w) + lambda_ * w
hes = calculate_hessian(y, tx, w)
w = w-gamma*np.linalg.inv(hes)@g
if len(losses) > 1 and np.abs(losses[-1]- losses[-2] < threshold):
break
loss = calculate_loss(y, tx, w)
return w,loss
def double_pen_logistic_regression(y, tx, lambda_, initial_w, max_iters, gamma):
"""
Perform the regularized logistic regression using gradient descent and double error for false detection of 0
:param y: labels
:param tx: features
:param lambda_: regularization
:param initial_w: initial weights
:param max_iters: maximum number of iteration
:param gamma: step-size
:return:
"""
losses = []
w = initial_w
threshold = 1e-8
for i in range(max_iters):
loss = calculate_loss(y, tx, w, lambda_)
losses.append(loss)
g = double_pen_gradient_likelihood(y, tx, w, lambda_)
w = w-gamma*g
#if len(losses) > 1 and np.abs(losses[-1]- losses[-2] < threshold):
# break
loss = calculate_loss(y, tx, w, lambda_)
return w, loss