forked from SapienzaNLP/neural-pagerank-wsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
58 lines (42 loc) · 1.85 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
from argparse import ArgumentParser
import torch
from torch.utils.data import DataLoader
from wsd.data.dataset import WordSenseDisambiguationDataset
from wsd.data.processor import Processor
from wsd.models.model import SimpleModel
if __name__ == '__main__':
parser = ArgumentParser()
# Add data args.
parser.add_argument('--processor', type=str, required=True)
parser.add_argument('--model', type=str, required=True)
parser.add_argument('--model_input', type=str, required=True)
parser.add_argument('--model_output', type=str, required=True)
# Add dataloader args.
parser.add_argument('--batch_size', type=int, default=32)
parser.add_argument('--num_workers', type=int, default=4)
# Other
parser.add_argument('--device', type=str, default='cuda')
# Store the arguments in hparams.
args = parser.parse_args()
processor = Processor.from_config(args.processor)
test_dataset = WordSenseDisambiguationDataset(args.model_input)
test_dataloader = DataLoader(
test_dataset,
batch_size=args.batch_size,
num_workers=args.num_workers,
collate_fn=processor.collate_sentences)
model = SimpleModel.load_from_checkpoint(args.model)
device = 'cuda' if torch.cuda.is_available() and args.device == 'cuda' else 'cpu'
model.to(device)
model.eval()
predictions = {}
with torch.no_grad():
for x, _ in test_dataloader:
x = {k: v.to(device) if not isinstance(v, list) else v for k, v in x.items()}
y = model(x)
batch_predictions = processor.decode(x, y)
predictions.update(batch_predictions)
predictions = sorted(list(predictions.items()), key=lambda kv: kv[0])
with open(args.model_output, 'w') as f:
for instance_id, synset_id in predictions:
f.write('{} {}\n'.format(instance_id, synset_id))