-
Notifications
You must be signed in to change notification settings - Fork 4
/
diff_methods.py
162 lines (133 loc) · 6.88 KB
/
diff_methods.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
from numpy import add
import torch
from collections import defaultdict
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.utils._testing import ignore_warnings
# mean diff
def mean_diff(activations, labels, eval_activations, eval_labels):
means, counts = {}, defaultdict(int)
# accumulate
for activation, label in zip(activations, labels):
if label not in means:
means[label] = torch.zeros_like(activation)
means[label] += activation
counts[label] += 1
# calc means
for k in means:
means[k] /= counts[k]
# make vector
vecs = list(means.values())
vec = vecs[1] - vecs[0]
return vec / torch.norm(vec), None
@ignore_warnings(category=Warning)
def kmeans_diff(activations, labels, eval_activations, eval_labels):
# fit kmeans
kmeans = KMeans(n_clusters=2, random_state=0, n_init=10).fit(activations)
# make vector
vecs = kmeans.cluster_centers_
vec = torch.tensor(vecs[0] - vecs[1], dtype=torch.float32)
return vec / torch.norm(vec), None
def pca_diff(n_components=1):
def diff_func(activations, labels, eval_activations, eval_labels):
# fit pca
pca = PCA(n_components=n_components).fit(activations)
explained_variance = sum(pca.explained_variance_ratio_)
# average all components
vec = torch.tensor(pca.components_.mean(axis=0), dtype=torch.float32)
return vec / torch.norm(vec), explained_variance
return diff_func
def probe_diff(fit_intercept=False, penalty='l2', solver="lbfgs", C=1.0) -> callable:
@ignore_warnings(category=Warning)
def diff_func(activations, labels, eval_activations, eval_labels):
# fit lr
lr = LogisticRegression(random_state=0, max_iter=1000, l1_ratio=0.5,
fit_intercept=fit_intercept, C=C,
penalty=penalty, solver=solver).fit(activations, labels)
accuracy = lr.score(eval_activations, eval_labels)
# extract weight
vec = torch.tensor(lr.coef_[0], dtype=torch.float32)
return vec / torch.norm(vec), accuracy
return diff_func
def lda_diff(activations, labels, eval_activations, eval_labels):
# fit lda
lda = LinearDiscriminantAnalysis(n_components=1).fit(activations, labels)
accuracy = lda.score(eval_activations, eval_labels)
# extract weight
vec = torch.tensor(lda.coef_[0], dtype=torch.float32)
return vec / torch.norm(vec), accuracy
def random_diff(activations, labels, eval_activations, eval_labels):
vec = torch.randn_like(activations[0])
return vec / torch.norm(vec), None
method_mapping = {
"mean": mean_diff,
"kmeans": kmeans_diff,
"pca": pca_diff(n_components=1),
"lda": lda_diff,
"random": random_diff,
}
probe_mapping = {
"EleutherAI/pythia-14m": [probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-1)],
"EleutherAI/pythia-31m": [probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-2)],
"EleutherAI/pythia-70m": [probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-3)],
"EleutherAI/pythia-160m": [
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-4),
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-5)
],
"EleutherAI/pythia-410m": [
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-4),
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-5)
],
"EleutherAI/pythia-1b": [
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-5),
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-6)
],
"EleutherAI/pythia-1.4b": [
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-5),
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-6)
],
"EleutherAI/pythia-2.8b": [
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-5),
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-6)
],
"EleutherAI/pythia-6.9b": [
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-6),
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-7)
],
"EleutherAI/pythia-12b": [
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-6),
probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1e-7)
],
}
additional_method_mapping = {
# various pca components (up to 5)
"pca_2": pca_diff(n_components=2),
"pca_3": pca_diff(n_components=3),
"pca_4": pca_diff(n_components=4),
"pca_5": pca_diff(n_components=5),
# various linear probe types
"probe_noreg_noint": probe_diff(fit_intercept=False, penalty=None, solver="saga", C=1.0),
"probe_noreg_int": probe_diff(fit_intercept=True, penalty=None, solver="saga", C=1.0),
"probe_l1_noint_1": probe_diff(fit_intercept=False, penalty='l1', solver="saga", C=1.0),
"probe_l2_noint_1": probe_diff(fit_intercept=False, penalty='l2', solver="saga", C=1.0),
"probe_elastic_noint_1": probe_diff(fit_intercept=False, penalty="elasticnet", solver="saga", C=1.0),
"probe_l1_int_1": probe_diff(fit_intercept=True, penalty='l1', solver="saga", C=1.0),
"probe_l2_int_1": probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=1.0),
"probe_elastic_int_1": probe_diff(fit_intercept=True, penalty="elasticnet", solver="saga", C=1.0),
"probe_l1_noint_0.1": probe_diff(fit_intercept=False, penalty='l1', solver="saga", C=0.1),
"probe_l2_noint_0.1": probe_diff(fit_intercept=False, penalty='l2', solver="saga", C=0.1),
"probe_elastic_noint_0.1": probe_diff(fit_intercept=False, penalty="elasticnet", solver="saga", C=0.1),
"probe_l1_int_0.1": probe_diff(fit_intercept=True, penalty='l1', solver="saga", C=0.1),
"probe_l2_int_0.1": probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=0.1),
"probe_elastic_int_0.1": probe_diff(fit_intercept=True, penalty="elasticnet", solver="saga", C=0.1),
"probe_l1_noint_0.001": probe_diff(fit_intercept=False, penalty='l1', solver="saga", C=0.001),
"probe_l2_noint_0.001": probe_diff(fit_intercept=False, penalty='l2', solver="saga", C=0.001),
"probe_elastic_noint_0.001": probe_diff(fit_intercept=False, penalty="elasticnet", solver="saga", C=0.001),
"probe_l1_int_0.001": probe_diff(fit_intercept=True, penalty='l1', solver="saga", C=0.001),
"probe_l2_int_0.001": probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=0.001),
"probe_elastic_int_0.001": probe_diff(fit_intercept=True, penalty="elasticnet", solver="saga", C=0.001),
"probe_l2_int_0.01": probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=0.01),
"probe_l2_int_0.0001": probe_diff(fit_intercept=True, penalty='l2', solver="saga", C=0.0001),
}