Manikya Bardhan
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    @def title = "FastAI.jl Tabular" <!-- @def reeval = true --> # Working with Tabular Data using [FastAI.jl](https://www.github.com/FluxML/FastAI.jl) [GSOC] \toc ## Introduction This summer, I worked on FastAI.jl as a part of Google Summer of Code'21 under The Julia Language. My work involved adding tabular support to the package. If you are unfamiliar with FastAI.jl, it is a package inspired by [`fastai`](https://github.com/fastai/fastai)[^1] and is a repository of best practices for deep learning in julia. It offers a layered API that lets you use the higher-level functionalities to perform various learning tasks in around 5 lines of code, and the middle and lower-level APIs can be mixed and matched to create new approaches. The GSOC project page can be found [here](https://summerofcode.withgoogle.com/projects/#5088642453733376). ## Implemented Functionalities We'll go through an end-to-end task to understand the added functionalities. Here's we will be working with the `adult` dataset and try to perform tabular classification on the `salary` column to predict if its `<50k` or `>=50k` ### Containers [Link to PR](https://github.com/FluxML/FastAI.jl/pull/26) The first thing I worked on was adding an index-based data container suitable for tabular data, which follows the interface defined by [MLDataPattern.jl](https://github.com/JuliaML/MLDataPattern.jl). `TableDataset` accepts any type satisfying the [Tables.jl](https://github.com/JuliaData/Tables.jl) interface and allows querying for any row using `getobs` and the total number of observations using `nobs`. On top of this, among some of the other things the container lets you do are performing arbitrary functions on the observations lazily, splitting containers, and creating a `DataLoader` suitable for training. ```julia:container_dummy #hideall using FastAI ENV["DATADEPS_ALWAYS_ACCEPT"] = "true" data = TableDataset(joinpath(datasetpath("adult_sample"), "adult.csv")); ``` ```julia:container using FastAI data = TableDataset(joinpath(datasetpath("adult_sample"), "adult.csv")); getobs(data, 1), nobs(data) ``` \show{container} We'll now split off our target column from the data container using `mapobs`. ```julia:split splitdata = mapobs(row -> (row, row[:salary]), data) ``` ### Transformations [Link to PR](https://github.com/lorenzoh/DataAugmentation.jl/pull/45) Once we have our container, to make it usable for training we'll pre-process it using the tabular transformations added to DataAugmentation.jl which are - * **Normalization** - Normalizes a row of data using column mean and standard deviation. * **Fill Missing** - Fills any `missing` values in the row. * **Categorify** - Label encodes the categorical columns As these transformations are applied on individual rows (or `TabularItem`), we collect all the needed dataset statistics beforehand. This is done by creating an indexable collection (such as `Dict`) with the column as keys and required statistics as the value. For example, to create the normalization dictionary, we'll need the values to be tuples of the mean and standard deviations of the columns. There are various helper methods defined which make this process very easy if there is a `TableDataset` created already, but it is still possible to prepare everything manually and pass in the required statistics for maximum flexibility. ```julia:transforms using DataAugmentation using FastAI # hide catcols, contcols = FastAI.getcoltypes(data) normdict = FastAI.gettransformdict(data, DataAugmentation.NormalizeRow, contcols) ``` \show{transforms} Now we can create the transform by passing in the constructed collections. ```julia:norm normalize = DataAugmentation.NormalizeRow(normdict, contcols) ``` \show{norm} To now use the transformation, we'll have to create a `TabularItem` to apply it on, and then call `apply` on it. ```julia:item using Tables item = TabularItem(getobs(data, 1), Tables.columnnames(data.table)) ``` \show{item} ```julia:applytrans apply(normalize, item).data ``` \show{applytrans} The other transforms work similarly. ### Model [Link to Model PR](https://github.com/FluxML/FastAI.jl/pull/124)\\ [Link to Embedding PR](https://github.com/FluxML/Flux.jl/pull/1656) We created a deep learning model suitable for tabular data by re-engineering the model present in `fastai`[^2]. The backbone of the model is structured with a categorical and a continuous component. It accepts a 2-tuple of categorical (label or one-hot encoded) and continuous values, where each backbone applies to each element of the tuple. The output from these backbones is then concatenated and passed through a series of linear-batchnorm-dropout layers before a `finalclassifier` block, whose size could be task dependant. The categorical backbone consists of an embedding layer for each categorical column and makes use of the concept of entity embeddings[^3]. Instead of just one-hot encoding the categorical columns, representing these values in an "embedding space" makes it so that similar values could be closer to each other. This can reveal the intrinsic properties of the categorical variables and even reduce memory usage. We have mainly two methods for creating a `TabularModel`. * by passing required metadata * by passing custom backbones The simplest method of constructing a `TabularModel` is through the first method, which involves passing the number of continuous columns, output size and a collection of cardinalities (number of unique classes) for the categorical columns. ```julia:simp_model num_cont = length(contcols) outsize = 2 catdict = FastAI.gettransformdict(data, DataAugmentation.Categorify, catcols) cardinalities = [length(catdict[col]) for col in catcols] FastAI.TabularModel(num_cont, outsize; cardinalities=cardinalities) ``` \show{simp_model} While using the second method for creating the model, we'll have to define our categorical backbone, continuous backbone, and an optional finalclassifier layer. To create a categorical backbone with entity embeddings, we'll have to decide on the embedding dimensions. By default, these can be calculated using `fastai`'s rule of thumb (see `emb_sz_rule` function) but can be overridden easily using `size_overrides`. Or we can even pass in an arbitrary vector of embedding sizes if you prefer not to use `get_emb_sz`. Check out the docs for more information about different methods available for `get_emb_sz`. ```julia:embedsize embedszs = FastAI.Models.get_emb_sz(cardinalities, catcols, Dict(:workclass => 20)) ``` \show{embedsize} ```julia:backs contback = FastAI.Models.tabular_continuous_backbone(6) catback = FastAI.Models.tabular_embedding_backbone(embedszs, 0.2) ``` \show{backs} And now we can pass these in `TabularModel`, along with a bunch of other optional keyword args to get our model. ```julia:comp_model FastAI.TabularModel( catback, contback, Chain(Dense(100, 2), x->FastAI.Models.sigmoidrange(x, 2, 5)), layersizes=(200, 100, 100), dropout_rates = [0.1, 0.2, 0.1], activation=Flux.sigmoid ) ``` \show{comp_model} `layersizes` and `size_overrides` keyword args are also available in the first method if needed. ### Learning Methods [Link to PR](https://github.com/FluxML/FastAI.jl/pull/141) A Learning Method can be thought of as a concrete approach for solving a "learning task" (eg. tabular classification, tabular regression etc.)[^4] To implement a Learning Method in FastAI.jl, we have 2 main options. * Implement a Learning Method from scratch satisfying the DLPipelines.jl interface. * Use the DataBlock API to create a `BlockMethod`, or use an implemented high-level wrapper. In most cases, using the DataBlock API should be sufficient for most learning tasks. The DataBlock API lets us put together a learning method using "data blocks" which represent the type of data. For example, a `TableRow` data block carries all the information about the row, like which columns are categorical and continuous, and what classes a particular categorical column can have. In addition to `TableRow`, a `Continuous` block was also added which can help in regression tasks. One important thing to keep in mind is that these data blocks don't actually carry the data, but just contain meta-data about the underlying data. In addition to specifying the data blocks, we can also choose to apply any processing step which might be required to make the data suitable for training. For example `TabularPreprocessing` allows us to use the transformations specified [previously](#transformations) to pre-process the rows. Since our target column is a categorical value and we want to perform classification, we'll use the `Label` block to represent the target. ```julia:classblock method = BlockMethod( ( TableRow(catcols, contcols, catdict), Label(unique(data.table[:, :salary])) ), ((FastAI.TabularPreprocessing(data)), FastAI.OneHot()) ) ``` \show{classblock} The DataBlock API is very flexible and we can put together an arbitrary number of blocks for any kind of learning task. Creating a learning method for regression tasks is as easy as substituting the target block with a `Continuous` block where the size would represent the number of target columns. ```julia:contblock method2 = BlockMethod( ( TableRow(catcols, contcols, catdict), Continuous(3) ), ((FastAI.TabularPreprocessing(data),)) ) ``` \show{contblock} If our learning task is either tabular classification or regression, we can even use the high-level wrappers defined for these tasks to get the `method` easily. ```julia:tabclas method = TabularClassificationSingle( catcols, contcols, unique(data.table[:, :salary]); data) ``` \show{tabclas} With this method, it is possible for us to encode our data, get a model and loss function suitable for the data, and even directly create a `Learner` which can be used for training. To get a quick summary of the steps which will be performed while encoding, we can use the `describemethod` function. ```julia:describe describemethod(method) ``` \show{describe} Let's use this method to encode a row of data. The input here is a tuple of all row values and the target value. ```julia:getrow row = getobs(splitdata, 1000) ``` \show{getrow} On encoding, we get back a tuple where the input values have been normalized, missing values filled with the column median, and categorical values label encoded. The output value has been one-hot encoded. ```julia:encode encode(method, Training(), row) ``` \show{encode} To get a model suitable for this learning method, we can use the `methodmodel` function. ```julia:defmod methodmodel(method, NamedTuple()) ``` \show{defmod} Here the second parameter is a `NamedTuple` of backbones. The keys can be * `:categorical` * `:continuous` * `:finalclassifier` corresponding to the specific backbones, and we can choose to specify any combination of these to customize our model. ```julia:catmod methodmodel(method, (categorical=catback,)) ``` \show{catmod} To get an iterable for our data, we can use the `methoddataloader` function ```julia:datalod traindl, valdl = methoddataloaders(splitdata, method, 128; pctgval = 0.2, shuffle = true, buffered=false) ``` \show{datalod} and for getting a suitable loss function, `methodlossfn` comes in handy. ```julia:loss methodlossfn(method) ``` \show{loss} All of these steps can be customized according to requirements and can be put together to create a `Learner` for training. The `methodlearner` function abstracts these functions to directly get a `Learner`. ```julia:learn learner = methodlearner(method, splitdata, (categorical=catback,), Metrics(accuracy), batchsize=128, dlkwargs=NamedTuple(zip([:buffered], [false]))) ``` \show{learn} Now, we can use this for training our model by calling the `fit` or `fitonecycle` function on it provided by `FluxTraining`. ```julia:fit fit!(learner, 1) ``` \show{fit} ## Summary of PRs | Link to PR/commit | Description | Status | |-------------------|-------------|--------| | [FastAI.jl #26](https://github.com/FluxML/FastAI.jl/pull/26) | Adds TableContainer | Merged | | [DataAugmentation.jl #45](https://github.com/lorenzoh/DataAugmentation.jl/pull/45) | Adds table transforms and item | Merged | | [FastAI.jl #124](https://github.com/FluxML/FastAI.jl/pull/124) | Adds table model | Merged | | [Flux.jl #1656](https://github.com/FluxML/Flux.jl/pull/1656) | Updates `Embedding` layer, adds `Flux.outputsize` support for `Embedding` layer, and doc updates | Waiting for a decision on the best way to handle this case for `Flux.outputsize` | | [FastAI.jl #141](https://github.com/FluxML/FastAI.jl/pull/141) | Adds tabular blocks and encodings, fixes some bugs, adds hash for `adult_sample` dataset. | Merged | | [fluxml.github.io #94](https://github.com/FluxML/fluxml.github.io/pull/94) | Blog post showing some of the implemented functionalities.| Will be moved to the new FastAI.jl website (under construction) | ## Future Work More comprehensive documentation and notebooks can be added to demonstrate the various features. Out of the box support for additional learning methods like multiple column classification or a combination of regression and classification tasks would also be nice to have. Implementation for complex tabular models like SAINT[^5] will further improve the capabilities of the package. ## Acknowledgement All of this wouldn't have been possible at all without the continuous support and teachings of my mentors Kyle Daruwalla, Brian Chen and Lorenz Ohly. From knowing nothing about the Julia Language to completing this project really shows the amount of effort they have put in for helping me through every step of this project. The whole community has been really helpful as well, with their constant support and suggestions. From the informative references Ari provided about what's happening in the ecosystem, the various critiques and suggestions from Dhairya, the helpful reviews from Michael, Logan and the rest of the community, it has really been a very fun and great learning experience. I would also like to thank Google and the whole Summer of Code team for creating a really wonderful program and providing us with this opportunity. ## References [^1]: [fastai: A Layered API for Deep Learning](https://arxiv.org/abs/2002.04688) [^2]: [fastai's TabularModel](https://docs.fast.ai/tabular.model.html#TabularModel) [^3]: [Entity Embeddings of Categorical Variables](https://arxiv.org/abs/1604.06737) [^4]: [From the DLPipelines.jl docs](https://lorenzoh.github.io/DLPipelines.jl/dev/docstrings/DLPipelines.LearningMethod.html) [^5]: [SAINT: Improved Neural Networks for Tabular Data via Row Attention and Contrastive Pre-Training](https://arxiv.org/abs/2106.01342)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully