-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml.py
324 lines (245 loc) · 7.68 KB
/
ml.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File name: basic_linear_classifier.py
# Author: Jorge Alejandro Rodriguez Aldana
# Date: 1mar2023
# Import libraries
# -----------------
import numpy as np
from collections.abc import Iterable
import random as rnd
# Define blc predictor
# --------------------
def blc_predictor(w:np.ndarray,x:np.ndarray) -> float:
"""**B**inary **l**inear **c**lassifier predictor. Returns the sign of a prediction given a weigth (w) and a feauture vector (x).
Parameters
----------
w : np.ndarray
n-dimensional weight vector
x : np.ndarray
n-dimensional feauture vector
Returns
-------
float
sign value
"""
score = w.dot(x)
if score > 0:
sign = 1
elif score < 0:
sign = -1
else:
sign = np.nan
return sign
# Define regression predictor
# ---------------------------
def reg_predictor(w:np.ndarray,x:np.ndarray) -> float:
"""**Reg**ression predictor. Returns the sign of a prediction given a weigth (w) and a feauture vector (x).
Parameters
----------
w : np.ndarray
n-dimensional weight vector
x : np.ndarray
n-dimensional feauture vector
Returns
-------
float
sign value
"""
sign = w.dot(x)
return sign
# Define margin
# -------------
def margin(w:np.ndarray,x:np.ndarray,y:float):
"""Return the **margin** value for a given weight (w), feauture vector (x) and true prediction value (y).
Args:
w (np.ndarray): n-dimensional weight vector
x (np.ndarray): n-dimensional feauture vector
y (float): true prediction value for x
Returns:
float: Margin value
"""
return (w.dot(x)) * y
# Define loss and derivatives
# ---------------------------
def zero_one_loss(w:np.ndarray,x:np.ndarray,y:float) -> float:
"""Returns the **0-1 loss** for a given weight (w), feauture vector (x) and true prediction value (y).
Parameters
----------
w : np.ndarray
n-dimensional weight vector
x : np.ndarray
n-dimensional feauture vector
y : float
true prediction value for x
Returns
-------
float
0-1 loss value
"""
mar = margin(w,x,y)
if mar <= 0:
return 1
else:
return 0
def hinge_loss(w:np.ndarray,x:np.ndarray,y:float) -> float:
"""Returns the **hinge loss** for a given weight (w), feauture vector (x) and true prediction value (y).
Parameters
----------
w : np.ndarray
n-dimensional weight vector
x : np.ndarray
n-dimensional feauture vector
y : float
true prediction value for x
Returns
-------
float
Hinge loss value
"""
mar = margin(w,x,y)
hinge = max([0,1-mar])
return hinge
def hinge_loss_derivative(w:np.ndarray,x:np.ndarray,y:float) -> float:
"""Returns the **derivative** of the **hinge loss** for a given weight (w), feauture vector (x) and true prediction value (y).
Parameters
----------
w : np.ndarray
n-dimensional weight vector
x : np.ndarray
n-dimensional feauture vector
y : float
true prediction value for x
Returns
-------
float
Hinge loss derivative value
"""
mar = margin(w,x,y)
if mar < 1:
return - x * y
if mar >= 1:
return 0
def squared_loss(w:np.ndarray,x:np.ndarray,y:float,predictor = reg_predictor) -> float:
"""Returns the **squared loss** for a given weight (w), feauture vector (x) and true prediction value (y).
Parameters
----------
w : np.ndarray
n-dimensional weight vector
x : np.ndarray
n-dimensional feauture vector
y : float
true prediction value for x
Returns
-------
float
Squared loss value
"""
score = predictor(w,x)
residual = score - y
return residual**2
def squared_loss_derivative(w:np.ndarray,x:np.ndarray,y:float,predictor = reg_predictor) -> float:
"""Returns the **squared loss derivative** for a given weight (w), feauture vector (x) and true prediction value (y).
Parameters
----------
w : np.ndarray
n-dimensional weight vector
x : np.ndarray
n-dimensional feauture vector
y : float
true prediction value for x
Returns
-------
float
Squared loss derivative value
"""
score = predictor(w,x)
residual = score - y
return 2*residual*x
# Gradient descent
# ----------------
def gradient_descent(loss,dd_loss,training_dataset:Iterable,eta:float = 0.01,iterations = 500,verbose:bool = True,w:np.ndarray = None) -> np.ndarray:
"""Execute the classic **gradient descent** for all the loss functions for a given training dataset and returns a trained weight.
Parameters
----------
loss : function
Loss function.
dd_loss : function
Loss derivative function
training_dataset : Iterable
A list containing the (weight,label) pair for training.
eta : float, optional
The step size, by default 0.01
iterations : int, optional
The number of iterations, by default 500
verbose : bool, optional
True for verbose, by default True
w : np.ndarray, optional
A starting width np.ndarray, by default None
Returns
-------
np.ndarray
Trained width
"""
dimension = training_dataset[0][0].shape[0]
try:
if w == None:
w = np.zeros(dimension)
except:
pass
for iteration in range(iterations):
gradient = sum(dd_loss(w,x,y) for x,y in training_dataset)/len(training_dataset)
w = w - (eta * gradient)
loss_value = sum(loss(w,x,y) for x,y in training_dataset)/len(training_dataset)
if loss_value == 0:
break
if verbose:
print('iteration {}:'.format(iteration+1),'w = {},'.format(w),'Loss(w) = {}'.format(loss_value))
return w
# Stocastic Gradient descent
# --------------------------
def stocastic_gradient_descent(loss,dd_loss,training_dataset:Iterable,init_eta:float = 0.1,iterations = 500,verbose:bool = True, w:np.ndarray = None):
"""Execute the **stocastic gradient descent** for all the loss functions for a given training dataset and returns a trained weight.
Parameters
----------
loss : function
Loss function.
dd_loss : function
Loss derivative function
training_dataset : Iterable
A list containing the (weight,label) pair for training.
eta : float, optional
The step size, by default 0.01
iterations : int, optional
The number of iterations, by default 500
verbose : bool, optional
True for verbose, by default True
w : np.ndarray, optional
A starting width np.ndarray, by default None
Returns
-------
np.ndarray
Trained width
"""
dimension = training_dataset[0][0].shape[0]
if w is None:
w = np.zeros(dimension)
n = 0
dataset_len = len(training_dataset)
for iteration in range(iterations):
n += 1
eta = init_eta/np.sqrt(n)
# eta = init_eta
indexlist = np.arange(dataset_len)
rnd.shuffle(indexlist)
for i in indexlist:
x = training_dataset[i][0]
y = training_dataset[i][1]
sample_loss = loss(w,x,y)
if sample_loss:
gradient = dd_loss(w,x,y)
w = w - (eta * gradient)
loss_value = sum(loss(w,x,y) for x,y in training_dataset)/dataset_len
if verbose:
print('iteration {}, index {}: w = {}, Loss(w) = {}'.format(iteration+1,i,w,loss_value))
if loss_value == 0:
break
return w