Setting Up for Fast.ai Deep Learning

Getting ready for Fastbook lesson 2

!UPDATE: i'm now publishing here

Introduction

In Python data science and machine learning, package and environment management systems are indispensable. Conda, initially birthed by Anaconda, is an open-source manager for packages and environments. Its popularity is unmistakable within the Python community, particularly among data science and machine learning enthusiasts. While an Anaconda install comes with Conda bundled, venturing into community-driven alternatives can often unveil more hackery delights.

Enter Mamba - a swift and efficient reimplementation of Conda designed to speed up your workflow. Mamba promises all the functionalities of Conda while aiming for a faster package handling experience. Installing Mamba not only equips you with a high-speed package manager but also retains Conda for environment activation tasks. In other words, Mamba is designed to be a drop-in replacement for Conda, such as installing (mamba install instead of conda install), updating, and removing packages. However, environment activation should still be handled by Conda's shell commands.

I took sometime to go through the following steps a couple times to gain some familiarity with the basic tools of the trade. It's similar to a surgeon setting up the surgical space and learn his tools. How else will it go well?

Setting Up Mamba

Getting started with Mamba is straightforward. Here's a quick guide:

  1. Visit the official Mamba installation page for an overview. Two important heads-up:

    • I opted for Homebrew to install Miniforge on my Mac, a decision inspired by its simplicity and effectiveness.

    • Ensure you stick to the Conda-forge channel for a community-driven alternative to Anaconda's default channel. This shouldn't be an issue unless you start customizing the install. You don't want dependencies conflicts as you get going. On that note, it's best to stick with mamba install instead of also using pip in places. Again, it's a matter of keeping package dependencies consistent and clear.

  2. After installation, you might encounter a message prompting shell setup. Execute the following command to initialize Conda (and Mamba) for CLI interactions:

     conda init "$(basename "${SHELL}")"
    
    • To prevent the base environment from auto-activating with each terminal session, run:
    conda config --set auto_activate_base false
  • A word of caution: REFRAIN from installing other packages in the base environment, which is set as the default environment.
  1. You know the conda environment is active is you see (base) your_path in your CLI. Base here is the name of the active environment. Remember: do NOT add other packages to the default base environment. It may lead to issues with your mamba/conda install. Keep reading to see how I created a custom environment

Diving into Fast.ai

Since Fastbook's release and along with its incipient online lectures, navigating its library updates can be tricky due to library updates and the book/lectures not keeping up. This is why the code in Fastbook may not run--but more on this in my next blog post. Below are steps I took to setup my custom conda environment for Fast.ai development. This is important to keep dependencies, library versions, base install, etc. from keeping you from the fun.

Environment Setup

  1. Create a new environment specifically for your Fast.ai projects:

     conda create -n your_env_name
    
    • Remember, environment creation should still be managed with Conda, not Mamba.
  2. Activate your newly created environment:

     conda activate your_env_name
    

Installing Libraries

Fast.ai's documentation suggests installing PyTorch first, but I encountered dependency issues. Here's an adjusted installation sequence:

  • SKIP PyTorch Installation: Due to dependency conflicts, I bypassed the initial PyTorch installation step as suggested on docs.fast.ai. I realized the conflicts I was seeing meant the fast.ai package can install all the dependencies it needs, including Pytorch.

  • **Install Fastai: Make sure your in your custom environment. You should see '(your_env_name) path' in your terminal. '**mamba install -c fastai fastai' is the actual command. The rest is all part of the path name in the CLI.

      (your_env_name) your_path ~ % mamba install -c fastai fastai
    
    • The -c flag specifies the channel to use, in this case, fastai, which is the Fast.ai team's official channel. I won't go further in breaking down this command.
  • Install nbdev:

      mamba install -c fastai nbdev
    
    • Not absolutely required, but it has useful libraries that you may want to experiment with.

Verifying Installation

To ensure the libraries are correctly installed:

  1. Open your terminal and list all Conda environments:

     conda env list
    

    or

     conda info --envs
    
  2. Activate your custom environment where you installed Fast.ai etc and verify their installation. Example:

     conda activate your_env_name
     conda list | grep jupyter
     conda list | grep torch
     conda list | grep nbdev
    

If the packages appear in the list, they're installed in your custom environment.

FAQ

  1. Sometimes I just had to restart and open a new terminal.

Up Next

In my next blog post, I'll share my experiences working with lesson 2 itself, and how I used print statements and try/except blocks to help debug the code.


Did you find this article valuable?

Support Zhus on First by becoming a sponsor. Any amount is appreciated!