Skip to content

Environment

Setting up a proper environment is crucial for developing deep learning applications in Python. This chapter guides you through installing the necessary software and libraries.

  • Python: A recent version of Python 3 (e.g., 3.8 or higher) is recommended.
  • Package Manager: pip (Python’s default) or conda (popular in data science, manages environments and packages).
  • Virtual Environment: Essential for managing dependencies and avoiding conflicts between projects. Tools include venv (built-in with Python) or conda environments.
  • Scientific Libraries: NumPy (numerical computing), SciPy (scientific and technical computing), Pandas (data manipulation and analysis).
  • Visualization: Matplotlib or Seaborn for plotting data and results.
  • Deep Learning Frameworks: TensorFlow (v2.x or later) or PyTorch are the current industry standards. Keras is a high-level API often used with TensorFlow (tf.keras).

Using the Anaconda or Miniconda distribution simplifies setup significantly. It includes Python, conda, and many scientific libraries. Miniconda is a lighter version containing only Python and conda.

  1. Download and install Anaconda or Miniconda from their official websites.

  2. Create a dedicated virtual environment (replace my_dl_env with your preferred name):

# Using conda
conda create --name my_dl_env python=3.9 # Or your desired Python 3 version
conda activate my_dl_env

Alternatively, using Python’s built-in venv:

# Using venv (ensure Python 3 is in your PATH)
python -m venv my_dl_env
# Activate on Linux/macOS:
source my_dl_env/bin/activate
# Activate on Windows:
my_dl_env\Scripts\activate

Installing Deep Learning Frameworks and Libraries

Section titled “Installing Deep Learning Frameworks and Libraries”

Once your virtual environment is activated, install the necessary packages using pip or conda.

# Ensure pip is up-to-date
pip install --upgrade pip
# Install essential libraries
pip install numpy scipy pandas matplotlib seaborn
# Install TensorFlow (includes Keras API)
pip install tensorflow
# OR Install PyTorch (visit pytorch.org for specific command based on your system/CUDA)
# Example (CPU version):
# pip install torch torchvision torchaudio
# Install essential libraries (often included with Anaconda, but good to ensure)
conda install numpy scipy pandas matplotlib seaborn
# Install TensorFlow
conda install -c conda-forge tensorflow # Check Anaconda docs for best channel
# OR Install PyTorch
conda install pytorch torchvision torchaudio cpuonly -c pytorch # Example CPU version

Note: For GPU acceleration (highly recommended for deep learning), ensure you have compatible NVIDIA drivers and CUDA toolkit installed. Then, install the GPU-enabled versions of TensorFlow or PyTorch (installation commands differ, check their official websites).

After installation, verify the packages are working correctly. Open a Python interpreter within your activated virtual environment:

$ python

Then, import the libraries and print their versions:

import numpy
print(f'NumPy Version: {numpy.__version__}')
import pandas
print(f'Pandas Version: {pandas.__version__}')
import tensorflow as tf
print(f'TensorFlow Version: {tf.__version__}')
# If you installed PyTorch:
# import torch
# print(f'PyTorch Version: {torch.__version__}')

Seeing the version numbers without errors confirms a successful setup. The exact versions will vary based on when you install them.