Real world exmpl
In [1]:
Copied!
from river.datasets import Insects
from river.preprocessing import MinMaxScaler
from river.metrics import Accuracy
from river.utils import Rolling
from river.tree import HoeffdingTreeClassifier
from deep_river.classification import Classifier
from torch import nn
from tqdm import tqdm
import matplotlib.pyplot as plt
import matplotlib
import torch
from river.datasets import Insects
from river.preprocessing import MinMaxScaler
from river.metrics import Accuracy
from river.utils import Rolling
from river.tree import HoeffdingTreeClassifier
from deep_river.classification import Classifier
from torch import nn
from tqdm import tqdm
import matplotlib.pyplot as plt
import matplotlib
import torch
In [2]:
Copied!
insects = Insects("abrupt_balanced")
def test_train_eval(model, stream, update_interval=100):
results = []
steps = []
step = 0
metric = Rolling(Accuracy(), window_size=400)
scaler = MinMaxScaler()
for x, y in tqdm(list(stream)):
x = scaler.learn_one(x).transform_one(x)
y_pred = model.predict_one(x)
model.learn_one(x, y)
metric.update(y, y_pred)
step += 1
if step % update_interval == 0:
results.append(metric.get())
steps.append(step)
return steps, results
insects = Insects("abrupt_balanced")
def test_train_eval(model, stream, update_interval=100):
results = []
steps = []
step = 0
metric = Rolling(Accuracy(), window_size=400)
scaler = MinMaxScaler()
for x, y in tqdm(list(stream)):
x = scaler.learn_one(x).transform_one(x)
y_pred = model.predict_one(x)
model.learn_one(x, y)
metric.update(y, y_pred)
step += 1
if step % update_interval == 0:
results.append(metric.get())
steps.append(step)
return steps, results
In [3]:
Copied!
class SimpleMLP(nn.Module):
def __init__(self, n_features):
super().__init__()
self.hidden1 = nn.Linear(n_features, 30)
self.hidden2 = nn.Linear(30, 30)
self.logits = nn.Linear(30, 6)
def forward(self, x):
h = torch.relu(self.hidden1(x))
h = torch.relu(self.hidden2(h))
return self.logits(h)
mlp = Classifier(
SimpleMLP,
loss_fn="binary_cross_entropy_with_logits",
optimizer_fn="sgd",
lr=0.05,
seed=42,
)
steps, results_mlp = test_train_eval(mlp, insects)
class SimpleMLP(nn.Module):
def __init__(self, n_features):
super().__init__()
self.hidden1 = nn.Linear(n_features, 30)
self.hidden2 = nn.Linear(30, 30)
self.logits = nn.Linear(30, 6)
def forward(self, x):
h = torch.relu(self.hidden1(x))
h = torch.relu(self.hidden2(h))
return self.logits(h)
mlp = Classifier(
SimpleMLP,
loss_fn="binary_cross_entropy_with_logits",
optimizer_fn="sgd",
lr=0.05,
seed=42,
)
steps, results_mlp = test_train_eval(mlp, insects)
--------------------------------------------------------------------------- HTTPError Traceback (most recent call last) Cell In[3], line 21 11 return self.logits(h) 14 mlp = Classifier( 15 SimpleMLP, 16 loss_fn="binary_cross_entropy_with_logits", (...) 19 seed=42, 20 ) ---> 21 steps, results_mlp = test_train_eval(mlp, insects) Cell In[2], line 9, in test_train_eval(model, stream, update_interval) 7 metric = Rolling(Accuracy(), window_size=400) 8 scaler = MinMaxScaler() ----> 9 for x, y in tqdm(list(stream)): 10 x = scaler.learn_one(x).transform_one(x) 11 y_pred = model.predict_one(x) File ~/Documents/Environments/deep-river/lib/python3.11/site-packages/river/datasets/base.py:325, in RemoteDataset.__iter__(self) 323 def __iter__(self): 324 if not self.is_downloaded: --> 325 self.download(verbose=True) 326 if not self.is_downloaded: 327 raise RuntimeError("Something went wrong during the download") File ~/Documents/Environments/deep-river/lib/python3.11/site-packages/river/datasets/base.py:272, in RemoteDataset.download(self, force, verbose) 269 directory.mkdir(parents=True, exist_ok=True) 270 archive_path = directory.joinpath(os.path.basename(self.url)) --> 272 with request.urlopen(self.url) as r: 273 # Notify the user 274 if verbose: 275 meta = r.info() File /usr/local/Cellar/python@3.11/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py:216, in urlopen(url, data, timeout, cafile, capath, cadefault, context) 214 else: 215 opener = _opener --> 216 return opener.open(url, data, timeout) File /usr/local/Cellar/python@3.11/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py:525, in OpenerDirector.open(self, fullurl, data, timeout) 523 for processor in self.process_response.get(protocol, []): 524 meth = getattr(processor, meth_name) --> 525 response = meth(req, response) 527 return response File /usr/local/Cellar/python@3.11/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py:634, in HTTPErrorProcessor.http_response(self, request, response) 631 # According to RFC 2616, "2xx" code indicates that the client's 632 # request was successfully received, understood, and accepted. 633 if not (200 <= code < 300): --> 634 response = self.parent.error( 635 'http', request, response, code, msg, hdrs) 637 return response File /usr/local/Cellar/python@3.11/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py:563, in OpenerDirector.error(self, proto, *args) 561 if http_err: 562 args = (dict, 'default', 'http_error_default') + orig_args --> 563 return self._call_chain(*args) File /usr/local/Cellar/python@3.11/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py:496, in OpenerDirector._call_chain(self, chain, kind, meth_name, *args) 494 for handler in handlers: 495 func = getattr(handler, meth_name) --> 496 result = func(*args) 497 if result is not None: 498 return result File /usr/local/Cellar/python@3.11/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py:643, in HTTPDefaultErrorHandler.http_error_default(self, req, fp, code, msg, hdrs) 642 def http_error_default(self, req, fp, code, msg, hdrs): --> 643 raise HTTPError(req.full_url, code, msg, hdrs, fp) HTTPError: HTTP Error 404: Not Found
In [ ]:
Copied!
tree = HoeffdingTreeClassifier()
steps, results_tree = test_train_eval(tree, insects)
tree = HoeffdingTreeClassifier()
steps, results_tree = test_train_eval(tree, insects)
In [5]:
Copied!
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(steps, results_mlp, label="MLP")
ax.plot(steps, results_tree, label="Hoeffding Tree")
change_points = [0, 14352, 19500, 33240, 38682, 39510, 52848]
temps = [30, 20, 36, 26, 34, 30]
cmap = plt.cm.plasma
norm = matplotlib.colors.Normalize(vmin=20, vmax=36)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
for i in range(len(change_points) - 1):
ax.axvspan(
change_points[i],
change_points[i + 1],
alpha=0.4,
color=cmap(norm(temps[i])),
lw=0,
)
ax.set_xlim(0, change_points[-1])
cbar = fig.colorbar(sm, ax=ax)
cbar.ax.set_ylabel("Temperature in °C")
ax.set_xlabel("Steps")
ax.set_ylabel("Moving Accuracy")
ax.legend()
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(steps, results_mlp, label="MLP")
ax.plot(steps, results_tree, label="Hoeffding Tree")
change_points = [0, 14352, 19500, 33240, 38682, 39510, 52848]
temps = [30, 20, 36, 26, 34, 30]
cmap = plt.cm.plasma
norm = matplotlib.colors.Normalize(vmin=20, vmax=36)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
for i in range(len(change_points) - 1):
ax.axvspan(
change_points[i],
change_points[i + 1],
alpha=0.4,
color=cmap(norm(temps[i])),
lw=0,
)
ax.set_xlim(0, change_points[-1])
cbar = fig.colorbar(sm, ax=ax)
cbar.ax.set_ylabel("Temperature in °C")
ax.set_xlabel("Steps")
ax.set_ylabel("Moving Accuracy")
ax.legend()
Out[5]:
<matplotlib.legend.Legend at 0x142fbceb0>