-
Notifications
You must be signed in to change notification settings - Fork 0
/
bert.py
151 lines (117 loc) · 4.38 KB
/
bert.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
%%time
import os
import gc
import cv2
import copy
import time
import random
# For data manipulation
import numpy as np
import pandas as pd
# Pytorch Imports
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
# For Transformer Models
from transformers import AutoTokenizer, AutoModel
# Utils
from tqdm import tqdm
# For descriptive error messages
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
CONFIG = dict(
seed = 42,
model_name = '../input/roberta-base',
test_batch_size = 64,
max_length = 128,
num_classes = 1,
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
)
CONFIG["tokenizer"] = AutoTokenizer.from_pretrained(CONFIG['model_name'])
MODEL_PATHS = [
'../input/k/saurabhbagchi/pytorch-w-b-jigsaw-starter/Loss-Fold-0.bin',
'../input/k/saurabhbagchi/pytorch-w-b-jigsaw-starter/Loss-Fold-1.bin',
'../input/k/saurabhbagchi/pytorch-w-b-jigsaw-starter/Loss-Fold-2.bin',
'../input/k/saurabhbagchi/pytorch-w-b-jigsaw-starter/Loss-Fold-3.bin',
'../input/k/saurabhbagchi/pytorch-w-b-jigsaw-starter/Loss-Fold-4.bin'
]
def set_seed(seed = 42):
'''Sets the seed of the entire notebook so results are the same every time we run.
This is for REPRODUCIBILITY.'''
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
# When running on the CuDNN backend, two further options must be set
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# Set a fixed value for the hash seed
os.environ['PYTHONHASHSEED'] = str(seed)
class JigsawDataset(Dataset):
def __init__(self, df, tokenizer, max_length):
self.df = df
self.max_len = max_length
self.tokenizer = tokenizer
self.text = df['text'].values
def __len__(self):
return len(self.df)
def __getitem__(self, index):
text = self.text[index]
inputs = self.tokenizer.encode_plus(
text,
truncation=True,
add_special_tokens=True,
max_length=self.max_len,
padding='max_length'
)
ids = inputs['input_ids']
mask = inputs['attention_mask']
return {
'ids': torch.tensor(ids, dtype=torch.long),
'mask': torch.tensor(mask, dtype=torch.long)
}
class JigsawModel(nn.Module):
def __init__(self, model_name):
super(JigsawModel, self).__init__()
self.model = AutoModel.from_pretrained(model_name)
self.drop = nn.Dropout(p=0.2)
self.fc = nn.Linear(768, CONFIG['num_classes'])
def forward(self, ids, mask):
out = self.model(input_ids=ids,attention_mask=mask,
output_hidden_states=False)
out = self.drop(out[1])
outputs = self.fc(out)
return outputs
@torch.no_grad()
def valid_fn(model, dataloader, device):
model.eval()
dataset_size = 0
running_loss = 0.0
PREDS = []
bar = tqdm(enumerate(dataloader), total=len(dataloader))
for step, data in bar:
ids = data['ids'].to(device, dtype = torch.long)
mask = data['mask'].to(device, dtype = torch.long)
outputs = model(ids, mask)
PREDS.append(outputs.view(-1).cpu().detach().numpy())
PREDS = np.concatenate(PREDS)
gc.collect()
return PREDS
def inference(model_paths, dataloader, device):
final_preds = []
for i, path in enumerate(model_paths):
model = JigsawModel(CONFIG['model_name'])
model.to(CONFIG['device'])
model.load_state_dict(torch.load(path))
print(f"Getting predictions for model {i+1}")
preds = valid_fn(model, dataloader, device)
final_preds.append(preds)
final_preds = np.array(final_preds)
final_preds = np.mean(final_preds, axis=0)
return final_preds
set_seed(CONFIG['seed'])
df = pd.read_csv("input/jigsaw-toxic-severity-rating/comments_to_score.csv")
df.head()
test_dataset = JigsawDataset(df, CONFIG['tokenizer'], max_length=CONFIG['max_length'])
test_loader = DataLoader(test_dataset, batch_size=CONFIG['test_batch_size'],
num_workers=2, shuffle=False, pin_memory=True)
preds1 = inference(MODEL_PATHS, test_loader, CONFIG['device'])