-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementations.py
More file actions
180 lines (165 loc) · 4.77 KB
/
Copy pathimplementations.py
File metadata and controls
180 lines (165 loc) · 4.77 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
import numpy as np
def compute_mse(y, tx, w):
"""
y:
x:
w:
return the MSE loss
"""
# ***************************************************
e = y - tx@w
return (1/2)*np.mean(e**2)
# ***************************************************
def least_squares(y, tx):
"""
y:
x:
w:
return the least squares weights and the associated loss
"""
# ***************************************************
A = tx.T@tx
b = tx.T@y
w = np.linalg.solve(A, b)
e = y - tx@w
loss = compute_mse(y, tx, w)
return w, loss
# ***************************************************
def compute_gradient(y, tx, w):
"""
y:
tx:
w:
"""
grad = -np.mean(tx.T@(y - tx@w))
return grad
def least_squares_GD(y, tx, initial_w, max_iters, gamma):
"""
y:
tx:
initial_w:
max_iters:
gamma:
return the optimal weights using gradient descent and the associated loss
"""
# ***************************************************
w = initial_w
for n in range(max_iters):
grad = compute_gradient(y, tx, w)
w -= gamma*grad
loss = compute_mse(y, tx, w)
return w, loss
# ***************************************************
def least_squares_SGD(y, tx, initial_w, max_iters, gamma):
"""
y:
tx:
initial_w:
max_iters:
gamma:
return the optimal weights using stochatic gradient descent and the associated loss
"""
# ***************************************************
w = initial_w
for n in range(max_iters):
for y_batch, tx_batch in batch_iter(y, tx, batch_size=1):
grad = (compute_gradienty_batch, tx_batch, w)
w -= gamma * grad
loss = compute_mse(y, tx, w)
return w, loss
# ***************************************************
def ridge_regression(y, tx, lambda_):
"""
y:
tx:
lambda_:
return the optimal weights using ridge regression and the associated loss
"""
# ***************************************************
A = tx.T.dot(tx) + 2 * tx.shape[0] * lambda_ * np.identity(tx.shape[1])
b = tx.T.dot(y)
return np.linalg.solve(A, b)
# ***************************************************
def sigmoid(t):
"""apply the sigmoid function on t."""
# ***************************************************
# INSERT YOUR CODE HERE
# TODO
# ***************************************************
return 1 / (1 + np.exp(-t))
def logistic_loss(y, tx, w):
"""
y:
x:
w:
return the logistic loss
"""
# ***************************************************
sig = sigmoid(-tx.dot(w))
return - (y.T.dot(np.log(sig)) + (1 - y).T.dot(np.log(1 - sig)))
# ***************************************************
def logistic_gradient(y, tx, w):
"""
y:
x:
w:
return the gradient of the logistic function
"""
# ***************************************************
sig = sigmoid(-tx.dot(w))
return tx.T@(sig - y)
# ***************************************************
def logistic_regression(y, tx, initial_w, max_iters, gamma):
"""
y:
tx:
initial_w:
max_iters:
gamma:
return the optimal weights using gradient descent with logistic loss and the associated loss
"""
# ***************************************************
w = initial_w
for n in range(max_iters):
grad = logistic_gradient(y, tx, w)
w = w - gamma*grad
loss = logistic_loss(y, tx, w)
return loss, w
# ***************************************************
def reglogistic_loss(y, tx, w, lambda_):
"""
y:
x:
w:
return the logistic loss
"""
# ***************************************************
return logistic_loss(y, tx, w) + (lambda_/2) * np.squeeze(w.T@w)
# ***************************************************
def reglogistic_gradient(y, tx, w, lambda_):
"""
y:
x:
w:
return the gradient of the logistic function
"""
# ***************************************************
return logistic_gradient(y, tx, w) + lambda_ * np.squeeze(w.T@w)
# ***************************************************
def reglogistic_regression(y, tx, lambda_, initial_w, max_iters, gamma):
"""
y:
tx:
initial_w:
max_iters:
gamma:
return the optimal weights using stochatic gradient descent with logistic loss and the associated loss
"""
# ***************************************************
w = initial_w
for n in range(max_iters):
grad = reglogistic_gradient(y, tx, w, lambda_)
w = w - gamma*grad
loss = reglogistic_loss(y, tx, w, lambda_)
return loss, w
# ***************************************************