-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlp_utils.py
51 lines (42 loc) · 1.49 KB
/
nlp_utils.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
import sys
import torch
from transformers import DistilBertTokenizerFast
from transformers import DistilBertForSequenceClassification
sys.path.append('../../..')
class DistilBertClassifier(DistilBertForSequenceClassification):
"""
Adapted from https://github.com/p-lambda/wilds
"""
def __call__(self, x):
input_ids = x[:, :, 0]
attention_mask = x[:, :, 1]
outputs = super().__call__(
input_ids=input_ids,
attention_mask=attention_mask,
)[0]
return outputs
def get_transform(arch, max_token_length):
"""
Adapted from https://github.com/p-lambda/wilds
"""
if arch == 'distilbert-base-uncased':
tokenizer = DistilBertTokenizerFast.from_pretrained(arch)
else:
raise ValueError("Model: {arch} not recognized".format(arch))
def transform(text):
tokens = tokenizer(text, padding='max_length', truncation=True,
max_length=max_token_length, return_tensors='pt')
if arch == 'bert_base_uncased':
x = torch.stack(
(
tokens["input_ids"],
tokens["attention_mask"],
tokens["token_type_ids"],
),
dim=2,
)
elif arch == 'distilbert-base-uncased':
x = torch.stack((tokens["input_ids"], tokens["attention_mask"]), dim=2)
x = torch.squeeze(x, dim=0) # First shape dim is always 1
return x
return transform