-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogistic.py
339 lines (261 loc) · 9.64 KB
/
logistic.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from __future__ import division, print_function
import numpy as np
import scipy as sp
from matplotlib import pyplot as plt
from matplotlib import cm
def binary_train(X, y, w0=None, b0=None, step_size=0.5, max_iterations=1000):
"""
Inputs:
- X: training features, a N-by-D numpy array, where N is the
number of training points and D is the dimensionality of features
- y: binary training labels, a N dimensional numpy array where
N is the number of training points, indicating the labels of
training data
- step_size: step size (learning rate)
Returns:
- w: D-dimensional vector, a numpy array which is the weight
vector of logistic regression
- b: scalar, which is the bias of logistic regression
Find the optimal parameters w and b for inputs X and y.
"""
N, D = X.shape
assert len(np.unique(y)) == 2
w = np.zeros(D)
if w0 is not None:
w = w0
b = 0
if b0 is not None:
b = b0
w_hat=np.insert(w, 0, [b])
for i in range(max_iterations):
sum_error = np.zeros(D+1)
for j in range(len(X)):
x_hat=np.insert(X[j], 0, [1])
error=(sigmoid(np.dot(w_hat, x_hat))-y[j])*x_hat
sum_error=sum_error + error
if(np.any(sum_error)==False):
break
w_hat=w_hat-(step_size*sum_error)/N
b=w_hat[0]
w=np.delete(w_hat, 0)
assert w.shape == (D,)
return w, b
def binary_predict(X, w, b):
"""
Inputs:
- X: testing features, a N-by-D numpy array, where N is the
number of training points and D is the dimensionality of features
Returns:
- preds: N dimensional vector of binary predictions: {0, 1}
"""
N, D = X.shape
preds = np.zeros(N)
a=np.matmul(X,w)+b
for i in range(len(a)):
if(sigmoid(a[i])>0.5):
preds[i]=1
else:
preds[i]=0
assert preds.shape == (N,)
return preds
def multinomial_train(X, y, C,
w0=None,
b0=None,
step_size=0.5,
max_iterations=1000):
"""
Inputs:
- X: training features, a N-by-D numpy array, where N is the
number of training points and D is the dimensionality of features
- y: multiclass training labels, a N dimensional numpy array where
N is the number of training points, indicating the labels of
training data
- C: number of classes in the data
- step_size: step size (learning rate)
- max_iterations: maximum number for iterations to perform
Returns:
- w: C-by-D weight matrix of multinomial logistic regression, where
C is the number of classes and D is the dimensionality of features.
- b: bias vector of length C, where C is the number of classes
"""
N, D = X.shape
w = np.zeros((C, D))
if w0 is not None:
w = w0
b = np.zeros(C)
if b0 is not None:
b = b0
encoded = list()
for value in y:
code = [0 for _ in range(C)]
code[value] = 1
encoded.append(code)
y_onehot_encoded = np.asarray(encoded)
w_hat=np.insert(w, 0, b, axis=1)
X_hat=np.insert(X, 0, 1, axis=1)
for iter in range(max_iterations):
sum_error = np.zeros((C,D+1))
wx_product=np.matmul(w_hat, X_hat.transpose())
temp=np.exp(wx_product - wx_product.max(axis=0))
softmax=temp/temp.sum(axis=0)
error=np.matmul((softmax-y_onehot_encoded.transpose()), X_hat)
sum_error=sum_error + error
if(np.any(sum_error)==False):
break
w_hat=w_hat-(step_size*sum_error)/N
b=w_hat[:,0]
w=np.delete(w_hat, 0, axis=1)
assert w.shape == (C, D)
assert b.shape == (C,)
return w, b
def multinomial_predict(X, w, b):
"""
Inputs:
- X: testing features, a N-by-D numpy array, where N is the
number of training points and D is the dimensionality of features
- w: weights of the trained multinomial classifier
- b: bias terms of the trained multinomial classifier
Returns:
- preds: N dimensional vector of multiclass predictions.
Outputted predictions should be from {0, C - 1}, where
C is the number of classes
Makes predictions for multinomial classifier.
"""
N, D = X.shape
C = w.shape[0]
preds = np.zeros(N)
w_hat=np.insert(w, 0, b, axis=1)
X_hat=np.insert(X, 0, 1, axis=1)
product=np.matmul(X_hat, w_hat.transpose())
for i in range(len(product)):
preds[i]=np.argmax(product[i])
assert preds.shape == (N,)
return preds
def OVR_train(X, y, C, w0=None, b0=None, step_size=0.5, max_iterations=1000):
"""
Inputs:
- X: training features, a N-by-D numpy array, where N is the
number of training points and D is the dimensionality of features
- y: multiclass training labels, a N dimensional numpy array,
indicating the labels of each training point
- C: number of classes in the data
- w0: initial value of weight matrix
- b0: initial value of bias term
- step_size: step size (learning rate)
- max_iterations: maximum number of iterations for gradient descent
Returns:
- w: a C-by-D weight matrix of OVR logistic regression
- b: bias vector of length C
Implemented multiclass classification using binary classifier and
one-versus-rest strategy. OVR classifier is
trained by training C different classifiers.
"""
N, D = X.shape
w = np.zeros((C, D))
if w0 is not None:
w = w0
b = np.zeros(C)
if b0 is not None:
b = b0
"""
TODO: add your code here
"""
y_ovr=np.zeros(N)
for c in range(C):
for i in range(len(y)):
if(y[i]==c):
y_ovr[i]=1
else:
y_ovr[i]=0
wc,bc=binary_train(X, y_ovr, w0, b0, step_size, max_iterations)
w[c]=wc
b[c]=bc
assert w.shape == (C, D), 'wrong shape of weights matrix'
assert b.shape == (C,), 'wrong shape of bias terms vector'
return w, b
def OVR_predict(X, w, b):
"""
Inputs:
- X: testing features, a N-by-D numpy array, where N is the
number of training points and D is the dimensionality of features
- w: weights of the trained OVR model
- b: bias terms of the trained OVR model
Returns:
- preds: vector of class label predictions.
Outputted predictions should be from {0, C - 1}, where
C is the number of classes.
Makes predictions using OVR strategy and predictions from binary
classifier.
"""
N, D = X.shape
C = w.shape[0]
preds = np.zeros(N)
for c in range(C):
temp=binary_predict(X, w[c], b[c])
for i in range(len(temp)):
if(temp[i]==1):
preds[i]=c
assert preds.shape == (N,)
return preds
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def accuracy_score(true, preds):
return np.sum(true == preds).astype(float) / len(true)
def run_binary():
from data_loader import toy_data_binary, data_loader_mnist
print('Performing binary classification on synthetic data')
X_train, X_test, y_train, y_test = toy_data_binary()
w, b = binary_train(X_train, y_train)
train_preds = binary_predict(X_train, w, b)
preds = binary_predict(X_test, w, b)
print('train acc: %f, test acc: %f' %
(accuracy_score(y_train, train_preds),
accuracy_score(y_test, preds)))
print('Performing binary classification on binarized MNIST')
X_train, X_test, y_train, y_test = data_loader_mnist()
binarized_y_train = [0 if yi < 5 else 1 for yi in y_train]
binarized_y_test = [0 if yi < 5 else 1 for yi in y_test]
w, b = binary_train(X_train, binarized_y_train)
train_preds = binary_predict(X_train, w, b)
preds = binary_predict(X_test, w, b)
print('train acc: %f, test acc: %f' %
(accuracy_score(binarized_y_train, train_preds),
accuracy_score(binarized_y_test, preds)))
def run_multiclass():
from data_loader import toy_data_multiclass_3_classes_non_separable, \
toy_data_multiclass_5_classes, \
data_loader_mnist
datasets = [(toy_data_multiclass_3_classes_non_separable(),
'Synthetic data', 3),
(toy_data_multiclass_5_classes(), 'Synthetic data', 5),
(data_loader_mnist(), 'MNIST', 10)]
for data, name, num_classes in datasets:
print('%s: %d class classification' % (name, num_classes))
X_train, X_test, y_train, y_test = data
print('One-versus-rest:')
w, b = OVR_train(X_train, y_train, C=num_classes)
train_preds = OVR_predict(X_train, w=w, b=b)
preds = OVR_predict(X_test, w=w, b=b)
print('train acc: %f, test acc: %f' %
(accuracy_score(y_train, train_preds),
accuracy_score(y_test, preds)))
print('Multinomial:')
w, b = multinomial_train(X_train, y_train, C=num_classes)
train_preds = multinomial_predict(X_train, w=w, b=b)
preds = multinomial_predict(X_test, w=w, b=b)
print('train acc: %f, test acc: %f' %
(accuracy_score(y_train, train_preds),
accuracy_score(y_test, preds)))
if __name__ == '__main__':
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument("--type", )
parser.add_argument("--output")
args = parser.parse_args()
if args.output:
sys.stdout = open(args.output, 'w')
if not args.type or args.type == 'binary':
run_binary()
if not args.type or args.type == 'multiclass':
run_multiclass()