-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobes.py
164 lines (132 loc) · 3.77 KB
/
probes.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
import torch.nn as nn
from sklearn.linear_model import LogisticRegression
PROBE_TYPES = {'cnn', 'linear', 'mlp'}
LINEAR_PROBES = {'logreg'}
def get_default_probe_name(probe_type):
if probe_type == 'cnn':
return 's'
elif probe_type == 'linear':
return 'logreg'
elif probe_type == 'mlp':
return 's'
else:
print(f'Use probe_type from {PROBE_TYPES}')
return None
def build_cnn_probe(probe_name, n_tokens):
if probe_name not in CNN_CLASS:
print(f'For cnn probing model, use name from {CNN_CLASS.keys()}')
return None
return CNN_CLASS[probe_name](n_tokens)
def build_linear_probe(probe_name, seed):
if probe_name == 'logreg':
return LogisticRegression(random_state=seed, max_iter=1000)
else:
print(f'For linear probing model, use name from {LINEAR_PROBES}')
return None
def build_mlp_probe(probe_name):
if probe_name not in MLP_CLASS:
print(f'For mlp probing model, use name from {MLP_CLASS.keys()}')
return None
return MLP_CLASS[probe_name]()
def probe_factory(probe_type, probe_name, n_tokens, seed):
if probe_type == 'cnn':
return build_cnn_probe(probe_name, n_tokens)
if probe_type == 'linear':
return build_linear_probe(probe_name, seed)
elif probe_type == 'mlp':
return build_mlp_probe(probe_name)
else:
print(f'Use probe_type from {PROBE_TYPES}')
return None
class CNN_S(nn.Sequential):
def __init__(self, n_tokens):
super().__init__(
nn.Conv1d(in_channels=n_tokens, out_channels=1, kernel_size=1),
nn.Flatten(),
nn.Linear(128, 16),
nn.BatchNorm1d(16),
nn.ReLU(),
nn.Linear(16, 1),
nn.Sigmoid(),
)
CNN_CLASS = {
's': CNN_S
}
class MLP_S(nn.Sequential):
def __init__(self):
super().__init__(
nn.Linear(128, 16),
nn.BatchNorm1d(16),
nn.ReLU(),
nn.Linear(16, 1),
nn.Sigmoid(),
)
class MLP_M(nn.Sequential):
def __init__(self):
super().__init__(
nn.Linear(128, 32),
nn.BatchNorm1d(32),
nn.ReLU(),
nn.Linear(32, 8),
nn.BatchNorm1d(8),
nn.ReLU(),
nn.Linear(8, 1),
nn.Sigmoid(),
)
class MLP_L(nn.Sequential):
def __init__(self):
super().__init__(
nn.Linear(128, 256),
nn.BatchNorm1d(256),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(256, 64),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(64, 8),
nn.BatchNorm1d(8),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(8, 1),
nn.Sigmoid(),
)
class MLP_XL(nn.Sequential):
def __init__(self):
super().__init__(
nn.Linear(128, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(512, 16),
nn.BatchNorm1d(16),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(16, 1),
nn.Sigmoid(),
)
class MLP_XXL(nn.Sequential):
def __init__(self):
super().__init__(
nn.Linear(128, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(512, 64),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(64, 8),
nn.BatchNorm1d(8),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(8, 1),
nn.Sigmoid(),
)
MLP_CLASS = {
's': MLP_S,
'm': MLP_M,
'l': MLP_L,
'xl': MLP_XL,
'xxl': MLP_XXL,
}