Intermediate Deep Learning with PyTorch
Michal Oleszak
Machine Learning Engineer
Task: predict future electricity consumption based on past patterns
Electricity consumption dataset:
timestamp consumption
0 2011-01-01 00:15:00 -0.704319
1 2011-01-01 00:30:00 -0.704319
... ... ...
140254 2014-12-31 23:45:00 -0.095751
140255 2015-01-01 00:00:00 -0.095751
import numpy as np def create_sequences(df, seq_length):
xs, ys = [], []
for i in range(len(df) - seq_length):
x = df.iloc[i:(i+seq_length), 1] y = df.iloc[i+seq_length, 1]
xs.append(x) ys.append(y)
return np.array(xs), np.array(ys)
Create training examples
X_train, y_train = create_sequences(train_data, seq_length)
print(X_train.shape, y_train.shape)
(34944, 96) (34944,)
Convert them to a Torch Dataset
from torch.utils.data import TensorDataset
dataset_train = TensorDataset(
torch.from_numpy(X_train).float(),
torch.from_numpy(y_train).float(),
)
Same techniques are applicable to other sequences:
Intermediate Deep Learning with PyTorch