A flexible package for multimodal-deep-learning to combine tabular data with text and images using Wide and Deep models in Pytorch
A flexible package for multimodal-deep-learning to combine tabular data with text and images using Wide and Deep models in Pytorch
A flexible package for multimodal-deep-learning to combine tabular data with text and images using Wide and Deep models in Pytorch
Documentation: https://pytorch-widedeep.readthedocs.io
Companion posts and tutorials: infinitoml
Experiments and comparison with LightGBM: TabularDL vs LightGBM
Slack: if you want to contribute or just want to chat with us, join slack
The content of this document is organized as follows:
pytorch-widedeep is based on Google's Wide and Deep Algorithm,
adjusted for multi-modal datasets.
In general terms, pytorch-widedeep is a package to use deep learning with
tabular data. In particular, is intended to facilitate the combination of
text and images with corresponding tabular data using wide and deep models.
With that in mind there are a number of architectures that can be implemented
with the library. The main components of those architectures are shown in the
Figure below:
In math terms, and following the notation in the
paper, the expression for the architecture
without a deephead component can be formulated as:
Where σ is the sigmoid function, 'W' are the weight matrices applied to the wide model and to the final activations of the deep models, 'a' are these final activations, φ(x) are the cross product transformations of the original features 'x', and , and 'b' is the bias term. In case you are wondering what are "cross product transformations", here is a quote taken directly from the paper: "For binary features, a cross-product transformation (e.g., “AND(gender=female, language=en)”) is 1 if and only if the constituent features (“gender=female” and “language=en”) are all 1, and 0 otherwise".
It is perfectly possible to use custom models (and not necessarily those in
the library) as long as the the custom models have an property called
output_dim with the size of the last layer of activations, so that
WideDeep can be constructed. Examples on how to use custom components can
be found in the Examples folder and the section below.
The pytorch-widedeep library offers a number of different architectures. In
this section we will show some of them in their simplest form (i.e. with
default param values in most cases) with their corresponding code snippets.
Note that all the snippets below shoud run locally. For a more detailed
explanation of the different components and their parameters, please refer to
the documentation.
For the examples below we will be using a toy dataset generated as follows:
…This will create a 100 rows dataframe and a dir in your local folder, called
images with 100 random images (or images with just noise).
Perhaps the simplest architecture would be just one component, wide,
deeptabular, deeptext or deepimage on their own, which is also
possible, but let's start the examples with a standard Wide and Deep
architecture. From there, how to build a model comprised only of one
component will be straightforward.
Note that the examples shown below would be almost identical using any of the
models available in the library. For example, TabMlp can be replaced by
TabResnet, TabNet, TabTransformer, etc. Similarly, BasicRNN can be
replaced by AttentiveRNN, StackedAttentiveRNN, or HFModel with
their corresponding parameters and preprocessor in the case of the Hugging
Face models.
1. Wide and Tabular component (aka deeptabular)
…2. Tabular and Text data
…3. Tabular and text with a FC head on top via the head_hidden_dims param
in WideDeep
…4. Tabular and multiple text columns that are passed directly to
WideDeep
…5. Tabular data and multiple text columns that are fused via a the library's
ModelFuser class
…6. Tabular and multiple text columns, with an image column. The text columns
are fused via the library's ModelFuser and then all fused via the
deephead paramenter in WideDeep which is a custom ModelFuser coded by
the user
This is perhaps the less elegant solution as it involves a custom component by
the user and slicing the 'incoming' tensor. In the future, we will include a
TextAndImageModelFuser to make this process more straightforward. Still, is not
really complicated and it is a good example of how to use custom components in
pytorch-widedeep.
Note that the only requirement for the custom component is that it has a
property called output_dim that returns the size of the last layer of
activations. In other words, it does not need to inherit from
BaseWDModelComponent. This base class simply checks the existence of such
property and avoids some typing errors internally.
…7. A two-tower model
This is a popular model in the context of recommendation systems. Let's say we have a tabular dataset formed my triples (user features, item features, target). We can create a two-tower model where the user and item features are passed through two separate models and then "fused" via a dot product.
…8. Tabular with a multi-target loss
This one is "a bonus" to illustrate the use of multi-target losses, more than actually a different architecture.
…deeptabular componentIt is important to emphasize again that each individual component, wide,
deeptabular, deeptext and deepimage, can be used independently and in
isolation. For example, one could use only wide, which is in simply a
linear model. In fact, one of the most interesting functionalities
inpytorch-widedeep would be the use of the deeptabular component on
its own, i.e. what one might normally refer as Deep Learning for Tabular
Data. Currently, pytorch-widedeep offers the following different models
for that component:
Two simpler attention based models that we call:
The Tabformer family, i.e. Transformers for Tabular data:
And probabilistic DL models for tabular data based on Weight Uncertainty in Neural Networks:
Wide model.TabMlp modelNote that while there are scientific publications for the TabTransformer, SAINT and FT-Transformer, the TabFasfFormer and TabPerceiver are our own adaptation of those algorithms for tabular data.
In addition, Self-Supervised pre-training can be used for all deeptabular
models, with the exception of the TabPerceiver. Self-Supervised
pre-training can be used via two methods or routines which we refer as:
encoder-decoder method and constrastive-denoising method. Please, see the
documentation and the examples for details on this functionality, and all
other options in the library.
rec moduleThis module was introduced as an extension to the existing components in the library, addressing questions and issues related to recommendation systems. While still under active development, it currently includes a select number of powerful recommendation models.
It's worth noting that this library already supported the implementation of various recommendation algorithms using existing components. For example, models like Wide and Deep, Two-Tower, or Neural Collaborative Filtering could be constructed using the library's core functionalities.
The recommendation algorithms in the rec module are:
See the examples for details on how to use these models.
For the text component, deeptext, the library offers the following models:
No open issues yet, or sync has not completed.