Now that we have a conceptual understanding of deep learning and the role Keras plays, let's get our hands dirty and prepare the necessary software environment. Having a consistent and correctly configured environment is essential for running the examples and exercises in this course smoothly. We'll focus on installing Keras 3 and its preferred backend, PyTorch.
Before installing any packages, it's highly recommended to create an isolated Python environment for this project. This prevents conflicts between package dependencies required by different projects. Tools like Python's built-in venv
module or the conda
package manager are excellent choices for this purpose.
Using venv
:
.venv
):
python -m venv .venv
source .venv/bin/activate
.venv\Scripts\activate.bat
.venv\Scripts\Activate.ps1
Your terminal prompt should now indicate that you are inside the virtual environment.
Using conda
:
keras-env
) with a specific Python version:
conda create --name keras-env python=3.9 # Or your preferred Python 3 version
conda activate keras-env
All subsequent package installations will be confined to this active environment.
With your virtual environment activated, you can now install the necessary libraries using pip
, the Python package installer. Keras 3 is designed to work with multiple backends (PyTorch, TensorFlow, JAX). For this course, we will primarily use the PyTorch backend.
Install Keras and PyTorch with the following command:
pip install keras torch torchvision torchaudio
This command installs:
keras
: The core Keras library (version 3 or later).torch
: The PyTorch library, which will serve as the computational backend.torchvision
, torchaudio
: PyTorch companion libraries often useful for data loading and transformations, especially in vision and audio tasks.Keras 3 is typically configured to automatically detect and use an available backend like PyTorch. If you have multiple backends installed (e.g., TensorFlow) and want to ensure PyTorch is used, you can set the KERAS_BACKEND
environment variable before running your Python scripts or notebooks:
export KERAS_BACKEND="torch"
python your_script.py
set KERAS_BACKEND=torch
python your_script.py
$env:KERAS_BACKEND="torch"
python your_script.py
You can also configure this within your Python code before importing Keras, although setting the environment variable is often preferred:
import os
os.environ["KERAS_BACKEND"] = "torch"
import keras
# Now Keras will use the PyTorch backend
Deep learning often involves computationally intensive operations that can be significantly accelerated using Graphics Processing Units (GPUs), particularly NVIDIA GPUs. If you have a compatible NVIDIA GPU and want to leverage it:
pip
or conda
command for your system and desired CUDA version. The command might look slightly different from the basic one above, specifying CUDA versions.While GPU acceleration dramatically speeds up training for large models and datasets, it's not strictly required for following the fundamental concepts in this course. Most examples will run reasonably well on a modern CPU.
With Keras and its PyTorch backend installed, your environment is now ready. In the next section, we'll perform a quick verification step to confirm that Keras is installed correctly and can communicate with the backend.
© 2025 ApX Machine Learning