rohitg00/ai-engineering-from-scratch · error · SystemExit

torch is required for this lesson. Install with: pip…

Error message

torch is required for this lesson. Install with: pip install torch

What it means

Import guard at module load: torch (and torch.nn) could not be imported, so the training demo for this lesson cannot run and the program exits via SystemExit with install instructions.

Solutions

  1. pip install torch in the active environment
  2. Run the lesson with the interpreter that has the course dependencies installed
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at phases/19-capstone-projects/44-cosine-lr-warmup/code/main.py:30 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/12e035d11d8bc4e8. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/44-cosine-lr-warmup/code/main.py:30

steps on a fixed batch, prints a per-step log, and renders the schedule.
Run: python3 code/main.py
"""

from __future__ import annotations

import csv
import dataclasses
import math
import sys
from dataclasses import dataclass, field
from pathlib import Path
from typing import Callable, Iterable

try:
    import torch
    from torch import nn
except ImportError as exc:
    raise SystemExit(
        "torch is required for this lesson. Install with: pip install torch"
    ) from exc


PLOT_HEIGHT = 12
PLOT_WIDTH = 60


@dataclass
class CosineWithWarmup:
    """Stateless cosine-with-warmup learning-rate schedule.

    Step indexing convention: step zero is the very first training update. At
    step zero the rate is exactly zero (the warmup ramp starts there). At
    step warmup_steps the rate is exactly lr_max. At step total_steps the
    rate is exactly lr_min. Past total_steps the rate stays at lr_min.
    """

View on GitHub (pinned to 39ea8a1c6d)