-
Notifications
You must be signed in to change notification settings - Fork 2
/
run-sgd.py
256 lines (181 loc) · 6.74 KB
/
run-sgd.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
import logging
import random
import numpy as np
from numpy.linalg import norm
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
logging.basicConfig(
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
format="%(asctime)s %(message)s",
handlers=[
logging.StreamHandler()
]
)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def compute_accuracy(preds, gt):
tp = (preds == gt).sum()
return tp / len(preds)
def clean_data(data, labels):
N, D = data.shape
# setup classifier
cls = SGDClassifier(dim=D)
cls.set_params(n_epochs=100, batch_size=100, lr=0.2, dropout=False)
# fit data to model
cls.fit(data, labels, data, labels)
preds, p = cls.predict(data)
idx = np.where(preds != labels)[0]
p_wrong = p[idx]
logger.debug("idx: %s, prob: %s", idx, p_wrong)
# find the index where model prediction is wrong, but it has strong confidence.
_id = np.where((preds != labels) & ((p > 0.8) | (p < 0.2)))[0]
# Remove those elements
new_data = np.delete(data, _id, axis=0)
new_labels = np.delete(labels, _id)
return new_data, new_labels
class SGDClassifier:
def __init__(self, dim=30, n_cls=2, use_bias=True):
# weight of the model
self.w = np.zeros(dim)
# bia param
self.b = 0.0
# number of classes
self.n_cls = n_cls
self.use_bias = use_bias
def set_params(self, batch_size=32, lr=0.01, n_epochs=200, shuffle=True,
weight_penalty=True, alpha=0.001, dropout=False, dropout_p=0.5):
# number of epochs to train
self.n_epochs = n_epochs
# whether to shuffle data after each epoch
self.shuffle = shuffle
# training hyper parameters
self.batch_size = batch_size
self.lr = lr
# whether to use feature dropout
self.dropout = dropout
# the probability for dropout
self.dropout_p = dropout_p
# whether to add weight regularization
self.weight_penalty = weight_penalty
# weight penalty term if regularization is used, aka, weight decay
self.alpha = alpha
def fit(self, xtrain, ytrain, xval, yval):
# train the model for n_epochs
w_best = None
b_best = None
val_acc_best = 0
for i in range(self.n_epochs):
train_loss = 0
n_iter = 0
for x_batch, y_batch in self.__get_batch(xtrain, ytrain):
n_iter += 1
train_loss_batch = self.__compute_loss(x_batch, y_batch)
train_loss += train_loss_batch
# compute and update gradient
self.__compute_gradient(x_batch, y_batch)
val_loss = self.__compute_loss(xval, yval)
val_acc = compute_accuracy(self.predict(xval)[0], yval)
# best model checkpointing
if val_acc > val_acc_best:
val_acc_best = val_acc
w_best = self.w
b_best = self.b
logger.info("epoch: %s, train loss: %s, val loss: %s, val acc: %s",
i, train_loss/n_iter, val_loss, val_acc)
self.w = w_best
self.b = b_best
def __get_batch(self, x, y):
n_samples = x.shape[0]
x, y = x.copy(), y.copy()
idx = list(range(n_samples))
# shuffle the data
if self.shuffle:
random.shuffle(idx)
# get a batch of data
for i in range(0, n_samples, self.batch_size):
batch_idx = idx[i:i+self.batch_size]
xbatch, ybatch = x[batch_idx], y[batch_idx]
if self.dropout:
xbatch = self.__dropout(xbatch)
yield xbatch, ybatch
def __dropout(self, x):
"""
dropout for a batch of training samples
"""
# note that dropout is for each sample in the batch.
p = np.random.random(x.shape)
self.dropout_mask = np.where(p <= self.dropout_p, 0, 1)
# element-wise multiplication
x_new = x * self.dropout_mask / (1 - self.dropout_p)
return x_new
def predict(self, x):
# probability output
prob = 1 / (np.exp(-1* (np.dot(self.w, x.T) + self.b)) + 1)
y_pred = np.where(prob <= 0.5, 0, 1)
return y_pred, prob
def __compute_loss(self, x, y):
prob = 1 / (np.exp(-1* (np.dot(self.w, x.T) + self.b)) + 1)
# to deal with nan for np.log()
epsilon = 1e-8
prob[np.isclose(prob, 0.0)] = epsilon
prob[np.isclose(prob, 1.0)] = 1-epsilon
logger.debug("prob: %s", prob)
loss = -np.mean(y * np.log(prob) + (1-y) * np.log(1-prob)) + 0.5 * self.alpha * norm(self.w)
return loss
def __compute_gradient(self, x, y):
# compute gradient over the batch
prob = 1 / (np.exp(-1* (np.dot(self.w, x.T) + self.b)) + 1)
n = x.shape[0]
delta_w = 1.0 / n * np.dot((prob - y).T, x) + self.alpha * self.w
delta_b = np.mean(prob - y)
# update parameters
self.w -= self.lr * delta_w
self.b -= self.lr * delta_b
def data_aug(x, y):
"""
Augment the data
"""
idx0 = np.where(y == 0)[0]
idx1 = np.where(y == 1)[0]
logger.debug(f"cls 0: %s, cls 1: %s", len(idx0), len(idx1))
diff = len(idx1) - len(idx0)
_idx = random.sample(list(idx0), k=diff)
x_sampled = x[_idx]
y_sampled = np.zeros(diff, dtype=np.int64)
x_new = np.concatenate([x, x_sampled], axis=0)
y_new = np.concatenate([y, y_sampled])
# shuffle the data to make it random ordered
n = x_new.shape[0]
idx = list(range(n))
random.shuffle(idx)
x_new = x_new[idx]
y_new = y_new[idx]
return x_new, y_new
def run_training():
# load dataset from scikit-learn
data, labels = load_breast_cancer(return_X_y=True)
N, D = data.shape
print(f"N={N}, D={D}")
# normalize the features
scaler = StandardScaler()
data = scaler.fit_transform(data)
# clean the data
data, labels = clean_data(data, labels)
logger.debug("sample num: %s", data.shape[0])
# split the data into train and val
x_train, x_val, y_train, y_val = train_test_split(data, labels, test_size=0.2, random_state=2022)
# augment the data
x_train, y_train = data_aug(x_train, y_train)
# setup classifier
cls = SGDClassifier(dim=D)
cls.set_params(n_epochs=100, batch_size=32, lr=0.05, dropout=True)
# fit data to model
cls.fit(x_train, y_train, x_val, y_val)
# do prediction
preds, _ = cls.predict(x_val)
acc = compute_accuracy(preds, y_val)
logger.info("Validtion set num: %s, model accuracy: %s", len(y_val), acc*100)
if __name__ == '__main__':
run_training()