Simple Classification Model¶
In [1]:
Copied!
from river import metrics, datasets, compose, preprocessing
from deep_river.classification import Classifier
from torch import nn
from tqdm import tqdm
from river import metrics, datasets, compose, preprocessing
from deep_river.classification import Classifier
from torch import nn
from tqdm import tqdm
In [2]:
Copied!
dataset = datasets.Phishing()
metric = metrics.Accuracy()
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, 2)
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
model_pipeline = compose.Pipeline(
preprocessing.StandardScaler,
Classifier(
module=MyModule, loss_fn="binary_cross_entropy", optimizer_fn="adam"
),
)
model_pipeline
dataset = datasets.Phishing()
metric = metrics.Accuracy()
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, 2)
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
model_pipeline = compose.Pipeline(
preprocessing.StandardScaler,
Classifier(
module=MyModule, loss_fn="binary_cross_entropy", optimizer_fn="adam"
),
)
model_pipeline
Out[2]:
StandardScaler
StandardScaler (
with_std=True
)
Classifier
Classifier (
module=None
loss_fn="binary_cross_entropy"
optimizer_fn="adam"
lr=0.001
output_is_logit=True
is_class_incremental=False
is_feature_incremental=False
device="cpu"
seed=42
)
In [3]:
Copied!
for x, y in tqdm(dataset.take(5000)):
y_pred = model_pipeline.predict_one(x) # make a prediction
metric.update(y, y_pred) # update the metric
model_pipeline.learn_one(x, y) # make the model learn
print(f"Accuracy: {metric.get()}")
for x, y in tqdm(dataset.take(5000)):
y_pred = model_pipeline.predict_one(x) # make a prediction
metric.update(y, y_pred) # update the metric
model_pipeline.learn_one(x, y) # make the model learn
print(f"Accuracy: {metric.get()}")
0it [00:00, ?it/s]
1it [00:01, 1.06s/it]
132it [00:01, 156.37it/s]
261it [00:01, 320.51it/s]
395it [00:01, 494.88it/s]
523it [00:01, 648.83it/s]
655it [00:01, 794.83it/s]
787it [00:01, 918.74it/s]
920it [00:01, 1021.64it/s]
1055it [00:01, 1107.63it/s]
1185it [00:01, 1134.86it/s]
1250it [00:02, 618.42it/s]
Accuracy: 0.7264
In [ ]:
Copied!