-
Notifications
You must be signed in to change notification settings - Fork 1
/
torch_dataset.py
96 lines (71 loc) · 2.38 KB
/
torch_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import numpy as np
import pickle
from torch.utils.data import Dataset
from sklearn.preprocessing import MinMaxScaler
class TrainSet(Dataset):
def __init__(self):
# Load data
with open('data/X_train.p', 'rb') as f:
X = pickle.load(f)
with open('data/Y_train.p', 'rb') as f:
Y = pickle.load(f)
X_concat = np.concatenate(X, axis=0)
Y_concat = np.concatenate(Y, axis=0)
x_scaler = MinMaxScaler((-1, 1)).fit(X_concat)
y_scaler = MinMaxScaler((-1, 1)).fit(Y_concat)
X_scaled = list(map(x_scaler.transform, X))
Y_scaled = list(map(y_scaler.transform, Y))
self.X = X
self.Y = Y
self.X_ori = X
self.Y_ori = Y
self.X_scaled = X_scaled
self.Y_scaled = Y_scaled
self.x_scaler = x_scaler
self.y_scaler = y_scaler
def __len__(self):
return len(self.X)
def __getitem__(self, i):
return self.X[i], self.Y[i]
def scaling(self, flag):
if flag:
self.X = self.X_scaled
self.Y = self.Y_scaled
else:
self.X = self.X_ori
self.Y = self.Y_ori
def scale_x(self, x):
assert len(x.shape) == 2, "shape of y must be (t, dim)"
return self.x_scaler.transform(x)
def rescale_y(self, y):
assert len(y.shape) == 2, "shape of y must be (t, dim)"
return self.y_scaler.inverse_transform(y)
class TestSet(Dataset):
def __init__(self):
# Load data
with open('data/X_test.p', 'rb') as f:
X = pickle.load(f)
with open('data/Y_test.p', 'rb') as f:
Y = pickle.load(f)
X_concat = np.concatenate(X, axis=0)
Y_concat = np.concatenate(Y, axis=0)
x_scaler = MinMaxScaler((-1, 1)).fit(X_concat)
y_scaler = MinMaxScaler((-1, 1)).fit(Y_concat)
X_scaled = list(map(x_scaler.transform, X))
Y_scaled = list(map(y_scaler.transform, Y))
self.X = X
self.Y = Y
self.X_ori = X
self.Y_ori = Y
self.X_scaled = X_scaled
self.Y_scaled = Y_scaled
self.x_scaler = x_scaler
self.y_scaler = y_scaler
def __len__(self):
return len(self.X)
def __getitem__(self, i):
return self.X[i], self.Y[i]
if __name__ == "__main__":
seq_set = TrainSet()
seq_set.scaling(True)
print(seq_set[0][1].shape)