Saltar a contenido

01 - Setup

What this session is

About 45 minutes. Install PyTorch (with GPU support if you have one), confirm it works, run a tiny tensor program.

Step 1: Pick your Python environment

You should have Python ≥3.10 from the Python beginner path. Create a fresh virtual environment for this path:

mkdir -p ~/code/ai-learning
cd ~/code/ai-learning
python3 -m venv .venv
source .venv/bin/activate            # macOS / Linux
.venv\Scripts\activate                # Windows

You'll work inside this .venv for the whole path. Always activate it before working.

Step 2: Install PyTorch

Go to pytorch.org/get-started/locally. The site has a config-builder that gives you the exact pip install command for your OS / Python / CUDA.

Typical commands:

CPU only (any platform):

pip install torch torchvision

Linux + NVIDIA GPU (CUDA 12):

pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121

macOS (Apple Silicon - uses MPS, Apple's Metal-based backend):

pip install torch torchvision
PyTorch on macOS automatically uses MPS when available. Works well for small models; not as fast as CUDA.

Verify:

python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"

Should print a version (e.g., 2.4.0) and True or False depending on whether CUDA is available.

If you have an NVIDIA GPU and cuda.is_available() is False: the install picked the CPU-only wheel. Reinstall with the CUDA URL.

If you have Apple Silicon:

python -c "import torch; print(torch.backends.mps.is_available())"
Should be True.

Step 3: Install supporting libraries

The rest of the path uses these:

pip install jupyter ipykernel numpy pandas matplotlib
pip install transformers datasets accelerate
pip install sentence-transformers faiss-cpu      # for RAG (page 10)
pip install scikit-learn                          # for utilities
pip install httpx                                 # for serving (page 12)

That's a lot at once. Each tool has its own purpose:

  • transformers - Hugging Face's library; loading and using pre-trained models.
  • datasets - Hugging Face's data-loading library.
  • accelerate - multi-GPU + mixed-precision helper.
  • sentence-transformers + faiss - embeddings + vector search for RAG.
  • scikit-learn - classical ML utilities (data splits, metrics).

If any installation fails, read the error. Often it's a missing system dependency (e.g., libstdc++); search the error message for guidance.

Step 4: First PyTorch program

Create tensor_hello.py:

import torch

# Create a tensor - PyTorch's central data type
x = torch.tensor([1.0, 2.0, 3.0])
print("tensor:", x)
print("shape:", x.shape)
print("dtype:", x.dtype)

# Some math
y = torch.tensor([4.0, 5.0, 6.0])
print("x + y:", x + y)
print("x · y:", torch.dot(x, y))

# A 2D tensor (matrix)
mat = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
print("matrix:")
print(mat)
print("matrix shape:", mat.shape)

# Move to GPU if available
if torch.cuda.is_available():
    x_gpu = x.cuda()
    print("x on GPU:", x_gpu.device)
elif torch.backends.mps.is_available():
    x_mps = x.to("mps")
    print("x on MPS:", x_mps.device)
else:
    print("running on CPU")

Run:

python tensor_hello.py

You should see output like:

tensor: tensor([1., 2., 3.])
shape: torch.Size([3])
dtype: torch.float32
x + y: tensor([5., 7., 9.])
x · y: tensor(32.)
matrix:
tensor([[1., 2.],
        [3., 4.]])
matrix shape: torch.Size([2, 2])
running on CPU

(32 because 1·4 + 2·5 + 3·6 = 32. We'll cover dot products properly in page 03.)

Step 5: Jupyter notebooks (optional but valuable)

ML work happens in notebooks more than scripts. They mix code, output, plots, and prose in one document.

jupyter notebook

Opens a browser. Click "New" → "Python 3". You get a cell-based editor. Each cell runs independently; output appears below it. Great for exploration.

Many tutorials are notebook files (.ipynb). You'll meet them. VS Code also has a built-in notebook UI.

Step 6: Google Colab as a backup

For pages 09+ you'll want a GPU. If you don't have one:

colab.research.google.com gives you a free Jupyter notebook with a GPU (~T4-class) for a few hours per session.

!nvidia-smi              # in Colab - shows the GPU you got

For more compute: Kaggle Notebooks (free), Lambda Labs (paid by the minute), or any cloud provider.

We'll note when a page benefits from GPU.

A note on the AI ecosystem's pace

AI libraries change fast. PyTorch APIs are stable across minor versions but the broader ecosystem (Hugging Face, vLLM, training optimizers) iterates monthly. When in doubt, read the library's current docs, not blog posts from 2023.

Exercise

  1. Verify PyTorch installation:

    python -c "import torch; print(torch.__version__, torch.cuda.is_available() or torch.backends.mps.is_available())"
    

  2. Run tensor_hello.py above. Confirm the output.

  3. Modify it:

  4. Create a tensor z = torch.arange(10). Print it. (Range from 0 to 9.)
  5. Create a random 3×3 matrix with torch.randn(3, 3). Print.
  6. Compute the matrix's sum (m.sum()) and mean (m.mean()).

  7. (Optional) Set up Jupyter:

    jupyter notebook
    
    Create a new notebook. Run the tensor code in cells.

What you might wonder

"Why PyTorch and not TensorFlow / JAX?" PyTorch is the dominant ML framework as of 2026 - research uses it, most OSS uses it, the job market wants it. JAX has its place (Google ecosystem, transformer research); TensorFlow is mostly legacy. Stick with PyTorch.

"What is CUDA?" NVIDIA's parallel computing platform - the way GPUs run general-purpose code. PyTorch built with CUDA support uses your GPU automatically when you .cuda() tensors. AMD GPUs use ROCm; Apple Silicon uses MPS.

"My GPU isn't supported / I don't have one." Use CPU for pages 01-08. They run fine. For pages 09+, use Google Colab's free tier.

"How much disk space will this take?" ~3-5 GB for PyTorch and core deps. Pre-trained models you download in later pages can add 1-10 GB each. Plan for 30-50 GB free if you'll experiment broadly.

Done

You have: - Python venv with PyTorch installed (CPU or GPU). - Supporting libraries (transformers, datasets, sentence-transformers, faiss). - Verified PyTorch can create and operate on tensors. - (Optional) Jupyter set up; Colab as backup.

Next: Tensors →

Comments