Package: deep-river
deep-river is a Python library for online deep learning.
    deep-river's ambition is to enableonline machine learningfor neural networks.
    It combines theriverAPI with the capabilities of designing neural networks based onPyTorch.📚DocumentationThedocumentationcontains an overview of all features of this repository as well as the repository's full features list. In each of these, the git repo reference is listed in a section that shows examples of the features and functionality.💈 Installationpipinstalldeep-riverorpipinstall"river[deep]"You can install the latest development version from GitHub as so:pipinstallhttps://github.com/online-ml/deep-river/archive/refs/heads/master.zip🍫 QuickstartWe build the development of neural networks on top of theriver APIand refer to the rivers design principles.
The following example creates a simple MLP architecture based on PyTorch and incrementally predicts and trains on the website phishing dataset.
For further examples check out theDocumentation.Classification>>>fromriverimportmetrics,datasets,preprocessing,compose>>>fromdeep_riverimportclassification>>>fromtorchimportnn>>>fromtorchimportoptim>>>fromtorchimportmanual_seed>>>_=manual_seed(42)>>>classMyModule(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)......defforward(self,X,**kwargs):...X=self.nonlin(self.dense0(X))...X=self.nonlin(self.dense1(X))...X=self.softmax(X)...returnX>>>model_pipeline=compose.Pipeline(...preprocessing.StandardScaler(),...classification.Classifier(module=MyModule,loss_fn='binary_cross_entropy',optimizer_fn='adam')...)>>>dataset=datasets.Phishing()>>>metric=metrics.Accuracy()>>>forx,yindataset:...y_pred=model_pipeline.predict_one(x)# make a prediction...metric=metric.update(y,y_pred)# update the metric...model_pipeline=model_pipeline.learn_one(x,y)# make the model learn>>>print(f"Accuracy:{metric.get():.4f}")Accuracy:0.6728Multi Target Regression>>>fromriverimportevaluate,compose>>>fromriverimportmetrics>>>fromriverimportpreprocessing>>>fromriverimportstream>>>fromsklearnimportdatasets>>>fromtorchimportnn>>>fromdeep_river.regression.multioutputimportMultiTargetRegressor>>>classMyModule(nn.Module):...def__init__(self,n_features):...super(MyModule,self).__init__()...self.dense0=nn.Linear(n_features,3)......defforward(self,X,**kwargs):...X=self.dense0(X)...returnX>>>dataset=stream.iter_sklearn_dataset(...dataset=datasets.load_linnerud(),...shuffle=True,...seed=42...)>>>model=compose.Pipeline(...preprocessing.StandardScaler(),...MultiTargetRegressor(...module=MyModule,...loss_fn='mse',...lr=0.3,...optimizer_fn='sgd',...))>>>metric=metrics.multioutput.MicroAverage(metrics.MAE())>>>ev=evaluate.progressive_val_score(dataset,model,metric)>>>print(f"MicroAverage(MAE):{metric.get():.2f}")MicroAverage(MAE):28.36Anomaly Detection>>>fromdeep_river.anomalyimportAutoencoder>>>fromriverimportmetrics>>>fromriver.datasetsimportCreditCard>>>fromtorchimportnn>>>importmath>>>fromriver.composeimportPipeline>>>fromriver.preprocessingimportMinMaxScaler>>>dataset=CreditCard().take(5000)>>>metric=metrics.ROCAUC(n_thresholds=50)>>>classMyAutoEncoder(nn.Module):...def__init__(self,n_features,latent_dim=3):...super(MyAutoEncoder,self).__init__()...self.linear1=nn.Linear(n_features,latent_dim)...self.nonlin=nn.LeakyReLU()...self.linear2=nn.Linear(latent_dim,n_features)...self.sigmoid=nn.Sigmoid()......defforward(self,X,**kwargs):...X=self.linear1(X)...X=self.nonlin(X)...X=self.linear2(X)...returnself.sigmoid(X)>>>ae=Autoencoder(module=MyAutoEncoder,lr=0.005)>>>scaler=MinMaxScaler()>>>model=Pipeline(scaler,ae)>>>forx,yindataset:...score=model.score_one(x)...model=model.learn_one(x=x)...metric=metric.update(y,score)...>>>print(f"ROCAUC:{metric.get():.4f}")ROCAUC:0.7447🏫 Affiliations