-
Notifications
You must be signed in to change notification settings - Fork 0
/
gridsearch.py
152 lines (128 loc) · 4.69 KB
/
gridsearch.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
import numpy as np
import plotly
from sklearn import svm
from sklearn.linear_model import RidgeClassifier, LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from tqdm import tqdm
from classifier import train, test
from dataset import FaceDataset
def find_svm_hyperparams():
"""
Finds best SVM hyperparams based on evaluating on test split
"""
NUM_ITERS = 10
# coefs = np.arange(-5, 5).astype(np.float)
coefs = np.linspace(0.25, 1, 10)
Cs = np.power(2, coefs)
results = []
for _ in range(NUM_ITERS):
data = FaceDataset("embeddings/dev", n=50)
train_data, train_labels = data.train()
test_data, test_labels = data.test()
accs = []
for c in tqdm(Cs):
clf = svm.SVC(kernel="linear", C=c)
clf, _ = train(clf, train_data, train_labels)
acc, _ = test(clf, test_data, test_labels)
accs.append(acc)
results.append(accs)
results = np.mean(results, axis=0)
s = plotly.graph_objs.Scatter(x=Cs, y=results)
plotly.offline.plot([s], filename="svm_linear.html")
print("C={}".format(Cs[np.argmax(results)]))
def find_logistic_regression_hp():
NUM_ITERS = 10
# coefs = np.arange(-5, 5).astype(np.float)
coefs = np.linspace(1, 5, 10)
Cs = np.power(2, coefs)
results = []
for _ in range(NUM_ITERS):
data = FaceDataset("embeddings/dev", n=50)
train_data, train_labels = data.train()
test_data, test_labels = data.test()
accs = []
for c in tqdm(Cs):
clf = LogisticRegression(solver="lbfgs", multi_class="auto", C=c, max_iter=1000)
clf, _ = train(clf, train_data, train_labels)
acc, _ = test(clf, test_data, test_labels)
accs.append(acc)
results.append(accs)
results = np.mean(results, axis=0)
s = plotly.graph_objs.Scatter(x=Cs, y=results)
plotly.offline.plot([s], filename="svm_linear.html")
print("C={}".format(Cs[np.argmax(results)]))
def ridge_regression():
NUM_ITERS = 10
# coefs = np.arange(-5, 0).astype(np.float)
coefs = np.linspace(-10, -5, 10)
As = np.power(2, coefs)
results = []
for _ in range(NUM_ITERS):
data = FaceDataset("embeddings/dev", n=50)
train_data, train_labels = data.train()
test_data, test_labels = data.test()
accs = []
for a in tqdm(As):
clf = RidgeClassifier(alpha=a, solver="lsqr")
clf, _ = train(clf, train_data, train_labels)
acc, _ = test(clf, test_data, test_labels)
accs.append(acc)
results.append(accs)
results = np.mean(results, axis=0)
s = plotly.graph_objs.Scatter(x=As, y=results)
plotly.offline.plot([s], filename="svm_linear.html")
print("A={}".format(As[np.argmax(results)]))
def find_linear_svm_hyperparams():
"""
Fits a linear SVM model
"""
NUM_ITERS = 10
# coefs = np.arange(-5, 5).astype(np.float)
coefs = np.linspace(0.25, 1, 10)
Cs = np.power(2, coefs)
results = []
for _ in range(NUM_ITERS):
data = FaceDataset("embeddings/dev", n=50)
train_data, train_labels = data.train()
test_data, test_labels = data.test()
accs = []
for c in tqdm(Cs):
clf = svm.SVC(kernel="linear", C=c)
clf, _ = train(clf, train_data, train_labels)
acc, _ = test(clf, test_data, test_labels)
accs.append(acc)
results.append(accs)
results = np.mean(results, axis=0)
s = plotly.graph_objs.Scatter(x=Cs, y=results)
plotly.offline.plot([s], filename="svm_linear.html")
print("C={}".format(Cs[np.argmax(results)]))
def find_knn_hyperparams():
"""
Finds best KNN hyperparams
"""
n_neighbors = np.arange(5, 10)
ps = np.arange(1, 10)
results = []
for p in ps:
result = []
for _ in range(10):
data = FaceDataset("embeddings/known", n=50)
train_data, train_labels = data.train()
test_data, test_labels = data.test()
accs = []
for n in n_neighbors:
clf = KNeighborsClassifier(n_neighbors=n, weights="distance", p=p)
clf, _ = train(clf, train_data, train_labels)
acc, _ = test(clf, test_data, test_labels)
accs.append(acc)
result.append(accs)
result = np.mean(result, axis=0)
results.append(result)
plots = []
for i in range(len(ps)):
p = plotly.graph_objs.Scatter(x=n_neighbors, y=results[i], name="p={}".format(ps[i]))
plots.append(p)
plotly.offline.plot(plots, filename="knn.html")
print("C={}".format(n_neighbors[np.argmax(results)]))
if __name__ == '__main__':
gmm()