-
Notifications
You must be signed in to change notification settings - Fork 1
/
toy_dataset.py
58 lines (49 loc) · 1.96 KB
/
toy_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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import pickle
import torch
from torch.utils.data import DataLoader, Dataset
class ToyDataset(Dataset):
data_dir = os.path.join(os.path.dirname(__file__), "data_pickle")
params_file = "params.pickle"
t_file = "t.pickle"
x_file = "x.pickle"
y_file = "y.pickle"
y_true_file = "y_true.pickle"
idx_train_file = "idx_train.pickle"
idx_val_file = "idx_val.pickle"
def __init__(self, subset="train"):
self.subset = subset
self.params = pickle.load(open(os.path.join(self.data_dir,
self.params_file), "rb"))
self.t = pickle.load(open(os.path.join(self.data_dir,
self.t_file), "rb"))
self.x = pickle.load(open(os.path.join(self.data_dir,
self.x_file), "rb"))
self.y = pickle.load(open(os.path.join(self.data_dir,
self.y_file), "rb"))
self.y_true = pickle.load(open(os.path.join(self.data_dir,
self.y_true_file), "rb"))
if self.subset == "train":
idx_train = pickle.load(open(os.path.join(self.data_dir,
self.idx_train_file), "rb"))
self.t = self.t[idx_train]
self.x = self.x[idx_train]
self.y = self.y[idx_train]
self.y_true = self.y_true[idx_train]
else:
idx_val = pickle.load(open(os.path.join(self.data_dir,
self.idx_val_file), "rb"))
self.t = self.t[idx_val]
self.x = self.x[idx_val]
self.y = self.y[idx_val]
self.y_true = self.y_true[idx_val]
def __len__(self):
return len(self.x)
def __getitem__(self, idx):
return torch.tensor(self.t[idx], dtype=torch.float),\
torch.tensor(self.x[idx], dtype=torch.float),\
torch.tensor(self.y[idx], dtype=torch.float),\
torch.tensor(self.y_true[idx], dtype=torch.float)
@staticmethod
def get_dataloader(dataset, batch_size):
return DataLoader(dataset, batch_size)