-
Notifications
You must be signed in to change notification settings - Fork 6
/
properties.py
341 lines (303 loc) · 9.93 KB
/
properties.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
340
341
import math
import pandas as pd
def generate_property_data(
prop,
counter_section,
train_base,
test_base,
train_counterexample,
test_counterexample,
section_size,
rates,
test_section_size: int = None,
):
"""See `gap.py` for an example use case.
Parameters
----------
``prop``: string
The name of the prop
``counter_section``: str
The section of counterexample data. It should be either `strong` or `weak`.
TODO: Refactor to allow both strong and weak counter example.
``train_base``: pd.DataFrame
both/neither training data
``test_base``: pd.DataFrame
strong/weak test data
``train_counterexample``: pd.DataFrame
Counterexample training data
``test_counterexample``: pd.DataFrame
Counterexample test data
``section_size``: int
The number of examples from each split.
``rates``: List[float]
The rates to be generated
``test_section_size``: Optional(int), default = None
The number of examples from each split (for testing).
If None, set to the section size.
NOTES
-----
1. data format is `.tsv`
The data is a `.tsv` format: with a `sentence`, `section` and `label` column.
The `sentence` is the sentence, the `section` is one of (neither, both, weak, strong),
and the `label` is 0 or 1. This allows us to use the same pipeline for the probing and finetuning.
```
# This is an example. Any additional columns are no problem and will be tracked/kept together,
# esp. with the test data for analysis.
sentence section acceptable template parenthetical_count clause_count label
Guests hoped who guests determined him last week neither no S_wh_no_gap 0 1 0
Teachers believe who you held before the trial both yes S_wh_gap 0 1 1
You think that guests determined that visitors recommended someone over the summer both yes S_that_no_gap 0 2 1
Professors believe that professors loved over the summer neither no S_that_gap 0 1 0
```
2. data files are saved as
```
# finetune
path = f"{task}_{rate}"
# probing
path = f"{task}_{feature}"
"./properties/{prop}/{path}_train.tsv"
"./properties/{prop}/{path}_val.tsv"
"./properties/{prop}/test.tsv"
```
"""
if test_section_size is None:
test_section_size = section_size
# Weak probing.
if counter_section == "weak":
# Neither vs Weak
target_section = "weak"
other_section = "neither"
weak_probing_train = probing_split(
train_base,
train_counterexample,
section_size,
target_section,
other_section,
)
weak_probing_test = probing_split(
test_base,
test_counterexample,
test_section_size,
target_section,
other_section,
)
weak_probing_train.to_csv(
f"./properties/{prop}/probing_weak_train.tsv", index=False, sep="\t"
)
weak_probing_test.to_csv(
f"./properties/{prop}/probing_weak_val.tsv", index=False, sep="\t"
)
else:
# Both vs Strong
target_section = "both"
other_section = "strong"
weak_probing_train = probing_split(
train_base,
train_counterexample,
section_size,
target_section,
other_section,
)
weak_probing_test = probing_split(
test_base,
test_counterexample,
test_section_size,
target_section,
other_section,
)
weak_probing_train.to_csv(
f"./properties/{prop}/probing_weak_train.tsv", index=False, sep="\t"
)
weak_probing_test.to_csv(
f"./properties/{prop}/probing_weak_val.tsv", index=False, sep="\t"
)
# Strong probing.
if counter_section == "strong":
# Neither vs Strong
target_section = "strong"
other_section = "neither"
strong_probing_train = probing_split(
train_base,
train_counterexample,
section_size,
target_section,
other_section,
)
strong_probing_test = probing_split(
test_base,
test_counterexample,
test_section_size,
target_section,
other_section,
)
strong_probing_train.to_csv(
f"./properties/{prop}/probing_strong_train.tsv", index=False, sep="\t"
)
strong_probing_test.to_csv(
f"./properties/{prop}/probing_strong_val.tsv", index=False, sep="\t"
)
else:
# Both vs Strong
target_section = "both"
other_section = "weak"
strong_probing_train = probing_split(
train_base,
train_counterexample,
section_size,
target_section,
other_section,
)
strong_probing_test = probing_split(
test_base,
test_counterexample,
test_section_size,
target_section,
other_section,
)
strong_probing_train.to_csv(
f"./properties/{prop}/probing_strong_train.tsv", index=False, sep="\t"
)
strong_probing_test.to_csv(
f"./properties/{prop}/probing_strong_val.tsv", index=False, sep="\t"
)
# set up fine-tuning.
for rate in rates:
finetune_train = finetune_split(
train_base,
train_counterexample,
# We keep the probing and finetune set sizes the same, even though we#
# could make the finetuning bigger.
2 * section_size,
rate,
)
finetune_val = finetune_split(
test_base,
test_counterexample,
# We keep the probing and finetune set sizes the same, even though we#
# could make the finetuning bigger.
2 * test_section_size,
rate,
)
finetune_train.to_csv(
f"./properties/{prop}/finetune_{rate}_train.tsv",
index=False,
sep="\t",
)
finetune_val.to_csv(
f"./properties/{prop}/finetune_{rate}_val.tsv",
index=False,
sep="\t",
)
# save test.
test = pd.concat([test_base, test_counterexample])
test.to_csv(f"./properties/{prop}/test.tsv", index=False, sep="\t")
def generate_property_data_strong_direct(
prop,
counter_section,
train_base,
test_base,
train_counterexample,
test_counterexample,
section_size,
rates,
test_section_size: int = None,
):
if test_section_size is None:
test_section_size = section_size
# Neither vs Strong
target_section = "strong"
other_section = "neither"
strong_probing_train = probing_split(
train_base,
train_counterexample,
section_size,
target_section,
other_section,
)
strong_probing_test = probing_split(
test_base,
test_counterexample,
test_section_size,
target_section,
other_section,
)
strong_probing_train.to_csv(
f"./properties/{prop}/probing_strong_direct_train.tsv", index=False, sep="\t"
)
strong_probing_test.to_csv(
f"./properties/{prop}/probing_strong_direct_val.tsv", index=False, sep="\t"
)
def probing_split(
train_base,
test_base,
train_counterexample,
test_counterexample,
section_size,
target_section,
other_section,
):
"""Generate a split for probing target_section vs other_section where
target_section is set as the positive section.
"""
def filter_sample(df, section):
return df[df.section == section].sample(section_size)
train_data = pd.concat([train_base, train_counterexample])
test_data = pd.concat([test_base, test_counterexample])
train = pd.concat(
[
filter_sample(train_data, other_section),
filter_sample(train_data, target_section),
]
)
test = pd.concat(
[
filter_sample(test_data, other_section),
filter_sample(test_data, target_section),
]
)
train["label"] = (train.section == target_section).astype(int)
test["label"] = (test.section == target_section).astype(int)
train["label_str"] = train["label"].apply(lambda x: {0: "False", 1: "True"}[x])
test["label_str"] = test["label"].apply(lambda x: {0: "False", 1: "True"}[x])
return train, test
def probing_split(
base,
counterexample,
section_size,
target_section,
other_section,
):
"""Generate a split for probing target_section vs other_section where
target_section is set as the positive section.
"""
def filter_sample(df, section):
return df[df.section == section].sample(section_size)
data = pd.concat([base, counterexample])
data = pd.concat(
[
filter_sample(data, other_section),
filter_sample(data, target_section),
]
)
data["label"] = (data.section == target_section).astype(int)
return data
def get_config(config_path):
section_to_configs = {"both": [], "neither": [], "weak": [], "strong": []}
try:
with open(config_path, "r") as f:
df = pd.read_csv(f)
df_as_dict = df.to_dict(orient="records")
for config in df_as_dict:
section = config["section"]
section_to_configs[section].append(config)
except OSError as e:
print("No config file for this template.")
raise (e)
return section_to_configs
def finetune_split(base, counterexample, total_size, rate):
size_base, size_target = (
math.floor(total_size * (1.0 - rate)),
math.ceil(total_size * rate),
)
finetune = pd.concat([base.sample(size_base), counterexample.sample(size_target)])
return finetune