-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathdata_loader.py
40 lines (33 loc) · 1.37 KB
/
data_loader.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
import torch
import numpy as np
from torch.utils.data import Dataset
class NerDataset(Dataset):
def __init__(self, data, args, tokenizer):
self.data = data
self.args = args
self.tokenizer = tokenizer
self.label2id = args.label2id
self.max_seq_len = args.max_seq_len
def __len__(self):
return len(self.data)
def __getitem__(self, item):
text = self.data[item]["text"]
labels = self.data[item]["labels"]
if len(text) > self.max_seq_len - 2:
text = text[:self.max_seq_len - 2]
labels = labels[:self.max_seq_len - 2]
tmp_input_ids = self.tokenizer.convert_tokens_to_ids(["[CLS]"] + text + ["[SEP]"])
attention_mask = [1] * len(tmp_input_ids)
input_ids = tmp_input_ids + [0] * (self.max_seq_len - len(tmp_input_ids))
attention_mask = attention_mask + [0] * (self.max_seq_len - len(tmp_input_ids))
labels = [self.label2id[label] for label in labels]
labels = [0] + labels + [0] + [0] * (self.max_seq_len - len(tmp_input_ids))
input_ids = torch.tensor(np.array(input_ids))
attention_mask = torch.tensor(np.array(attention_mask))
labels = torch.tensor(np.array(labels))
data = {
"input_ids": input_ids,
"attention_mask": attention_mask,
"labels": labels,
}
return data