-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasets.py
66 lines (54 loc) · 2.17 KB
/
datasets.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
import numpy as np
import torch
import pandas as pd
# from torch.utils.data import Dataset
class Dataset(torch.utils.data.Dataset):
def __init__(self,file_path, tokenizer, mode='train'):
self.file_path = file_path
self.mode = mode
self.tokenizer = tokenizer
self.df = pd.read_csv(file_path)
self.data = self.preprocess()
def preprocess(self):
self.df['token'] = self.df['token'].map(lambda x: eval(x))
self.df['ner_label'] = self.df['ner_label'].map(lambda x: eval(x))
return {
"token": self.df['token'].tolist(),
"ner_label": self.df['ner_label'].tolist()
}
def __len__(self):
return len(self.df)
def __getitem__(self, index):
return self.data['token'][index], self.data['ner_label'][index]
def align_labels_with_tokens(self, labels, word_ids):
previous_word_id = None
label_ids = []
for word_id in word_ids:
if word_id is None:
label_ids.append(0)
elif word_id != previous_word_id:
label_ids.append(labels[word_id])
else:
label_ids.append(0)
previous_word_id = word_id
return label_ids
def tokenize_and_align_labels(self,tokens, labels):
tokenized_inputs = self.tokenizer(
tokens,
padding="longest",
truncation=True,
is_split_into_words=True,
return_tensors="pt"
)
labels_align = []
for i, label in enumerate(labels):
word_ids = tokenized_inputs.word_ids(batch_index=i)
labels_align.append(self.align_labels_with_tokens(label, word_ids))
tokenized_inputs["labels"] = torch.from_numpy(np.array(labels_align)).long()
return tokenized_inputs
def data_collator(self, batch):
tokens = [x[0] for x in batch]
labels = [x[1] for x in batch]
model_inputs = self.tokenize_and_align_labels(tokens, labels)
model_inputs['attention_mask'] = torch.from_numpy(np.array(model_inputs['attention_mask'])).long()
return model_inputs