-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
76 lines (52 loc) · 2.05 KB
/
inference.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
import numpy as np
import pandas as pd
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import Ridge
from xgboost import XGBClassifier
from sklearn.svm import SVC
import pickle
# loading model checkpoints
input_path = '/model_checkpoints/mnb_model.pkl'
with open(input_path, 'rb') as file:
mnb = pickle.load(file)
input_path = '/model_checkpoints/ridge_model.pkl'
with open(input_path, 'rb') as file:
ridge = pickle.load(file)
input_path = '/model_checkpoints/xgb_model.pkl'
with open(input_path, 'rb') as file:
xgb = pickle.load(file)
input_path = '/model_checkpoints/svc_model.pkl'
with open(input_path, 'rb') as file:
svc = pickle.load(file)
# loading the test set
test = pd.read_csv('/data/test_essays.csv')
from scipy.sparse import save_npz, load_npz
X_test = load_npz('/data/processed_test.npz')
# making predictions using classical ML models
mnb_preds = mnb.predict_proba(X_test)[:, 1]
ridge_preds = ridge.predict_proba(X_test)[:, 1]
xgb_preds = xgb.predict_proba(X_test)[:, 1]
svc_preds = svc.predict_proba(X_test)[:, 1]
classical_ML_preds = 0.25*mnb_preds + 0.25*ridge_preds + 0.25*xgb_preds + 0.25*svc_preds
# making predictions using distilroberta
import torch
model_checkpoint = "/model_checkpoints/distilroberta/"
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
def preprocess_function(examples):
return tokenizer(examples['text'], max_length = 512 , padding=True, truncation=True)
num_labels = 2
model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint, num_labels=num_labels)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device);
trainer = Trainer(
model,
tokenizer=tokenizer,
)
test_ds = Dataset.from_pandas(test)
test_ds_enc = test_ds.map(preprocess_function, batched=True)
test_preds = trainer.predict(test_ds_enc)
test_logits = test_preds.predictions
distilroberta_preds = sigmoid(test_logits[:,1])
if torch.cuda.is_available():
torch.cuda.empty_cache()
final_preds = 0.4*classical_ML_preds + 0.6*distilroberta_preds