-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_util.py
616 lines (517 loc) · 22.3 KB
/
model_util.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
"""Basic model training and evaluation functions."""
from enum import Enum
import random
import sys
import json
import progressbar
import model.torch_utils
import data_util.sql_util
import torch
def write_prediction(fileptr,
identifier,
input_seq,
probability,
prediction,
flat_prediction,
gold_query,
flat_gold_queries,
gold_tables,
index_in_interaction,
database_username,
database_password,
database_timeout,
compute_metrics=True):
pred_obj = {}
pred_obj["identifier"] = identifier
if len(identifier.split('/')) == 2:
database_id, interaction_id = identifier.split('/')
else:
database_id = 'atis'
interaction_id = identifier
pred_obj["database_id"] = database_id
pred_obj["interaction_id"] = interaction_id
pred_obj["input_seq"] = input_seq
pred_obj["probability"] = probability
pred_obj["prediction"] = prediction
pred_obj["flat_prediction"] = flat_prediction
pred_obj["gold_query"] = gold_query
pred_obj["flat_gold_queries"] = flat_gold_queries
pred_obj["index_in_interaction"] = index_in_interaction
pred_obj["gold_tables"] = str(gold_tables)
# Now compute the metrics we want.
if compute_metrics:
# First metric: whether flat predicted query is in the gold query set.
correct_string = " ".join(flat_prediction) in [
" ".join(q) for q in flat_gold_queries]
pred_obj["correct_string"] = correct_string
# Database metrics
if not correct_string:
syntactic, semantic, pred_table = sql_util.execution_results(
" ".join(flat_prediction), database_username, database_password, database_timeout)
pred_table = sorted(pred_table)
best_prec = 0.
best_rec = 0.
best_f1 = 0.
for gold_table in gold_tables:
num_overlap = float(len(set(pred_table) & set(gold_table)))
if len(set(gold_table)) > 0:
prec = num_overlap / len(set(gold_table))
else:
prec = 1.
if len(set(pred_table)) > 0:
rec = num_overlap / len(set(pred_table))
else:
rec = 1.
if prec > 0. and rec > 0.:
f1 = (2 * (prec * rec)) / (prec + rec)
else:
f1 = 1.
best_prec = max(best_prec, prec)
best_rec = max(best_rec, rec)
best_f1 = max(best_f1, f1)
else:
syntactic = True
semantic = True
pred_table = []
best_prec = 1.
best_rec = 1.
best_f1 = 1.
assert best_prec <= 1.
assert best_rec <= 1.
assert best_f1 <= 1.
pred_obj["syntactic"] = syntactic
pred_obj["semantic"] = semantic
correct_table = (pred_table in gold_tables) or correct_string
pred_obj["correct_table"] = correct_table
pred_obj["strict_correct_table"] = correct_table and syntactic
pred_obj["pred_table"] = str(pred_table)
pred_obj["table_prec"] = best_prec
pred_obj["table_rec"] = best_rec
pred_obj["table_f1"] = best_f1
fileptr.write(json.dumps(pred_obj) + "\n")
class Metrics(Enum):
"""Definitions of simple metrics to compute."""
LOSS = 1
TOKEN_ACCURACY = 2
STRING_ACCURACY = 3
CORRECT_TABLES = 4
STRICT_CORRECT_TABLES = 5
SEMANTIC_QUERIES = 6
SYNTACTIC_QUERIES = 7
def get_progressbar(name, size):
"""Gets a progress bar object given a name and the total size.
Inputs:
name (str): The name to display on the side.
size (int): The maximum size of the progress bar.
"""
return progressbar.ProgressBar(maxval=size,
widgets=[name,
progressbar.Bar('=', '[', ']'),
' ',
progressbar.Percentage(),
' ',
progressbar.ETA()])
def train_epoch_with_utterances(batches,
model,
randomize=True):
"""Trains model for a single epoch given batches of utterance data.
Inputs:
batches (UtteranceBatch): The batches to give to training.
model (ATISModel): The model obect.
learning_rate (float): The learning rate to use during training.
dropout_amount (float): Amount of dropout to set in the model.
randomize (bool): Whether or not to randomize the order that the batches are seen.
"""
if randomize:
random.shuffle(batches)
progbar = get_progressbar("train ", len(batches))
progbar.start()
loss_sum = 0.
for i, batch in enumerate(batches):
batch_loss = model.train_step(batch)
loss_sum += batch_loss
progbar.update(i)
progbar.finish()
total_loss = loss_sum / len(batches)
return total_loss
def train_epoch_with_interactions(interaction_batches,
params,
model,
randomize=True,
db2id=None,
id2db=None,
step=None):
"""Trains model for single epoch given batches of interactions.
Inputs:
interaction_batches (list of InteractionBatch): The batches to train on.
params (namespace): Parameters to run with.
model (ATISModel): Model to train.
randomize (bool): Whether or not to randomize the order that batches are seen.
"""
if randomize:
random.shuffle(interaction_batches)
progbar = get_progressbar("train ", len(interaction_batches))
progbar.start()
loss_sum = 0.
skip_ls = ["sakila_1", "baseball_1", "soccer_1", "cre_Drama_Workshop_Groups", "formula_1", "assets_maintenance/8"]
skip_num = 0
for i, interaction_batch in enumerate(interaction_batches):
assert len(interaction_batch) == 1
interaction = interaction_batch.items[0]
if interaction.identifier == "raw/atis2/12-1.1/ATIS2/TEXT/TEST/NOV92/770/5":
continue
if 'sparc' in params.data_directory and "baseball_1" in interaction.identifier:
continue
skip = False
if 'cosql' in params.data_directory:
print(interaction.identifier, i, skip_num)
for ele in skip_ls:
if ele in interaction.identifier:
print("skip")
skip = True
continue
if skip:
print("skip, length:", len(interaction.gold_utterances()))
skip_num += 1
continue
batch_loss, step = model.train_step(interaction, params.train_maximum_sql_length, db2id=db2id, id2db=id2db, step=step)
loss_sum += batch_loss
#torch.cuda.empty_cache()
progbar.update(i)
progbar.finish()
total_loss = loss_sum / len(interaction_batches)
return total_loss, step
def update_sums(metrics,
metrics_sums,
predicted_sequence,
flat_sequence,
gold_query,
original_gold_query,
gold_forcing=False,
loss=None,
token_accuracy=0.,
database_username="",
database_password="",
database_timeout=0,
gold_table=None):
"""" Updates summing for metrics in an aggregator.
TODO: don't use sums, just keep the raw value.
"""
if Metrics.LOSS in metrics:
metrics_sums[Metrics.LOSS] += loss.item()
if Metrics.TOKEN_ACCURACY in metrics:
if gold_forcing:
metrics_sums[Metrics.TOKEN_ACCURACY] += token_accuracy
else:
num_tokens_correct = 0.
for j, token in enumerate(gold_query):
if len(
predicted_sequence) > j and predicted_sequence[j] == token:
num_tokens_correct += 1
metrics_sums[Metrics.TOKEN_ACCURACY] += num_tokens_correct / \
len(gold_query)
if Metrics.STRING_ACCURACY in metrics:
metrics_sums[Metrics.STRING_ACCURACY] += int(
flat_sequence == original_gold_query)
if Metrics.CORRECT_TABLES in metrics:
assert database_username, "You did not provide a database username"
assert database_password, "You did not provide a database password"
assert database_timeout > 0, "Database timeout is 0 seconds"
# Evaluate SQL
if flat_sequence != original_gold_query:
syntactic, semantic, table = sql_util.execution_results(
" ".join(flat_sequence), database_username, database_password, database_timeout)
else:
syntactic = True
semantic = True
table = gold_table
metrics_sums[Metrics.CORRECT_TABLES] += int(table == gold_table)
if Metrics.SYNTACTIC_QUERIES in metrics:
metrics_sums[Metrics.SYNTACTIC_QUERIES] += int(syntactic)
if Metrics.SEMANTIC_QUERIES in metrics:
metrics_sums[Metrics.SEMANTIC_QUERIES] += int(semantic)
if Metrics.STRICT_CORRECT_TABLES in metrics:
metrics_sums[Metrics.STRICT_CORRECT_TABLES] += int(
table == gold_table and syntactic)
def construct_averages(metrics_sums, total_num):
""" Computes the averages for metrics.
Inputs:
metrics_sums (dict Metric -> float): Sums for a metric.
total_num (int): Number to divide by (average).
"""
metrics_averages = {}
for metric, value in metrics_sums.items():
metrics_averages[metric] = value / total_num
if metric != "loss":
metrics_averages[metric] *= 100.
return metrics_averages
def evaluate_utterance_sample(sample,
model,
max_generation_length,
name="",
gold_forcing=False,
metrics=None,
total_num=-1,
database_username="",
database_password="",
database_timeout=0,
write_results=False):
"""Evaluates a sample of utterance examples.
Inputs:
sample (list of Utterance): Examples to evaluate.
model (ATISModel): Model to predict with.
max_generation_length (int): Maximum length to generate.
name (str): Name to log with.
gold_forcing (bool): Whether to force the gold tokens during decoding.
metrics (list of Metric): Metrics to evaluate with.
total_num (int): Number to divide by when reporting results.
database_username (str): Username to use for executing queries.
database_password (str): Password to use when executing queries.
database_timeout (float): Timeout on queries when executing.
write_results (bool): Whether to write the results to a file.
"""
assert metrics
if total_num < 0:
total_num = len(sample)
metrics_sums = {}
for metric in metrics:
metrics_sums[metric] = 0.
predictions_file = open(name + "_predictions.json", "w")
print("Predicting with filename " + str(name) + "_predictions.json")
progbar = get_progressbar(name, len(sample))
progbar.start()
predictions = []
for i, item in enumerate(sample):
_, loss, predicted_seq = model.eval_step(
item, max_generation_length, feed_gold_query=gold_forcing)
loss = loss / len(item.gold_query())
predictions.append(predicted_seq)
flat_sequence = item.flatten_sequence(predicted_seq)
token_accuracy = torch_utils.per_token_accuracy(
item.gold_query(), predicted_seq)
if write_results:
write_prediction(
predictions_file,
identifier=item.interaction.identifier,
input_seq=item.input_sequence(),
probability=0,
prediction=predicted_seq,
flat_prediction=flat_sequence,
gold_query=item.gold_query(),
flat_gold_queries=item.original_gold_queries(),
gold_tables=item.gold_tables(),
index_in_interaction=item.utterance_index,
database_username=database_username,
database_password=database_password,
database_timeout=database_timeout)
update_sums(metrics,
metrics_sums,
predicted_seq,
flat_sequence,
item.gold_query(),
item.original_gold_queries()[0],
gold_forcing,
loss,
token_accuracy,
database_username=database_username,
database_password=database_password,
database_timeout=database_timeout,
gold_table=item.gold_tables()[0])
progbar.update(i)
progbar.finish()
predictions_file.close()
return construct_averages(metrics_sums, total_num), None
def evaluate_interaction_sample(sample,
model,
max_generation_length,
name="",
gold_forcing=False,
metrics=None,
total_num=-1,
database_username="",
database_password="",
database_timeout=0,
use_predicted_queries=False,
write_results=False,
use_gpu=False,
compute_metrics=False):
""" Evaluates a sample of interactions. """
predictions_file = open(name + "_predictions.json", "w")
print("Predicting with file " + str(name + "_predictions.json"))
metrics_sums = {}
for metric in metrics:
metrics_sums[metric] = 0.
progbar = get_progressbar(name, len(sample))
progbar.start()
num_utterances = 0
ignore_with_gpu = [line.strip() for line in open(
"data/cpu_full_interactions.txt").readlines()]
predictions = []
use_gpu = not ("--no_gpus" in sys.argv or "--no_gpus=1" in sys.argv)
model.eval()
for i, interaction in enumerate(sample):
# if use_gpu and interaction.identifier in ignore_with_gpu:
# continue
# elif not use_gpu and interaction.identifier not in ignore_with_gpu:
# continue
try:
with torch.no_grad():
if use_predicted_queries:
example_preds = model.predict_with_predicted_queries(
interaction,
max_generation_length)
else:
example_preds = model.predict_with_gold_queries(
interaction,
max_generation_length,
feed_gold_query=gold_forcing)
#torch.cuda.empty_cache()
except RuntimeError as exception:
print("Failed on interaction: " + str(interaction.identifier))
print(exception)
print("\n\n")
exit()
predictions.extend(example_preds)
assert len(example_preds) == len(
interaction.interaction.utterances) or not example_preds
for j, pred in enumerate(example_preds):
num_utterances += 1
sequence, loss, token_accuracy, _, decoder_results = pred
if use_predicted_queries:
item = interaction.processed_utterances[j]
original_utt = interaction.interaction.utterances[item.index]
gold_query = original_utt.gold_query_to_use
original_gold_query = original_utt.original_gold_query
gold_table = original_utt.gold_sql_results
gold_queries = [q[0] for q in original_utt.all_gold_queries]
gold_tables = [q[1] for q in original_utt.all_gold_queries]
index = item.index
else:
item = interaction.gold_utterances()[j]
gold_query = item.gold_query()
original_gold_query = item.original_gold_query()
gold_table = item.gold_table()
gold_queries = item.original_gold_queries()
gold_tables = item.gold_tables()
index = item.utterance_index
if loss:
loss = loss / len(gold_query)
flat_sequence = item.flatten_sequence(sequence)
if write_results:
write_prediction(
predictions_file,
identifier=interaction.identifier,
input_seq=item.input_sequence(),
probability=decoder_results.probability,
prediction=sequence,
flat_prediction=flat_sequence,
gold_query=gold_query,
flat_gold_queries=gold_queries,
gold_tables=gold_tables,
index_in_interaction=index,
database_username=database_username,
database_password=database_password,
database_timeout=database_timeout,
compute_metrics=compute_metrics)
update_sums(metrics,
metrics_sums,
sequence,
flat_sequence,
gold_query,
original_gold_query,
gold_forcing,
loss,
token_accuracy,
database_username=database_username,
database_password=database_password,
database_timeout=database_timeout,
gold_table=gold_table)
progbar.update(i)
progbar.finish()
if total_num < 0:
total_num = num_utterances
predictions_file.close()
return construct_averages(metrics_sums, total_num), predictions
def evaluate_using_predicted_queries(sample,
model,
name="",
gold_forcing=False,
metrics=None,
total_num=-1,
database_username="",
database_password="",
database_timeout=0,
snippet_keep_age=1):
predictions_file = open(name + "_predictions.json", "w")
print("Predicting with file " + str(name + "_predictions.json"))
assert not gold_forcing
metrics_sums = {}
for metric in metrics:
metrics_sums[metric] = 0.
progbar = get_progressbar(name, len(sample))
progbar.start()
num_utterances = 0
predictions = []
for i, item in enumerate(sample):
int_predictions = []
item.start_interaction()
while not item.done():
utterance = item.next_utterance(snippet_keep_age)
predicted_sequence, loss, _, probability = model.eval_step(
utterance)
int_predictions.append((utterance, predicted_sequence))
flat_sequence = utterance.flatten_sequence(predicted_sequence)
if sql_util.executable(
flat_sequence,
username=database_username,
password=database_password,
timeout=database_timeout) and probability >= 0.24:
utterance.set_pred_query(
item.remove_snippets(predicted_sequence))
item.add_utterance(utterance,
item.remove_snippets(predicted_sequence),
previous_snippets=utterance.snippets())
else:
# Add the /previous/ predicted query, guaranteed to be syntactically
# correct
seq = []
utterance.set_pred_query(seq)
item.add_utterance(
utterance, seq, previous_snippets=utterance.snippets())
original_utt = item.interaction.utterances[utterance.index]
write_prediction(
predictions_file,
identifier=item.interaction.identifier,
input_seq=utterance.input_sequence(),
probability=probability,
prediction=predicted_sequence,
flat_prediction=flat_sequence,
gold_query=original_utt.gold_query_to_use,
flat_gold_queries=[
q[0] for q in original_utt.all_gold_queries],
gold_tables=[
q[1] for q in original_utt.all_gold_queries],
index_in_interaction=utterance.index,
database_username=database_username,
database_password=database_password,
database_timeout=database_timeout)
update_sums(metrics,
metrics_sums,
predicted_sequence,
flat_sequence,
original_utt.gold_query_to_use,
original_utt.original_gold_query,
gold_forcing,
loss,
token_accuracy=0,
database_username=database_username,
database_password=database_password,
database_timeout=database_timeout,
gold_table=original_utt.gold_sql_results)
predictions.append(int_predictions)
progbar.update(i)
progbar.finish()
if total_num < 0:
total_num = num_utterances
predictions_file.close()
return construct_averages(metrics_sums, total_num), predictions