-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogReg.jl
121 lines (86 loc) · 2.19 KB
/
logReg.jl
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
include("misc.jl")
include("findMin.jl")
function logReg(X,y)
(n,d) = size(X)
# Initial guess
w = zeros(d,1)
# Function we're going to minimize (and that computes gradient)
funObj(w) = logisticObj(w,X,y)
# Solve least squares problem
w = findMin(funObj,w,derivativeCheck=false)
# Make linear prediction function
predict(Xhat) = sign.(Xhat*w)
# Return model
return LinearModel(predict,w)
end
function logisticObj(w,X,y)
yXw = y.*(X*w)
f = sum(log.(1 + exp.(-yXw)))
g = -X'*(y./(1+exp.(yXw)))
return (f,g)
end
# Multi-class Softmax version
function softmaxClassifier(X,y)
(n,d) = size(X)
k = maximum(y)
# Initial guess
w = zeros(d*k,1)
# Function we're going to minimize (and that computes gradient)
funObj(w) = softmaxObj(w,X,y)
# Solve least squares problem
w = findMin(funObj,w,derivativeCheck=true)
W = reshape(w,(d,k))
# Make linear prediction function
predict(Xhat) = mapslices(indmax,Xhat*W,2)
return LinearModel(predict,W)
end
function softmaxObj(w,X,y)
(n,d) = size(X)
k = maximum(y)
# reshape W to d x k
W = reshape(w,(d,k))
f = 0
g = zeros(d,k)
for i in 1:n
h = 0
yi = y[i]
for c in 1:k
h += exp(dot(X[i,:],W[:,c]))
end
f += -dot(X[i,:],W[:,yi]) + log(h)
end
for j in 1:d
for c in 1:k
m = 0
for i in 1:n
term1 = -(y[i] == c)*X[i,j]
top = X[i,j]*exp(dot(X[i,:],W[:,c]))
bottom = 0
for s in 1:k
bottom += exp(dot(X[i,:],W[:,s]))
end
m += term1 + (top/bottom)
end
#@printf("Found g[%d,%d]\n",j,c)
g[j,c] = m
end
end
return (f,g[:])
end
# Multi-class one-vs-all version (assumes y_i in {1,2,...,k})
function logRegOnevsAll(X,y)
(n,d) = size(X)
k = maximum(y)
# Each column of 'w' will be a logistic regression classifier
W = zeros(d,k)
for c in 1:k
yc = ones(n,1) # Treat class 'c' as +1
yc[y .!= c] = -1 # Treat other classes as -1
# Each binary objective has the same features but different labels
funObj(w) = logisticObj(w,X,yc)
W[:,c] = findMin(funObj,W[:,c],verbose=false)
end
# Make linear prediction function
predict(Xhat) = mapslices(indmax,Xhat*W,2)
return LinearModel(predict,W)
end