-
Notifications
You must be signed in to change notification settings - Fork 9
/
linear_classifier.py
88 lines (73 loc) · 2.35 KB
/
linear_classifier.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
from forward_mode import *
from random import uniform
def vplus(u, v):
return [ad_plus(u[i], v[i]) for i in range(0, len(u))]
def ktimesv(k, u):
return [ad_times(k, u[i]) for i in range(0, len(u))]
def dot(u, v):
sum = 0
for i in range(0, len(u)):
sum = ad_plus(sum, ad_times(u[i], v[i]))
return sum
def vminus(u, v):
return vplus(u, ktimesv(-1, v))
def distance(u, v):
return dot(vminus(u, v), vminus(u, v))
def naive_gradient_descent(f, x0, learning_rate, n):
x = x0
for i in range(0, n):
x = vminus(x, ktimesv(learning_rate, gradient(f)(x)))
return x
def linear_model(point, weights, bias):
return ad_plus(dot(weights, point), bias)
def cost(points, labels, weights, bias):
return reduce(ad_plus,
[distance([linear_model(points[i], weights, bias)],
[labels[i]])
for i in range(0, len(points))],
0)
def pack(weights, bias):
parameters = [bias]
for weight in weights:
parameters.append(weight)
return parameters
def unpack(parameters):
bias = parameters[0]
weights = parameters[1:]
return weights, bias
def initialize(points, labels):
weights = [uniform(-1, 1) for i in range(0, len(points[0]))]
bias = uniform(-1, 1)
return weights, bias
def step(points, labels, weights, bias):
def loss(parameters):
weights, bias = unpack(parameters)
return cost(points, labels, weights, bias)
parameters = pack(weights, bias)
parameters = vminus(parameters, ktimesv(0.01, gradient(loss)(parameters)))
weights, bias = unpack(parameters)
return weights, bias
def train(points, labels):
def loss(parameters):
weights, bias = unpack(parameters)
return cost(points, labels, weights, bias)
weights = [uniform(-1, 1) for i in range(0, len(points[0]))]
bias = uniform(-1, 1)
parameters = pack(weights, bias)
parameters = naive_gradient_descent(loss, parameters, 0.01, 100)
weights, bias = unpack(parameters)
return weights, bias
def classify(point, weights, bias):
if linear_model(point, weights, bias)<0:
return -1
else:
return +1
def all_labels(labels):
red = False
blue = False
for label in labels:
if label<0:
red = True
else:
blue = True
return red and blue