← LibraryBlog · AI

Deep Reinforcement Learning Pong from Pixels

Andrej Karpathy
✓ Source-grounded

Modern RL progress is driven by scaling compute and data using a 'brosity' policy gradient mechanism that learns from experience without needing explicit human instructions.

Read the original

In one sentence

The core of modern reinforcement learning (RL) is a "fancy form of guess-and-check" where an agent samples actions (the guess), observes the outcome, and uses policy gradients to nudge the probability distribution toward higher rewards (the check).

Why read this (and who it's for)

This is for engineers and researchers who want to move past treating RL as a "black box." It explains the mechanics of how an agent learns from raw pixels, why certain algorithms (like Policy Gradients) are preferred over others, and the fundamental difference between how humans learn via abstraction versus how AI learns via brute force. You will be able to identify why some environments are "hard" for RL and how to architect systems that handle non-differentiable components like hard attention or memory I/O.

The big idea

The central thesis is that progress in AI is not primarily driven by "new amazing ideas," but rather by the scaling of four pillars: Compute, Data, Algorithms, and Infrastructure. For example, AlexNet was essentially a scaled-up version of 1990s ConvNets; similarly, modern RL success comes from applying standard components (like CNNs as function approximators) to massive amounts of data and compute.

The logic of the "Pong" experiment demonstrates that we can train an agent to play a game using only raw pixels and a binary reward signal (+1 for win, -1 for loss). By treating the policy as a stochastic distribution, we use the Score Function Gradient Estimator. This allows us to nudge the parameters ($\theta$) in the direction of $\nabla_{\theta} \log p(x)$ multiplied by the scalar score $f(x)$. In plain English: if an action led to a win, we increase its probability; if it led to a loss, we decrease it.

Key ideas

Policy Gradients (PG) vs. Q-Learning

The Credit Assignment Problem

Difference Frames

Human vs. AI Learning (Abstraction vs. Brute Force)

Non-differentiable Computation

Frameworks & tools

The Policy Gradient Loss Function

To train an agent, substitute "fake" labels (the action actually sampled) into a supervised learning loss, modulated by an Advantage ($A$):

  1. Run Rollouts: Execute 100 games/episodes.
  2. Calculate Advantage: Determine $A$ (e.g., $+1$ for win, $-1$ for loss).
  3. Compute Loss: $\sum A_i \log p(y_i \mid x_i)$.
  4. Backpropagate: Update parameters to increase the probability of actions with positive $A$.

Trust Region Policy Optimization (TRPO)

Use this variation of PG for more consistent results:

  1. Constraint: Ensure the new policy distribution doesn't deviate too far from the old one.
  2. Mechanism: Use KL divergence constraints to avoid large, destructive parameter updates.

The "BB Gun" (Cross-Entropy Method)

Before reaching for the "Bazooka" of Policy Gradients:

  1. Method: Use CEM (stochastic hill-climbing).
  2. Application: Try this first on simpler tasks where exploration is easier.

Memorable examples & stories

The Pong Agent Strategy

The agent learns to wait for the ball, then rapidly dashes to catch it at the edge of the screen to launch it with high vertical velocity. Lesson: Brute force discovery can lead to highly effective, non-intuitive strategies.

Montezuma's Revenge

This game is notoriously difficult for RL because the agent cannot easily "stumble" into rewards like jumping or climbing. Lesson: Without prior knowledge or dense rewards, brute force search fails.

AlphaGo’s Pipeline

AlphaGo uses supervised learning first (learning from human experts) before switching to Policy Gradients. Lesson: Combining supervision with RL can overcome the "stumbling" problem of pure exploration.

Key quotes

"The progress in RL is not driven as much as you might reasonably assume by new amazing ideas." Why it matters: It highlights that scaling compute/data is currently more effective than seeking a "magic" algorithm.

"At the core the approach we use is also really quite profoundly dumb." Why it matters: It strips away the mysticism of AI, revealing it as a simple gradient-based nudge of probabilities.

"Policy Gradients are a brute force solution, where the correct actions are eventually discovered and internalized into a policy." Why it matters: It defines the fundamental limitation of current RL—it requires frequent positive rewards to learn.

How to apply it

This week

Ongoing

Common pitfalls

Connects to

If you remember only three things

Internalize it

Fidelity note

Confidence: High. This summary is grounded strictly in the provided notes. I am certain about the distinction between human/AI learning, the mechanics of Policy Gradients, and the specific Pong experiment details. Note that "Pong from Pixels" refers to a blog post/experiment by Karpathy rather than a formal textbook.

Keep going — related reads

More from Andrej Karpathy