Deep Neural Network Model in sklearn Pipeline

Is it possible to add a deep neural network model as the estimator/model in an sklearn Pipeline? or is it only possible for ML models as the estimator.

For example, can I have a transformation pipeline (that consists of some Imputers or Encoders) then followed by an LSTM or CNN model as the final estimator. If so, can someone guide me as to how to go about creating something like that. (using either resources or examples)

Topic pipelines scikit-learn neural-network

Category Data Science


I think Skorch is what you’re looking for. This will wrap a PyTorch model with the standard sklearn API so you can use it as a drop-in estimator.


There is support for neural networks in scikit-learn.
sklearn.neural_network : Sklearn Neural network supervised documentation link. It is part of sklearn for Multi-layer Perceptron implementation can be made for both MLPClassifier and MLPRegressor

Note: however this library is not intended for large scale application and moreover has no GPU support

*Warning This implementation is not intended for large-scale applications. In particular, scikit-learn offers no GPU support. For much faster, GPU-based implementations, as well as frameworks offering much more flexibility to build deep learning architectures, see this link for Examples of related Projects.

Implementation of the deep neural network in sklearn pipeline with an example is given in this link

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_moons, make_circles, make_classification
from sklearn.neural_network import MLPClassifier
from sklearn.pipeline import make_pipeline

h = .02  # step size in the mesh

alphas = np.logspace(-1, 1, 5)

classifiers = []
names = []
for alpha in alphas:
    classifiers.append(make_pipeline(
        StandardScaler(),
        MLPClassifier(
            solver='lbfgs', alpha=alpha, random_state=1, max_iter=2000,
            early_stopping=True, hidden_layer_sizes=[100, 100],
        )
    ))
    names.append(f"alpha {alpha:.2f}")

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.