LSTM Regression Model¶
In [1]:
Copied!
from deep_river.regression import RollingRegressor
from river import (
metrics,
compose,
preprocessing,
datasets,
stats,
feature_extraction,
)
from torch import nn
from tqdm import tqdm
from deep_river.regression import RollingRegressor
from river import (
metrics,
compose,
preprocessing,
datasets,
stats,
feature_extraction,
)
from torch import nn
from tqdm import tqdm
In [2]:
Copied!
def get_hour(x):
x["hour"] = x["moment"].hour
return x
def get_hour(x):
x["hour"] = x["moment"].hour
return x
Simple RNN Regression Model¶
In [3]:
Copied!
class RnnModule(nn.Module):
def __init__(self, n_features, hidden_size):
super().__init__()
self.n_features = n_features
self.rnn = nn.RNN(
input_size=n_features, hidden_size=hidden_size, num_layers=1
)
self.fc = nn.Linear(in_features=hidden_size, out_features=1)
def forward(self, X, **kwargs):
output, hn = self.rnn(X) # lstm with input, hidden, and internal state
return self.fc(output[-1, :])
class RnnModule(nn.Module):
def __init__(self, n_features, hidden_size):
super().__init__()
self.n_features = n_features
self.rnn = nn.RNN(
input_size=n_features, hidden_size=hidden_size, num_layers=1
)
self.fc = nn.Linear(in_features=hidden_size, out_features=1)
def forward(self, X, **kwargs):
output, hn = self.rnn(X) # lstm with input, hidden, and internal state
return self.fc(output[-1, :])
In [4]:
Copied!
dataset = datasets.Bikes()
metric = metrics.MAE()
model_pipeline = compose.Select(
"clouds", "humidity", "pressure", "temperature", "wind"
)
model_pipeline += get_hour | feature_extraction.TargetAgg(
by=["station", "hour"], how=stats.Mean()
)
model_pipeline |= preprocessing.StandardScaler()
model_pipeline |= RollingRegressor(
module=RnnModule,
loss_fn="mse",
optimizer_fn="sgd",
window_size=20,
lr=1e-2,
hidden_size=32, # parameters of MyModule can be overwritten
append_predict=True,
)
model_pipeline
dataset = datasets.Bikes()
metric = metrics.MAE()
model_pipeline = compose.Select(
"clouds", "humidity", "pressure", "temperature", "wind"
)
model_pipeline += get_hour | feature_extraction.TargetAgg(
by=["station", "hour"], how=stats.Mean()
)
model_pipeline |= preprocessing.StandardScaler()
model_pipeline |= RollingRegressor(
module=RnnModule,
loss_fn="mse",
optimizer_fn="sgd",
window_size=20,
lr=1e-2,
hidden_size=32, # parameters of MyModule can be overwritten
append_predict=True,
)
model_pipeline
Out[4]:
['clouds', [...]
Select (
clouds
humidity
pressure
temperature
wind
)
get_hour
def get_hour(x):
x["hour"] = x["moment"].hour
return x
y_mean_by_station_and_hour
TargetAgg (
by=['station', 'hour']
how=Mean ()
target_name="y"
)
StandardScaler
StandardScaler (
with_std=True
)
RollingRegressor
RollingRegressor (
module=None
loss_fn="mse"
optimizer_fn="sgd"
lr=0.01
is_feature_incremental=False
window_size=20
append_predict=True
device="cpu"
seed=42
)
In [5]:
Copied!
for x, y in tqdm(dataset.take(5000)):
y_pred = model_pipeline.predict_one(x)
metric.update(y_true=y, y_pred=y_pred)
model_pipeline.learn_one(x=x, y=y)
print(f"MAE: {metric.get():.2f}")
for x, y in tqdm(dataset.take(5000)):
y_pred = model_pipeline.predict_one(x)
metric.update(y_true=y, y_pred=y_pred)
model_pipeline.learn_one(x=x, y=y)
print(f"MAE: {metric.get():.2f}")
0it [00:00, ?it/s]
1it [00:00, 1.32it/s]
43it [00:00, 67.61it/s]
95it [00:00, 150.91it/s]
145it [00:01, 222.99it/s]
191it [00:01, 276.40it/s]
240it [00:01, 328.17it/s]
290it [00:01, 372.09it/s]
341it [00:01, 407.35it/s]
392it [00:01, 434.36it/s]
441it [00:01, 444.48it/s]
489it [00:01, 441.17it/s]
536it [00:01, 435.48it/s]
585it [00:01, 448.57it/s]
636it [00:02, 465.84it/s]
685it [00:02, 471.82it/s]
733it [00:02, 471.79it/s]
783it [00:02, 478.74it/s]
832it [00:02, 475.08it/s]
881it [00:02, 478.90it/s]
930it [00:02, 474.18it/s]
978it [00:02, 473.61it/s]
1026it [00:02, 472.70it/s]
1076it [00:03, 478.32it/s]
1126it [00:03, 482.34it/s]
1175it [00:03, 482.14it/s]
1224it [00:03, 482.02it/s]
1273it [00:03, 480.28it/s]
1323it [00:03, 485.48it/s]
1374it [00:03, 491.39it/s]
1424it [00:03, 485.53it/s]
1473it [00:03, 469.39it/s]
1523it [00:03, 476.88it/s]
1572it [00:04, 479.50it/s]
1622it [00:04, 484.55it/s]
1671it [00:04, 485.63it/s]
1720it [00:04, 483.89it/s]
1770it [00:04, 487.62it/s]
1821it [00:04, 492.89it/s]
1872it [00:04, 496.10it/s]
1922it [00:04, 493.71it/s]
1972it [00:04, 485.80it/s]
2022it [00:04, 488.00it/s]
2072it [00:05, 490.82it/s]
2122it [00:05, 493.50it/s]
2172it [00:05, 489.03it/s]
2221it [00:05, 477.90it/s]
2270it [00:05, 480.45it/s]
2320it [00:05, 483.41it/s]
2369it [00:05, 479.67it/s]
2417it [00:05, 478.69it/s]
2465it [00:05, 478.17it/s]
2515it [00:05, 484.47it/s]
2564it [00:06, 482.40it/s]
2614it [00:06, 485.44it/s]
2664it [00:06, 486.28it/s]
2713it [00:06, 477.68it/s]
2761it [00:06, 476.44it/s]
2813it [00:06, 486.90it/s]
2862it [00:06, 471.59it/s]
2910it [00:06, 470.15it/s]
2958it [00:06, 462.33it/s]
3008it [00:07, 472.57it/s]
3058it [00:07, 478.48it/s]
3106it [00:07, 473.88it/s]
3157it [00:07, 482.38it/s]
3206it [00:07, 477.33it/s]
3255it [00:07, 480.77it/s]
3307it [00:07, 489.95it/s]
3357it [00:07, 491.79it/s]
3410it [00:07, 500.45it/s]
3461it [00:07, 491.46it/s]
3512it [00:08, 496.57it/s]
3562it [00:08, 497.41it/s]
3613it [00:08, 498.96it/s]
3663it [00:08, 495.46it/s]
3713it [00:08, 485.39it/s]
3763it [00:08, 488.13it/s]
3813it [00:08, 491.18it/s]
3865it [00:08, 497.26it/s]
3915it [00:08, 492.44it/s]
3965it [00:08, 479.16it/s]
4014it [00:09, 477.70it/s]
4064it [00:09, 484.02it/s]
4115it [00:09, 489.03it/s]
4166it [00:09, 494.26it/s]
4216it [00:09, 486.31it/s]
4268it [00:09, 494.77it/s]
4319it [00:09, 497.11it/s]
4369it [00:09, 497.06it/s]
4419it [00:09, 485.99it/s]
4469it [00:10, 487.42it/s]
4520it [00:10, 492.12it/s]
4572it [00:10, 497.62it/s]
4622it [00:10, 495.40it/s]
4672it [00:10, 487.50it/s]
4722it [00:10, 489.70it/s]
4774it [00:10, 497.22it/s]
4826it [00:10, 502.29it/s]
4877it [00:10, 500.93it/s]
4928it [00:10, 495.09it/s]
4978it [00:11, 495.79it/s]
5000it [00:11, 451.61it/s]
MAE: 3.79
LSTM Regression Model¶
In [6]:
Copied!
class LstmModule(nn.Module):
def __init__(self, n_features, hidden_size=1):
super().__init__()
self.n_features = n_features
self.hidden_size = hidden_size
self.lstm = nn.LSTM(
input_size=n_features,
hidden_size=hidden_size,
num_layers=1,
bidirectional=False,
)
self.fc = nn.Linear(in_features=hidden_size, out_features=1)
def forward(self, X, **kwargs):
output, (hn, cn) = self.lstm(
X
) # lstm with input, hidden, and internal state
return self.fc(output[-1, :])
class LstmModule(nn.Module):
def __init__(self, n_features, hidden_size=1):
super().__init__()
self.n_features = n_features
self.hidden_size = hidden_size
self.lstm = nn.LSTM(
input_size=n_features,
hidden_size=hidden_size,
num_layers=1,
bidirectional=False,
)
self.fc = nn.Linear(in_features=hidden_size, out_features=1)
def forward(self, X, **kwargs):
output, (hn, cn) = self.lstm(
X
) # lstm with input, hidden, and internal state
return self.fc(output[-1, :])
In [7]:
Copied!
dataset = datasets.Bikes()
metric = metrics.MAE()
model_pipeline = compose.Select(
"clouds", "humidity", "pressure", "temperature", "wind"
)
model_pipeline += get_hour | feature_extraction.TargetAgg(
by=["station", "hour"], how=stats.Mean()
)
model_pipeline |= preprocessing.StandardScaler()
model_pipeline |= RollingRegressor(
module=LstmModule,
loss_fn="mse",
optimizer_fn="sgd",
window_size=20,
lr=1e-2,
hidden_size=32, # parameters of MyModule can be overwritten
append_predict=True,
)
model_pipeline
dataset = datasets.Bikes()
metric = metrics.MAE()
model_pipeline = compose.Select(
"clouds", "humidity", "pressure", "temperature", "wind"
)
model_pipeline += get_hour | feature_extraction.TargetAgg(
by=["station", "hour"], how=stats.Mean()
)
model_pipeline |= preprocessing.StandardScaler()
model_pipeline |= RollingRegressor(
module=LstmModule,
loss_fn="mse",
optimizer_fn="sgd",
window_size=20,
lr=1e-2,
hidden_size=32, # parameters of MyModule can be overwritten
append_predict=True,
)
model_pipeline
Out[7]:
['clouds', [...]
Select (
clouds
humidity
pressure
temperature
wind
)
get_hour
def get_hour(x):
x["hour"] = x["moment"].hour
return x
y_mean_by_station_and_hour
TargetAgg (
by=['station', 'hour']
how=Mean ()
target_name="y"
)
StandardScaler
StandardScaler (
with_std=True
)
RollingRegressor
RollingRegressor (
module=None
loss_fn="mse"
optimizer_fn="sgd"
lr=0.01
is_feature_incremental=False
window_size=20
append_predict=True
device="cpu"
seed=42
)
In [8]:
Copied!
for x, y in tqdm(dataset.take(5000)):
y_pred = model_pipeline.predict_one(x)
metric.update(y_true=y, y_pred=y_pred)
model_pipeline.learn_one(x=x, y=y)
print(f"MAE: {metric.get():.2f}")
for x, y in tqdm(dataset.take(5000)):
y_pred = model_pipeline.predict_one(x)
metric.update(y_true=y, y_pred=y_pred)
model_pipeline.learn_one(x=x, y=y)
print(f"MAE: {metric.get():.2f}")
0it [00:00, ?it/s]
43it [00:00, 424.32it/s]
93it [00:00, 467.02it/s]
145it [00:00, 488.83it/s]
194it [00:00, 484.04it/s]
248it [00:00, 500.65it/s]
299it [00:00, 503.61it/s]
350it [00:00, 479.84it/s]
400it [00:00, 485.69it/s]
449it [00:00, 479.78it/s]
499it [00:01, 483.66it/s]
550it [00:01, 489.44it/s]
601it [00:01, 495.08it/s]
651it [00:01, 496.08it/s]
701it [00:01, 492.19it/s]
751it [00:01, 493.28it/s]
801it [00:01, 494.40it/s]
852it [00:01, 497.34it/s]
902it [00:01, 489.25it/s]
951it [00:01, 487.44it/s]
1001it [00:02, 490.96it/s]
1051it [00:02, 492.80it/s]
1101it [00:02, 493.00it/s]
1151it [00:02, 480.43it/s]
1201it [00:02, 485.42it/s]
1252it [00:02, 489.93it/s]
1302it [00:02, 491.20it/s]
1352it [00:02, 483.61it/s]
1401it [00:02, 485.37it/s]
1450it [00:02, 485.49it/s]
1500it [00:03, 487.10it/s]
1549it [00:03, 486.61it/s]
1598it [00:03, 484.81it/s]
1647it [00:03, 475.10it/s]
1697it [00:03, 481.17it/s]
1747it [00:03, 483.96it/s]
1797it [00:03, 487.37it/s]
1846it [00:03, 486.54it/s]
1895it [00:03, 479.73it/s]
1944it [00:03, 479.98it/s]
1994it [00:04, 484.49it/s]
2044it [00:04, 488.27it/s]
2093it [00:04, 487.41it/s]
2142it [00:04, 477.22it/s]
2192it [00:04, 481.26it/s]
2241it [00:04, 482.43it/s]
2290it [00:04, 449.76it/s]
2336it [00:04, 441.63it/s]
2381it [00:04, 439.74it/s]
2429it [00:05, 449.24it/s]
2479it [00:05, 462.03it/s]
2528it [00:05, 469.25it/s]
2576it [00:05, 471.22it/s]
2624it [00:05, 471.51it/s]
2673it [00:05, 473.39it/s]
2721it [00:05, 456.79it/s]
2770it [00:05, 465.84it/s]
2817it [00:05, 464.86it/s]
2865it [00:05, 468.90it/s]
2917it [00:06, 482.63it/s]
2968it [00:06, 490.03it/s]
3018it [00:06, 491.49it/s]
3069it [00:06, 496.58it/s]
3120it [00:06, 497.89it/s]
3170it [00:06, 498.06it/s]
3222it [00:06, 502.33it/s]
3273it [00:06, 495.73it/s]
3324it [00:06, 499.53it/s]
3376it [00:06, 503.68it/s]
3427it [00:07, 505.24it/s]
3479it [00:07, 508.46it/s]
3530it [00:07, 502.86it/s]
3581it [00:07, 502.88it/s]
3632it [00:07, 503.35it/s]
3683it [00:07, 503.22it/s]
3735it [00:07, 506.44it/s]
3786it [00:07, 495.98it/s]
3836it [00:07, 495.69it/s]
3886it [00:08, 494.41it/s]
3937it [00:08, 498.61it/s]
3988it [00:08, 501.01it/s]
4039it [00:08, 497.15it/s]
4089it [00:08, 493.27it/s]
4141it [00:08, 500.15it/s]
4192it [00:08, 501.72it/s]
4244it [00:08, 505.43it/s]
4295it [00:08, 499.87it/s]
4346it [00:08, 499.81it/s]
4398it [00:09, 503.11it/s]
4449it [00:09, 496.05it/s]
4501it [00:09, 500.42it/s]
4552it [00:09, 493.78it/s]
4602it [00:09, 490.95it/s]
4654it [00:09, 498.03it/s]
4704it [00:09, 498.05it/s]
4755it [00:09, 498.76it/s]
4805it [00:09, 497.63it/s]
4855it [00:09, 492.67it/s]
4905it [00:10, 490.55it/s]
4955it [00:10, 490.07it/s]
5000it [00:10, 488.04it/s]
MAE: 2.78