Skip to content
SDB
ML Fundamentals

Chapter 03 · intermediate · 28 min

Reinforcement Learning

Agents, environments, rewards, and the algorithms that learn through trial and error

Subhendu Datta BhowmikAI Tutorials

The Reinforcement Learning Framework

Reinforcement Learning (RL) trains an agent to make sequential decisions by interacting with an environment to maximize cumulative reward.

Unlike supervised learning, there are no labeled (X,y)(X, y) pairs — the agent must discover good behavior through trial and error.

Core Components

ComponentDescription
AgentThe learner and decision maker
EnvironmentEverything the agent interacts with
State ssCurrent observation of the environment
Action aaWhat the agent can do
Reward rrScalar feedback signal from environment
**Policy $\pi(as)$**

The Agent-Environment Loop

Agent observes state s_t
→ Agent selects action a_t using policy π
→ Environment transitions to s_{t+1}
→ Environment emits reward r_t
→ Agent updates policy based on (s_t, a_t, r_t, s_{t+1})
→ Repeat

Markov Decision Processes (MDPs)

The formal framework for RL problems is the MDP, defined by the tuple (S,A,P,R,γ)(\mathcal{S}, \mathcal{A}, P, R, \gamma):

  • S\mathcal{S}: state space
  • A\mathcal{A}: action space
  • P(ss,a)P(s'|s, a): transition probability
  • R(s,a)R(s, a): expected reward
  • γ[0,1)\gamma \in [0,1): discount factor (how much to value future rewards)

Return and Value Functions

The discounted return from timestep tt: Gt=rt+γrt+1+γ2rt+2+...=k=0γkrt+kG_t = r_t + \gamma r_{t+1} + \gamma^2 r_{t+2} + ... = \sum_{k=0}^{\infty} \gamma^k r_{t+k}

State-value function Vπ(s)V^\pi(s): expected return from state ss under policy π\pi

Action-value function Qπ(s,a)Q^\pi(s, a): expected return from taking action aa in state ss then following π\pi

The Bellman Equation

Vπ(s)=aπ(as)sP(ss,a)[R(s,a)+γVπ(s)]V^\pi(s) = \sum_a \pi(a|s) \sum_{s'} P(s'|s,a)\left[R(s,a) + \gamma V^\pi(s')\right]

Value-Based Methods

Value-based methods learn the value function and derive a policy from it (usually greedy: π(s)=argmaxaQ(s,a)\pi(s) = \arg\max_a Q(s,a)).

Q-Learning (Tabular)

A model-free, off-policy algorithm that updates a Q-table:

Q(s,a)Q(s,a)+α[r+γmaxaQ(s,a)Q(s,a)]Q(s, a) \leftarrow Q(s, a) + \alpha \left[r + \gamma \max_{a'} Q(s', a') - Q(s, a)\right]

The bracketed term is the TD error (Temporal Difference error).

Deep Q-Network (DQN)

When the state space is too large for a table (e.g., images), approximate Q(s,a)Q(s,a) with a neural network:

  • Experience Replay: store (s,a,r,s)(s, a, r, s') tuples in a buffer; sample random mini-batches to break correlations
  • Target Network: use a slowly-updated copy of the Q-network as the target to stabilize training
  • ε-greedy: explore with probability ε, exploit with probability 1-ε

DQN beat human performance on 49 Atari games (DeepMind, 2015).

Policy-Based Methods

Policy-based methods directly optimize the policy πθ(as)\pi_\theta(a|s) parameterized by θ\theta.

REINFORCE (Monte Carlo Policy Gradient)

θJ(θ)=Eτπθ[tGtθlogπθ(atst)]\nabla_\theta J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta}\left[\sum_t G_t \nabla_\theta \log \pi_\theta(a_t|s_t)\right]

  • High variance (needs many episodes to estimate gradient)
  • Subtract a baseline b(s)b(s) (usually V(s)V(s)) to reduce variance: Actor-Critic

Proximal Policy Optimization (PPO)

The most widely used modern RL algorithm:

  • Clips the policy update to stay close to the old policy (prevents destructive updates)
  • Used to train ChatGPT/Claude (RLHF stage) and game-playing agents
  • Stable, sample-efficient, easy to implement

LCLIP=Et[min(rt(θ)A^t, clip(rt(θ),1ϵ,1+ϵ)A^t)]\mathcal{L}^{CLIP} = \mathbb{E}_t\left[\min\left(r_t(\theta) \hat{A}_t,\ \text{clip}(r_t(\theta), 1-\epsilon, 1+\epsilon)\hat{A}_t\right)\right]

where rt(θ)=πθ(atst)πθold(atst)r_t(\theta) = \frac{\pi_\theta(a_t|s_t)}{\pi_{\theta_{old}}(a_t|s_t)} is the probability ratio.

Algorithm Comparison

AlgorithmTypeOn/Off PolicyKey Strength
Q-LearningValue-basedOff-policySimple, guaranteed convergence (tabular)
DQNValue-basedOff-policyHandles image/continuous state spaces
REINFORCEPolicy-basedOn-policyDirect policy optimization
Actor-Critic (A2C/A3C)BothOn-policyLower variance than REINFORCE
PPOPolicy-basedOn-policyStable, state-of-the-art general purpose
SACBothOff-policyBest for continuous action spaces
TD3Value-basedOff-policyStable continuous control

When to Use RL

  • Sequential decision making over many steps
  • No labeled training data but can define a reward signal
  • Game playing, robotics, recommendation systems, RLHF
  • Don't use RL when supervised learning can solve it — RL is data-hungry and complex
Q-Learning on a Grid Worldpython
import numpy as np

# Simple 4x4 grid: 0=empty, -1=wall, +10=goal, -10=pit
# State: (row, col), Actions: 0=up, 1=right, 2=down, 3=left

class GridWorld:
    def __init__(self, size=4):
        self.size = size
        self.goal = (3, 3)
        self.pit = (1, 3)
        self.reset()

    def reset(self):
        self.pos = (0, 0)
        return self.pos

    def step(self, action):
        moves = [(-1,0),(0,1),(1,0),(0,-1)]
        dr, dc = moves[action]
        r, c = self.pos
        new_r = max(0, min(self.size-1, r + dr))
        new_c = max(0, min(self.size-1, c + dc))
        self.pos = (new_r, new_c)

        if self.pos == self.goal:
            return self.pos, +10.0, True
        elif self.pos == self.pit:
            return self.pos, -10.0, True
        else:
            return self.pos, -0.1, False

# Q-Learning
env = GridWorld()
Q = np.zeros((4, 4, 4))   # Q[row, col, action]
alpha, gamma, epsilon = 0.1, 0.9, 0.3

for episode in range(5000):
    s = env.reset()
    for _ in range(50):
        r, c = s
        # ε-greedy action selection
        if np.random.random() < epsilon:
            a = np.random.randint(4)
        else:
            a = np.argmax(Q[r, c])

        s_next, reward, done = env.step(a)
        r2, c2 = s_next

        # TD update
        td_target = reward + gamma * np.max(Q[r2, c2]) * (not done)
        Q[r, c, a] += alpha * (td_target - Q[r, c, a])

        s = s_next
        if done:
            break

# Evaluate learned policy
s = env.reset()
path = [s]
for _ in range(20):
    r, c = s
    a = np.argmax(Q[r, c])
    s, reward, done = env.step(a)
    path.append(s)
    if done:
        break

print("Optimal path:", path)
print("Reached goal:", path[-1] == (3, 3))

Knowledge check

In reinforcement learning, what does the discount factor γ control?

Summary

  • RL is about agents learning to maximize cumulative reward through interaction
  • MDPs formalize the environment; the Bellman equation expresses value recursively
  • Q-Learning / DQN: learn value functions, derive greedy policy
  • REINFORCE / PPO: directly optimize the policy — PPO is the current industry standard
  • RL is powerful but data-hungry — supervised learning is preferable when you have labeled data

Next: Semi-Supervised & Self-Supervised Learning — getting more from less labeled data.

ML Fundamentals