-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
31 lines (23 loc) · 909 Bytes
/
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
30
31
import pickle
import numpy as np
import torch
from torch.utils.data import Dataset
class EurDataset(Dataset):
def __init__(self, split='train'):
data_dir = 'data/'
with open(data_dir + '{}_data.pkl'.format(split), 'rb') as f:
self.data = pickle.load(f)
def __getitem__(self, index):
sents = self.data[index]
return sents
def __len__(self):
return len(self.data)
def collate_data(batch):
batch_size = len(batch)
max_len = max(map(lambda x: len(x), batch)) # get the max length of sentence in current batch
sents = np.zeros((batch_size, max_len), dtype=np.int64)
sort_by_len = sorted(batch, key=lambda x: len(x), reverse=True)
for i, sent in enumerate(sort_by_len):
length = len(sent)
sents[i, :length] = sent # padding the questions
return torch.from_numpy(sents)