-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.py
57 lines (45 loc) · 1.69 KB
/
process.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
import pdb
import torch
import torch.utils.data
from transformer import Constants
class EventData(torch.utils.data.Dataset):
def __init__(self, data):
self.time = [[elem['time_since_start'] for elem in inst] for inst in data]
self.time_gap = [[elem['time_since_last_event'] for elem in inst] for inst in data]
self.event_type = [[elem['type_event'] + 1 for elem in inst] for inst in data]
self.event_goal = [[elem['type_goal'] for elem in inst] for inst in data]
self.length = len(data)
def __len__(self):
return self.length
def __getitem__(self, idx):
return self.time[idx], self.time_gap[idx], self.event_type[idx], self.event_goal[idx]
def pad_time(insts):
max_len = max(len(inst) for inst in insts)
batch_seq = np.array([
inst + [Constants.PAD] * (max_len - len(inst))
for inst in insts])
return torch.tensor(batch_seq, dtype=torch.float32)
def pad_type(insts):
max_len = max(len(inst) for inst in insts)
batch_seq = np.array([
inst + [Constants.PAD] * (max_len - len(inst))
for inst in insts])
return torch.tensor(batch_seq, dtype=torch.long)
def collate_fn(insts):
time, time_gap, event_type, event_goal = list(zip(*insts))
time = pad_time(time)
time_gap = pad_time(time_gap)
event_type = pad_type(event_type)
event_goal = pad_type(event_goal)
return time, time_gap, event_type, event_goal
def get_dataloader(data, batch_size, shuffle=True):
ds = EventData(data)
dl = torch.utils.data.DataLoader(
ds,
num_workers=2,
batch_size=batch_size,
collate_fn=collate_fn,
shuffle=shuffle
)
return dl