-
Notifications
You must be signed in to change notification settings - Fork 82
/
dataset.py
29 lines (22 loc) · 1 KB
/
dataset.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
import torch
from torch.utils.data import Dataset, DataLoader
from preprocess import BertFeature
from utils import commonUtils
class NerDataset(Dataset):
def __init__(self, features):
# self.callback_info = callback_info
self.nums = len(features)
self.token_ids = [torch.tensor(example.token_ids).long() for example in features]
self.attention_masks = [torch.tensor(example.attention_masks, dtype=torch.uint8) for example in features]
self.token_type_ids = [torch.tensor(example.token_type_ids).long() for example in features]
self.labels = [torch.tensor(example.labels).long() for example in features]
def __len__(self):
return self.nums
def __getitem__(self, index):
data = {
'token_ids': self.token_ids[index],
'attention_masks': self.attention_masks[index],
'token_type_ids': self.token_type_ids[index]
}
data['labels'] = self.labels[index]
return data