-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
41 lines (31 loc) · 1.52 KB
/
Copy pathdataset.py
File metadata and controls
41 lines (31 loc) · 1.52 KB
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
import torch
import torch.utils.data
import numpy as np
epsilon = 1e-8
class ForecastDataset(torch.utils.data.Dataset):
def __init__(self, data, window_size, pred_len, stride=1, normalize=True):
super().__init__()
self.window_size = window_size
self.pred_len = pred_len
self.stride = stride
self.data = self._normalize_data(data) if normalize else data
self.univariate = self.data.shape[1] == 1
self.sample_num = max((self.data.shape[0] - window_size - pred_len) // stride + 1, 0)
# Generate samples efficiently
self.samples, self.targets = self._generate_samples()
def _normalize_data(self, data, epsilon=1e-8):
""" Normalize data using mean and standard deviation. """
mean, std = np.mean(data, axis=0), np.std(data, axis=0)
std = np.where(std == 0, epsilon, std) # Avoid division by zero
return (data - mean) / std
def _generate_samples(self):
""" Generate windowed samples efficiently using vectorized slicing. """
data = torch.tensor(self.data, dtype=torch.float32)
indices = np.arange(0, self.sample_num * self.stride, self.stride)
X = torch.stack([data[i : i + self.window_size] for i in indices])
Y = torch.stack([data[i + self.window_size : i + self.window_size + self.pred_len] for i in indices])
return X, Y # Inputs & targets
def __len__(self):
return self.sample_num
def __getitem__(self, index):
return self.samples[index], self.targets[index]