-
Notifications
You must be signed in to change notification settings - Fork 88
/
predict.py
75 lines (58 loc) · 2.3 KB
/
predict.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
# from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import numpy as np
import argparse
import json
import os
from tqdm import tqdm
from keras import backend as K
from keras.models import Model, load_model
from layers import Argmax
from data import BatchGen, load_dataset
from utils import custom_objects
from preprocessing import CoreNLP_tokenizer
np.random.seed(10)
parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', default=70, type=int, help='Batch size')
parser.add_argument('--dev_data', default='data/dev_data.pkl', type=str,
help='Validation Set')
parser.add_argument('model', type=str, help='Model to run')
parser.add_argument('prediction', type=str, default='pred.json',
help='Outfile to save predictions')
args = parser.parse_args()
print('Preparing model...', end='')
model = load_model(args.model, custom_objects())
inputs = model.inputs
outputs = [ Argmax() (output) for output in model.outputs ]
predicting_model = Model(inputs, outputs)
print('Done!')
print('Loading data...', end='')
dev_data = load_dataset(args.dev_data)
char_level_embeddings = len(dev_data[0]) is 4
maxlen = [300, 300, 30, 30] if char_level_embeddings else [300, 30]
dev_data_gen = BatchGen(*dev_data, batch_size=args.batch_size, shuffle=False, group=False, maxlen=maxlen)
with open('data/dev_parsed.json') as f:
samples = json.load(f)
print('Done!')
print('Running predicting model...', end='')
predictions = predicting_model.predict_generator(generator=dev_data_gen,
steps=dev_data_gen.steps(),
verbose=1)
print('Done!')
print('Initiating CoreNLP service connection... ', end='')
tokenize = CoreNLP_tokenizer()
print('Done!')
print('Preparing prediction file...', end='')
contexts = [sample['context'] for sample in samples]
answers = {}
for sample, context, start, end in tqdm(zip(samples, contexts, *predictions)):
id = sample['id']
context_tokens, _ = tokenize(context)
answer = ' '.join(context_tokens[start : end+1])
answers[id] = answer
print('Done!')
print('Writing predictions to file {}...'.format(args.prediction), end='')
with open(args.prediction, 'w') as f:
json.dump(answers, f)
print('Done!')