1  What is a Deep Learning Framework?

Before we write a single line of code, let’s understand what we’re building.

1.1 The Black Box Problem

Here’s typical PyTorch code:

import torch

# Simple forward pass
x = torch.tensor([[20.0], [25.0], [30.0]])  # Celsius temperatures
w = torch.tensor([[1.8]])
b = torch.tensor([32.0])

f = x @ w.T + b  # Convert to Fahrenheit
print(f)  # tensor([[68.], [77.], [86.]])

Simple, right? But what’s actually happening inside? How does PyTorch:

  • Store the numbers?
  • Perform matrix multiplication?
  • Handle different data types and shapes?

If these feel like magic, you’re not alone. Production frameworks hide complexity behind abstractions. This book reveals what’s underneath.

1.2 What We’re Building

A deep learning framework has three core responsibilities:

flowchart LR
    A[1. Store Data] --> B[2. Compute Operations]
    B --> C[3. Track History]

1.2.1 1. Store Data: Tensors

Tensors are n-dimensional arrays — the universal data structure:

Shape Name Example
() Scalar Loss value: 3.14
(3,) Vector Bias: [1, 2, 3]
(2, 3) Matrix Weights: [[1,2,3], [4,5,6]]
(2, 3, 4) 3D Tensor Batch of images

1.2.2 2. Compute Operations: Operators

Operations transform tensors:

# Element-wise
c = a + b
c = a * b

# Matrix operations
c = a @ b  # matmul

# Reductions
c = a.sum()
c = a.mean()

1.2.3 3. Track History: Computational Graph

This is what enables learning (we’ll cover this in Part II):

y = x @ w + b  # TensorWeaver remembers: "y came from x, w, b via matmul and add"

1.3 Part I Focus: Forward Only

In this part, we focus on inference — computing outputs from inputs:

flowchart LR
    C[Celsius] --> Model[y = x × 1.8 + 32] --> F[Fahrenheit]

Given the temperature conversion formula:

\[F = C \times 1.8 + 32\]

We’ll build a framework that can:

import tensorweaver as tw

# Input: Celsius temperatures
celsius = tw.Tensor([[0.0], [100.0]])

# Model parameters (we know the answer!)
w = tw.Tensor([[1.8]])
b = tw.Tensor([32.0])

# Forward pass
fahrenheit = celsius @ w.T + b

print(fahrenheit)
# [[32.0],    <- 0°C = 32°F  ✓
#  [212.0]]   <- 100°C = 212°F ✓
Tip

Import Styles: We use import tensorweaver as tw here to show the full API. In later chapters, we’ll use from tensorweaver import Tensor for brevity.

No training yet — just computation. We’ll learn how to find w=1.8 and b=32 in Part II.

1.4 Why Build Our Own?

Production Frameworks TensorWeaver
C++/CUDA internals Pure Python
Optimized for speed Optimized for understanding
“Trust us, it works” “Here’s exactly how it works”

You can step through every line with a debugger. Every operation is visible.

1.5 Our Running Example: Temperature Conversion

Throughout Parts I-III, we’ll use the Celsius-to-Fahrenheit conversion:

\[F = C \times 1.8 + 32\]

Why this example?

  1. Real and meaningful — Not abstract y = 2x
  2. Easily verifiable — Everyone knows 0°C = 32°F, 100°C = 212°F
  3. Linear relationship — Perfect for understanding basics
  4. Simple numbersw=1.8, b=32 are memorable

1.6 What’s Next

In this part:

  • Chapter 2: Build the Tensor class
  • Chapter 3: Implement operators (add, matmul, etc.)

By the end, you’ll have a working forward pass:

fahrenheit = celsius @ w.T + b  # This will work!

Let’s start building.