-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathDataloader.py
69 lines (52 loc) · 2.16 KB
/
Dataloader.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
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
# coding: utf-8
import torch
import matplotlib.pyplot as plt
import numpy as np
from torch.utils.data import Dataset
class time_series_decoder_paper(Dataset):
"""synthetic time series dataset from section 5.1"""
def __init__(self,t0=96,N=4500,transform=None):
"""
Args:
t0: previous t0 data points to predict from
N: number of data points
transform: any transformations to be applied to time series
"""
self.t0 = t0
self.N = N
self.transform = None
# time points
self.x = torch.cat(N*[torch.arange(0,t0+24).type(torch.float).unsqueeze(0)])
# sinuisoidal signal
A1,A2,A3 = 60 * torch.rand(3,N)
A4 = torch.max(A1,A2)
self.fx = torch.cat([A1.unsqueeze(1)*torch.sin(np.pi*self.x[0,0:12]/6)+72 ,
A2.unsqueeze(1)*torch.sin(np.pi*self.x[0,12:24]/6)+72 ,
A3.unsqueeze(1)*torch.sin(np.pi*self.x[0,24:t0]/6)+72,
A4.unsqueeze(1)*torch.sin(np.pi*self.x[0,t0:t0+24]/12)+72],1)
# add noise
self.fx = self.fx + torch.randn(self.fx.shape)
self.masks = self._generate_square_subsequent_mask(t0)
# print out shapes to confirm desired output
print("x: {}*{}".format(*list(self.x.shape)),
"fx: {}*{}".format(*list(self.fx.shape)))
def __len__(self):
return len(self.fx)
def __getitem__(self,idx):
if torch.is_tensor(idx):
idx = idx.tolist()
sample = (self.x[idx,:],
self.fx[idx,:],
self.masks)
if self.transform:
sample=self.transform(sample)
return sample
def _generate_square_subsequent_mask(self,t0):
mask = torch.zeros(t0+24,t0+24)
for i in range(0,t0):
mask[i,t0:] = 1
for i in range(t0,t0+24):
mask[i,i+1:] = 1
mask = mask.float().masked_fill(mask == 1, float('-inf'))#.masked_fill(mask == 1, float(0.0))
return mask