Velamuvi AIAcademy

Lesson 8 of 8 · A First Network

Deep Learning Frameworks

Why deep learning uses frameworks: automatic gradients, GPUs and ready-made layers. Build a tiny autograd engine, then compare PyTorch, TensorFlow and JAX.

  • 6 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • Why real projects use a framework instead of hand-written maths
  • How automatic differentiation works out every gradient for you
  • How PyTorch, TensorFlow, Keras and JAX compare
  • Where to go next after this course

Why frameworks exist

In the last lesson you wrote the backpropagation step by hand for 9 parameters. Real networks are far bigger:

  • ResNet-50, a well-known image model, has about 25 million parameters.
  • Llama 3 language models from Meta have 8 billion or 70 billion, and the largest Llama 3.1 model has 405 billion.

Nobody could write out the gradients for those by hand. A deep learning framework does that work, and more:

  1. Automatic differentiation: it records the calculations of the forward pass and computes every gradient for you.
  2. Hardware: the same Python code runs on a GPU or other AI chips, which do the maths in parallel.
  3. Building blocks: ready-made layers, loss functions, optimisers and data loaders.

Automatic differentiation

While your code runs forward, the framework builds a computation graph: a record of every value and the operation that produced it. When you ask for gradients, it walks that graph backwards. Each operation knows its own small rule. For example, for out = a × b, the gradient for a is b, and the gradient for b is a.

Here is a complete, miniature autograd engine, in the spirit of Andrej Karpathy's micrograd. It supports addition and multiplication, which is enough for one neuron without an activation.

Try it yourselfPython
class Value:
    def __init__(self, data, parents=()):
        self.data = float(data)
        self.grad = 0.0
        self._backward = lambda: None
        self._parents = parents

    def __add__(self, other):
        other = other if isinstance(other, Value) else Value(other)
        out = Value(self.data + other.data, (self, other))
        def _backward():
            self.grad += out.grad
            other.grad += out.grad
        out._backward = _backward
        return out

    def __mul__(self, other):
        other = other if isinstance(other, Value) else Value(other)
        out = Value(self.data * other.data, (self, other))
        def _backward():
            self.grad += other.data * out.grad
            other.grad += self.data * out.grad
        out._backward = _backward
        return out

    def backward(self):
        # put every value in order, so each one is handled after everything that uses it
        order, seen = [], set()
        def visit(v):
            if v not in seen:
                seen.add(v)
                for p in v._parents:
                    visit(p)
                order.append(v)
        visit(self)
        self.grad = 1.0
        for v in reversed(order):
            v._backward()

x = Value(3.0)    # input
w = Value(2.0)    # weight
b = Value(-5.0)   # bias

out = x * w + b   # forward pass: 3 × 2 − 5 = 1
out.backward()    # backward pass: every gradient at once

print("out =", out.data)
print("gradient for w:", w.grad)
print("gradient for x:", x.grad)
print("gradient for b:", b.grad)

The gradient for w is 3.0 (the value of x), the gradient for x is 2.0 (the value of w), and the gradient for b is 1.0. One call to backward() found all three.

The same idea in PyTorch

PyTorch does exactly this, for tensors of any size and on GPUs. The code below is only shown, because PyTorch does not run in this browser editor:

import torch

x = torch.tensor(3.0)
w = torch.tensor(2.0, requires_grad=True)
b = torch.tensor(-5.0, requires_grad=True)

out = x * w + b
out.backward()
print(w.grad, b.grad)   # tensor(3.) tensor(1.)

requires_grad=True tells PyTorch to track the operations on w and b, so it can compute their gradients.

Comparing the main frameworks

FrameworkMade byKnown for
PyTorchMeta, now run by the PyTorch FoundationMost research papers and most open-source models, including large language models. Feels like ordinary Python.
TensorFlowGoogleProduction systems built over many years, and running models on phones and small devices.
KerasGoogleA simple, high-level way to build models. Keras 3 can run on JAX, TensorFlow or PyTorch.
JAXGoogleNumPy-style code with fast compiled gradients. Popular for large-scale training on Google's TPUs.
ONNXStarted by Microsoft and Facebook, now an open Linux Foundation projectNot a training framework: a common file format for moving trained models between frameworks and devices.

Where to go next

You now have the big picture. These courses go deeper, in a sensible order:

  1. Artificial Neural Networks: build a network from scratch in NumPy, then in PyTorch.
  2. Activation Functions and Loss Functions: the choices inside every network.
  3. Backpropagation and Optimizers: how learning really works, from the chain rule to Adam.
  4. Convolutional Neural Networks: the networks that learned to see.
  5. Transformers: the architecture behind ChatGPT, Claude and Gemini.
  6. PyTorch: the framework, from tensors to full training loops.

Common mistakes

Practice exercise

Frameworks compute exact gradients, but you can check any gradient with a quick estimate: nudge the weight a tiny bit each way and see how much the loss changes. This is called a gradient check. Complete numerical_gradient.

Your solutionPython
def loss(w):
    return (3 * w - 5) ** 2

def numerical_gradient(f, w, h=1e-4):
    # Estimate the slope of f at w by trying a point a little to the right
    # and a little to the left: (f(w + h) - f(w - h)) / (2 * h)
    return 0.0

print(round(numerical_gradient(loss, 2.0), 4))
print(round(numerical_gradient(loss, 0.0), 4))

# Output must be exactly:
# 6.0
# -30.0
Knowledge check

What does automatic differentiation (autograd) do in a framework such as PyTorch?

Summary

  • Frameworks compute gradients automatically, run on GPUs and supply ready-made building blocks.
  • Automatic differentiation records the forward pass as a graph and applies the chain rule backwards.
  • PyTorch leads in research and open models; TensorFlow, Keras and JAX are the main alternatives.
  • Next steps: neural networks in depth, backpropagation, CNNs, Transformers and PyTorch.

Next: test what you have learned with the course quiz.

Finished reading? Tick it off to track your progress.