Tabular model

Open In Colab

A basic model that can be used on tabular data

  1. /usr/local/lib/python3.8/dist-packages/torch/cuda/__init__.py:52: UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx (Triggered internally at /pytorch/c10/cuda/CUDAFunctions.cpp:100.)
  2. return torch._C._cuda_getDeviceCount() > 0

Embeddings

emb_sz_rule[source]

emb_sz_rule(n_cat)

Rule of thumb to pick embedding size corresponding to n_cat

Through trial and error, this general rule takes the lower of two values:

  • A dimension space of 600
  • A dimension space equal to 1.6 times the cardinality of the variable to 0.56.

This provides a good starter for a good embedding space for your variables. For more advanced users who wish to lean into this practice, you can tweak these values to your discretion. It is not uncommon for slight adjustments to this general formula to provide more success.

get_emb_sz[source]

get_emb_sz(to, sz_dict=None)

Get default embedding size from TabularPreprocessor proc or the ones in sz_dict

class TabularModel[source]

TabularModel(emb_szs, n_cont, out_sz, layers, ps=None, embed_p=0.0, y_range=None, use_bn=True, bn_final=False, bn_cont=True, act_cls=ReLU(inplace=True)) :: Module

Basic model for tabular data.

This model expects your cat and cont variables seperated. cat is passed through an Embedding layer and potential Dropout, while cont is passed though potential BatchNorm1d. Afterwards both are concatenated and passed through a series of LinBnDrop, before a final Linear layer corresponding to the expected outputs.

  1. emb_szs = [(4,2), (17,8)]
  2. m = TabularModel(emb_szs, n_cont=2, out_sz=2, layers=[200,100]).eval()
  3. x_cat = torch.tensor([[2,12]]).long()
  4. x_cont = torch.tensor([[0.7633, -0.1887]]).float()
  5. out = m(x_cat, x_cont)

tabular_config[source]

tabular_config(ps=None, embed_p=0.0, y_range=None, use_bn=True, bn_final=False, bn_cont=True, act_cls=ReLU(inplace=True))

Convenience function to easily create a config for TabularModel

Any direct setup of TabularModel‘s internals should be passed through here:

  1. config = tabular_config(embed_p=0.6, use_bn=False); config
  1. {'embed_p': 0.6, 'use_bn': False}

Company logo

©2021 fast.ai. All rights reserved.
Site last generated: Mar 31, 2021