forked from fd2014cl/RP-CRE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loader.py
43 lines (33 loc) · 1.07 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
41
42
43
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
class data_set(Dataset):
def __init__(self, data,config=None):
self.data = data
self.config = config
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return self.data[idx]
def collate_fn(self, data):
label = torch.tensor([item['relation'] for item in data])
tokens = [torch.tensor(item['tokens']) for item in data]
return (
label,
tokens
)
def get_data_loader(config, data, shuffle = False, drop_last = False, batch_size = None):
dataset = data_set(data, config)
if batch_size == None:
batch_size = min(config.batch_size_per_step, len(data))
else:
batch_size = min(batch_size, len(data))
data_loader = DataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=shuffle,
pin_memory=True,
num_workers=config.num_workers,
collate_fn=dataset.collate_fn,
drop_last=drop_last)
return data_loader