-
Notifications
You must be signed in to change notification settings - Fork 45
/
dataset.py
40 lines (31 loc) · 1.06 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
30
31
32
33
34
35
36
37
38
39
40
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author: Huiqiang Xie
@File: EurDataset.py
@Time: 2021/3/31 23:20
"""
import os
import pickle
import numpy as np
import torch
from torch.utils.data import Dataset
class EurDataset(Dataset):
def __init__(self, split='train'):
data_dir = '/import/antennas/Datasets/hx301/'
with open(data_dir + 'europarl/{}_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)