-
Notifications
You must be signed in to change notification settings - Fork 3
/
predictor.py
68 lines (51 loc) · 1.56 KB
/
predictor.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
# -*- coding: utf-8 -*-
import os
import sys
from glob import glob
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint
from transformers import AutoTokenizer
import utils
from data import Data
from model import Model
def predict(model_file, data_file, prediction_file, gpu_id=0):
# For reproducibility
pl.seed_everything(seed=42, workers=True) #
model = Model.load_from_checkpoint(model_file)
# Tokenizer
tokenizer = AutoTokenizer.from_pretrained(
model.hparams.configs["pretrained_model_dir"]
)
# Data
test_data = Data(
data_file=data_file,
config=model.model.config,
tokenizer=tokenizer,
label_encoders=model.label_encoders,
batch_size=100,
shuffle=False,
num_workers=20,
use_gold_data=False,
negative_sampling_rate=1.0,
)
test_dataloader = test_data.get_dataloader()
trainer = pl.Trainer(gpus=[gpu_id], precision=16, logger=False, deterministic=True)
predictions = trainer.predict(
model, dataloaders=test_dataloader, return_predictions=True
)
utils.serialize_objects(predictions, prediction_file)
if __name__ == "__main__":
model_file = sys.argv[1] + ".ckpt"
data_file = sys.argv[2]
prediction_file = (
os.path.splitext(model_file)[0]
+ "_"
+ os.path.splitext(os.path.basename(data_file))[0]
+ ".preds"
)
predict(
model_file=model_file,
data_file=data_file,
prediction_file=prediction_file,
gpu_id=0,
)