Simple Regression Model¶
In [1]:
Copied!
from river import (
metrics,
compose,
preprocessing,
datasets,
stats,
feature_extraction,
)
from deep_river.regression import Regressor
from torch import nn
from pprint import pprint
from tqdm import tqdm
from river import (
metrics,
compose,
preprocessing,
datasets,
stats,
feature_extraction,
)
from deep_river.regression import Regressor
from torch import nn
from pprint import pprint
from tqdm import tqdm
In [2]:
Copied!
dataset = datasets.Bikes()
for x, y in dataset:
pprint(x)
print(f"Number of available bikes: {y}")
break
dataset = datasets.Bikes()
for x, y in dataset:
pprint(x)
print(f"Number of available bikes: {y}")
break
{'clouds': 75, 'description': 'light rain', 'humidity': 81, 'moment': datetime.datetime(2016, 4, 1, 0, 0, 7), 'pressure': 1017.0, 'station': 'metro-canal-du-midi', 'temperature': 6.54, 'wind': 9.3} Number of available bikes: 1
In [3]:
Copied!
class MyModule(nn.Module):
def __init__(self, n_features):
super(MyModule, self).__init__()
self.dense0 = nn.Linear(n_features, 5)
self.nonlin = nn.ReLU()
self.dense1 = nn.Linear(5, 1)
self.softmax = nn.Softmax(dim=-1)
def forward(self, X, **kwargs):
X = self.nonlin(self.dense0(X))
X = self.nonlin(self.dense1(X))
X = self.softmax(X)
return X
def get_hour(x):
x["hour"] = x["moment"].hour
return x
class MyModule(nn.Module):
def __init__(self, n_features):
super(MyModule, self).__init__()
self.dense0 = nn.Linear(n_features, 5)
self.nonlin = nn.ReLU()
self.dense1 = nn.Linear(5, 1)
self.softmax = nn.Softmax(dim=-1)
def forward(self, X, **kwargs):
X = self.nonlin(self.dense0(X))
X = self.nonlin(self.dense1(X))
X = self.softmax(X)
return X
def get_hour(x):
x["hour"] = x["moment"].hour
return x
In [4]:
Copied!
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 |= Regressor(module=MyModule, loss_fn="mse", optimizer_fn="sgd")
model_pipeline
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 |= Regressor(module=MyModule, loss_fn="mse", optimizer_fn="sgd")
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
)
Regressor
Regressor (
module=None
loss_fn="mse"
optimizer_fn="sgd"
lr=0.001
is_feature_incremental=False
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.23it/s]
129it [00:00, 192.04it/s]
260it [00:01, 388.31it/s]
385it [00:01, 560.43it/s]
508it [00:01, 708.11it/s]
638it [00:01, 848.91it/s]
758it [00:01, 938.58it/s]
888it [00:01, 1034.05it/s]
1017it [00:01, 1103.51it/s]
1142it [00:01, 1138.26it/s]
1271it [00:01, 1181.32it/s]
1402it [00:01, 1216.95it/s]
1534it [00:02, 1243.86it/s]
1665it [00:02, 1262.07it/s]
1794it [00:02, 1234.51it/s]
1925it [00:02, 1254.09it/s]
2053it [00:02, 1258.22it/s]
2183it [00:02, 1270.42it/s]
2311it [00:02, 1264.28it/s]
2441it [00:02, 1272.27it/s]
2569it [00:02, 1261.41it/s]
2696it [00:02, 1249.11it/s]
2827it [00:03, 1265.79it/s]
2954it [00:03, 1260.24it/s]
3082it [00:03, 1265.05it/s]
3210it [00:03, 1267.21it/s]
3341it [00:03, 1279.19it/s]
3472it [00:03, 1287.47it/s]
3605it [00:03, 1297.92it/s]
3735it [00:03, 1281.67it/s]
3864it [00:03, 1281.18it/s]
3994it [00:03, 1286.05it/s]
4130it [00:04, 1306.16it/s]
4264it [00:04, 1314.29it/s]
4396it [00:04, 1315.94it/s]
4528it [00:04, 1305.63it/s]
4659it [00:04, 1298.81it/s]
4795it [00:04, 1316.10it/s]
4929it [00:04, 1322.14it/s]
5000it [00:04, 1061.07it/s]
MAE: 6.83