-
Notifications
You must be signed in to change notification settings - Fork 23
/
cli.py
349 lines (262 loc) · 12.2 KB
/
cli.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
342
343
344
345
346
347
348
349
"""
Run experiments for paper results
- for each experiment: sub-task A + B
- save model weights to disk
- save val results to disk
/experiments/
bert_extras/
weights/
config/
validation/
stats/
export BERT_MODELS_DIR="/home/mostendorff/datasets/BERT_pre_trained_models/pytorch"
python cli.py run_on_val <name> $GPU_ID $EXTRAS_DIR $TRAIN_DF_PATH $VAL_DF_PATH $OUTPUT_DIR --epochs 5
4 \
data/extras \
germeval_train_df_meta.pickle \
germeval_val_df_meta.pickle \
experiments_output
python experiments.py run task-a__bert-german_manual_author-embedding_author-gender \
4 \
data/extras \
germeval_train_df_meta.pickle \
germeval_val_df_meta.pickle \
experiments_output
python experiments.py run task-a__bert-german_manual_no-embedding_author-gender \
4 \
data/extras \
germeval_train_df_meta.pickle \
germeval_val_df_meta.pickle \
experiments_output
python experiments.py run task-a__bert-german_manual_no-embedding_author-gender \
4 \
data/extras \
germeval_train_df_meta.pickle \
germeval_val_df_meta.pickle \
experiments_output
python experiments.py run task-a__bert-german_text-only \
4 \
data/extras \
germeval_train_df_meta.pickle \
germeval_val_df_meta.pickle \
experiments_output
python experiments.py run task-a__author-only \
4 \
data/extras \
germeval_train_df_meta.pickle \
germeval_val_df_meta.pickle \
experiments_output
python experiments.py run task-b__author-only \
4 \
data/extras \
germeval_train_df_meta.pickle \
germeval_val_df_meta.pickle \
experiments_output
python experiments.py run task-b__bert-german_full \
4 \
data/extras \
germeval_train_df_meta.pickle \
germeval_val_df_meta.pickle \
experiments_output \
--epochs 5
----
python experiments.py final task-a__bert-german_full \
4 \
data/extras \
germeval_fulltrain_df_meta.pickle \
germeval_test_df_meta.pickle \
experiments_output \
--epochs 1
python experiments.py final task-b__bert-german_full 3 data/extras germeval_fulltrain_df_meta.pickle germeval_test_df_meta.pickle experiments_output --epochs 5
python experiments.py final task-a__bert-german_text-only 2 data/extras germeval_fulltrain_df_meta.pickle germeval_test_df_meta.pickle experiments_output --epochs 5
python experiments.py final task-b__bert-german_full 2 data/extras germeval_fulltrain_df_meta.pickle germeval_test_df_meta.pickle experiments_output --epochs 5
"""
import json
import os
import pickle
import numpy as np
import fire
import torch
import logging
from torch.optim import Adam
from sklearn.metrics import classification_report
from config import AUTHOR_DIM, LEARNING_RATE, TASK_A_LABELS_COUNT, TASK_B_LABELS_COUNT, most_popular_label
from data_utils import get_best_thresholds, nn_output_to_submission
from experiment import Experiment
from models import LinearMultiClassifier
logging.basicConfig(level=logging.INFO)
# Define experiments
experiments = {
########## A
'task-a__bert-german_full': Experiment(
'a', 'bert-base-german-cased', with_text=True, with_author_gender=True, with_manual=True, with_author_vec=True
),
'task-a__bert-german_full_2': Experiment(
'a', 'bert-base-german-cased', with_text=True, with_author_gender=True, with_manual=True, with_author_vec=True, mlp_dim=500,
),
'task-a__bert-german_manual_no-embedding': Experiment(
'a', 'bert-base-german-cased', with_text=True, with_author_gender=True, with_manual=True, with_author_vec=False
),
'task-a__bert-german_no-manual_embedding': Experiment(
'a', 'bert-base-german-cased', with_text=True, with_author_gender=False, with_manual=False, with_author_vec=True
),
'task-a__bert-german_text-only': Experiment(
'a', 'bert-base-german-cased', with_text=True, with_author_gender=False, with_manual=False, with_author_vec=False
),
# author only
'task-a__author-only': Experiment(
'a', '-', with_text=False, with_author_gender=False, with_manual=False, with_author_vec=True,
classifier_model=LinearMultiClassifier(
labels_count=TASK_A_LABELS_COUNT,
extras_dim=AUTHOR_DIM,
)
),
# bert-base-multilingual-cased
'task-a__bert-multilingual_text-only': Experiment(
'a', 'bert-base-multilingual-cased', with_text=True, with_author_gender=False, with_manual=False, with_author_vec=False
),
##### B
'task-b__bert-german_full': Experiment(
'b', 'bert-base-german-cased', with_text=True, with_author_gender=True, with_manual=True, with_author_vec=True
),
'task-b__bert-german_manual_no-embedding': Experiment(
'b', 'bert-base-german-cased', with_text=True, with_author_gender=True, with_manual=True, with_author_vec=False
),
'task-b__bert-german_no-manual_embedding': Experiment(
'b', 'bert-base-german-cased', with_text=True, with_author_gender=False, with_manual=False, with_author_vec=True
),
'task-b__bert-german_text-only': Experiment(
'b', 'bert-base-german-cased', with_text=True, with_author_gender=False, with_manual=False, with_author_vec=False
),
# author only
'task-b__author-only': Experiment(
'b', '-', with_text=False, with_author_gender=False, with_manual=False, with_author_vec=True,
classifier_model=LinearMultiClassifier(
labels_count=TASK_B_LABELS_COUNT,
extras_dim=AUTHOR_DIM,
)
),
# bert-base-multilingual-cased
'task-b__bert-multilingual_text-only': Experiment(
'b', 'bert-base-multilingual-cased', with_text=True, with_author_gender=False, with_manual=False, with_author_vec=False
),
######
# switch does not work
'task-a__bert-german_full-switch': Experiment(
'a', 'bert-base-german-cased', with_text=True, with_author_gender=True, with_manual=True, with_author_vec=True,
author_vec_switch=True,
),
# manual and gender goes only together (for paper)
# 'task-a__bert-german_manual_no-embedding_no-gender': Experiment(
# 'a', 'bert-base-german-cased', with_text=True, with_author_gender=False, with_manual=True, with_author_vec=False
# ),
}
def run_on_val(name, cuda_device, extras_dir, df_train_path, df_val_path, output_dir, epochs=None, continue_training=False,
batch_size=None):
if name not in experiments:
print(f'Experiment not found: {name}')
exit(1)
experiment = experiments[name]
experiment.name = name
experiment.output_dir = output_dir
experiment.init(cuda_device, epochs, batch_size, continue_training)
train_dataloader, val_dataloader, vec_found_selector, val_df, val_y = experiment.prepare_data_loaders(df_train_path, df_val_path, extras_dir)
model = experiment.get_model()
print(f'Using model: {type(model).__name__}')
# Load existing model weights
if continue_training:
print('Loading existing model weights...')
model.load_state_dict(torch.load(os.path.join(experiment.get_output_dir(), 'model_weights')))
# Training
optimizer = Adam(model.parameters(), lr=LEARNING_RATE)
# Model to GPU
model = model.cuda()
experiment.train(model, optimizer, train_dataloader)
# Validation
output_ids, outputs = experiment.eval(model, val_dataloader)
t_max, f_max = get_best_thresholds(experiment.labels, val_y, outputs, plot=False)
report = classification_report(val_y, np.where(outputs>t_max, 1, 0), target_names=experiment.labels, output_dict=True)
report_str = classification_report(val_y, np.where(outputs > t_max, 1, 0), target_names=experiment.labels)
if vec_found_selector is not None and len(vec_found_selector) > 0:
try:
report_author_vec = classification_report(val_y[vec_found_selector], np.where(outputs[vec_found_selector] > t_max, 1, 0), target_names=experiment.labels, output_dict=True)
report_author_vec_str = classification_report(val_y[vec_found_selector], np.where(outputs[vec_found_selector] > t_max, 1, 0), target_names=experiment.labels)
except BaseException:
print('Cannot report author_vec_found')
# Save
with open(os.path.join(experiment.get_output_dir(), 'report.json'), 'w') as f:
json.dump(report, f)
with open(os.path.join(experiment.get_output_dir(), 'report.txt'), 'w') as f:
f.write(report_str)
if vec_found_selector is not None and len(vec_found_selector) > 0:
try:
with open(os.path.join(experiment.get_output_dir(), 'report_author_vec_found.json'), 'w') as f:
json.dump(report_author_vec, f)
with open(os.path.join(experiment.get_output_dir(), 'report_author_vec_found.txt'), 'w') as f:
f.write(report_author_vec_str)
except BaseException:
print('Cannot write report_author_vec_found')
with open(os.path.join(experiment.get_output_dir(), 'best_thresholds.csv'), 'w') as f:
f.write(','.join([str(t) for t in t_max]))
with open(os.path.join(experiment.get_output_dir(), 'outputs_with_ids.pickle'), 'wb') as f:
pickle.dump((outputs, output_ids), f)
torch.save(model.state_dict(), os.path.join(experiment.get_output_dir(), 'model_weights'))
with open(os.path.join(experiment.get_output_dir(), 'model_config.json'), 'w') as f:
json.dump(model.config, f)
# Submission
lines, no_label = nn_output_to_submission('subtask_' + experiment.task, val_df, outputs, output_ids, t_max, experiment.labels,
most_popular_label)
print(f'-- no found: {no_label}')
fn = os.path.join(experiment.get_output_dir(), 'submission.txt')
with open(fn, 'w') as f:
f.write('\n'.join(lines))
print(f'Submission file saved to: {fn}')
def run_on_test(name, cuda_device, extras_dir, df_full_path, df_test_path, output_dir, epochs=None, continue_training=False,
batch_size=None):
if name not in experiments:
print(f'Experiment not found: {name}')
exit(1)
experiment = experiments[name]
experiment.name = 'final-' + name
experiment.output_dir = output_dir
experiment.init(cuda_device, epochs, batch_size, continue_training)
# best thresholds from validation set
t_fn = os.path.join(output_dir, name, 'best_thresholds.csv')
if not os.path.exists(t_fn):
raise ValueError('Could not load threshold values')
train_dataloader, test_dataloader, vec_found_selector, test_df, _ = experiment.prepare_data_loaders(df_full_path, df_test_path, extras_dir, test_set=True)
# Parse thresholds
with open(t_fn, 'r') as f:
t_max = [float(t) for t in f.read().split(',')]
if len(t_max) != len(experiment.labels):
raise ValueError('Threshold values does not match label count')
model = experiment.get_model()
print(f'Using model: {type(model).__name__}')
# Load existing model weights
if continue_training:
print('Loading existing model weights...')
model.load_state_dict(torch.load(os.path.join(experiment.get_output_dir(), 'full_model_weights')))
# Training
optimizer = Adam(model.parameters(), lr=LEARNING_RATE)
# Model to GPU
model = model.cuda()
experiment.train(model, optimizer, train_dataloader)
# Save trained model
torch.save(model.state_dict(), os.path.join(experiment.get_output_dir(), 'model_weights'))
with open(os.path.join(experiment.get_output_dir(), 'model_config.json'), 'w') as f:
json.dump(model.config, f)
# Test results
output_ids, outputs = experiment.eval(model, test_dataloader)
# Store predictions
with open(os.path.join(experiment.get_output_dir(), 'outputs_with_ids.pickle'), 'wb') as f:
pickle.dump((outputs, output_ids), f)
# Submission
lines, no_label = nn_output_to_submission('subtask_' + experiment.task, test_df, outputs, output_ids, t_max, experiment.labels,
most_popular_label)
print(f'-- no found: {no_label}')
fn = os.path.join(experiment.get_output_dir(), 'submission.txt')
with open(fn, 'w') as f:
f.write('\n'.join(lines))
print(f'Submission file saved to: {fn}')
if __name__ == '__main__':
fire.Fire()