Chapter 17

PyTorch as Compression

Tensors, `requires_grad`, modules, parameters, losses, optimizers, data loaders, NumPy-to-PyTorch translation, and saved state.

What this chapter does

PyTorch appears after tensors, forward passes, losses, gradients, updates, batching, and numerical stability are already visible. This chapter maps those known moves to framework names and code patterns.

Lessons

Read these in order.

Start with the chapter introduction, then move through the topic lessons. The order is chosen so each page can reuse ideas from the pages before it.

  1. 01
    Introduction

    Why PyTorch belongs after the underlying deep-learning moves are visible.

  2. 02
    Tensors

    PyTorch tensors as arrays with shape, dtype, device, and gradient settings.

  3. 03
    Requires Grad and Autograd

    `requires_grad`, `backward`, and automatic differentiation as chain-rule bookkeeping.

  4. 04
    Modules and Forward

    Modules as named containers for forward computation and trainable state.

  5. 05
    Parameters

    Trainable tensors, parameter counting, and optimizer-visible state.

  6. 06
    Loss Functions

    Loss functions as scalar training signals, not mysterious framework calls.

  7. 07
    Optimizers

    Zeroing gradients, backward passes, and optimizer steps in the PyTorch loop.

  8. 08
    Data Loaders

    Data loaders as practical batch suppliers for repeated training steps.

  9. 09
    From Visible NumPy to PyTorch

    Mapping each short PyTorch training-loop line back to visible computation.

  10. 10
    Saving and Loading Preview

    State dictionaries as saved learned tensors and experiment state.

Before moving on

  • Map PyTorch tensors to arrays with shapes, dtypes, devices, and gradient settings.
  • Explain `requires_grad`, `backward`, modules, parameters, losses, and optimizers as compressed known moves.
  • Read a small PyTorch training step without treating it as magic.
  • Explain what data loaders and saved state add to a practical experiment.

Where this leads

  • Language Modeling
  • Transformers
  • Reinforcement Learning

Chapter progress