skorch-dev/skorch

[Feature request] Adding NeuralNetTranformer

Open

#482 opened on Jun 9, 2019

 (24 comments) (4 reactions) (0 assignees)Jupyter Notebook (414 forks)auto 404
enhancementhelp wanted

Repository metrics

Stars
 (6,171 stars)
PR merge metrics
 (PR metrics pending)

Description

Hi,

To be really sklearn-ish I think it would be very nice to have a class NeuralNetTransformer which inherits from TransformerMixin and implements a .transform method.

Usecases: representation learning.

Example: use a VAE for dimensionality reduction then work in this new representation space (no joint learning). This is often done in semi supervised learning (e.g. M1-M2 model), reinforcement learning (e.g. world models), ... but could also enable the use of all the predictors in sklearn by first transforming using a VAE / pretrained CNN without last layers and then predict using any sklearn predictor (e.g. svm) or perform some clustering all in a single Pipeline.

How: the quickest is to have a transform function which does the same as predict (i.e. concatenate the first outputs of forward). But the representation is typically a temporary output of the model (i.e. latent in VAE or one of the middle layer in a CNN), so it would be better not to compute the whole forward pass if only the first few steps are needed. One could maybe have a .transform function in the model that only returns what is needed? Or maybe a is_transform flag to the forward ?

The first method has the downside of making more computation than needed and not being able to predict and transform with the same module (although this latter issue could be solved by using for example the second output of forward as transform). The second method is better but it requires the model to have an additional function / flag, which skorch was able to circumvent for the classifier and the regressor.

PS: thanks for the great library :)

Contributor guide