-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
56 lines (42 loc) · 2.01 KB
/
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
import numpy as np
from torch.utils.data import Dataset
class RNNDataset(Dataset):
"""PyTorch dataset class so we can use their Dataloaders."""
def __init__(self, x, y=None):
self.data = x
self.labels = y
def __len__(self):
return self.data.shape[0]
def __getitem__(self, idx):
if self.labels is not None:
return self.data[idx], self.labels[idx]
else:
return self.data[idx]
def create_dataset(sequence_length, train_percent=0.8):
"""
Prepare sinosoidal dataset for input into model.
Data should be in this order: [batch, sequence, feature] where feature is size INPUT_SIZE
Target labels will the be next value after a sequence of values.
"""
# Create sin wave at discrete time steps.
num_time_steps = 1500
time_steps = np.linspace(start=0, stop=1000, num=num_time_steps, dtype=np.float32)
discrete_sin_wave = (np.sin(time_steps * 0.5)).reshape(-1, 1)
# Take (sequence_length + 1) elements & put as a row in sequence_data, extra element is value we want to predict.
# Move one time step and keep grabbing till we reach the end of our sampled sin wave.
sequence_data = []
for i in range(num_time_steps - sequence_length):
sequence_data.append(discrete_sin_wave[i: i + sequence_length + 1, 0])
sequence_data = np.array(sequence_data)
# Split for train/val.
num_total_samples = sequence_data.shape[0]
num_train_samples = int(train_percent * num_total_samples)
train_set = sequence_data[:num_train_samples, :]
test_set = sequence_data[num_train_samples:, :]
print('{} total sequence samples, {} used for training'.format(num_total_samples, num_train_samples))
# Take off the last element of each row and this will be our target value to predict.
x_train = train_set[:, :-1][:, :, np.newaxis]
y_train = train_set[:, -1][:, np.newaxis]
x_test = test_set[:, :-1][:, :, np.newaxis]
y_test = test_set[:, -1][:, np.newaxis]
return x_train, y_train, x_test, y_test